Skip to content

Commit 62b7de5

Browse files
Support for namespace in grpc prometheus counter and histogram metrics (#718)
* Support for namespace in grpc prometheus counter and histogram metrics * Update providers/prometheus/options.go Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]> --------- Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
1 parent 3606823 commit 62b7de5

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

providers/prometheus/client_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,17 @@ func (s *ClientInterceptorTestSuite) TestWithSubsystem() {
114114
requireSubsystemName(s.T(), "subsystem1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
115115
requireHistSubsystemName(s.T(), "subsystem1", clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
116116
}
117+
118+
func (s *ClientInterceptorTestSuite) TestWithNamespace() {
119+
counterOpts := []CounterOption{
120+
WithNamespace("namespace1"),
121+
}
122+
histOpts := []HistogramOption{
123+
WithHistogramNamespace("namespace1"),
124+
}
125+
clientCounterOpts := WithClientCounterOptions(counterOpts...)
126+
clientMetrics := NewClientMetrics(clientCounterOpts, WithClientHandlingTimeHistogram(histOpts...))
127+
128+
requireNamespaceName(s.T(), "namespace1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
129+
requireHistNamespaceName(s.T(), "namespace1", clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
130+
}

providers/prometheus/options.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func WithSubsystem(subsystem string) CounterOption {
4646
}
4747
}
4848

49+
// WithNamespace allows you to add a Namespace to Counter metrics.
50+
func WithNamespace(namespace string) CounterOption {
51+
return func(o *prometheus.CounterOpts) {
52+
o.Namespace = namespace
53+
}
54+
}
55+
4956
// A HistogramOption lets you add options to Histogram metrics using With*
5057
// funcs.
5158
type HistogramOption func(*prometheus.HistogramOpts)
@@ -95,6 +102,13 @@ func WithHistogramSubsystem(subsystem string) HistogramOption {
95102
}
96103
}
97104

105+
// WithHistogramNamespace allows you to add a Namespace to histograms metrics.
106+
func WithHistogramNamespace(namespace string) HistogramOption {
107+
return func(o *prometheus.HistogramOpts) {
108+
o.Namespace = namespace
109+
}
110+
}
111+
98112
func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType {
99113
if !mInfo.IsClientStream && !mInfo.IsServerStream {
100114
return Unary

providers/prometheus/server_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,18 @@ func requireSubsystemName(t *testing.T, expect string, c prometheus.Collector) {
259259
t.Fail()
260260
}
261261

262+
func requireNamespaceName(t *testing.T, expect string, c prometheus.Collector) {
263+
t.Helper()
264+
metricFullName := reflect.ValueOf(*c.(prometheus.Metric).Desc()).FieldByName("fqName").String()
265+
266+
if strings.Split(metricFullName, "_")[0] == expect {
267+
return
268+
}
269+
270+
t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
271+
t.Fail()
272+
}
273+
262274
func requireValue(t *testing.T, expect int, c prometheus.Collector) {
263275
t.Helper()
264276
v := int(testutil.ToFloat64(c))
@@ -283,6 +295,18 @@ func requireHistSubsystemName(t *testing.T, expect string, o prometheus.Observer
283295
t.Fail()
284296
}
285297

298+
func requireHistNamespaceName(t *testing.T, expect string, o prometheus.Observer) {
299+
t.Helper()
300+
metricFullName := reflect.ValueOf(*o.(prometheus.Metric).Desc()).FieldByName("fqName").String()
301+
302+
if strings.Split(metricFullName, "_")[0] == expect {
303+
return
304+
}
305+
306+
t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
307+
t.Fail()
308+
}
309+
286310
func requireValueHistCount(t *testing.T, expect int, o prometheus.Observer) {
287311
t.Helper()
288312
v := int(toFloat64HistCount(o))

0 commit comments

Comments
 (0)