Skip to content

Commit

Permalink
Adds handling of Datadog reserved tag keys
Browse files Browse the repository at this point in the history
  • Loading branch information
froque committed Nov 7, 2024
1 parent d2d8504 commit a6445a5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
53 changes: 51 additions & 2 deletions metrics/provider_dogstatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type DogstatsdProvider struct {
}

func (dp *DogstatsdProvider) NewCounter(name string, labels ...string) gkm.Counter {
return dp.D.NewCounter(name, 1)
return &dogstatsdCounter{dp.D.NewCounter(name, 1)}
}

func (dp *DogstatsdProvider) NewGauge(name string, labels ...string) gkm.Gauge {
return dp.D.NewGauge(name)
return &dogstatsdGauge{dp.D.NewGauge(name)}
}

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

type dogstatsdCounter struct {
gkm.Counter
}
type dogstatsdGauge struct {
gkm.Gauge
}
type dogstatsdHistogram struct {
gkm.Histogram
}

func (dh *dogstatsdHistogram) Observe(value float64) {
dh.Histogram.Observe(value * 1000.0)
}

func (dh *dogstatsdCounter) With(labelValues ...string) gkm.Counter {
return dh.Counter.With(correctReservedTagKeys(labelValues)...)
}

func (dh *dogstatsdGauge) With(labelValues ...string) gkm.Gauge {
return dh.Gauge.With(correctReservedTagKeys(labelValues)...)
}

func (dh *dogstatsdHistogram) With(labelValues ...string) gkm.Histogram {
return dh.Histogram.With(correctReservedTagKeys(labelValues)...)
}

func correctReservedTagKeys(labelValues []string) []string {
var rval []string
for i, v := range labelValues {
if i%2 == 0 {
rval = append(rval, correctReservedTagKey(v))
} else {
rval = append(rval, v)
}
}
return rval
}

func correctReservedTagKey(label string) string {
switch label {
case "host":
return "fabio-host"
case "device":
return "fabio-device"
case "source":
return "fabio-source"
case "service":
return "fabio-service"
case "env":
return "fabio-env"
case "version":
return "fabio-version"
default:
return label
}
}
32 changes: 17 additions & 15 deletions metrics/provider_dogstatsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ func TestDogstatsdProvider(t *testing.T) {
d := dogstatsd.New(prefix, log.NewNopLogger())
provider := &DogstatsdProvider{D: d}
for _, tst := range []struct {
name string
labels []string
values []string
prefix string
countval float64
gaugeval float64
histoval float64
name string
labels []string
values []string
expected_values []string
prefix string
countval float64
gaugeval float64
histoval float64
}{
{
name: "simpleTest",
labels: []string{"service", "host", "path", "target", "other"},
values: []string{"service", "foo", "host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
prefix: "tst",
countval: 20,
gaugeval: 30,
histoval: (time.Microsecond * 50).Seconds(),
name: "simpleTest",
labels: []string{"service", "host", "path", "target", "other"},
values: []string{"service", "foo", "host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
expected_values: []string{"fabio-service", "foo", "fabio-host", "bar", "path", "/asdf", "target", "http://jkl.org:1234", "other", "trailer"},
prefix: "tst",
countval: 20,
gaugeval: 30,
histoval: (time.Microsecond * 50).Seconds(),
},
} {
t.Run(tst.name, func(t *testing.T) {
Expand Down Expand Up @@ -59,7 +61,7 @@ func TestDogstatsdProvider(t *testing.T) {
if se.value != v.v {
t.Errorf("%s failed: expected: %.02f, got %02f", v.n, v.v, se.value)
}
if len(tst.values) > 0 && !reflect.DeepEqual(se.tags, tst.values) {
if len(tst.expected_values) > 0 && !reflect.DeepEqual(se.tags, tst.expected_values) {
t.Errorf("tags did not survive round trip parsing")
}
} else {
Expand Down

0 comments on commit a6445a5

Please sign in to comment.