Skip to content

Commit

Permalink
Support for subsystem in grpc prometheus counter and histogram metrics (
Browse files Browse the repository at this point in the history
  • Loading branch information
rohsaini committed Sep 25, 2023
1 parent 47ca7d6 commit 71d7422
Show file tree
Hide file tree
Showing 3 changed files with 66 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 @@ -100,3 +100,17 @@ func (s *ClientInterceptorTestSuite) TestStreamingIncrementsMetrics() {
requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList", "FailedPrecondition"))
requireValueHistCount(s.T(), 2, s.clientMetrics.clientHandledHistogram.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList"))
}

func (s *ClientInterceptorTestSuite) TestWithSubsystem() {
counterOpts := []CounterOption{
WithSubsystem("subsystem1"),
}
histOpts := []HistogramOption{
WithHistogramSubsystem("subsystem1"),
}
clientCounterOpts := WithClientCounterOptions(counterOpts...)
clientMetrics := NewClientMetrics(clientCounterOpts, WithClientHandlingTimeHistogram(histOpts...))

requireSubsystemName(s.T(), "subsystem1", clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
requireHistSubsystemName(s.T(), "subsystem1", 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 @@ -39,6 +39,13 @@ func WithConstLabels(labels prometheus.Labels) CounterOption {
}
}

// WithSubsystem allows you to add a Subsystem to Counter metrics.
func WithSubsystem(subsystem string) CounterOption {
return func(o *prometheus.CounterOpts) {
o.Subsystem = subsystem
}
}

// A HistogramOption lets you add options to Histogram metrics using With*
// funcs.
type HistogramOption func(*prometheus.HistogramOpts)
Expand Down Expand Up @@ -81,6 +88,13 @@ func WithHistogramConstLabels(labels prometheus.Labels) HistogramOption {
}
}

// WithHistogramSubsystem allows you to add a Subsystem to histograms metrics.
func WithHistogramSubsystem(subsystem string) HistogramOption {
return func(o *prometheus.HistogramOpts) {
o.Subsystem = subsystem
}
}

func typeFromMethodInfo(mInfo *grpc.MethodInfo) grpcType {
if !mInfo.IsClientStream && !mInfo.IsServerStream {
return Unary
Expand Down
38 changes: 38 additions & 0 deletions providers/prometheus/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func (s *ServerInterceptorTestSuite) SetupTest() {
s.serverMetrics.InitializeMetrics(s.Server)
}

func (s *ServerInterceptorTestSuite) TestWithSubsystem() {
counterOpts := []CounterOption{
WithSubsystem("subsystem1"),
}
histOpts := []HistogramOption{
WithHistogramSubsystem("subsystem1"),
}
serverCounterOpts := WithServerCounterOptions(counterOpts...)
serverMetrics := NewServerMetrics(serverCounterOpts, WithServerHandlingTimeHistogram(histOpts...))

requireSubsystemName(s.T(), "subsystem1", serverMetrics.serverStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
requireHistSubsystemName(s.T(), "subsystem1", serverMetrics.serverHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "dummy"))
}

func (s *ServerInterceptorTestSuite) TestRegisterPresetsStuff() {
registry := prometheus.NewPedanticRegistry()
s.Require().NoError(registry.Register(s.serverMetrics))
Expand Down Expand Up @@ -233,6 +247,18 @@ func toFloat64HistCount(h prometheus.Observer) uint64 {
panic(fmt.Errorf("collected a non-histogram metric: %s", pb))
}

func requireSubsystemName(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 @@ -245,6 +271,18 @@ func requireValue(t *testing.T, expect int, c prometheus.Collector) {
t.Fail()
}

func requireHistSubsystemName(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 71d7422

Please sign in to comment.