Add support for sending DogStatsD metrics over Unix Socket

This commit is contained in:
Liam van der Viven 2024-01-29 17:08:05 +01:00 committed by GitHub
parent d37ea3e882
commit 18203f57d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 67 additions and 5 deletions

View file

@ -27,6 +27,8 @@ _Required, Default="127.0.0.1:8125"_
Address instructs exporter to send metrics to datadog-agent at this address. Address instructs exporter to send metrics to datadog-agent at this address.
This address can be a Unix Domain Socket (UDS) address with the following form: `unix:///path/to/datadog.socket`.
```yaml tab="File (YAML)" ```yaml tab="File (YAML)"
metrics: metrics:
datadog: datadog:

View file

@ -153,7 +153,11 @@ nav:
- 'Access Logs': 'observability/access-logs.md' - 'Access Logs': 'observability/access-logs.md'
- 'Metrics': - 'Metrics':
- 'Overview': 'observability/metrics/overview.md' - 'Overview': 'observability/metrics/overview.md'
- 'Datadog': 'observability/metrics/datadog.md'
- 'InfluxDB2': 'observability/metrics/influxdb2.md'
- 'OpenTelemetry': 'observability/metrics/opentelemetry.md' - 'OpenTelemetry': 'observability/metrics/opentelemetry.md'
- 'Prometheus': 'observability/metrics/prometheus.md'
- 'StatsD': 'observability/metrics/statsd.md'
- 'Tracing': - 'Tracing':
- 'Overview': 'observability/tracing/overview.md' - 'Overview': 'observability/tracing/overview.md'
- 'OpenTelemetry': 'observability/tracing/opentelemetry.md' - 'OpenTelemetry': 'observability/tracing/opentelemetry.md'

View file

@ -2,6 +2,7 @@ package metrics
import ( import (
"context" "context"
"strings"
"time" "time"
"github.com/go-kit/kit/metrics/dogstatsd" "github.com/go-kit/kit/metrics/dogstatsd"
@ -16,6 +17,8 @@ var (
datadogLoopCancelFunc context.CancelFunc datadogLoopCancelFunc context.CancelFunc
) )
const unixAddressPrefix = "unix://"
// Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64 // Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64
const ( const (
ddConfigReloadsName = "config.reload.total" ddConfigReloadsName = "config.reload.total"
@ -99,10 +102,7 @@ func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry {
} }
func initDatadogClient(ctx context.Context, config *types.Datadog) { func initDatadogClient(ctx context.Context, config *types.Datadog) {
address := config.Address network, address := parseDatadogAddress(config.Address)
if len(address) == 0 {
address = "localhost:8125"
}
ctx, datadogLoopCancelFunc = context.WithCancel(ctx) ctx, datadogLoopCancelFunc = context.WithCancel(ctx)
@ -110,10 +110,27 @@ func initDatadogClient(ctx context.Context, config *types.Datadog) {
ticker := time.NewTicker(time.Duration(config.PushInterval)) ticker := time.NewTicker(time.Duration(config.PushInterval))
defer ticker.Stop() defer ticker.Stop()
datadogClient.SendLoop(ctx, ticker.C, "udp", address) datadogClient.SendLoop(ctx, ticker.C, network, address)
}) })
} }
func parseDatadogAddress(address string) (string, string) {
network := "udp"
var addr string
switch {
case strings.HasPrefix(address, unixAddressPrefix):
network = "unix"
addr = address[len(unixAddressPrefix):]
case address != "":
addr = address
default:
addr = "localhost:8125"
}
return network, addr
}
// StopDatadog stops the Datadog metrics pusher. // StopDatadog stops the Datadog metrics pusher.
func StopDatadog() { func StopDatadog() {
if datadogLoopCancelFunc != nil { if datadogLoopCancelFunc != nil {

View file

@ -7,6 +7,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/stvp/go-udp-testing" "github.com/stvp/go-udp-testing"
ptypes "github.com/traefik/paerser/types" ptypes "github.com/traefik/paerser/types"
"github.com/traefik/traefik/v3/pkg/types" "github.com/traefik/traefik/v3/pkg/types"
@ -39,6 +40,44 @@ func TestDatadogWithPrefix(t *testing.T) {
testDatadogRegistry(t, "testPrefix", datadogRegistry) testDatadogRegistry(t, "testPrefix", datadogRegistry)
} }
func TestDatadog_parseDatadogAddress(t *testing.T) {
tests := []struct {
desc string
address string
expNetwork string
expAddress string
}{
{
desc: "empty address",
expNetwork: "udp",
expAddress: "localhost:8125",
},
{
desc: "udp address",
address: "127.0.0.1:8080",
expNetwork: "udp",
expAddress: "127.0.0.1:8080",
},
{
desc: "unix address",
address: "unix:///path/to/datadog.socket",
expNetwork: "unix",
expAddress: "/path/to/datadog.socket",
},
}
for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
gotNetwork, gotAddress := parseDatadogAddress(test.address)
assert.Equal(t, test.expNetwork, gotNetwork)
assert.Equal(t, test.expAddress, gotAddress)
})
}
}
func testDatadogRegistry(t *testing.T, metricsPrefix string, datadogRegistry Registry) { func testDatadogRegistry(t *testing.T, metricsPrefix string, datadogRegistry Registry) {
t.Helper() t.Helper()