2017-08-23 18:46:03 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2017-08-23 18:46:03 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/types"
|
2017-08-23 18:46:03 +00:00
|
|
|
"github.com/stvp/go-udp-testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatsD(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
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
statsdRegistry := RegisterStatsd(context.Background(), &types.Statsd{Address: ":18125", PushInterval: types.Duration(time.Second), AddEntryPointsLabels: true, AddServicesLabels: true})
|
2017-08-23 18:46:03 +00:00
|
|
|
defer StopStatsd()
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
if !statsdRegistry.IsEpEnabled() || !statsdRegistry.IsSvcEnabled() {
|
2017-11-08 14:14:03 +00:00
|
|
|
t.Errorf("Statsd registry should return true for IsEnabled()")
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
// We are only validating counts, as it is nearly impossible to validate latency, since it varies every run
|
2019-07-18 19:36:05 +00:00
|
|
|
"traefik.service.request.total:2.000000|c\n",
|
|
|
|
"traefik.service.retries.total:2.000000|c\n",
|
|
|
|
"traefik.service.request.duration:10000.000000|ms",
|
2018-02-21 09:04:03 +00:00
|
|
|
"traefik.config.reload.total:1.000000|c\n",
|
|
|
|
"traefik.config.reload.total:1.000000|c\n",
|
|
|
|
"traefik.entrypoint.request.total:1.000000|c\n",
|
|
|
|
"traefik.entrypoint.request.duration:10000.000000|ms",
|
|
|
|
"traefik.entrypoint.connections.open:1.000000|g\n",
|
2019-07-18 19:36:05 +00:00
|
|
|
"traefik.service.server.up:1.000000|g\n",
|
2019-11-12 17:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udp.ShouldReceiveAll(t, expected, func() {
|
2019-07-18 19:36:05 +00:00
|
|
|
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)
|
2018-02-21 09:04:03 +00:00
|
|
|
statsdRegistry.ConfigReloadsCounter().Add(1)
|
|
|
|
statsdRegistry.ConfigReloadsFailureCounter().Add(1)
|
2019-07-18 19:36:05 +00:00
|
|
|
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)
|
2017-08-23 18:46:03 +00:00
|
|
|
})
|
|
|
|
}
|