diff --git a/api/metrics.go b/api/metrics.go index 93ec55d..4df71d0 100644 --- a/api/metrics.go +++ b/api/metrics.go @@ -1,7 +1,7 @@ package api import ( - "github.com/voc/srtrelay/config" + "github.com/voc/srtrelay/internal/metrics" "github.com/voc/srtrelay/srt" "github.com/prometheus/client_golang/prometheus" @@ -11,128 +11,128 @@ const srtSubsystem = "srt" var ( activeSocketsDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "active_sockets"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "active_sockets"), "The number of active SRT sockets", nil, nil, ) // Metrics from: https://pkg.go.dev/github.com/haivision/srtgo#SrtStats pktSentTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_packets_total"), "total number of sent data packets, including retransmissions", []string{"address", "stream_id"}, nil, ) pktRecvTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_packets_total"), "total number of received packets", []string{"address", "stream_id"}, nil, ) pktSndLossTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_lost_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_lost_packets_total"), "total number of lost packets (sender side)", []string{"address", "stream_id"}, nil, ) pktRcvLossTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_lost_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_lost_packets_total"), "total number of lost packets (receive_side)", []string{"address", "stream_id"}, nil, ) pktRetransTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "retransmitted_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "retransmitted_packets_total"), "total number of retransmitted packets", []string{"address", "stream_id"}, nil, ) pktSentACKTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_ack_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_ack_packets_total"), "total number of sent ACK packets", []string{"address", "stream_id"}, nil, ) pktRecvACKTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_ack_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_ack_packets_total"), "total number of received ACK packets", []string{"address", "stream_id"}, nil, ) pktSentNAKTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_nak_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_nak_packets_total"), "total number of received NAK packets", []string{"address", "stream_id"}, nil, ) pktRecvNAKTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_nak_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_nak_packets_total"), "total number of received NAK packets", []string{"address", "stream_id"}, nil, ) sndDurationTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "udt_sent_duration_seconds_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "udt_sent_duration_seconds_total"), "total time duration when UDT is sending data (idle time exclusive)", []string{"address", "stream_id"}, nil, ) pktSndDropTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_dropped_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_dropped_packets_total"), "number of too-late-to-send dropped packets", []string{"address", "stream_id"}, nil, ) pktRcvDropTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_dropped_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_dropped_packets_total"), "number of too-late-to play missing packets", []string{"address", "stream_id"}, nil, ) pktRcvUndecryptTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_undecrypted_packets_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_undecrypted_packets_total"), "number of undecrypted packets", []string{"address", "stream_id"}, nil, ) byteSentTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_bytes_total"), "total number of sent data bytes, including retransmissions", []string{"address", "stream_id"}, nil, ) byteRecvTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_bytes_total"), "total number of received bytes", []string{"address", "stream_id"}, nil, ) byteRcvLossTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_lost_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_lost_bytes_total"), "total number of lost bytes", []string{"address", "stream_id"}, nil, ) byteRetransTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "retransmitted_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "retransmitted_bytes_total"), "total number of retransmitted bytes", []string{"address", "stream_id"}, nil, ) byteSndDropTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "sent_dropped_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "sent_dropped_bytes_total"), "number of too-late-to-send dropped bytes", []string{"address", "stream_id"}, nil, ) byteRcvDropTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_dropped_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_dropped_bytes_total"), "number of too-late-to play missing bytes (estimate based on average packet size)", []string{"address", "stream_id"}, nil, ) byteRcvUndecryptTotalDesc = prometheus.NewDesc( - prometheus.BuildFQName(config.MetricsNamespace, srtSubsystem, "receive_undecrypted_bytes_total"), + prometheus.BuildFQName(metrics.Namespace, srtSubsystem, "receive_undecrypted_bytes_total"), "number of undecrypted bytes", []string{"address", "stream_id"}, nil, ) diff --git a/auth/http.go b/auth/http.go index fdcba27..fe868c4 100644 --- a/auth/http.go +++ b/auth/http.go @@ -6,7 +6,26 @@ import ( "net/url" "time" + "github.com/voc/srtrelay/internal/metrics" "github.com/voc/srtrelay/stream" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +var ( + requestDurations = promauto.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: metrics.Namespace, + Subsystem: "auth", + Name: "request_duration_seconds", + Help: "A histogram of auth http request latencies.", + Buckets: prometheus.DefBuckets, + NativeHistogramBucketFactor: 1.1, + }, + []string{"url", "application"}, + ) ) type httpAuth struct { @@ -33,11 +52,13 @@ type HTTPAuthConfig struct { } // NewHttpAuth creates an Authenticator with a HTTP backend -func NewHTTPAuth(config HTTPAuthConfig) *httpAuth { +func NewHTTPAuth(authConfig HTTPAuthConfig) *httpAuth { + m := requestDurations.MustCurryWith(prometheus.Labels{"url": authConfig.URL, "application": authConfig.Application}) return &httpAuth{ - config: config, + config: authConfig, client: &http.Client{ - Timeout: time.Duration(config.Timeout), + Timeout: time.Duration(authConfig.Timeout), + Transport: promhttp.InstrumentRoundTripperDuration(m, http.DefaultTransport), }, } } diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go new file mode 100644 index 0000000..119c5b2 --- /dev/null +++ b/internal/metrics/metrics.go @@ -0,0 +1,3 @@ +package metrics + +const Namespace = "srtrelay" diff --git a/relay/channel.go b/relay/channel.go index 716f79a..f137ac0 100644 --- a/relay/channel.go +++ b/relay/channel.go @@ -6,7 +6,7 @@ import ( "sync/atomic" "time" - "github.com/voc/srtrelay/config" + "github.com/voc/srtrelay/internal/metrics" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -17,14 +17,14 @@ const relaySubsystem = "relay" var ( activeClients = promauto.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(config.MetricsNamespace, relaySubsystem, "active_clients"), + Name: prometheus.BuildFQName(metrics.Namespace, relaySubsystem, "active_clients"), Help: "The number of active clients per channel", }, []string{"channel_name"}, ) channelCreatedTimestamp = promauto.NewGaugeVec( prometheus.GaugeOpts{ - Name: prometheus.BuildFQName(config.MetricsNamespace, relaySubsystem, "created_timestamp_seconds"), + Name: prometheus.BuildFQName(metrics.Namespace, relaySubsystem, "created_timestamp_seconds"), Help: "The UNIX timestamp when the channel was created", }, []string{"channel_name"},