Browse Source

metrics: avoid sprintf

pull/190/head
aler9 5 years ago
parent
commit
9b052f1cdc
  1. 37
      internal/metrics/metrics.go

37
internal/metrics/metrics.go

@ -2,10 +2,10 @@ package metrics
import ( import (
"context" "context"
"fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
"strconv"
"sync/atomic" "sync/atomic"
"time" "time"
@ -17,6 +17,11 @@ const (
address = ":9998" address = ":9998"
) )
func formatMetric(key string, value int64, nowUnix int64) string {
return key + " " + strconv.FormatInt(value, 10) + " " +
strconv.FormatInt(nowUnix, 10) + "\n"
}
// Parent is implemented by program. // Parent is implemented by program.
type Parent interface { type Parent interface {
Log(logger.Level, string, ...interface{}) Log(logger.Level, string, ...interface{})
@ -69,7 +74,7 @@ func (m *Metrics) run() {
} }
func (m *Metrics) onMetrics(w http.ResponseWriter, req *http.Request) { func (m *Metrics) onMetrics(w http.ResponseWriter, req *http.Request) {
now := time.Now().UnixNano() / 1000000 nowUnix := time.Now().UnixNano() / 1000000
countClients := atomic.LoadInt64(m.stats.CountClients) countClients := atomic.LoadInt64(m.stats.CountClients)
countPublishers := atomic.LoadInt64(m.stats.CountPublishers) countPublishers := atomic.LoadInt64(m.stats.CountPublishers)
@ -80,20 +85,20 @@ func (m *Metrics) onMetrics(w http.ResponseWriter, req *http.Request) {
countSourcesRtmpRunning := atomic.LoadInt64(m.stats.CountSourcesRtmpRunning) countSourcesRtmpRunning := atomic.LoadInt64(m.stats.CountSourcesRtmpRunning)
out := "" out := ""
out += fmt.Sprintf("rtsp_clients{state=\"idle\"} %d %v\n", out += formatMetric("rtsp_clients{state=\"idle\"}",
countClients-countPublishers-countReaders, now) countClients-countPublishers-countReaders, nowUnix)
out += fmt.Sprintf("rtsp_clients{state=\"publishing\"} %d %v\n", out += formatMetric("rtsp_clients{state=\"publishing\"}",
countPublishers, now) countPublishers, nowUnix)
out += fmt.Sprintf("rtsp_clients{state=\"reading\"} %d %v\n", out += formatMetric("rtsp_clients{state=\"reading\"}",
countReaders, now) countReaders, nowUnix)
out += fmt.Sprintf("rtsp_sources{type=\"rtsp\",state=\"idle\"} %d %v\n", out += formatMetric("rtsp_sources{type=\"rtsp\",state=\"idle\"}",
countSourcesRtsp-countSourcesRtspRunning, now) countSourcesRtsp-countSourcesRtspRunning, nowUnix)
out += fmt.Sprintf("rtsp_sources{type=\"rtsp\",state=\"running\"} %d %v\n", out += formatMetric("rtsp_sources{type=\"rtsp\",state=\"running\"}",
countSourcesRtspRunning, now) countSourcesRtspRunning, nowUnix)
out += fmt.Sprintf("rtsp_sources{type=\"rtmp\",state=\"idle\"} %d %v\n", out += formatMetric("rtsp_sources{type=\"rtmp\",state=\"idle\"}",
countSourcesRtmp-countSourcesRtmpRunning, now) countSourcesRtmp-countSourcesRtmpRunning, nowUnix)
out += fmt.Sprintf("rtsp_sources{type=\"rtmp\",state=\"running\"} %d %v\n", out += formatMetric("rtsp_sources{type=\"rtmp\",state=\"running\"}",
countSourcesRtmpRunning, now) countSourcesRtmpRunning, nowUnix)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
io.WriteString(w, out) io.WriteString(w, out)

Loading…
Cancel
Save