2017-11-08 14:14:03 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2018-11-14 09:18:03 +00:00
|
|
|
"context"
|
2018-05-29 20:58:03 +00:00
|
|
|
"fmt"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2017-11-08 14:14:03 +00:00
|
|
|
"net/http"
|
2018-05-29 20:58:03 +00:00
|
|
|
"net/http/httptest"
|
2017-11-08 14:14:03 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stvp/go-udp-testing"
|
2020-08-17 16:04:03 +00:00
|
|
|
ptypes "github.com/traefik/paerser/types"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/types"
|
2017-11-08 14:14:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInfluxDB(t *testing.T) {
|
|
|
|
udp.SetAddr(":8089")
|
|
|
|
// 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
|
|
|
influxDBRegistry := RegisterInfluxDB(context.Background(), &types.InfluxDB{Address: ":8089", PushInterval: ptypes.Duration(time.Second), AddEntryPointsLabels: true, AddRoutersLabels: true, AddServicesLabels: true})
|
2017-11-08 14:14:03 +00:00
|
|
|
defer StopInfluxDB()
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
if !influxDBRegistry.IsEpEnabled() || !influxDBRegistry.IsRouterEnabled() || !influxDBRegistry.IsSvcEnabled() {
|
|
|
|
t.Fatalf("InfluxDBRegistry should return true for IsEnabled(), IsRouterEnabled() and IsSvcEnabled()")
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
expectedServer := []string{
|
|
|
|
`(traefik\.config\.reload\.total count=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.total\.failure count=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.lastSuccessTimestamp value=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.lastFailureTimestamp value=1) [\d]{19}`,
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
msgServer := udp.ReceiveString(t, func() {
|
2018-03-12 08:34:03 +00:00
|
|
|
influxDBRegistry.ConfigReloadsCounter().Add(1)
|
|
|
|
influxDBRegistry.ConfigReloadsFailureCounter().Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry.LastConfigReloadSuccessGauge().Set(1)
|
|
|
|
influxDBRegistry.LastConfigReloadFailureGauge().Set(1)
|
2017-11-08 14:14:03 +00:00
|
|
|
})
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
assertMessage(t, msgServer, expectedServer)
|
|
|
|
|
|
|
|
expectedTLS := []string{
|
|
|
|
`(traefik\.tls\.certs\.notAfterTimestamp,key=value value=1) [\d]{19}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
msgTLS := udp.ReceiveString(t, func() {
|
|
|
|
influxDBRegistry.TLSCertsNotAfterTimestampGauge().With("key", "value").Set(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
assertMessage(t, msgTLS, expectedTLS)
|
2018-03-12 08:34:03 +00:00
|
|
|
|
|
|
|
expectedEntrypoint := []string{
|
2021-04-30 08:22:04 +00:00
|
|
|
`(traefik\.entrypoint\.requests\.total,code=200,entrypoint=test,method=GET count=1) [\d]{19}`,
|
|
|
|
`(traefik\.entrypoint\.requests\.tls\.total,entrypoint=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.entrypoint\.request\.duration(?:,code=[\d]{3})?,entrypoint=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
2018-04-16 08:28:04 +00:00
|
|
|
`(traefik\.entrypoint\.connections\.open,entrypoint=test value=1) [\d]{19}`,
|
2018-03-12 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msgEntrypoint := udp.ReceiveString(t, func() {
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry.EntryPointReqsCounter().With("entrypoint", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.EntryPointReqsTLSCounter().With("entrypoint", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
2019-07-18 19:36:05 +00:00
|
|
|
influxDBRegistry.EntryPointReqDurationHistogram().With("entrypoint", "test").Observe(10000)
|
|
|
|
influxDBRegistry.EntryPointOpenConnsGauge().With("entrypoint", "test").Set(1)
|
2018-03-12 08:34:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assertMessage(t, msgEntrypoint, expectedEntrypoint)
|
2020-12-18 17:44:03 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
expectedRouter := []string{
|
|
|
|
`(traefik\.router\.requests\.total,code=200,method=GET,router=demo,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.requests\.total,code=404,method=GET,router=demo,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.requests\.tls\.total,router=demo,service=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.request\.duration,code=200,router=demo,service=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
|
|
|
`(traefik\.router\.connections\.open,router=demo,service=test value=1) [\d]{19}`,
|
2020-12-18 17:44:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
msgRouter := udp.ReceiveString(t, func() {
|
|
|
|
influxDBRegistry.RouterReqsCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.RouterReqsCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.RouterReqsTLSCounter().With("router", "demo", "service", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
|
|
|
influxDBRegistry.RouterReqDurationHistogram().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
|
|
|
influxDBRegistry.RouterOpenConnsGauge().With("router", "demo", "service", "test").Set(1)
|
2020-12-18 17:44:03 +00:00
|
|
|
})
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
assertMessage(t, msgRouter, expectedRouter)
|
|
|
|
|
|
|
|
expectedService := []string{
|
|
|
|
`(traefik\.service\.requests\.total,code=200,method=GET,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.requests\.total,code=404,method=GET,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.requests\.tls\.total,service=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.request\.duration,code=200,service=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
|
|
|
`(traefik\.service\.retries\.total(?:,code=[\d]{3},method=GET)?,service=test count=2) [\d]{19}`,
|
|
|
|
`(traefik\.service\.server\.up,service=test,url=http://127.0.0.1 value=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.connections\.open,service=test value=1) [\d]{19}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
msgService := udp.ReceiveString(t, func() {
|
|
|
|
influxDBRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.ServiceReqsTLSCounter().With("service", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
|
|
|
influxDBRegistry.ServiceReqDurationHistogram().With("service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
|
|
|
influxDBRegistry.ServiceOpenConnsGauge().With("service", "test").Set(1)
|
|
|
|
influxDBRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
|
|
|
influxDBRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
|
|
|
influxDBRegistry.ServiceServerUpGauge().With("service", "test", "url", "http://127.0.0.1").Set(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
assertMessage(t, msgService, expectedService)
|
2017-11-08 14:14:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 20:58:03 +00:00
|
|
|
func TestInfluxDBHTTP(t *testing.T) {
|
|
|
|
c := make(chan *string)
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-05-29 20:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "can't read body "+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bodyStr := string(body)
|
|
|
|
c <- &bodyStr
|
2019-07-18 19:36:05 +00:00
|
|
|
_, _ = fmt.Fprintln(w, "ok")
|
2018-05-29 20:58:03 +00:00
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry := RegisterInfluxDB(context.Background(), &types.InfluxDB{Address: ts.URL, Protocol: "http", PushInterval: ptypes.Duration(time.Second), Database: "test", RetentionPolicy: "autogen", AddEntryPointsLabels: true, AddServicesLabels: true, AddRoutersLabels: true})
|
2018-05-29 20:58:03 +00:00
|
|
|
defer StopInfluxDB()
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
if !influxDBRegistry.IsEpEnabled() || !influxDBRegistry.IsRouterEnabled() || !influxDBRegistry.IsSvcEnabled() {
|
2019-07-18 19:36:05 +00:00
|
|
|
t.Fatalf("InfluxDB registry must be epEnabled")
|
2018-05-29 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
expectedServer := []string{
|
|
|
|
`(traefik\.config\.reload\.total count=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.total\.failure count=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.lastSuccessTimestamp value=1) [\d]{19}`,
|
|
|
|
`(traefik\.config\.reload\.lastFailureTimestamp value=1) [\d]{19}`,
|
2018-05-29 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
influxDBRegistry.ConfigReloadsCounter().Add(1)
|
|
|
|
influxDBRegistry.ConfigReloadsFailureCounter().Add(1)
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry.LastConfigReloadSuccessGauge().Set(1)
|
|
|
|
influxDBRegistry.LastConfigReloadFailureGauge().Set(1)
|
|
|
|
msgServer := <-c
|
2018-05-29 20:58:03 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
assertMessage(t, *msgServer, expectedServer)
|
|
|
|
|
|
|
|
expectedTLS := []string{
|
|
|
|
`(traefik\.tls\.certs\.notAfterTimestamp,key=value value=1) [\d]{19}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
influxDBRegistry.TLSCertsNotAfterTimestampGauge().With("key", "value").Set(1)
|
|
|
|
msgTLS := <-c
|
|
|
|
|
|
|
|
assertMessage(t, *msgTLS, expectedTLS)
|
2018-05-29 20:58:03 +00:00
|
|
|
|
|
|
|
expectedEntrypoint := []string{
|
2021-04-30 08:22:04 +00:00
|
|
|
`(traefik\.entrypoint\.requests\.total,code=200,entrypoint=test,method=GET count=1) [\d]{19}`,
|
|
|
|
`(traefik\.entrypoint\.requests\.tls\.total,entrypoint=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.entrypoint\.request\.duration(?:,code=[\d]{3})?,entrypoint=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
2018-05-29 20:58:03 +00:00
|
|
|
`(traefik\.entrypoint\.connections\.open,entrypoint=test value=1) [\d]{19}`,
|
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry.EntryPointReqsCounter().With("entrypoint", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.EntryPointReqsTLSCounter().With("entrypoint", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
2019-07-18 19:36:05 +00:00
|
|
|
influxDBRegistry.EntryPointReqDurationHistogram().With("entrypoint", "test").Observe(10000)
|
|
|
|
influxDBRegistry.EntryPointOpenConnsGauge().With("entrypoint", "test").Set(1)
|
2018-05-29 20:58:03 +00:00
|
|
|
msgEntrypoint := <-c
|
|
|
|
|
|
|
|
assertMessage(t, *msgEntrypoint, expectedEntrypoint)
|
2020-12-18 17:44:03 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
expectedRouter := []string{
|
|
|
|
`(traefik\.router\.requests\.total,code=200,method=GET,router=demo,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.requests\.total,code=404,method=GET,router=demo,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.requests\.tls\.total,router=demo,service=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.router\.request\.duration,code=200,router=demo,service=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
|
|
|
`(traefik\.router\.connections\.open,router=demo,service=test value=1) [\d]{19}`,
|
2020-12-18 17:44:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
influxDBRegistry.RouterReqsCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.RouterReqsCounter().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.RouterReqsTLSCounter().With("router", "demo", "service", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
|
|
|
influxDBRegistry.RouterReqDurationHistogram().With("router", "demo", "service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
|
|
|
influxDBRegistry.RouterOpenConnsGauge().With("router", "demo", "service", "test").Set(1)
|
|
|
|
msgRouter := <-c
|
2020-12-18 17:44:03 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
assertMessage(t, *msgRouter, expectedRouter)
|
|
|
|
|
|
|
|
expectedService := []string{
|
|
|
|
`(traefik\.service\.requests\.total,code=200,method=GET,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.requests\.total,code=404,method=GET,service=test count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.requests\.tls\.total,service=test,tls_cipher=bar,tls_version=foo count=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.request\.duration,code=200,service=test p50=10000,p90=10000,p95=10000,p99=10000) [\d]{19}`,
|
|
|
|
`(traefik\.service\.retries\.total(?:,code=[\d]{3},method=GET)?,service=test count=2) [\d]{19}`,
|
|
|
|
`(traefik\.service\.server\.up,service=test,url=http://127.0.0.1 value=1) [\d]{19}`,
|
|
|
|
`(traefik\.service\.connections\.open,service=test value=1) [\d]{19}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
influxDBRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.ServiceReqsCounter().With("service", "test", "code", strconv.Itoa(http.StatusNotFound), "method", http.MethodGet).Add(1)
|
|
|
|
influxDBRegistry.ServiceReqsTLSCounter().With("service", "test", "tls_version", "foo", "tls_cipher", "bar").Add(1)
|
|
|
|
influxDBRegistry.ServiceReqDurationHistogram().With("service", "test", "code", strconv.Itoa(http.StatusOK)).Observe(10000)
|
|
|
|
influxDBRegistry.ServiceOpenConnsGauge().With("service", "test").Set(1)
|
|
|
|
influxDBRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
|
|
|
influxDBRegistry.ServiceRetriesCounter().With("service", "test").Add(1)
|
|
|
|
influxDBRegistry.ServiceServerUpGauge().With("service", "test", "url", "http://127.0.0.1").Set(1)
|
|
|
|
msgService := <-c
|
|
|
|
|
|
|
|
assertMessage(t, *msgService, expectedService)
|
2018-05-29 20:58:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:22:03 +00:00
|
|
|
func assertMessage(t *testing.T, msg string, patterns []string) {
|
2017-11-08 14:14:03 +00:00
|
|
|
t.Helper()
|
|
|
|
for _, pattern := range patterns {
|
|
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
match := re.FindStringSubmatch(msg)
|
|
|
|
if len(match) != 2 {
|
|
|
|
t.Errorf("Got %q %v, want %q", msg, match, pattern)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|