Skip to content

Commit

Permalink
Support for namespace in grpc prometheus counter and histogram metrics (
Browse files Browse the repository at this point in the history
#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]>
  • Loading branch information
hyungi and johanbrandhorst authored May 28, 2024
1 parent 3606823 commit 62b7de5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions providers/prometheus/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ func (s *ClientInterceptorTestSuite) TestWithSubsystem() {
requireSubsystemName(s.T(), "subsystem1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
requireHistSubsystemName(s.T(), "subsystem1", clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
}

func (s *ClientInterceptorTestSuite) TestWithNamespace() {
counterOpts := []CounterOption{
WithNamespace("namespace1"),
}
histOpts := []HistogramOption{
WithHistogramNamespace("namespace1"),
}
clientCounterOpts := WithClientCounterOptions(counterOpts...)
clientMetrics := NewClientMetrics(clientCounterOpts, WithClientHandlingTimeHistogram(histOpts...))

requireNamespaceName(s.T(), "namespace1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
requireHistNamespaceName(s.T(), "namespace1", clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
}
14 changes: 14 additions & 0 deletions providers/prometheus/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func WithSubsystem(subsystem string) CounterOption {
}
}

// WithNamespace allows you to add a Namespace to Counter metrics.
func WithNamespace(namespace string) CounterOption {
return func(o *prometheus.CounterOpts) {
o.Namespace = namespace
}
}

// A HistogramOption lets you add options to Histogram metrics using With*
// funcs.
type HistogramOption func(*prometheus.HistogramOpts)
Expand Down Expand Up @@ -95,6 +102,13 @@ func WithHistogramSubsystem(subsystem string) HistogramOption {
}
}

// WithHistogramNamespace allows you to add a Namespace to histograms metrics.
func WithHistogramNamespace(namespace string) HistogramOption {
return func(o *prometheus.HistogramOpts) {
o.Namespace = namespace
}
}

func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType {
if !mInfo.IsClientStream && !mInfo.IsServerStream {
return Unary
Expand Down
24 changes: 24 additions & 0 deletions providers/prometheus/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ func requireSubsystemName(t *testing.T, expect string, c prometheus.Collector) {
t.Fail()
}

func requireNamespaceName(t *testing.T, expect string, c prometheus.Collector) {
t.Helper()
metricFullName := reflect.ValueOf(*c.(prometheus.Metric).Desc()).FieldByName("fqName").String()

if strings.Split(metricFullName, "_")[0] == expect {
return
}

t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
t.Fail()
}

func requireValue(t *testing.T, expect int, c prometheus.Collector) {
t.Helper()
v := int(testutil.ToFloat64(c))
Expand All @@ -283,6 +295,18 @@ func requireHistSubsystemName(t *testing.T, expect string, o prometheus.Observer
t.Fail()
}

func requireHistNamespaceName(t *testing.T, expect string, o prometheus.Observer) {
t.Helper()
metricFullName := reflect.ValueOf(*o.(prometheus.Metric).Desc()).FieldByName("fqName").String()

if strings.Split(metricFullName, "_")[0] == expect {
return
}

t.Errorf("expected %s value to start with %s; ", metricFullName, expect)
t.Fail()
}

func requireValueHistCount(t *testing.T, expect int, o prometheus.Observer) {
t.Helper()
v := int(toFloat64HistCount(o))
Expand Down

0 comments on commit 62b7de5

Please sign in to comment.