Skip to content

Commit 6b19b7e

Browse files
authored
Merge branch 'main' into bigcache_replacement
2 parents e856439 + 7e0af40 commit 6b19b7e

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

.golangci.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#
22
# Generated by @zondax/cli
33
#
4+
version: "2"
45
run:
56
timeout: 5m
67

78
linters:
89
enable:
9-
- gofmt
1010
- gosec
1111
- goconst
1212
- gocritic
1313
- staticcheck
1414
- unused
15-
- gosimple
1615
- goconst
1716
- bodyclose
1817
- unconvert
@@ -22,5 +21,15 @@ linters:
2221
- copyloopvar
2322
- makezero
2423
- nilerr
25-
- reassign
24+
- reassign
25+
exclusions:
26+
rules:
27+
- source: "^\\s*defer\\s+"
28+
linters:
29+
- errcheck
30+
31+
32+
formatters:
33+
enable:
34+
- gofmt
2635

pkg/metrics/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (t *taskMetrics) performMetricAction(name string, action func(MetricHandler
2020
labels = append(labels, t.appName)
2121

2222
t.mux.RLock()
23-
metricDetail, ok := t.metrics[name]
23+
metricDetail, ok := t.metrics[formatMetricName(name)]
2424
t.mux.RUnlock()
2525

2626
if !ok {

pkg/metrics/register_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metrics
33
import (
44
"github.com/prometheus/client_golang/prometheus"
55
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/mock"
67
"github.com/zondax/golem/pkg/metrics/collectors"
78
"testing"
89
)
@@ -36,3 +37,48 @@ func TestFormatMetricName(t *testing.T) {
3637
})
3738
}
3839
}
40+
41+
func (suite *MetricsTestSuite) TestMetricNameFormatting() {
42+
tests := []struct {
43+
metricName string
44+
formattedName string
45+
}{
46+
{
47+
metricName: "test-metric-name",
48+
formattedName: "test_metric_name",
49+
},
50+
{
51+
metricName: "another.test@metric",
52+
formattedName: "another_test_metric",
53+
},
54+
}
55+
for _, tt := range tests {
56+
suite.Run(tt.metricName, func() {
57+
// Create a gauge for testing
58+
realGauge := prometheus.NewGauge(prometheus.GaugeOpts{
59+
Name: "test_gauge",
60+
Help: "Test gauge",
61+
})
62+
63+
// Create a new task metrics instance
64+
taskMetric := &taskMetrics{
65+
metrics: make(map[string]MetricDetail),
66+
}
67+
68+
// Store the metric with a formatted name
69+
taskMetric.metrics[tt.formattedName] = MetricDetail{
70+
Collector: realGauge,
71+
Handler: suite.mockH,
72+
}
73+
74+
suite.mockH.On("Update", realGauge, 1.0, mock.Anything).Return(nil).Once()
75+
76+
// "another.test@metric" should be formatted to "another_test_metric"
77+
err := taskMetric.UpdateMetric(tt.metricName, 1.0)
78+
79+
// Verify there are no errors and the mock expectations are met
80+
suite.Nil(err)
81+
suite.mockH.AssertExpectations(suite.T())
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)