diff --git a/README.md b/README.md index 1d075128..3aeeaf31 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,8 @@ There are multiple ways to monitor the server usage over time: * `rtsp_clients{state="idle"}` is the count of clients that are neither publishing nor reading * `rtsp_clients{state="publishing"}` is the count of clients that are publishing * `rtsp_clients{state="reading"}` is the count of clients that are reading + * `rtsp_proxies{state="idle"}` is the count of proxy sources that are not running + * `rtsp_proxies{state="running"}` is the count of proxy sources that are running * A performance monitor, compatible with pprof, can be enabled with the option `pprof: yes`; then the server can be queried for metrics with pprof-compatible tools, like: ``` diff --git a/conf.go b/conf.go index 42b541ee..3baa408a 100644 --- a/conf.go +++ b/conf.go @@ -216,7 +216,7 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) { if pconf.Source != "record" { if pconf.regexp != nil { - return nil, fmt.Errorf("a path with a regular expression cannot have a RTSP source; use another path") + return nil, fmt.Errorf("a path with a regular expression (or path 'all') cannot have a RTSP source; use another path") } pconf.sourceUrl, err = url.Parse(pconf.Source) diff --git a/main.go b/main.go index c87f6d36..31fc1588 100644 --- a/main.go +++ b/main.go @@ -20,20 +20,22 @@ const ( ) type program struct { - conf *conf - logHandler *logHandler - metrics *metrics - pprof *pprof - paths map[string]*path - serverRtp *serverUDP - serverRtcp *serverUDP - serverRtsp *serverTCP - clients map[*client]struct{} - udpPublishersMap *udpPublishersMap - readersMap *readersMap - countClients int64 - countPublishers int64 - countReaders int64 + conf *conf + logHandler *logHandler + metrics *metrics + pprof *pprof + paths map[string]*path + serverRtp *serverUDP + serverRtcp *serverUDP + serverRtsp *serverTCP + clients map[*client]struct{} + udpPublishersMap *udpPublishersMap + readersMap *readersMap + countClients int64 + countPublishers int64 + countReaders int64 + countProxies int64 + countProxiesRunning int64 clientNew chan net.Conn clientClose chan *client diff --git a/metrics.go b/metrics.go index be1c47fc..6f6ba806 100644 --- a/metrics.go +++ b/metrics.go @@ -60,6 +60,8 @@ func (m *metrics) onMetrics(w http.ResponseWriter, req *http.Request) { countClients := atomic.LoadInt64(&m.p.countClients) countPublishers := atomic.LoadInt64(&m.p.countPublishers) countReaders := atomic.LoadInt64(&m.p.countReaders) + countProxies := atomic.LoadInt64(&m.p.countProxies) + countProxiesRunning := atomic.LoadInt64(&m.p.countProxiesRunning) out := "" out += fmt.Sprintf("rtsp_clients{state=\"idle\"} %d %v\n", @@ -68,6 +70,10 @@ func (m *metrics) onMetrics(w http.ResponseWriter, req *http.Request) { countPublishers, now) out += fmt.Sprintf("rtsp_clients{state=\"reading\"} %d %v\n", countReaders, now) + out += fmt.Sprintf("rtsp_proxies{state=\"idle\"} %d %v\n", + countProxies, now) + out += fmt.Sprintf("rtsp_proxies{state=\"running\"} %d %v\n", + countProxiesRunning, now) w.WriteHeader(http.StatusOK) io.WriteString(w, out) diff --git a/path.go b/path.go index 801ec14d..0c0af7e9 100644 --- a/path.go +++ b/path.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "sync/atomic" "time" ) @@ -147,7 +148,8 @@ func (pa *path) onCheck() { pa.source.state == sourceStateRunning && !pa.hasClients() && time.Since(pa.lastDescribeReq) >= sourceStopAfterDescribeSecs { - pa.log("stopping on demand source since (not requested anymore)") + pa.log("stopping on demand source (not requested anymore)") + atomic.AddInt64(&pa.p.countProxiesRunning, -1) pa.source.state = sourceStateStopped pa.source.setState <- pa.source.state } @@ -241,6 +243,7 @@ func (pa *path) onDescribe(client *client) { if pa.source != nil && pa.source.state == sourceStateStopped { // start if needed pa.log("starting on demand source") pa.lastDescribeActivation = time.Now() + atomic.AddInt64(&pa.p.countProxiesRunning, +1) pa.source.state = sourceStateRunning pa.source.setState <- pa.source.state } diff --git a/source.go b/source.go index 5836162c..65cf7bfa 100644 --- a/source.go +++ b/source.go @@ -3,6 +3,7 @@ package main import ( "math/rand" "sync" + "sync/atomic" "time" "github.com/aler9/gortsplib" @@ -46,10 +47,13 @@ func newSource(p *program, path *path, pathConf *pathConf) *source { done: make(chan struct{}), } + atomic.AddInt64(&p.countProxies, +1) + if pathConf.SourceOnDemand { s.state = sourceStateStopped } else { s.state = sourceStateRunning + atomic.AddInt64(&p.countProxiesRunning, +1) } return s