Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configurable prefix for statsd metrics collection #5336

Merged
merged 3 commits into from Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/content/observability/metrics/statsd.md
Expand Up @@ -103,3 +103,25 @@ metrics:
```bash tab="CLI"
--metrics.statsd.pushInterval=10s
```

#### `prefix`

_Optional, Default="traefik"_

The prefix to use for metrics collection.

```toml tab="File (TOML)"
[metrics]
[metrics.statsD]
prefix = "traefik"
```

```yaml tab="File (YAML)"
metrics:
statsD:
prefix: traefik
```

```bash tab="CLI"
--metrics.statsd.prefix="traefik"
```
3 changes: 3 additions & 0 deletions docs/content/reference/static-configuration/cli-ref.md
Expand Up @@ -225,6 +225,9 @@ StatsD address. (Default: ```localhost:8125```)
`--metrics.statsd.addserviceslabels`:
Enable metrics on services. (Default: ```true```)

`--metrics.statsd.prefix`:
Prefix to use for metrics collection. (Default: ```traefik```)

`--metrics.statsd.pushinterval`:
StatsD push interval. (Default: ```10```)

Expand Down
3 changes: 3 additions & 0 deletions docs/content/reference/static-configuration/env-ref.md
Expand Up @@ -225,6 +225,9 @@ StatsD address. (Default: ```localhost:8125```)
`TRAEFIK_METRICS_STATSD_ADDSERVICESLABELS`:
Enable metrics on services. (Default: ```true```)

`TRAEFIK_METRICS_STATSD_PREFIX`:
Prefix to use for metrics collection. (Default: ```traefik```)

`TRAEFIK_METRICS_STATSD_PUSHINTERVAL`:
StatsD push interval. (Default: ```10```)

Expand Down
1 change: 1 addition & 0 deletions docs/content/reference/static-configuration/file.toml
Expand Up @@ -151,6 +151,7 @@
pushInterval = "10s"
addEntryPointsLabels = true
addServicesLabels = true
prefix = "traefik"
[metrics.influxDB]
address = "foobar"
protocol = "foobar"
Expand Down
1 change: 1 addition & 0 deletions docs/content/reference/static-configuration/file.yaml
Expand Up @@ -158,6 +158,7 @@ metrics:
pushInterval: 42
addEntryPointsLabels: true
addServicesLabels: true
prefix: traefik
influxDB:
address: foobar
protocol: foobar
Expand Down
16 changes: 11 additions & 5 deletions pkg/metrics/statsd.go
Expand Up @@ -11,11 +11,7 @@ import (
"github.com/go-kit/kit/metrics/statsd"
)

var statsdClient = statsd.New("traefik.", kitlog.LoggerFunc(func(keyvals ...interface{}) error {
log.WithoutContext().WithField(log.MetricsProviderName, "statsd").Info(keyvals)
return nil
}))

var statsdClient *statsd.Statsd
var statsdTicker *time.Ticker

const (
Expand All @@ -35,6 +31,16 @@ const (

// RegisterStatsd registers the metrics pusher if this didn't happen yet and creates a statsd Registry instance.
func RegisterStatsd(ctx context.Context, config *types.Statsd) Registry {
// just to be sure there is a prefix defined
if config.Prefix == "" {
config.Prefix = "traefik"
}

statsdClient = statsd.New(config.Prefix+".", kitlog.LoggerFunc(func(keyvals ...interface{}) error {
log.WithoutContext().WithField(log.MetricsProviderName, "statsd").Info(keyvals)
return nil
}))

if statsdTicker == nil {
statsdTicker = initStatsdTicker(ctx, config)
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/metrics/statsd_test.go
Expand Up @@ -49,3 +49,43 @@ func TestStatsD(t *testing.T) {
statsdRegistry.ServiceServerUpGauge().With("service:test", "url", "http://127.0.0.1").Set(1)
})
}

func TestStatsDWithPrefix(t *testing.T) {
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

statsdRegistry := RegisterStatsd(context.Background(), &types.Statsd{Address: ":18125", PushInterval: types.Duration(time.Second), AddEntryPointsLabels: true, AddServicesLabels: true, Prefix: "testPrefix"})
defer StopStatsd()

if !statsdRegistry.IsEpEnabled() || !statsdRegistry.IsSvcEnabled() {
t.Errorf("Statsd registry should return true for IsEnabled()")
}

expected := []string{
// We are only validating counts, as it is nearly impossible to validate latency, since it varies every run
"testPrefix.service.request.total:2.000000|c\n",
"testPrefix.service.retries.total:2.000000|c\n",
"testPrefix.service.request.duration:10000.000000|ms",
"testPrefix.config.reload.total:1.000000|c\n",
"testPrefix.config.reload.total:1.000000|c\n",
"testPrefix.entrypoint.request.total:1.000000|c\n",
"testPrefix.entrypoint.request.duration:10000.000000|ms",
"testPrefix.entrypoint.connections.open:1.000000|g\n",
"testPrefix.service.server.up:1.000000|g\n",
}

udp.ShouldReceiveAll(t, expected, func() {
statsdRegistry.ServiceReqsCounter().With("service", "test", "code", string(http.StatusOK), "method", http.MethodGet).Add(1)
statsdRegistry.ServiceReqsCounter().With("service", "test", "code", string(http.StatusNotFound), "method", http.MethodGet).Add(1)
statsdRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
statsdRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
statsdRegistry.ServiceReqDurationHistogram().With("service", "test", "code", string(http.StatusOK)).Observe(10000)
statsdRegistry.ConfigReloadsCounter().Add(1)
statsdRegistry.ConfigReloadsFailureCounter().Add(1)
statsdRegistry.EntryPointReqsCounter().With("entrypoint", "test").Add(1)
statsdRegistry.EntryPointReqDurationHistogram().With("entrypoint", "test").Observe(10000)
statsdRegistry.EntryPointOpenConnsGauge().With("entrypoint", "test").Set(1)
statsdRegistry.ServiceServerUpGauge().With("service:test", "url", "http://127.0.0.1").Set(1)
})
}
2 changes: 2 additions & 0 deletions pkg/types/metrics.go
Expand Up @@ -50,6 +50,7 @@ type Statsd struct {
PushInterval Duration `description:"StatsD push interval." json:"pushInterval,omitempty" toml:"pushInterval,omitempty" yaml:"pushInterval,omitempty" export:"true"`
AddEntryPointsLabels bool `description:"Enable metrics on entry points." json:"addEntryPointsLabels,omitempty" toml:"addEntryPointsLabels,omitempty" yaml:"addEntryPointsLabels,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.
Expand All @@ -58,6 +59,7 @@ func (s *Statsd) SetDefaults() {
s.PushInterval = Duration(10 * time.Second)
s.AddEntryPointsLabels = true
s.AddServicesLabels = true
s.Prefix = "traefik"
}

// InfluxDB contains address, login and metrics pushing interval configuration.
Expand Down