From 34fff57424af2b55df34bc7571f53b72d71f0b71 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 25 Aug 2025 21:52:11 +0000 Subject: [PATCH] chore: enable gofumpt with extra rules Signed-off-by: Matthieu MOREL --- .golangci.yml | 7 ++ bridge_test.go | 147 ++++++++++++++++++++++------------ exporter_benchmark_test.go | 2 + line_benchmark_test.go | 2 + pkg/exporter/exporter.go | 2 +- pkg/exporter/exporter_test.go | 1 + pkg/line/line.go | 4 +- pkg/listener/listener.go | 2 +- pkg/mapper/fsm/formatter.go | 4 +- pkg/mapper/fsm/fsm.go | 7 +- pkg/mapper/mapper_test.go | 2 +- pkg/relay/relay_test.go | 4 +- 12 files changed, 122 insertions(+), 62 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8ed3571..6b3fc61 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,4 +23,11 @@ issues: max-same-issues: 0 formatters: enable: + - gofumpt - goimports + settings: + gofumpt: + extra-rules: true + goimports: + local-prefixes: + - github.com/prometheus/statsd_exporter diff --git a/bridge_test.go b/bridge_test.go index e3962af..7928a51 100644 --- a/bridge_test.go +++ b/bridge_test.go @@ -41,7 +41,8 @@ func TestHandlePacket(t *testing.T) { }{ { name: "empty", - }, { + }, + { name: "simple counter", in: "foo:2|c", out: event.Events{ @@ -51,7 +52,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "simple gauge", in: "foo:3|g", out: event.Events{ @@ -61,7 +63,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { name: "gauge with sampling", in: "foo:3|g|@0.2", out: event.Events{ @@ -71,7 +74,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { name: "gauge decrement", in: "foo:-10|g", out: event.Events{ @@ -82,7 +86,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { name: "gauge increment", in: "foo:+10|g", out: event.Events{ @@ -93,7 +98,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { name: "gauge set negative", in: "foo:0|g\nfoo:-1|g", out: event.Events{ @@ -110,7 +116,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { // Test the sequence given here https://github.com/statsd/statsd/blob/master/docs/metric_types.md#gauges name: "gauge up and down", in: "gaugor:333|g\ngaugor:-10|g\ngaugor:+4|g", @@ -134,7 +141,8 @@ func TestHandlePacket(t *testing.T) { GLabels: map[string]string{}, }, }, - }, { + }, + { name: "simple timer", in: "foo:200|ms", out: event.Events{ @@ -144,7 +152,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "simple histogram", in: "foo:200|h", out: event.Events{ @@ -154,7 +163,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "simple distribution", in: "foo:200|d", out: event.Events{ @@ -164,7 +174,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "distribution with sampling", in: "foo:0.01|d|@0.2|#tag1:bar,#tag2:baz", out: event.Events{ @@ -194,7 +205,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "librato tag extension", in: "foo#tag1=bar,tag2=baz:100|c", out: event.Events{ @@ -204,7 +216,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "librato tag extension with tag keys unsupported by prometheus", in: "foo#09digits=0,tag.with.dots=1:100|c", out: event.Events{ @@ -214,7 +227,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"_09digits": "0", "tag_with_dots": "1"}, }, }, - }, { + }, + { name: "influxdb tag extension", in: "foo,tag1=bar,tag2=baz:100|c", out: event.Events{ @@ -224,7 +238,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "SignalFx tag extension", in: "foo.[tag1=bar,tag2=baz]test:100|c", out: event.Events{ @@ -234,7 +249,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "SignalFx tag extension, tags at end of name", in: "foo.test[tag1=bar,tag2=baz]:100|c", out: event.Events{ @@ -244,7 +260,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "SignalFx tag extension, tags at beginning of name", in: "[tag1=bar,tag2=baz]foo.test:100|c", out: event.Events{ @@ -254,7 +271,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "SignalFx tag extension, no tags", in: "foo.[]test:100|c", out: event.Events{ @@ -264,7 +282,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "SignalFx tag extension, non-kv tags", in: "foo.[tag1,tag2]test:100|c", out: event.Events{ @@ -274,7 +293,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "SignalFx tag extension, missing closing bracket", in: "[tag1=bar,tag2=bazfoo.test:100|c", out: event.Events{ @@ -284,7 +304,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "SignalFx tag extension, missing opening bracket", in: "tag1=bar,tag2=baz]foo.test:100|c", out: event.Events{ @@ -294,7 +315,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "influxdb tag extension with tag keys unsupported by prometheus", in: "foo,09digits=0,tag.with.dots=1:100|c", out: event.Events{ @@ -304,7 +326,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"_09digits": "0", "tag_with_dots": "1"}, }, }, - }, { + }, + { name: "datadog tag extension", in: "foo:100|c|#tag1:bar,tag2:baz", out: event.Events{ @@ -314,7 +337,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "datadog tag extension with # in all keys (as sent by datadog php client)", in: "foo:100|c|#tag1:bar,#tag2:baz", out: event.Events{ @@ -324,7 +348,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "datadog tag extension with tag keys unsupported by prometheus", in: "foo:100|c|#09digits:0,tag.with.dots:1", out: event.Events{ @@ -334,7 +359,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"_09digits": "0", "tag_with_dots": "1"}, }, }, - }, { + }, + { name: "datadog tag extension with valueless tags: ignored", in: "foo:100|c|#tag_without_a_value", out: event.Events{ @@ -344,7 +370,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "datadog tag extension with valueless tags (edge case)", in: "foo:100|c|#tag_without_a_value,tag:value", out: event.Events{ @@ -354,7 +381,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag": "value"}, }, }, - }, { + }, + { name: "datadog tag extension with empty tags (edge case)", in: "foo:100|c|#tag:value,,", out: event.Events{ @@ -364,7 +392,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag": "value"}, }, }, - }, { + }, + { name: "datadog tag extension with sampling", in: "foo:100|c|@0.1|#tag1:bar,#tag2:baz", out: event.Events{ @@ -374,23 +403,28 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "librato/dogstatsd mixed tag styles without sampling", in: "foo#tag1=foo,tag3=bing:100|c|#tag1:bar,#tag2:baz", out: event.Events{}, - }, { + }, + { name: "signalfx/dogstatsd mixed tag styles without sampling", in: "foo[tag1=foo,tag3=bing]:100|c|#tag1:bar,#tag2:baz", out: event.Events{}, - }, { + }, + { name: "influxdb/dogstatsd mixed tag styles without sampling", in: "foo,tag1=foo,tag3=bing:100|c|#tag1:bar,#tag2:baz", out: event.Events{}, - }, { + }, + { name: "mixed tag styles with sampling", in: "foo#tag1=foo,tag3=bing:100|c|@0.1|#tag1:bar,#tag2:baz", out: event.Events{}, - }, { + }, + { name: "histogram with sampling", in: "foo:0.01|h|@0.2|#tag1:bar,#tag2:baz", out: event.Events{ @@ -420,7 +454,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{"tag1": "bar", "tag2": "baz"}, }, }, - }, { + }, + { name: "datadog tag extension with multiple colons", in: "foo:100|c|@0.1|#tag1:foo:bar", out: event.Events{ @@ -430,13 +465,16 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag1": "foo:bar"}, }, }, - }, { + }, + { name: "datadog tag extension with invalid utf8 tag values", in: "foo:100|c|@0.1|#tag:\xc3\x28invalid", - }, { + }, + { name: "datadog tag extension with both valid and invalid utf8 tag values", in: "foo:100|c|@0.1|#tag1:valid,tag2:\xc3\x28invalid", - }, { + }, + { name: "multiple metrics with invalid datadog utf8 tag values", in: "foo:200|c|#tag:value\nfoo:300|c|#tag:\xc3\x28invalid", out: event.Events{ @@ -446,7 +484,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{"tag": "value"}, }, }, - }, { + }, + { name: "combined multiline metrics", in: "foo:200|ms:300|ms:5|c|@0.1:6|g\nbar:1|c:5|ms", out: event.Events{ @@ -481,7 +520,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "timings with sampling factor", in: "foo.timing:0.5|ms|@0.1", out: event.Events{ @@ -496,16 +536,20 @@ func TestHandlePacket(t *testing.T) { &event.ObserverEvent{OMetricName: "foo.timing", OValue: 0.0005, OLabels: map[string]string{}}, &event.ObserverEvent{OMetricName: "foo.timing", OValue: 0.0005, OLabels: map[string]string{}}, }, - }, { + }, + { name: "bad line", in: "foo", - }, { + }, + { name: "bad component", in: "foo:1", - }, { + }, + { name: "bad value", in: "foo:1o|c", - }, { + }, + { name: "illegal sampling factor", in: "foo:1|c|@bar", out: event.Events{ @@ -515,7 +559,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "zero sampling factor", in: "foo:2|c|@0", out: event.Events{ @@ -525,7 +570,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "illegal stat type", in: "foo:2|t", }, @@ -551,7 +597,8 @@ func TestHandlePacket(t *testing.T) { CLabels: map[string]string{}, }, }, - }, { + }, + { name: "ms timer with conversion to seconds", in: "foo:200|ms", out: event.Events{ @@ -561,7 +608,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "histogram with no unit conversion", in: "foo:200|h", out: event.Events{ @@ -571,7 +619,8 @@ func TestHandlePacket(t *testing.T) { OLabels: map[string]string{}, }, }, - }, { + }, + { name: "distribution with no unit conversion", in: "foo:200|d", out: event.Events{ diff --git a/exporter_benchmark_test.go b/exporter_benchmark_test.go index f8ab266..23131e8 100644 --- a/exporter_benchmark_test.go +++ b/exporter_benchmark_test.go @@ -91,9 +91,11 @@ func benchmarkUDPListener(times int, b *testing.B) { func BenchmarkUDPListener1(b *testing.B) { benchmarkUDPListener(1, b) } + func BenchmarkUDPListener5(b *testing.B) { benchmarkUDPListener(5, b) } + func BenchmarkUDPListener50(b *testing.B) { benchmarkUDPListener(50, b) } diff --git a/line_benchmark_test.go b/line_benchmark_test.go index b917bcd..1fb1863 100644 --- a/line_benchmark_test.go +++ b/line_benchmark_test.go @@ -65,9 +65,11 @@ func benchmarkLinesToEvents(times int, b *testing.B, input []string) { func BenchmarkLineToEventsMixed1(b *testing.B) { benchmarkLinesToEvents(1, b, mixedLines) } + func BenchmarkLineToEventsMixed5(b *testing.B) { benchmarkLinesToEvents(5, b, mixedLines) } + func BenchmarkLineToEventsMixed50(b *testing.B) { benchmarkLinesToEvents(50, b, mixedLines) } diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index 01206d0..07d5a46 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -196,7 +196,7 @@ func (b *Exporter) handleEvent(thisEvent event.Event) { } } -func NewExporter(reg prometheus.Registerer, mapper *mapper.MetricMapper, logger *slog.Logger, eventsActions *prometheus.CounterVec, eventsUnmapped prometheus.Counter, errorEventStats *prometheus.CounterVec, eventStats *prometheus.CounterVec, conflictingEventStats *prometheus.CounterVec, metricsCount *prometheus.GaugeVec) *Exporter { +func NewExporter(reg prometheus.Registerer, mapper *mapper.MetricMapper, logger *slog.Logger, eventsActions *prometheus.CounterVec, eventsUnmapped prometheus.Counter, errorEventStats, eventStats, conflictingEventStats *prometheus.CounterVec, metricsCount *prometheus.GaugeVec) *Exporter { return &Exporter{ Mapper: mapper, Registry: registry.NewRegistry(reg, mapper), diff --git a/pkg/exporter/exporter_test.go b/pkg/exporter/exporter_test.go index 213949b..0eea4a8 100644 --- a/pkg/exporter/exporter_test.go +++ b/pkg/exporter/exporter_test.go @@ -857,6 +857,7 @@ func TestHistogramUnits(t *testing.T) { t.Fatalf("Received unexpected value for histogram observation %f != .300", *value) } } + func TestCounterIncrement(t *testing.T) { // Start exporter with a synchronous channel events := make(chan event.Events) diff --git a/pkg/line/line.go b/pkg/line/line.go index 7ee3570..827d152 100644 --- a/pkg/line/line.go +++ b/pkg/line/line.go @@ -201,7 +201,7 @@ func (p *Parser) parseNameAndTags(name string, labels map[string]string, tagErro return name } -func (p *Parser) LineToEvents(line string, sampleErrors prometheus.CounterVec, samplesReceived prometheus.Counter, tagErrors prometheus.Counter, tagsReceived prometheus.Counter, logger *slog.Logger) event.Events { +func (p *Parser) LineToEvents(line string, sampleErrors prometheus.CounterVec, samplesReceived, tagErrors, tagsReceived prometheus.Counter, logger *slog.Logger) event.Events { events := event.Events{} if line == "" { return events @@ -276,7 +276,7 @@ samples: } valueStr, statType := components[0], components[1] - var relative = false + relative := false if strings.Index(valueStr, "+") == 0 || strings.Index(valueStr, "-") == 0 { relative = true } diff --git a/pkg/listener/listener.go b/pkg/listener/listener.go index ac27c14..9f7a502 100644 --- a/pkg/listener/listener.go +++ b/pkg/listener/listener.go @@ -28,7 +28,7 @@ import ( ) type Parser interface { - LineToEvents(line string, sampleErrors prometheus.CounterVec, samplesReceived prometheus.Counter, tagErrors prometheus.Counter, tagsReceived prometheus.Counter, logger *slog.Logger) event.Events + LineToEvents(line string, sampleErrors prometheus.CounterVec, samplesReceived, tagErrors, tagsReceived prometheus.Counter, logger *slog.Logger) event.Events } type StatsDUDPListener struct { diff --git a/pkg/mapper/fsm/formatter.go b/pkg/mapper/fsm/formatter.go index a18d220..8a9d2eb 100644 --- a/pkg/mapper/fsm/formatter.go +++ b/pkg/mapper/fsm/formatter.go @@ -20,9 +20,7 @@ import ( "strings" ) -var ( - templateReplaceCaptureRE = regexp.MustCompile(`\$\{?([a-zA-Z0-9_\$]+)\}?`) -) +var templateReplaceCaptureRE = regexp.MustCompile(`\$\{?([a-zA-Z0-9_\$]+)\}?`) type TemplateFormatter struct { captureIndexes []int diff --git a/pkg/mapper/fsm/fsm.go b/pkg/mapper/fsm/fsm.go index 9928dc0..a77f972 100644 --- a/pkg/mapper/fsm/fsm.go +++ b/pkg/mapper/fsm/fsm.go @@ -66,7 +66,7 @@ func NewFSM(metricTypes []string, maxPossibleTransitions int, orderingDisabled b // AddState adds a mapping rule into the existing FSM. // The maxPossibleTransitions parameter sets the expected count of transitions left. // The result parameter sets the generic type to be returned when fsm found a match in GetMapping. -func (f *FSM) AddState(match string, matchMetricType string, maxPossibleTransitions int, result interface{}) int { +func (f *FSM) AddState(match, matchMetricType string, maxPossibleTransitions int, result interface{}) int { // first split by "." matchFields := strings.Split(match, ".") // fill into our FSM @@ -125,7 +125,7 @@ func (f *FSM) AddState(match string, matchMetricType string, maxPossibleTransiti // GetMapping using the fsm to find matching rules according to given statsdMetric and statsdMetricType. // If it finds a match, the final state and the captured strings are returned; // if there's no match found, nil and a empty list will be returned. -func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mappingState, []string) { +func (f *FSM) GetMapping(statsdMetric, statsdMetricType string) (*mappingState, []string) { matchFields := strings.Split(statsdMetric, ".") currentState := f.root.transitions[statsdMetricType] @@ -168,7 +168,8 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping if !present || fieldsLeft > altState.maxRemainingLength || fieldsLeft < altState.minRemainingLength { } else { // push to backtracking stack - newCursor := fsmBacktrackStackCursor{prev: backtrackCursor, state: altState, + newCursor := fsmBacktrackStackCursor{ + prev: backtrackCursor, state: altState, fieldIndex: i, captureIndex: captureIdx, currentCapture: field, } diff --git a/pkg/mapper/mapper_test.go b/pkg/mapper/mapper_test.go index eab7439..4c19bad 100644 --- a/pkg/mapper/mapper_test.go +++ b/pkg/mapper/mapper_test.go @@ -216,7 +216,7 @@ mappings: }, }, }, - //Config with backtracking, the non-matched rule has star(s) + // Config with backtracking, the non-matched rule has star(s) // A metric like full.name.anothertest will first match full.name.* and then tries // to match *.dummy.* and then failed. // This test case makes sure the captures in the non-matched later rule diff --git a/pkg/relay/relay_test.go b/pkg/relay/relay_test.go index ea0fca0..b5e88e7 100644 --- a/pkg/relay/relay_test.go +++ b/pkg/relay/relay_test.go @@ -22,8 +22,9 @@ import ( "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/promslog" - "github.com/prometheus/statsd_exporter/pkg/clock" "github.com/stvp/go-udp-testing" + + "github.com/prometheus/statsd_exporter/pkg/clock" ) func TestRelay_RelayLine(t *testing.T) { @@ -60,7 +61,6 @@ func TestRelay_RelayLine(t *testing.T) { "localhost:1160", 200, ) - if err != nil { t.Errorf("Did not expect error while creating relay.") }