forked from easyCZ/connect-go-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics_test.go
132 lines (112 loc) · 6.58 KB
/
metrics_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package connect_go_prometheus
import (
"strings"
"testing"
"connectrpc.com/connect"
"github.com/easyCZ/connect-go-prometheus/gen/greet/greetconnect"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)
func TestServerMetrics(t *testing.T) {
reg := prom.NewRegistry()
sm := NewServerMetrics(
WithHistogram(true),
WithByteMetrics(true),
WithInflightMetrics(true),
WithNamespace("namespace"),
WithSubsystem("subsystem"),
WithConstLabels(prom.Labels{"component": "foo"}),
WithHistogramBuckets([]float64{0.5, 1, 1.5}),
)
require.NoError(t, reg.Register(sm))
started := sm.requestStarted.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
started.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(started))
handled := sm.requestHandled.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet", connect.CodeAborted.String())
handled.Inc()
require.EqualValues(t, 1, testutil.ToFloat64(handled))
msgSent := sm.streamMsgSent.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
msgSent.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(msgSent))
msgReceived := sm.streamMsgReceived.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
msgReceived.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(msgReceived))
require.NotNil(t, sm.bytesSent)
bytesSent := sm.bytesSent.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
bytesSent.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(bytesSent))
require.NotNil(t, sm.bytesReceived)
bytesReceived := sm.bytesReceived.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
bytesReceived.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(bytesReceived))
require.NotNil(t, sm.inflightRequests)
inflight := sm.inflightRequests.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
inflight.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(inflight))
require.NotNil(t, sm.requestHandledSeconds)
handledHist := sm.requestHandledSeconds.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet", connect.CodeAborted.String())
handledHist.Observe(1)
err := testutil.CollectAndCompare(sm.requestHandledSeconds, strings.NewReader(`
# HELP namespace_subsystem_connect_server_handled_seconds Histogram of RPCs handled server-side
# TYPE namespace_subsystem_connect_server_handled_seconds histogram
namespace_subsystem_connect_server_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="0.5"} 0
namespace_subsystem_connect_server_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="1"} 1
namespace_subsystem_connect_server_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="1.5"} 1
namespace_subsystem_connect_server_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="Inf"} 1
namespace_subsystem_connect_server_handled_seconds_sum{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary"} 1
namespace_subsystem_connect_server_handled_seconds_count{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary"} 1
`))
require.NoError(t, err)
}
func TestClientMetrics(t *testing.T) {
reg := prom.NewRegistry()
cm := NewClientMetrics(
WithHistogram(true),
WithByteMetrics(true),
WithInflightMetrics(true),
WithNamespace("namespace"),
WithSubsystem("subsystem"),
WithConstLabels(prom.Labels{"component": "foo"}),
WithHistogramBuckets([]float64{0.5, 1, 1.5}),
)
require.NoError(t, reg.Register(cm))
started := cm.requestStarted.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
started.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(started))
msgSent := cm.streamMsgSent.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
msgSent.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(msgSent))
msgreceived := cm.streamMsgReceived.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
msgreceived.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(msgreceived))
handled := cm.requestHandled.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet", connect.CodeAborted.String())
handled.Inc()
require.EqualValues(t, 1, testutil.ToFloat64(handled))
require.NotNil(t, cm.bytesSent)
bytesSent := cm.bytesSent.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
bytesSent.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(bytesSent))
require.NotNil(t, cm.bytesReceived)
bytesReceived := cm.bytesReceived.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
bytesReceived.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(bytesReceived))
require.NotNil(t, cm.inflightRequests)
inflight := cm.inflightRequests.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet")
inflight.Inc()
require.EqualValues(t, float64(1), testutil.ToFloat64(inflight))
require.NotNil(t, cm.requestHandledSeconds)
handledHist := cm.requestHandledSeconds.WithLabelValues("unary", greetconnect.GreetServiceName, "Greet", connect.CodeAborted.String())
handledHist.Observe(1)
err := testutil.CollectAndCompare(cm.requestHandledSeconds, strings.NewReader(`
# HELP namespace_subsystem_connect_client_handled_seconds Histogram of RPCs handled client-side
# TYPE namespace_subsystem_connect_client_handled_seconds histogram
namespace_subsystem_connect_client_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="0.5"} 0
namespace_subsystem_connect_client_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="1"} 1
namespace_subsystem_connect_client_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="1.5"} 1
namespace_subsystem_connect_client_handled_seconds_bucket{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary",le="Inf"} 1
namespace_subsystem_connect_client_handled_seconds_sum{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary"} 1
namespace_subsystem_connect_client_handled_seconds_count{code="aborted",component="foo",method="Greet",service="greet.v1.GreetService",type="unary"} 1
`))
require.NoError(t, err)
}