mediamtx/internal/metrics/metrics.go

101 lines
2.5 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"
"fmt"
"io"
"net"
2020-07-30 15:30:50 +00:00
"net/http"
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
)
const (
2020-10-19 20:17:48 +00:00
address = ":9998"
2020-07-30 15:30:50 +00:00
)
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.
2020-10-19 20:17:48 +00:00
func New(stats *stats.Stats, parent Parent) (*Metrics, error) {
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) {
2020-09-19 21:37:54 +00:00
now := 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)
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 := ""
2020-09-19 21:37:54 +00:00
out += fmt.Sprintf("rtsp_clients{state=\"idle\"} %d %v\n",
countClients-countPublishers-countReaders, now)
out += fmt.Sprintf("rtsp_clients{state=\"publishing\"} %d %v\n",
countPublishers, now)
out += fmt.Sprintf("rtsp_clients{state=\"reading\"} %d %v\n",
countReaders, now)
2020-10-03 19:10:41 +00:00
out += fmt.Sprintf("rtsp_sources{type=\"rtsp\",state=\"idle\"} %d %v\n",
countSourcesRtsp-countSourcesRtspRunning, now)
out += fmt.Sprintf("rtsp_sources{type=\"rtsp\",state=\"running\"} %d %v\n",
countSourcesRtspRunning, now)
out += fmt.Sprintf("rtsp_sources{type=\"rtmp\",state=\"idle\"} %d %v\n",
countSourcesRtmp-countSourcesRtmpRunning, now)
out += fmt.Sprintf("rtsp_sources{type=\"rtmp\",state=\"running\"} %d %v\n",
countSourcesRtmpRunning, now)
2020-07-30 15:30:50 +00:00
w.WriteHeader(http.StatusOK)
io.WriteString(w, out)
}