add rtsp_proxies metrics (#61)

This commit is contained in:
aler9
2020-09-19 23:52:06 +02:00
parent 8180214098
commit a3a52101c0
6 changed files with 33 additions and 16 deletions

View File

@@ -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:
```

View File

@@ -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)

30
main.go
View File

@@ -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

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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