Skip to content

Commit

Permalink
Instrument auth package
Browse files Browse the repository at this point in the history
Add a histogram duration transport wrapper to the auth http
client connection.
* Refactor metrics namespace to a separate package to avoid recursive
  imports.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Dec 25, 2024
1 parent cece1cf commit 954c410
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
44 changes: 22 additions & 22 deletions api/metrics.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
)
Expand Down
27 changes: 24 additions & 3 deletions auth/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
},
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package metrics

const Namespace = "srtrelay"
6 changes: 3 additions & 3 deletions relay/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"},
Expand Down

0 comments on commit 954c410

Please sign in to comment.