2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-07-30 15:30:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2020-08-30 11:44:15 +00:00
|
|
|
"net"
|
2020-07-30 15:30:50 +00:00
|
|
|
"net/http"
|
2021-01-15 17:50:31 +00:00
|
|
|
"strconv"
|
2021-08-12 09:48:47 +00:00
|
|
|
"sync"
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
type metricsPathManager interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
onAPIPathsList(req apiPathsListReq1) apiPathsListRes1
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricsRTSPServer interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
onAPIRTSPSessionsList(req apiRTSPSessionsListReq) apiRTSPSessionsListRes
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricsRTMPServer interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
onAPIRTMPConnsList(req apiRTMPConnsListReq) apiRTMPConnsListRes
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type metricsParent interface {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type metrics struct {
|
2021-11-03 15:37:08 +00:00
|
|
|
ln net.Listener
|
|
|
|
server *http.Server
|
2021-08-12 09:48:47 +00:00
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
pathManager metricsPathManager
|
|
|
|
rtspServer metricsRTSPServer
|
|
|
|
rtspsServer metricsRTSPServer
|
|
|
|
rtmpServer metricsRTMPServer
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newMetrics(
|
2021-04-24 16:25:19 +00:00
|
|
|
address string,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent metricsParent,
|
|
|
|
) (*metrics, error) {
|
2021-11-03 15:37:08 +00:00
|
|
|
ln, err := net.Listen("tcp", address)
|
2020-08-30 11:44:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
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)
|
2020-08-30 11:44:15 +00:00
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
m.server = &http.Server{Handler: router}
|
2020-07-30 15:30:50 +00:00
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
parent.Log(logger.Info, "[metrics] opened on "+address)
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
go m.run()
|
2021-07-24 13:55:42 +00:00
|
|
|
|
2020-08-30 11:44:15 +00:00
|
|
|
return m, nil
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (m *metrics) close() {
|
2020-10-19 20:17:48 +00:00
|
|
|
m.server.Shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
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{})
|
2021-08-12 09:48:47 +00:00
|
|
|
if res.Err == nil {
|
2021-10-11 22:00:56 +00:00
|
|
|
for name, p := range res.Data.Items {
|
2021-08-12 09:48:47 +00:00
|
|
|
if p.SourceReady {
|
2021-10-11 22:00:56 +00:00
|
|
|
out += formatMetric("paths{name=\""+name+"\",state=\"ready\"}", 1)
|
2021-08-12 09:48:47 +00:00
|
|
|
} else {
|
2021-10-11 22:00:56 +00:00
|
|
|
out += formatMetric("paths{name=\""+name+"\",state=\"notReady\"}", 1)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !interfaceIsEmpty(m.rtspServer) {
|
2021-10-27 19:01:00 +00:00
|
|
|
res := m.rtspServer.onAPIRTSPSessionsList(apiRTSPSessionsListReq{})
|
2021-08-12 09:48:47 +00:00
|
|
|
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)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtsp_sessions{state=\"read\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
readCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtsp_sessions{state=\"publish\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
publishCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if !interfaceIsEmpty(m.rtspsServer) {
|
2021-10-27 19:01:00 +00:00
|
|
|
res := m.rtspsServer.onAPIRTSPSessionsList(apiRTSPSessionsListReq{})
|
2021-08-12 09:48:47 +00:00
|
|
|
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)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtsps_sessions{state=\"read\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
readCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtsps_sessions{state=\"publish\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
publishCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if !interfaceIsEmpty(m.rtmpServer) {
|
2021-10-27 19:01:00 +00:00
|
|
|
res := m.rtmpServer.onAPIRTMPConnsList(apiRTMPConnsListReq{})
|
2021-08-12 09:48:47 +00:00
|
|
|
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)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtmp_conns{state=\"read\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
readCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
out += formatMetric("rtmp_conns{state=\"publish\"}",
|
2021-10-11 22:00:56 +00:00
|
|
|
publishCount)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
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-08-12 09:48:47 +00:00
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onPathManagerSet is called by pathManager.
|
|
|
|
func (m *metrics) onPathManagerSet(s metricsPathManager) {
|
2021-08-12 09:48:47 +00:00
|
|
|
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) {
|
2021-08-12 09:48:47 +00:00
|
|
|
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) {
|
2021-08-12 09:48:47 +00:00
|
|
|
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) {
|
2021-08-12 09:48:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.rtmpServer = s
|
|
|
|
}
|