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"
|
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
|
|
|
|
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-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"
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
stats *stats
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2020-08-30 12:10:05 +00:00
|
|
|
listener net.Listener
|
2020-08-30 11:44:15 +00:00
|
|
|
mux *http.ServeMux
|
|
|
|
server *http.Server
|
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
|
|
|
stats *stats,
|
|
|
|
parent metricsParent,
|
|
|
|
) (*metrics, error) {
|
2020-10-19 20:17:48 +00:00
|
|
|
listener, 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{
|
2020-10-19 20:17:48 +00:00
|
|
|
stats: stats,
|
2020-08-30 11:44:15 +00:00
|
|
|
listener: listener,
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mux = http.NewServeMux()
|
|
|
|
m.mux.HandleFunc("/metrics", m.onMetrics)
|
2020-08-30 11:44:15 +00:00
|
|
|
|
2020-07-30 15:30:50 +00:00
|
|
|
m.server = &http.Server{
|
|
|
|
Handler: m.mux,
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-08-30 11:44:15 +00:00
|
|
|
err := m.server.Serve(m.listener)
|
2020-07-30 15:30:50 +00:00
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +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
|
|
|
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-05-08 20:52:10 +00:00
|
|
|
|
2021-01-15 17:50:31 +00:00
|
|
|
out += formatMetric("rtsp_clients{state=\"publishing\"}",
|
|
|
|
countPublishers, nowUnix)
|
|
|
|
out += formatMetric("rtsp_clients{state=\"reading\"}",
|
|
|
|
countReaders, nowUnix)
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2021-01-15 17:50:31 +00:00
|
|
|
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-05-08 20:52:10 +00:00
|
|
|
|
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)
|
|
|
|
}
|