mediamtx/internal/metrics/metrics.go

107 lines
2.6 KiB
Go
Raw Normal View History

2020-10-19 20:17:48 +00:00
package metrics
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"
2020-09-19 21:37:54 +00:00
"sync/atomic"
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-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
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"
}
2020-11-05 11:30:25 +00:00
// Parent is implemented by program.
2020-10-19 20:17:48 +00:00
type Parent interface {
Log(logger.Level, string, ...interface{})
2020-10-19 20:17:48 +00:00
}
2020-11-05 11:30:25 +00:00
// Metrics is a metrics exporter.
2020-10-19 20:17:48 +00:00
type Metrics struct {
stats *stats.Stats
listener net.Listener
mux *http.ServeMux
server *http.Server
2020-07-30 15:30:50 +00:00
}
2020-11-05 11:30:25 +00:00
// New allocates a metrics.
func New(
address string,
stats *stats.Stats,
parent Parent,
) (*Metrics, error) {
2020-10-19 20:17:48 +00:00
listener, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
2020-10-19 20:17:48 +00:00
m := &Metrics{
stats: stats,
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
}
2020-11-05 11:30:25 +00:00
// Close closes a Metrics.
2020-10-19 20:17:48 +00:00
func (m *Metrics) Close() {
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)
}
}
2020-10-19 20:17:48 +00:00
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
2020-10-19 20:17:48 +00:00
countClients := atomic.LoadInt64(m.stats.CountClients)
countPublishers := atomic.LoadInt64(m.stats.CountPublishers)
countReaders := atomic.LoadInt64(m.stats.CountReaders)
2021-04-25 14:44:10 +00:00
countSourcesRTSP := atomic.LoadInt64(m.stats.CountSourcesRTSP)
countSourcesRTSPRunning := atomic.LoadInt64(m.stats.CountSourcesRTSPRunning)
countSourcesRTMP := atomic.LoadInt64(m.stats.CountSourcesRTMP)
countSourcesRTMPRunning := atomic.LoadInt64(m.stats.CountSourcesRTMPRunning)
2020-07-30 15:30:50 +00:00
out := ""
2021-01-15 17:50:31 +00:00
out += formatMetric("rtsp_clients{state=\"idle\"}",
countClients-countPublishers-countReaders, nowUnix)
out += formatMetric("rtsp_clients{state=\"publishing\"}",
countPublishers, nowUnix)
out += formatMetric("rtsp_clients{state=\"reading\"}",
countReaders, nowUnix)
out += formatMetric("rtsp_sources{type=\"rtsp\",state=\"idle\"}",
2021-04-25 14:44:10 +00:00
countSourcesRTSP-countSourcesRTSPRunning, nowUnix)
2021-01-15 17:50:31 +00:00
out += formatMetric("rtsp_sources{type=\"rtsp\",state=\"running\"}",
2021-04-25 14:44:10 +00:00
countSourcesRTSPRunning, nowUnix)
2021-01-15 17:50:31 +00:00
out += formatMetric("rtsp_sources{type=\"rtmp\",state=\"idle\"}",
2021-04-25 14:44:10 +00:00
countSourcesRTMP-countSourcesRTMPRunning, nowUnix)
2021-01-15 17:50:31 +00:00
out += formatMetric("rtsp_sources{type=\"rtmp\",state=\"running\"}",
2021-04-25 14:44:10 +00:00
countSourcesRTMPRunning, nowUnix)
2020-07-30 15:30:50 +00:00
w.WriteHeader(http.StatusOK)
io.WriteString(w, out)
}