Browse Source

implement per-path metrics

pull/707/head
Konstantin Salnikov 5 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
Obtaining: Obtaining:
``` ```
paths{state="ready"} 2 1628760831152 paths{name="<path_name>",state="ready"} 1
paths{state="notReady"} 0 1628760831152 rtsp_sessions{state="idle"} 0
rtsp_sessions{state="idle"} 0 1628760831152 rtsp_sessions{state="read"} 0
rtsp_sessions{state="read"} 0 1628760831152 rtsp_sessions{state="publish"} 1
rtsp_sessions{state="publish"} 1 1628760831152 rtsps_sessions{state="idle"} 0
rtsps_sessions{state="idle"} 0 1628760831152 rtsps_sessions{state="read"} 0
rtsps_sessions{state="read"} 0 1628760831152 rtsps_sessions{state="publish"} 0
rtsps_sessions{state="publish"} 0 1628760831152 rtmp_conns{state="idle"} 0
rtmp_conns{state="idle"} 0 1628760831152 rtmp_conns{state="read"} 0
rtmp_conns{state="read"} 0 1628760831152 rtmp_conns{state="publish"} 1
rtmp_conns{state="publish"} 1 1628760831152
``` ```
where: where:
* `paths{state="ready"}` is the count of paths that are ready * `paths{name="<path_name>",state="ready"} 1` is a metric for every path that shows the path state
* `paths{state="notReady"}` is the count of paths that are not ready
* `rtsp_sessions{state="idle"}` is the count of RTSP sessions that are idle * `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="read"}` is the count of RTSP sessions that are reading
* `rtsp_sessions{state="publish"}` is the counf ot RTSP sessions that are publishing * `rtsp_sessions{state="publish"}` is the counf ot RTSP sessions that are publishing

40
internal/core/metrics.go

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

26
internal/core/metrics_test.go

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

Loading…
Cancel
Save