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"
|
|
|
|
"strconv"
|
|
|
|
"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 TestDatadog(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-09-02 10:18:04 +00:00
|
|
|
datadogRegistry := RegisterDatadog(context.Background(), &types.Datadog{Address: ":18125", PushInterval: types.Duration(time.Second), AddEntryPointsLabels: true, AddServicesLabels: true})
|
2017-08-23 18:46:03 +00:00
|
|
|
defer StopDatadog()
|
|
|
|
|
2019-07-18 19:36:05 +00:00
|
|
|
if !datadogRegistry.IsEpEnabled() || !datadogRegistry.IsSvcEnabled() {
|
2017-08-23 18:46:03 +00:00
|
|
|
t.Errorf("DatadogRegistry 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
|
2019-07-18 19:36:05 +00:00
|
|
|
"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.retries.total:2.000000|c|#service:test\n",
|
|
|
|
"traefik.service.request.duration:10000.000000|h|#service:test,code:200\n",
|
2018-02-21 09:04:03 +00:00
|
|
|
"traefik.config.reload.total:1.000000|c\n",
|
|
|
|
"traefik.config.reload.total:1.000000|c|#failure:true\n",
|
|
|
|
"traefik.entrypoint.request.total:1.000000|c|#entrypoint:test\n",
|
|
|
|
"traefik.entrypoint.request.duration:10000.000000|h|#entrypoint:test\n",
|
|
|
|
"traefik.entrypoint.connections.open:1.000000|g|#entrypoint:test\n",
|
2019-07-18 19:36:05 +00:00
|
|
|
"traefik.service.server.up:1.000000|g|#service:test,url:http://127.0.0.1,one:two\n",
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udp.ShouldReceiveAll(t, expected, func() {
|
2019-07-18 19:36:05 +00:00
|
|
|
datadogRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.ServiceReqDurationHistogram().With("service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
|
|
|
datadogRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
|
|
|
datadogRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
2018-02-21 09:04:03 +00:00
|
|
|
datadogRegistry.ConfigReloadsCounter().Add(1)
|
|
|
|
datadogRegistry.ConfigReloadsFailureCounter().Add(1)
|
2019-07-18 19:36:05 +00:00
|
|
|
datadogRegistry.EntryPointReqsCounter().With("entrypoint", "test").Add(1)
|
|
|
|
datadogRegistry.EntryPointReqDurationHistogram().With("entrypoint", "test").Observe(10000)
|
|
|
|
datadogRegistry.EntryPointOpenConnsGauge().With("entrypoint", "test").Set(1)
|
|
|
|
datadogRegistry.ServiceServerUpGauge().With("service", "test", "url", "http://127.0.0.1", "one", "two").Set(1)
|
2017-08-23 18:46:03 +00:00
|
|
|
})
|
|
|
|
}
|