1
0
Fork 0

Add prometheus metric requests_total with headers

Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
Romain 2023-03-20 18:06:07 +01:00 committed by GitHub
parent 4bc2305ed3
commit c823879097
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 295 additions and 90 deletions

View file

@ -92,7 +92,12 @@ func TestPrometheus(t *testing.T) {
promRegistry = prometheus.NewRegistry()
t.Cleanup(promState.reset)
prometheusRegistry := RegisterPrometheus(context.Background(), &types.Prometheus{AddEntryPointsLabels: true, AddRoutersLabels: true, AddServicesLabels: true})
prometheusRegistry := RegisterPrometheus(context.Background(), &types.Prometheus{
AddEntryPointsLabels: true,
AddRoutersLabels: true,
AddServicesLabels: true,
HeaderLabels: map[string]string{"useragent": "User-Agent"},
})
defer promRegistry.Unregister(promState)
if !prometheusRegistry.IsEpEnabled() || !prometheusRegistry.IsRouterEnabled() || !prometheusRegistry.IsSvcEnabled() {
@ -111,7 +116,7 @@ func TestPrometheus(t *testing.T) {
prometheusRegistry.
EntryPointReqsCounter().
With("code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http", "entrypoint", "http").
With(map[string][]string{"User-Agent": {"foobar"}}, "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http", "entrypoint", "http").
Add(1)
prometheusRegistry.
EntryPointReqDurationHistogram().
@ -132,7 +137,7 @@ func TestPrometheus(t *testing.T) {
prometheusRegistry.
RouterReqsCounter().
With("router", "demo", "service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "router", "demo", "service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
RouterReqsTLSCounter().
@ -157,7 +162,7 @@ func TestPrometheus(t *testing.T) {
prometheusRegistry.
ServiceReqsCounter().
With("service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(map[string][]string{"User-Agent": {"foobar"}}, "service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
ServiceReqsTLSCounter().
@ -229,6 +234,7 @@ func TestPrometheus(t *testing.T) {
"method": http.MethodGet,
"protocol": "http",
"entrypoint": "http",
"useragent": "foobar",
},
assert: buildCounterAssert(t, entryPointReqsTotalName, 1),
},
@ -274,11 +280,12 @@ func TestPrometheus(t *testing.T) {
{
name: routerReqsTotalName,
labels: map[string]string{
"code": "200",
"method": http.MethodGet,
"protocol": "http",
"service": "service1",
"router": "demo",
"code": "200",
"method": http.MethodGet,
"protocol": "http",
"service": "service1",
"router": "demo",
"useragent": "",
},
assert: buildCounterAssert(t, routerReqsTotalName, 1),
},
@ -338,10 +345,11 @@ func TestPrometheus(t *testing.T) {
{
name: serviceReqsTotalName,
labels: map[string]string{
"code": "200",
"method": http.MethodGet,
"protocol": "http",
"service": "service1",
"code": "200",
"method": http.MethodGet,
"protocol": "http",
"service": "service1",
"useragent": "foobar",
},
assert: buildCounterAssert(t, serviceReqsTotalName, 1),
},
@ -476,15 +484,15 @@ func TestPrometheusMetricRemoval(t *testing.T) {
// should be removed after that scrape.
prometheusRegistry.
EntryPointReqsCounter().
With("entrypoint", "entrypoint2", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "entrypoint", "entrypoint2", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
RouterReqsCounter().
With("router", "router2", "service", "bar@providerName", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "router", "router2", "service", "bar@providerName", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
ServiceReqsCounter().
With("service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "service", "service1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
ServiceServerUpGauge().
@ -502,15 +510,15 @@ func TestPrometheusMetricRemoval(t *testing.T) {
// here the counter examples.
prometheusRegistry.
EntryPointReqsCounter().
With("entrypoint", "entrypoint1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "entrypoint", "entrypoint1", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
RouterReqsCounter().
With("router", "foo@providerName", "service", "bar", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "router", "foo@providerName", "service", "bar", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
ServiceReqsCounter().
With("service", "bar@providerName", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
With(nil, "service", "bar@providerName", "code", strconv.Itoa(http.StatusOK), "method", http.MethodGet, "protocol", "http").
Add(1)
prometheusRegistry.
ServiceServerUpGauge().
@ -588,7 +596,7 @@ func TestPrometheusRemovedMetricsReset(t *testing.T) {
}
prometheusRegistry.
ServiceReqsCounter().
With(labelNamesValues...).
With(nil, labelNamesValues...).
Add(3)
delayForTrackingCompletion()
@ -602,7 +610,7 @@ func TestPrometheusRemovedMetricsReset(t *testing.T) {
prometheusRegistry.
ServiceReqsCounter().
With(labelNamesValues...).
With(nil, labelNamesValues...).
Add(1)
delayForTrackingCompletion()