Skip to content

Commit 71d7422

Browse files
authored
Support for subsystem in grpc prometheus counter and histogram metrics (#643)
1 parent 47ca7d6 commit 71d7422

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

providers/prometheus/client_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,17 @@ func (s *ClientInterceptorTestSuite) TestStreamingIncrementsMetrics() {
100100
requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList", "FailedPrecondition"))
101101
requireValueHistCount(s.T(), 2, s.clientMetrics.clientHandledHistogram.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList"))
102102
}
103+
104+
func (s *ClientInterceptorTestSuite) TestWithSubsystem() {
105+
counterOpts := []CounterOption{
106+
WithSubsystem("subsystem1"),
107+
}
108+
histOpts := []HistogramOption{
109+
WithHistogramSubsystem("subsystem1"),
110+
}
111+
clientCounterOpts := WithClientCounterOptions(counterOpts...)
112+
clientMetrics := NewClientMetrics(clientCounterOpts, WithClientHandlingTimeHistogram(histOpts...))
113+
114+
requireSubsystemName(s.T(), "subsystem1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
115+
requireHistSubsystemName(s.T(), "subsystem1", clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
116+
}

providers/prometheus/options.go

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ func WithConstLabels(labels prometheus.Labels) CounterOption {
3939
}
4040
}
4141

42+
// WithSubsystem allows you to add a Subsystem to Counter metrics.
43+
func WithSubsystem(subsystem string) CounterOption {
44+
return func(o *prometheus.CounterOpts) {
45+
o.Subsystem = subsystem
46+
}
47+
}
48+
4249
// A HistogramOption lets you add options to Histogram metrics using With*
4350
// funcs.
4451
type HistogramOption func(*prometheus.HistogramOpts)
@@ -81,6 +88,13 @@ func WithHistogramConstLabels(labels prometheus.Labels) HistogramOption {
8188
}
8289
}
8390

91+
// WithHistogramSubsystem allows you to add a Subsystem to histograms metrics.
92+
func WithHistogramSubsystem(subsystem string) HistogramOption {
93+
return func(o *prometheus.HistogramOpts) {
94+
o.Subsystem = subsystem
95+
}
96+
}
97+
8498
func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType {
8599
if !mInfo.IsClientStream && !mInfo.IsServerStream {
86100
return Unary

providers/prometheus/server_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ func (s *ServerInterceptorTestSuite) SetupTest() {
5656
s.serverMetrics.InitializeMetrics(s.Server)
5757
}
5858

59+
func (s *ServerInterceptorTestSuite) TestWithSubsystem() {
60+
counterOpts := []CounterOption{
61+
WithSubsystem("subsystem1"),
62+
}
63+
histOpts := []HistogramOption{
64+
WithHistogramSubsystem("subsystem1"),
65+
}
66+
serverCounterOpts := WithServerCounterOptions(counterOpts...)
67+
serverMetrics := NewServerMetrics(serverCounterOpts, WithServerHandlingTimeHistogram(histOpts...))
68+
69+
requireSubsystemName(s.T(), "subsystem1", serverMetrics.serverStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
70+
requireHistSubsystemName(s.T(), "subsystem1", serverMetrics.serverHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
71+
}
72+
5973
func (s *ServerInterceptorTestSuite) TestRegisterPresetsStuff() {
6074
registry := prometheus.NewPedanticRegistry()
6175
s.Require().NoError(registry.Register(s.serverMetrics))
@@ -233,6 +247,18 @@ func toFloat64HistCount(h prometheus.Observer) uint64 {
233247
panic(fmt.Errorf("collected a non-histogram metric: %s", pb))
234248
}
235249

250+
func requireSubsystemName(t *testing.T, expect string, c prometheus.Collector) {
251+
t.Helper()
252+
metricFullName := reflect.ValueOf(*c.(prometheus.Metric).Desc()).FieldByName("fqName").String()
253+
254+
if strings.Split(metricFullName, "_")[0] == expect {
255+
return
256+
}
257+
258+
t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
259+
t.Fail()
260+
}
261+
236262
func requireValue(t *testing.T, expect int, c prometheus.Collector) {
237263
t.Helper()
238264
v := int(testutil.ToFloat64(c))
@@ -245,6 +271,18 @@ func requireValue(t *testing.T, expect int, c prometheus.Collector) {
245271
t.Fail()
246272
}
247273

274+
func requireHistSubsystemName(t *testing.T, expect string, o prometheus.Observer) {
275+
t.Helper()
276+
metricFullName := reflect.ValueOf(*o.(prometheus.Metric).Desc()).FieldByName("fqName").String()
277+
278+
if strings.Split(metricFullName, "_")[0] == expect {
279+
return
280+
}
281+
282+
t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
283+
t.Fail()
284+
}
285+
248286
func requireValueHistCount(t *testing.T, expect int, o prometheus.Observer) {
249287
t.Helper()
250288
v := int(toFloat64HistCount(o))

0 commit comments

Comments
 (0)