diff --git a/providers/prometheus/client_test.go b/providers/prometheus/client_test.go index 4e5b9b02a..76dbd43db 100644 --- a/providers/prometheus/client_test.go +++ b/providers/prometheus/client_test.go @@ -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")) +} diff --git a/providers/prometheus/options.go b/providers/prometheus/options.go index 2a47d4b08..bdd171e29 100644 --- a/providers/prometheus/options.go +++ b/providers/prometheus/options.go @@ -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) @@ -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 diff --git a/providers/prometheus/server_test.go b/providers/prometheus/server_test.go index f64812ec0..422b2cfe2 100644 --- a/providers/prometheus/server_test.go +++ b/providers/prometheus/server_test.go @@ -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)) @@ -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)) @@ -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))