Browse Source

implement per-path metrics

pull/707/head
Konstantin Salnikov 4 years ago committed by Alessandro Ros
parent
commit
a83f57bc3e
  1. 24
      README.md
  2. 40
      internal/core/metrics.go
  3. 26
      internal/core/metrics_test.go

24
README.md

@ -418,23 +418,21 @@ wget -qO- localhost:9998/metrics @@ -418,23 +418,21 @@ wget -qO- localhost:9998/metrics
Obtaining:
```
paths{state="ready"} 2 1628760831152
paths{state="notReady"} 0 1628760831152
rtsp_sessions{state="idle"} 0 1628760831152
rtsp_sessions{state="read"} 0 1628760831152
rtsp_sessions{state="publish"} 1 1628760831152
rtsps_sessions{state="idle"} 0 1628760831152
rtsps_sessions{state="read"} 0 1628760831152
rtsps_sessions{state="publish"} 0 1628760831152
rtmp_conns{state="idle"} 0 1628760831152
rtmp_conns{state="read"} 0 1628760831152
rtmp_conns{state="publish"} 1 1628760831152
paths{name="<path_name>",state="ready"} 1
rtsp_sessions{state="idle"} 0
rtsp_sessions{state="read"} 0
rtsp_sessions{state="publish"} 1
rtsps_sessions{state="idle"} 0
rtsps_sessions{state="read"} 0
rtsps_sessions{state="publish"} 0
rtmp_conns{state="idle"} 0
rtmp_conns{state="read"} 0
rtmp_conns{state="publish"} 1
```
where:
* `paths{state="ready"}` is the count of paths that are ready
* `paths{state="notReady"}` is the count of paths that are not ready
* `paths{name="<path_name>",state="ready"} 1` is a metric for every path that shows the path state
* `rtsp_sessions{state="idle"}` is the count of RTSP sessions that are idle
* `rtsp_sessions{state="read"}` is the count of RTSP sessions that are reading
* `rtsp_sessions{state="publish"}` is the counf ot RTSP sessions that are publishing

40
internal/core/metrics.go

@ -7,16 +7,14 @@ import ( @@ -7,16 +7,14 @@ import (
"net/http"
"strconv"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/aler9/rtsp-simple-server/internal/logger"
)
func formatMetric(key string, value int64, nowUnix int64) string {
return key + " " + strconv.FormatInt(value, 10) + " " +
strconv.FormatInt(nowUnix, 10) + "\n"
func formatMetric(key string, value int64) string {
return key + " " + strconv.FormatInt(value, 10) + "\n"
}
type metricsPathManager interface {
@ -83,27 +81,17 @@ func (m *metrics) run() { @@ -83,27 +81,17 @@ func (m *metrics) run() {
}
func (m *metrics) onMetrics(ctx *gin.Context) {
nowUnix := time.Now().UnixNano() / 1000000
out := ""
res := m.pathManager.onAPIPathsList(apiPathsListReq1{})
if res.Err == nil {
readyCount := int64(0)
notReadyCount := int64(0)
for _, p := range res.Data.Items {
for name, p := range res.Data.Items {
if p.SourceReady {
readyCount++
out += formatMetric("paths{name=\""+name+"\",state=\"ready\"}", 1)
} else {
notReadyCount++
out += formatMetric("paths{name=\""+name+"\",state=\"notReady\"}", 1)
}
}
out += formatMetric("paths{state=\"ready\"}",
readyCount, nowUnix)
out += formatMetric("paths{state=\"notReady\"}",
notReadyCount, nowUnix)
}
if !interfaceIsEmpty(m.rtspServer) {
@ -125,11 +113,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) { @@ -125,11 +113,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) {
}
out += formatMetric("rtsp_sessions{state=\"idle\"}",
idleCount, nowUnix)
idleCount)
out += formatMetric("rtsp_sessions{state=\"read\"}",
readCount, nowUnix)
readCount)
out += formatMetric("rtsp_sessions{state=\"publish\"}",
publishCount, nowUnix)
publishCount)
}
}
@ -152,11 +140,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) { @@ -152,11 +140,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) {
}
out += formatMetric("rtsps_sessions{state=\"idle\"}",
idleCount, nowUnix)
idleCount)
out += formatMetric("rtsps_sessions{state=\"read\"}",
readCount, nowUnix)
readCount)
out += formatMetric("rtsps_sessions{state=\"publish\"}",
publishCount, nowUnix)
publishCount)
}
}
@ -179,11 +167,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) { @@ -179,11 +167,11 @@ func (m *metrics) onMetrics(ctx *gin.Context) {
}
out += formatMetric("rtmp_conns{state=\"idle\"}",
idleCount, nowUnix)
idleCount)
out += formatMetric("rtmp_conns{state=\"read\"}",
readCount, nowUnix)
readCount)
out += formatMetric("rtmp_conns{state=\"publish\"}",
publishCount, nowUnix)
publishCount)
}
}

26
internal/core/metrics_test.go

@ -31,7 +31,7 @@ func TestMetrics(t *testing.T) { @@ -31,7 +31,7 @@ func TestMetrics(t *testing.T) {
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
require.NoError(t, err)
source, err := gortsplib.DialPublish("rtsp://localhost:8554/mypath",
source, err := gortsplib.DialPublish("rtsp://localhost:8554/rtsp_path",
gortsplib.Tracks{track})
require.NoError(t, err)
defer source.Close()
@ -42,7 +42,7 @@ func TestMetrics(t *testing.T) { @@ -42,7 +42,7 @@ func TestMetrics(t *testing.T) {
"-i", "emptyvideo.mkv",
"-c", "copy",
"-f", "flv",
"rtmp://localhost:1935/test1/test2",
"rtmp://localhost:1935/rtmp_path",
})
require.NoError(t, err)
defer cnt1.close()
@ -66,16 +66,16 @@ func TestMetrics(t *testing.T) { @@ -66,16 +66,16 @@ func TestMetrics(t *testing.T) {
}
require.Equal(t, map[string]string{
"paths{state=\"notReady\"}": "0",
"paths{state=\"ready\"}": "2",
"rtmp_conns{state=\"idle\"}": "0",
"rtmp_conns{state=\"publish\"}": "1",
"rtmp_conns{state=\"read\"}": "0",
"rtsp_sessions{state=\"idle\"}": "0",
"rtsp_sessions{state=\"publish\"}": "1",
"rtsp_sessions{state=\"read\"}": "0",
"rtsps_sessions{state=\"idle\"}": "0",
"rtsps_sessions{state=\"publish\"}": "0",
"rtsps_sessions{state=\"read\"}": "0",
"paths{name=\"rtsp_path\",state=\"ready\"}": "1",
"paths{name=\"rtmp_path\",state=\"ready\"}": "1",
"rtmp_conns{state=\"idle\"}": "0",
"rtmp_conns{state=\"publish\"}": "1",
"rtmp_conns{state=\"read\"}": "0",
"rtsp_sessions{state=\"idle\"}": "0",
"rtsp_sessions{state=\"publish\"}": "1",
"rtsp_sessions{state=\"read\"}": "0",
"rtsps_sessions{state=\"idle\"}": "0",
"rtsps_sessions{state=\"publish\"}": "0",
"rtsps_sessions{state=\"read\"}": "0",
}, vals)
}

Loading…
Cancel
Save