Skip to content

Commit c520d1a

Browse files
froquetristanmorgan
authored andcommitted
Adds handling of Datadog reserved tag keys
Prepends "fabio-" to reserved tag keys https://docs.datadoghq.com/getting_started/tagging/#overview
1 parent 33c876b commit c520d1a

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

metrics/provider_dogstatsd.go

+51-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type DogstatsdProvider struct {
1616
}
1717

1818
func (dp *DogstatsdProvider) NewCounter(name string, labels ...string) gkm.Counter {
19-
return dp.D.NewCounter(name, 1)
19+
return &dogstatsdCounter{dp.D.NewCounter(name, 1)}
2020
}
2121

2222
func (dp *DogstatsdProvider) NewGauge(name string, labels ...string) gkm.Gauge {
23-
return dp.D.NewGauge(name)
23+
return &dogstatsdGauge{dp.D.NewGauge(name)}
2424
}
2525

2626
func (dp *DogstatsdProvider) NewHistogram(name string, labels ...string) gkm.Histogram {
@@ -42,10 +42,59 @@ func NewDogstatsdProvider(prefix, addr string, interval time.Duration) (*Dogstat
4242
return d, nil
4343
}
4444

45+
type dogstatsdCounter struct {
46+
gkm.Counter
47+
}
48+
type dogstatsdGauge struct {
49+
gkm.Gauge
50+
}
4551
type dogstatsdHistogram struct {
4652
gkm.Histogram
4753
}
4854

4955
func (dh *dogstatsdHistogram) Observe(value float64) {
5056
dh.Histogram.Observe(value * 1000.0)
5157
}
58+
59+
func (dh *dogstatsdCounter) With(labelValues ...string) gkm.Counter {
60+
return dh.Counter.With(correctReservedTagKeys(labelValues)...)
61+
}
62+
63+
func (dh *dogstatsdGauge) With(labelValues ...string) gkm.Gauge {
64+
return dh.Gauge.With(correctReservedTagKeys(labelValues)...)
65+
}
66+
67+
func (dh *dogstatsdHistogram) With(labelValues ...string) gkm.Histogram {
68+
return dh.Histogram.With(correctReservedTagKeys(labelValues)...)
69+
}
70+
71+
func correctReservedTagKeys(labelValues []string) []string {
72+
var rval []string
73+
for i, v := range labelValues {
74+
if i%2 == 0 {
75+
rval = append(rval, correctReservedTagKey(v))
76+
} else {
77+
rval = append(rval, v)
78+
}
79+
}
80+
return rval
81+
}
82+
83+
func correctReservedTagKey(label string) string {
84+
switch label {
85+
case "host":
86+
return "fabio-host"
87+
case "device":
88+
return "fabio-device"
89+
case "source":
90+
return "fabio-source"
91+
case "service":
92+
return "fabio-service"
93+
case "env":
94+
return "fabio-env"
95+
case "version":
96+
return "fabio-version"
97+
default:
98+
return label
99+
}
100+
}

metrics/provider_dogstatsd_test.go

+17-15
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ func TestDogstatsdProvider(t *testing.T) {
1414
d := dogstatsd.New(prefix, log.NewNopLogger())
1515
provider := &DogstatsdProvider{D: d}
1616
for _, tst := range []struct {
17-
name string
18-
labels []string
19-
values []string
20-
prefix string
21-
countval float64
22-
gaugeval float64
23-
histoval float64
17+
name string
18+
labels []string
19+
values []string
20+
expected_values []string
21+
prefix string
22+
countval float64
23+
gaugeval float64
24+
histoval float64
2425
}{
2526
{
26-
name: "simpleTest",
27-
labels: []string{"service", "host", "path", "target", "other"},
28-
values: []string{"service", "foo", "host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
29-
prefix: "tst",
30-
countval: 20,
31-
gaugeval: 30,
32-
histoval: (time.Microsecond * 50).Seconds(),
27+
name: "simpleTest",
28+
labels: []string{"service", "host", "path", "target", "other"},
29+
values: []string{"service", "foo", "host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
30+
expected_values: []string{"fabio-service", "foo", "fabio-host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
31+
prefix: "tst",
32+
countval: 20,
33+
gaugeval: 30,
34+
histoval: (time.Microsecond * 50).Seconds(),
3335
},
3436
} {
3537
t.Run(tst.name, func(t *testing.T) {
@@ -59,7 +61,7 @@ func TestDogstatsdProvider(t *testing.T) {
5961
if se.value != v.v {
6062
t.Errorf("%s failed: expected: %.02f, got %02f", v.n, v.v, se.value)
6163
}
62-
if len(tst.values) > 0 && !reflect.DeepEqual(se.tags, tst.values) {
64+
if len(tst.expected_values) > 0 && !reflect.DeepEqual(se.tags, tst.expected_values) {
6365
t.Errorf("tags did not survive round trip parsing")
6466
}
6567
} else {

0 commit comments

Comments
 (0)