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"
|
|
|
|
|
2024-01-29 16:08:05 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-08-23 18:46:03 +00:00
|
|
|
"github.com/stvp/go-udp-testing"
|
2020-08-17 16:04:03 +00:00
|
|
|
ptypes "github.com/traefik/paerser/types"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2017-08-23 18:46:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDatadog(t *testing.T) {
|
2024-01-24 15:58:05 +00:00
|
|
|
t.Cleanup(StopDatadog)
|
|
|
|
|
2017-08-23 18:46:03 +00:00
|
|
|
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
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
datadogRegistry := RegisterDatadog(context.Background(), &types.Datadog{Address: ":18125", PushInterval: ptypes.Duration(time.Second), AddEntryPointsLabels: true, AddRoutersLabels: true, AddServicesLabels: true})
|
2017-08-23 18:46:03 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
if !datadogRegistry.IsEpEnabled() || !datadogRegistry.IsRouterEnabled() || !datadogRegistry.IsSvcEnabled() {
|
|
|
|
t.Errorf("DatadogRegistry should return true for IsEnabled(), IsRouterEnabled() and IsSvcEnabled()")
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
2021-10-06 15:34:07 +00:00
|
|
|
testDatadogRegistry(t, defaultMetricsPrefix, datadogRegistry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatadogWithPrefix(t *testing.T) {
|
2024-01-24 15:58:05 +00:00
|
|
|
t.Cleanup(StopDatadog)
|
2021-10-06 15:34:07 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:08:05 +00:00
|
|
|
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",
|
|
|
|
},
|
2024-09-19 13:30:05 +00:00
|
|
|
{
|
|
|
|
desc: "unixgram address",
|
|
|
|
address: "unixgram:///path/to/datadog.socket",
|
|
|
|
expNetwork: "unixgram",
|
|
|
|
expAddress: "/path/to/datadog.socket",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "unixstream address",
|
|
|
|
address: "unixstream:///path/to/datadog.socket",
|
|
|
|
expNetwork: "unixstream",
|
|
|
|
expAddress: "/path/to/datadog.socket",
|
|
|
|
},
|
2024-01-29 16:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:34:07 +00:00
|
|
|
func testDatadogRegistry(t *testing.T, metricsPrefix string, datadogRegistry Registry) {
|
|
|
|
t.Helper()
|
2017-08-23 18:46:03 +00:00
|
|
|
|
|
|
|
expected := []string{
|
2021-10-06 15:34:07 +00:00
|
|
|
metricsPrefix + ".config.reload.total:1.000000|c\n",
|
|
|
|
metricsPrefix + ".config.reload.lastSuccessTimestamp:1.000000|g\n",
|
2023-03-20 15:02:06 +00:00
|
|
|
metricsPrefix + ".open.connections:1.000000|g|#entrypoint:test,protocol:TCP\n",
|
2021-10-06 15:34:07 +00:00
|
|
|
|
|
|
|
metricsPrefix + ".tls.certs.notAfterTimestamp:1.000000|g|#key:value\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",
|
2022-09-12 15:10:09 +00:00
|
|
|
metricsPrefix + ".entrypoint.requests.bytes.total:1.000000|c|#entrypoint:test\n",
|
|
|
|
metricsPrefix + ".entrypoint.responses.bytes.total:1.000000|c|#entrypoint:test\n",
|
2021-10-06 15:34:07 +00:00
|
|
|
|
|
|
|
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",
|
2022-09-12 15:10:09 +00:00
|
|
|
metricsPrefix + ".router.requests.bytes.total:1.000000|c|#router:demo,service:test,code:200,method:GET\n",
|
|
|
|
metricsPrefix + ".router.responses.bytes.total:1.000000|c|#router:demo,service:test,code:200,method:GET\n",
|
2021-10-06 15:34:07 +00:00
|
|
|
|
|
|
|
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.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",
|
2022-09-12 15:10:09 +00:00
|
|
|
metricsPrefix + ".service.requests.bytes.total:1.000000|c|#service:test,code:200,method:GET\n",
|
|
|
|
metricsPrefix + ".service.responses.bytes.total:1.000000|c|#service:test,code:200,method:GET\n",
|
2017-08-23 18:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
udp.ShouldReceiveAll(t, expected, func() {
|
2018-02-21 09:04:03 +00:00
|
|
|
datadogRegistry.ConfigReloadsCounter().Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
datadogRegistry.LastConfigReloadSuccessGauge().Add(1)
|
2023-03-20 15:02:06 +00:00
|
|
|
datadogRegistry.OpenConnectionsGauge().With("entrypoint", "test", "protocol", "TCP").Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
|
|
|
|
datadogRegistry.TLSCertsNotAfterTimestampGauge().With("key", "value").Set(1)
|
|
|
|
|
2023-03-20 17:06:07 +00:00
|
|
|
datadogRegistry.EntryPointReqsCounter().With(nil, "entrypoint", "test").Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
datadogRegistry.EntryPointReqsTLSCounter().With("entrypoint", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
2019-07-18 19:36:05 +00:00
|
|
|
datadogRegistry.EntryPointReqDurationHistogram().With("entrypoint", "test").Observe(10000)
|
2022-09-12 15:10:09 +00:00
|
|
|
datadogRegistry.EntryPointReqsBytesCounter().With("entrypoint", "test").Add(1)
|
|
|
|
datadogRegistry.EntryPointRespsBytesCounter().With("entrypoint", "test").Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
|
2023-03-20 17:06:07 +00:00
|
|
|
datadogRegistry.RouterReqsCounter().With(nil, "router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.RouterReqsCounter().With(nil, "router", "demo", "service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
datadogRegistry.RouterReqsTLSCounter().With("router", "demo", "service", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
|
|
|
datadogRegistry.RouterReqDurationHistogram().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
2022-09-12 15:10:09 +00:00
|
|
|
datadogRegistry.RouterReqsBytesCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.RouterRespsBytesCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
|
2023-03-20 17:06:07 +00:00
|
|
|
datadogRegistry.ServiceReqsCounter().With(nil, "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.ServiceReqsCounter().With(nil, "service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
datadogRegistry.ServiceReqsTLSCounter().With("service", "test", "tls_version", "foo", "tls_cipher", "bar").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)
|
2019-07-18 19:36:05 +00:00
|
|
|
datadogRegistry.ServiceServerUpGauge().With("service", "test", "url", "http://127.0.0.1", "one", "two").Set(1)
|
2022-09-12 15:10:09 +00:00
|
|
|
datadogRegistry.ServiceReqsBytesCounter().With("service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
datadogRegistry.ServiceRespsBytesCounter().With("service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
2017-08-23 18:46:03 +00:00
|
|
|
})
|
|
|
|
}
|