mediamtx/internal/core/metrics.go

222 lines
4.6 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-07-30 15:30:50 +00:00
"time"
2020-10-19 20:17:48 +00:00
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-07-30 15:30:50 +00:00
)
2021-01-15 17:50:31 +00:00
func formatMetric(key string, value int64, nowUnix int64) string {
return key + " " + strconv.FormatInt(value, 10) + " " +
strconv.FormatInt(nowUnix, 10) + "\n"
}
type metricsPathManager interface {
OnAPIPathsList(req apiPathsListReq1) apiPathsListRes1
}
type metricsRTSPServer interface {
OnAPIRTSPSessionsList(req apiRTSPSessionsListReq) apiRTSPSessionsListRes
}
type metricsRTMPServer interface {
OnAPIRTMPConnsList(req apiRTMPConnsListReq) apiRTMPConnsListRes
}
type metricsParent interface {
Log(logger.Level, string, ...interface{})
2020-10-19 20:17:48 +00:00
}
type metrics struct {
listener net.Listener
mux *http.ServeMux
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) {
2020-10-19 20:17:48 +00:00
listener, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
m := &metrics{
listener: listener,
2020-07-30 15:30:50 +00:00
}
m.mux = http.NewServeMux()
m.mux.HandleFunc("/metrics", m.onMetrics)
2020-07-30 15:30:50 +00:00
m.server = &http.Server{
Handler: m.mux,
}
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() {
err := m.server.Serve(m.listener)
2020-07-30 15:30:50 +00:00
if err != http.ErrServerClosed {
panic(err)
}
}
func (m *metrics) onMetrics(w http.ResponseWriter, req *http.Request) {
2021-01-15 17:50:31 +00:00
nowUnix := time.Now().UnixNano() / 1000000
2020-07-30 15:30:50 +00:00
out := ""
2021-05-08 20:52:10 +00:00
res := m.pathManager.OnAPIPathsList(apiPathsListReq1{})
if res.Err == nil {
readyCount := int64(0)
notReadyCount := int64(0)
for _, p := range res.Data.Items {
if p.SourceReady {
readyCount++
} else {
notReadyCount++
}
}
out += formatMetric("paths{state=\"ready\"}",
readyCount, nowUnix)
out += formatMetric("paths{state=\"notReady\"}",
notReadyCount, nowUnix)
}
if !interfaceIsEmpty(m.rtspServer) {
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\"}",
idleCount, nowUnix)
out += formatMetric("rtsp_sessions{state=\"read\"}",
readCount, nowUnix)
out += formatMetric("rtsp_sessions{state=\"publish\"}",
publishCount, nowUnix)
}
}
2021-05-08 20:52:10 +00:00
if !interfaceIsEmpty(m.rtspsServer) {
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\"}",
idleCount, nowUnix)
out += formatMetric("rtsps_sessions{state=\"read\"}",
readCount, nowUnix)
out += formatMetric("rtsps_sessions{state=\"publish\"}",
publishCount, nowUnix)
}
}
2021-05-08 20:52:10 +00:00
if !interfaceIsEmpty(m.rtmpServer) {
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\"}",
idleCount, nowUnix)
out += formatMetric("rtmp_conns{state=\"read\"}",
readCount, nowUnix)
out += formatMetric("rtmp_conns{state=\"publish\"}",
publishCount, nowUnix)
}
}
2020-07-30 15:30:50 +00:00
w.WriteHeader(http.StatusOK)
io.WriteString(w, out)
}
// OnPathManagerSet is called by pathManager.
func (m *metrics) OnPathManagerSet(s metricsPathManager) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.pathManager = s
}
// OnRTSPServer is called by rtspServer (plain).
func (m *metrics) OnRTSPServerSet(s metricsRTSPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtspServer = s
}
// OnRTSPServer is called by rtspServer (plain).
func (m *metrics) OnRTSPSServerSet(s metricsRTSPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtspsServer = s
}
// OnRTMPServerSet is called by rtmpServer.
func (m *metrics) OnRTMPServerSet(s metricsRTMPServer) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.rtmpServer = s
}