diff --git a/docs/content/observability/metrics/datadog.md b/docs/content/observability/metrics/datadog.md index 5e133e1df..dd10105b1 100644 --- a/docs/content/observability/metrics/datadog.md +++ b/docs/content/observability/metrics/datadog.md @@ -125,3 +125,24 @@ metrics: --metrics.datadog.pushInterval=10s ``` +#### `prefix` + +_Optional, Default="traefik"_ + +The prefix to use for metrics collection. + +```yaml tab="File (YAML)" +metrics: + datadog: + prefix: traefik +``` + +```toml tab="File (TOML)" +[metrics] + [metrics.datadog] + prefix = "traefik" +``` + +```bash tab="CLI" +--metrics.datadog.prefix="traefik" +``` diff --git a/docs/content/reference/static-configuration/cli-ref.md b/docs/content/reference/static-configuration/cli-ref.md index 86b26f7c8..5cd35d0a6 100644 --- a/docs/content/reference/static-configuration/cli-ref.md +++ b/docs/content/reference/static-configuration/cli-ref.md @@ -240,6 +240,9 @@ Enable metrics on routers. (Default: ```false```) `--metrics.datadog.addserviceslabels`: Enable metrics on services. (Default: ```true```) +`--metrics.datadog.prefix`: +Prefix to use for metrics collection. (Default: ```traefik```) + `--metrics.datadog.pushinterval`: Datadog push interval. (Default: ```10```) diff --git a/docs/content/reference/static-configuration/env-ref.md b/docs/content/reference/static-configuration/env-ref.md index 62a420fd6..4f31461e1 100644 --- a/docs/content/reference/static-configuration/env-ref.md +++ b/docs/content/reference/static-configuration/env-ref.md @@ -240,6 +240,9 @@ Enable metrics on routers. (Default: ```false```) `TRAEFIK_METRICS_DATADOG_ADDSERVICESLABELS`: Enable metrics on services. (Default: ```true```) +`TRAEFIK_METRICS_DATADOG_PREFIX`: +Prefix to use for metrics collection. (Default: ```traefik```) + `TRAEFIK_METRICS_DATADOG_PUSHINTERVAL`: Datadog push interval. (Default: ```10```) diff --git a/docs/content/reference/static-configuration/file.toml b/docs/content/reference/static-configuration/file.toml index d197feab4..5e77ec768 100644 --- a/docs/content/reference/static-configuration/file.toml +++ b/docs/content/reference/static-configuration/file.toml @@ -250,6 +250,7 @@ addEntryPointsLabels = true addRoutersLabels = true addServicesLabels = true + prefix = "foobar" [metrics.statsD] address = "foobar" pushInterval = "42s" diff --git a/docs/content/reference/static-configuration/file.yaml b/docs/content/reference/static-configuration/file.yaml index c6c607137..d445be38d 100644 --- a/docs/content/reference/static-configuration/file.yaml +++ b/docs/content/reference/static-configuration/file.yaml @@ -272,6 +272,7 @@ metrics: addEntryPointsLabels: true addRoutersLabels: true addServicesLabels: true + prefix: foobar statsD: address: foobar pushInterval: 42 diff --git a/pkg/metrics/datadog.go b/pkg/metrics/datadog.go index c006ffe88..31f8b63eb 100644 --- a/pkg/metrics/datadog.go +++ b/pkg/metrics/datadog.go @@ -11,12 +11,10 @@ import ( "github.com/traefik/traefik/v2/pkg/types" ) -var datadogClient = dogstatsd.New("traefik.", kitlog.LoggerFunc(func(keyvals ...interface{}) error { - log.WithoutContext().WithField(log.MetricsProviderName, "datadog").Info(keyvals) - return nil -})) - -var datadogTicker *time.Ticker +var ( + datadogClient *dogstatsd.Dogstatsd + datadogTicker *time.Ticker +) // Metric names consistent with https://github.com/DataDog/integrations-extras/pull/64 const ( @@ -46,6 +44,16 @@ const ( // RegisterDatadog registers the metrics pusher if this didn't happen yet and creates a datadog Registry instance. func RegisterDatadog(ctx context.Context, config *types.Datadog) Registry { + // just to be sure there is a prefix defined + if config.Prefix == "" { + config.Prefix = defaultMetricsPrefix + } + + datadogClient = dogstatsd.New(config.Prefix+".", kitlog.LoggerFunc(func(keyvals ...interface{}) error { + log.WithoutContext().WithField(log.MetricsProviderName, "datadog").Info(keyvals) + return nil + })) + if datadogTicker == nil { datadogTicker = initDatadogClient(ctx, config) } diff --git a/pkg/metrics/datadog_test.go b/pkg/metrics/datadog_test.go index 63c09a6f0..a9fac44c8 100644 --- a/pkg/metrics/datadog_test.go +++ b/pkg/metrics/datadog_test.go @@ -23,34 +23,53 @@ func TestDatadog(t *testing.T) { if !datadogRegistry.IsEpEnabled() || !datadogRegistry.IsRouterEnabled() || !datadogRegistry.IsSvcEnabled() { t.Errorf("DatadogRegistry should return true for IsEnabled(), IsRouterEnabled() and IsSvcEnabled()") } + testDatadogRegistry(t, defaultMetricsPrefix, datadogRegistry) +} + +func TestDatadogWithPrefix(t *testing.T) { + t.Cleanup(func() { + StopDatadog() + }) + + udp.SetAddr(":18125") + // This is needed to make sure that UDP Listener listens for data a bit longer, otherwise it will quit after a millisecond + udp.Timeout = 5 * time.Second + + datadogRegistry := RegisterDatadog(context.Background(), &types.Datadog{Prefix: "testPrefix", Address: ":18125", PushInterval: ptypes.Duration(time.Second), AddEntryPointsLabels: true, AddRoutersLabels: true, AddServicesLabels: true}) + + testDatadogRegistry(t, "testPrefix", datadogRegistry) +} + +func testDatadogRegistry(t *testing.T, metricsPrefix string, datadogRegistry Registry) { + t.Helper() expected := []string{ - "traefik.config.reload.total:1.000000|c\n", - "traefik.config.reload.total:1.000000|c|#failure:true\n", - "traefik.config.reload.lastSuccessTimestamp:1.000000|g\n", - "traefik.config.reload.lastFailureTimestamp:1.000000|g\n", + metricsPrefix + ".config.reload.total:1.000000|c\n", + metricsPrefix + ".config.reload.total:1.000000|c|#failure:true\n", + metricsPrefix + ".config.reload.lastSuccessTimestamp:1.000000|g\n", + metricsPrefix + ".config.reload.lastFailureTimestamp:1.000000|g\n", - "traefik.tls.certs.notAfterTimestamp:1.000000|g|#key:value\n", + metricsPrefix + ".tls.certs.notAfterTimestamp:1.000000|g|#key:value\n", - "traefik.entrypoint.request.total:1.000000|c|#entrypoint:test\n", - "traefik.entrypoint.request.tls.total:1.000000|c|#entrypoint:test,tls_version:foo,tls_cipher:bar\n", - "traefik.entrypoint.request.duration:10000.000000|h|#entrypoint:test\n", - "traefik.entrypoint.connections.open:1.000000|g|#entrypoint:test\n", + metricsPrefix + ".entrypoint.request.total:1.000000|c|#entrypoint:test\n", + metricsPrefix + ".entrypoint.request.tls.total:1.000000|c|#entrypoint:test,tls_version:foo,tls_cipher:bar\n", + metricsPrefix + ".entrypoint.request.duration:10000.000000|h|#entrypoint:test\n", + metricsPrefix + ".entrypoint.connections.open:1.000000|g|#entrypoint:test\n", - "traefik.router.request.total:1.000000|c|#router:demo,service:test,code:404,method:GET\n", - "traefik.router.request.total:1.000000|c|#router:demo,service:test,code:200,method:GET\n", - "traefik.router.request.tls.total:1.000000|c|#router:demo,service:test,tls_version:foo,tls_cipher:bar\n", - "traefik.router.request.duration:10000.000000|h|#router:demo,service:test,code:200\n", - "traefik.router.connections.open:1.000000|g|#router:demo,service:test\n", + metricsPrefix + ".router.request.total:1.000000|c|#router:demo,service:test,code:404,method:GET\n", + metricsPrefix + ".router.request.total:1.000000|c|#router:demo,service:test,code:200,method:GET\n", + metricsPrefix + ".router.request.tls.total:1.000000|c|#router:demo,service:test,tls_version:foo,tls_cipher:bar\n", + metricsPrefix + ".router.request.duration:10000.000000|h|#router:demo,service:test,code:200\n", + metricsPrefix + ".router.connections.open:1.000000|g|#router:demo,service:test\n", - "traefik.service.request.total:1.000000|c|#service:test,code:404,method:GET\n", - "traefik.service.request.total:1.000000|c|#service:test,code:200,method:GET\n", - "traefik.service.request.tls.total:1.000000|c|#service:test,tls_version:foo,tls_cipher:bar\n", - "traefik.service.request.duration:10000.000000|h|#service:test,code:200\n", - "traefik.service.connections.open:1.000000|g|#service:test\n", - "traefik.service.retries.total:2.000000|c|#service:test\n", - "traefik.service.request.duration:10000.000000|h|#service:test,code:200\n", - "traefik.service.server.up:1.000000|g|#service:test,url:http://127.0.0.1,one:two\n", + metricsPrefix + ".service.request.total:1.000000|c|#service:test,code:404,method:GET\n", + metricsPrefix + ".service.request.total:1.000000|c|#service:test,code:200,method:GET\n", + metricsPrefix + ".service.request.tls.total:1.000000|c|#service:test,tls_version:foo,tls_cipher:bar\n", + metricsPrefix + ".service.request.duration:10000.000000|h|#service:test,code:200\n", + metricsPrefix + ".service.connections.open:1.000000|g|#service:test\n", + metricsPrefix + ".service.retries.total:2.000000|c|#service:test\n", + metricsPrefix + ".service.request.duration:10000.000000|h|#service:test,code:200\n", + metricsPrefix + ".service.server.up:1.000000|g|#service:test,url:http://127.0.0.1,one:two\n", } udp.ShouldReceiveAll(t, expected, func() { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 40f3f6da5..9be5249a4 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -8,6 +8,8 @@ import ( "github.com/go-kit/kit/metrics/multi" ) +const defaultMetricsPrefix = "traefik" + // Registry has to implemented by any system that wants to monitor and expose metrics. type Registry interface { // IsEpEnabled shows whether metrics instrumentation is enabled on entry points. diff --git a/pkg/metrics/statsd.go b/pkg/metrics/statsd.go index c1bb635de..7a81dd351 100644 --- a/pkg/metrics/statsd.go +++ b/pkg/metrics/statsd.go @@ -46,7 +46,7 @@ const ( func RegisterStatsd(ctx context.Context, config *types.Statsd) Registry { // just to be sure there is a prefix defined if config.Prefix == "" { - config.Prefix = "traefik" + config.Prefix = defaultMetricsPrefix } statsdClient = statsd.New(config.Prefix+".", kitlog.LoggerFunc(func(keyvals ...interface{}) error { diff --git a/pkg/metrics/statsd_test.go b/pkg/metrics/statsd_test.go index 57f61a35f..63de0abf6 100644 --- a/pkg/metrics/statsd_test.go +++ b/pkg/metrics/statsd_test.go @@ -23,7 +23,7 @@ func TestStatsD(t *testing.T) { statsdRegistry := RegisterStatsd(context.Background(), &types.Statsd{Address: ":18125", PushInterval: ptypes.Duration(time.Second), AddEntryPointsLabels: true, AddRoutersLabels: true, AddServicesLabels: true}) - testRegistry(t, "", statsdRegistry) + testRegistry(t, defaultMetricsPrefix, statsdRegistry) } func TestStatsDWithPrefix(t *testing.T) { @@ -47,10 +47,6 @@ func testRegistry(t *testing.T, metricsPrefix string, registry Registry) { t.Errorf("Statsd registry should return true for IsEnabled(), IsRouterEnabled() and IsSvcEnabled()") } - if metricsPrefix == "" { - metricsPrefix = "traefik" - } - expected := []string{ metricsPrefix + ".config.reload.total:1.000000|c\n", metricsPrefix + ".config.reload.total.failure:1.000000|c\n", diff --git a/pkg/types/metrics.go b/pkg/types/metrics.go index 888432803..003097cff 100644 --- a/pkg/types/metrics.go +++ b/pkg/types/metrics.go @@ -41,6 +41,7 @@ type Datadog struct { AddEntryPointsLabels bool `description:"Enable metrics on entry points." json:"addEntryPointsLabels,omitempty" toml:"addEntryPointsLabels,omitempty" yaml:"addEntryPointsLabels,omitempty" export:"true"` AddRoutersLabels bool `description:"Enable metrics on routers." json:"addRoutersLabels,omitempty" toml:"addRoutersLabels,omitempty" yaml:"addRoutersLabels,omitempty" export:"true"` AddServicesLabels bool `description:"Enable metrics on services." json:"addServicesLabels,omitempty" toml:"addServicesLabels,omitempty" yaml:"addServicesLabels,omitempty" export:"true"` + Prefix string `description:"Prefix to use for metrics collection." json:"prefix,omitempty" toml:"prefix,omitempty" yaml:"prefix,omitempty" export:"true"` } // SetDefaults sets the default values. @@ -58,6 +59,7 @@ func (d *Datadog) SetDefaults() { d.PushInterval = types.Duration(10 * time.Second) d.AddEntryPointsLabels = true d.AddServicesLabels = true + d.Prefix = "traefik" } // Statsd contains address and metrics pushing interval configuration.