mediamtx/internal/core/metrics.go

209 lines
4.3 KiB
Go
Raw Normal View History

package core
2020-07-30 15:30:50 +00:00
import (
"context"
"io"
"net"
2020-07-30 15:30:50 +00:00
"net/http"
2021-01-15 17:50:31 +00:00
"strconv"
"sync"
2020-10-19 20:17:48 +00:00
2021-10-27 10:14:23 +00:00
"github.com/gin-gonic/gin"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-07-30 15:30:50 +00:00
)
2021-10-11 22:00:56 +00:00
func formatMetric(key string, value int64) string {
return key + " " + strconv.FormatInt(value, 10) + "\n"
2021-01-15 17:50:31 +00:00
}
type metricsPathManager interface {
2021-10-27 19:01:00 +00:00
onAPIPathsList(req apiPathsListReq1) apiPathsListRes1
}
type metricsRTSPServer interface {
2021-10-27 19:01:00 +00:00
onAPIRTSPSessionsList(req apiRTSPSessionsListReq) apiRTSPSessionsListRes
}
type metricsRTMPServer interface {
2021-10-27 19:01:00 +00:00
onAPIRTMPConnsList(req apiRTMPConnsListReq) apiRTMPConnsListRes
}
type metricsParent interface {
Log(logger.Level, string, ...interface{})
2020-10-19 20:17:48 +00:00
}
type metrics struct {
2021-11-03 15:37:08 +00:00
ln net.Listener
server *http.Server
mutex sync.Mutex
pathManager metricsPathManager
rtspServer metricsRTSPServer
rtspsServer metricsRTSPServer
rtmpServer metricsRTMPServer
2020-07-30 15:30:50 +00:00
}
func newMetrics(
address string,
parent metricsParent,
) (*metrics, error) {
2021-11-03 15:37:08 +00:00
ln, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
m := &metrics{
2021-11-03 15:37:08 +00:00
ln: ln,
2020-07-30 15:30:50 +00:00
}
2021-10-27 10:14:23 +00:00
router := gin.New()
router.GET("/metrics", m.onMetrics)
2021-10-27 10:14:23 +00:00
m.server = &http.Server{Handler: router}
2020-07-30 15:30:50 +00:00
parent.Log(logger.Info, "[metrics] opened on "+address)
2020-10-19 20:17:48 +00:00
go m.run()
return m, nil
2020-07-30 15:30:50 +00:00
}
func (m *metrics) close() {
2020-10-19 20:17:48 +00:00
m.server.Shutdown(context.Background())
}
func (m *metrics) run() {
2021-11-03 15:37:08 +00:00
err := m.server.Serve(m.ln)
2020-07-30 15:30:50 +00:00
if err != http.ErrServerClosed {
panic(err)
}
}
2021-10-27 10:14:23 +00:00
func (m *metrics) onMetrics(ctx *gin.Context) {
2020-07-30 15:30:50 +00:00
out := ""
2021-05-08 20:52:10 +00:00
2021-10-27 19:01:00 +00:00
res := m.pathManager.onAPIPathsList(apiPathsListReq1{})
if res.Err == nil {
2021-10-11 22:00:56 +00:00
for name, p := range res.Data.Items {
if p.SourceReady {
2021-10-11 22:00:56 +00:00
out += formatMetric("paths{name=\""+name+"\",state=\"ready\"}", 1)
} else {
2021-10-11 22:00:56 +00:00
out += formatMetric("paths{name=\""+name+"\",state=\"notReady\"}", 1)
}
}
}
if !interfaceIsEmpty(m.rtspServer) {
2021-10-27 19:01:00 +00:00
res := m.rtspServer.onAPIRTSPSessionsList(apiRTSPSessionsListReq{})
if res.Err == nil {
idleCount := int64(0)
readCount := int64(0)
publishCount := int64(0)
for _, i := range res.Data.Items {
switch i.State {
case "idle":
idleCount++
case "read":
readCount++
case "publish":
publishCount++
}
}
out += formatMetric("rtsp_sessions{state=\"idle\"}",
2021-10-11 22:00:56 +00:00
idleCount)
out += formatMetric("rtsp_sessions{state=\"read\"}",
2021-10-11 22:00:56 +00:00
readCount)
out += formatMetric("rtsp_sessions{state=\"publish\"}",
2021-10-11 22:00:56 +00:00
publishCount)
}
}
2021-05-08 20:52:10 +00:00
if !interfaceIsEmpty(m.rtspsServer) {
2021-10-27 19:01:00 +00:00
res := m.rtspsServer.onAPIRTSPSessionsList(apiRTSPSessionsListReq{})
if res.Err == nil {
idleCount := int64(0)
readCount := int64(0)
publishCount := int64(0)
for _, i := range res.Data.Items {
switch i.State {
case "idle":
idleCount++
case "read":
readCount++
case "publish":
publishCount++
}
}
out += formatMetric("rtsps_sessions{state=\"idle\"}",
2021-10-11 22:00:56 +00:00
idleCount)
out += formatMetric("rtsps_sessions{state=\"read\"}",
2021-10-11 22:00:56 +00:00
readCount)
out += formatMetric("rtsps_sessions{state=\"publish\"}",
2021-10-11 22:00:56 +00:00
publishCount)
}
}
2021-05-08 20:52:10 +00:00
if !interfaceIsEmpty(m.rtmpServer) {
2021-10-27 19:01:00 +00:00
res := m.rtmpServer.onAPIRTMPConnsList(apiRTMPConnsListReq{})
if res.Err == nil {
idleCount := int64(0)
readCount := int64(0)
publishCount := int64(0)
for _, i := range res.Data.Items {
switch i.State {
case "idle":
idleCount++
case "read":
readCount++
case "publish":
publishCount++
}
}
out += formatMetric("rtmp_conns{state=\"idle\"}",
2021-10-11 22:00:56 +00:00
idleCount)
out += formatMetric("rtmp_conns{state=\"read\"}",
2021-10-11 22:00:56 +00:00
readCount)
out += formatMetric("rtmp_conns{state=\"publish\"}",
2021-10-11 22:00:56 +00:00
publishCount)
}
}
2020-07-30 15:30:50 +00:00
2021-10-27 10:14:23 +00:00
ctx.Writer.WriteHeader(http.StatusOK)
io.WriteString(ctx.Writer, out)
2020-07-30 15:30:50 +00:00
}
2021-10-27 19:01:00 +00:00
// onPathManagerSet is called by pathManager.
func (m *metrics) onPathManagerSet(s metricsPathManager) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.pathManager = s
}
2021-10-27 19:01:00 +00:00
// onRTSPServer is called by rtspServer (plain).
func (m *metrics) onRTSPServerSet(s metricsRTSPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtspServer = s
}
2021-10-27 19:01:00 +00:00
// onRTSPServer is called by rtspServer (plain).
func (m *metrics) onRTSPSServerSet(s metricsRTSPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtspsServer = s
}
2021-10-27 19:01:00 +00:00
// onRTMPServerSet is called by rtmpServer.
func (m *metrics) onRTMPServerSet(s metricsRTMPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtmpServer = s
}