From ae26c506f51d9b58d74874915ed20a723f0a2bba Mon Sep 17 00:00:00 2001 From: SuperQ Date: Thu, 2 Sep 2021 11:56:15 +0200 Subject: [PATCH] Add metrics to relay Add some metrics to the statsd relay. * The number of StatsD packets relayed. * The number lines that were too long to relay. Also convert main.go to use promauto for metric registration. Signed-off-by: SuperQ --- main.go | 63 ++++++++++++++++------------------------------ pkg/relay/relay.go | 27 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/main.go b/main.go index 05e6481..d070e5d 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ import ( "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" @@ -50,118 +51,118 @@ const ( ) var ( - eventStats = prometheus.NewCounterVec( + eventStats = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_events_total", Help: "The total number of StatsD events seen.", }, []string{"type"}, ) - eventsFlushed = prometheus.NewCounter( + eventsFlushed = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_event_queue_flushed_total", Help: "Number of times events were flushed to exporter", }, ) - eventsUnmapped = prometheus.NewCounter( + eventsUnmapped = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_events_unmapped_total", Help: "The total number of StatsD events no mapping was found for.", }) - udpPackets = prometheus.NewCounter( + udpPackets = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_udp_packets_total", Help: "The total number of StatsD packets received over UDP.", }, ) - tcpConnections = prometheus.NewCounter( + tcpConnections = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_tcp_connections_total", Help: "The total number of TCP connections handled.", }, ) - tcpErrors = prometheus.NewCounter( + tcpErrors = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_tcp_connection_errors_total", Help: "The number of errors encountered reading from TCP.", }, ) - tcpLineTooLong = prometheus.NewCounter( + tcpLineTooLong = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_tcp_too_long_lines_total", Help: "The number of lines discarded due to being too long.", }, ) - unixgramPackets = prometheus.NewCounter( + unixgramPackets = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_unixgram_packets_total", Help: "The total number of StatsD packets received over Unixgram.", }, ) - linesReceived = prometheus.NewCounter( + linesReceived = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_lines_total", Help: "The total number of StatsD lines received.", }, ) - samplesReceived = prometheus.NewCounter( + samplesReceived = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_samples_total", Help: "The total number of StatsD samples received.", }, ) - sampleErrors = prometheus.NewCounterVec( + sampleErrors = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_sample_errors_total", Help: "The total number of errors parsing StatsD samples.", }, []string{"reason"}, ) - tagsReceived = prometheus.NewCounter( + tagsReceived = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_tags_total", Help: "The total number of DogStatsD tags processed.", }, ) - tagErrors = prometheus.NewCounter( + tagErrors = promauto.NewCounter( prometheus.CounterOpts{ Name: "statsd_exporter_tag_errors_total", Help: "The number of errors parsing DogStatsD tags.", }, ) - configLoads = prometheus.NewCounterVec( + configLoads = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_config_reloads_total", Help: "The number of configuration reloads.", }, []string{"outcome"}, ) - mappingsCount = prometheus.NewGauge(prometheus.GaugeOpts{ + mappingsCount = promauto.NewGauge(prometheus.GaugeOpts{ Name: "statsd_exporter_loaded_mappings", Help: "The current number of configured metric mappings.", }) - conflictingEventStats = prometheus.NewCounterVec( + conflictingEventStats = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_events_conflict_total", Help: "The total number of StatsD events with conflicting names.", }, []string{"type"}, ) - errorEventStats = prometheus.NewCounterVec( + errorEventStats = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_events_error_total", Help: "The total number of StatsD events discarded due to errors.", }, []string{"reason"}, ) - eventsActions = prometheus.NewCounterVec( + eventsActions = promauto.NewCounterVec( prometheus.CounterOpts{ Name: "statsd_exporter_events_actions_total", Help: "The total number of StatsD events by action.", }, []string{"action"}, ) - metricsCount = prometheus.NewGaugeVec( + metricsCount = promauto.NewGaugeVec( prometheus.GaugeOpts{ Name: "statsd_exporter_metrics_total", Help: "The total number of metrics.", @@ -170,29 +171,6 @@ var ( ) ) -func init() { - prometheus.MustRegister(version.NewCollector("statsd_exporter")) - prometheus.MustRegister(eventStats) - prometheus.MustRegister(eventsFlushed) - prometheus.MustRegister(eventsUnmapped) - prometheus.MustRegister(udpPackets) - prometheus.MustRegister(tcpConnections) - prometheus.MustRegister(tcpErrors) - prometheus.MustRegister(tcpLineTooLong) - prometheus.MustRegister(unixgramPackets) - prometheus.MustRegister(linesReceived) - prometheus.MustRegister(samplesReceived) - prometheus.MustRegister(sampleErrors) - prometheus.MustRegister(tagsReceived) - prometheus.MustRegister(tagErrors) - prometheus.MustRegister(configLoads) - prometheus.MustRegister(mappingsCount) - prometheus.MustRegister(conflictingEventStats) - prometheus.MustRegister(errorEventStats) - prometheus.MustRegister(eventsActions) - prometheus.MustRegister(metricsCount) -} - // uncheckedCollector wraps a Collector but its Describe method yields no Desc. // This allows incoming metrics to have inconsistent label sets type uncheckedCollector struct { @@ -310,6 +288,7 @@ func main() { level.Error(logger).Log("msg", "failed to set log level", "error", err) os.Exit(1) } + prometheus.MustRegister(version.NewCollector("statsd_exporter")) parser := line.NewParser() if *dogstatsdTagsEnabled { diff --git a/pkg/relay/relay.go b/pkg/relay/relay.go index d023616..e1b280c 100644 --- a/pkg/relay/relay.go +++ b/pkg/relay/relay.go @@ -22,6 +22,8 @@ import ( "github.com/go-kit/log" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/statsd_exporter/pkg/level" ) @@ -31,8 +33,28 @@ type Relay struct { conn *net.UDPConn logger log.Logger packetLength uint + + packetsTotal prometheus.Counter + longLinesTotal prometheus.Counter } +var ( + relayPacketsTotal = promauto.NewCounterVec( + prometheus.CounterOpts{ + Name: "statsd_exporter_relay_packets_total", + Help: "The number of StatsD packets relayed.", + }, + []string{"target"}, + ) + relayLongLinesTotal = promauto.NewCounterVec( + prometheus.CounterOpts{ + Name: "statsd_exporter_relay_long_lines_total", + Help: "The number lines that were too long to relay.", + }, + []string{"target"}, + ) +) + // NewRelay creates a statsd UDP relay. It can be used to send copies of statsd raw // lines to a separate service. func NewRelay(l log.Logger, target string, packetLength uint) (*Relay, error) { @@ -53,6 +75,9 @@ func NewRelay(l log.Logger, target string, packetLength uint) (*Relay, error) { conn: conn, logger: l, packetLength: packetLength, + + packetsTotal: relayPacketsTotal.WithLabelValues(target), + longLinesTotal: relayLongLinesTotal.WithLabelValues(target), } // Startup the UDP sender. @@ -106,6 +131,7 @@ func (r *Relay) sendPacket(buf []byte) error { } level.Debug(r.logger).Log("msg", "Sending packet", "length", len(buf), "data", buf) _, err := r.conn.WriteToUDP(buf, r.addr) + r.packetsTotal.Inc() return err } @@ -118,6 +144,7 @@ func (r *Relay) RelayLine(l string) { } if lineLength > r.packetLength-1 { level.Warn(r.logger).Log("msg", "line too long, not relaying", "length", lineLength, "max", r.packetLength) + r.longLinesTotal.Inc() return } level.Debug(r.logger).Log("msg", "Relaying line", "line", l)