-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmetrics_round_tripper_test.go
104 lines (84 loc) · 3.13 KB
/
metrics_round_tripper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package httpclient
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"github.com/acronis/go-appkit/testutil"
)
func TestNewMetricsRoundTripper(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusTeapot)
}))
defer server.Close()
collector := NewPrometheusMetricsCollector("")
defer collector.Unregister()
clientType := "test-client-type"
metricsRoundTripper := NewMetricsRoundTripperWithOpts(http.DefaultTransport, collector, MetricsRoundTripperOpts{
ClientType: clientType,
})
client := &http.Client{Transport: metricsRoundTripper}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, server.URL, nil)
require.NoError(t, err)
r, err := client.Do(req)
defer func() { _ = r.Body.Close() }()
require.NoError(t, err)
labels := prometheus.Labels{
"client_type": clientType,
"remote_address": strings.ReplaceAll(server.URL, "http://", ""),
"summary": "POST test-client-type",
"request_type": "",
"status": "418",
}
hist := collector.Durations.With(labels).(prometheus.Histogram)
testutil.AssertSamplesCountInHistogram(t, hist, 1)
}
func TestNewMetricsCollectionRequiredRoundTripper(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusTeapot)
}))
defer server.Close()
metricsRoundTripper := NewMetricsRoundTripper(http.DefaultTransport, nil)
client := &http.Client{Transport: metricsRoundTripper}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, server.URL, nil)
require.NoError(t, err)
_, err = client.Do(req) // nolint:bodyclose
require.Error(t, err)
require.Contains(t, err.Error(), "metrics collector is not provided")
}
func TestNewMetricsRequestTypeRoundTripper(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusTeapot)
}))
defer server.Close()
collector := NewPrometheusMetricsCollector("")
defer collector.Unregister()
clientType := "test-client-type"
requestType := "test-request-type"
metricsRoundTripper := NewMetricsRoundTripperWithOpts(http.DefaultTransport, collector, MetricsRoundTripperOpts{
ClientType: clientType,
})
client := &http.Client{Transport: metricsRoundTripper}
ctx := NewContextWithRequestType(context.Background(), requestType)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, server.URL, nil)
require.NoError(t, err)
r, err := client.Do(req)
defer func() { _ = r.Body.Close() }()
require.NoError(t, err)
labels := prometheus.Labels{
"client_type": clientType,
"remote_address": strings.ReplaceAll(server.URL, "http://", ""),
"summary": "POST test-client-type",
"request_type": requestType,
"status": "418",
}
hist := collector.Durations.With(labels).(prometheus.Histogram)
testutil.AssertSamplesCountInHistogram(t, hist, 1)
}