Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for subsystem in grpc prometheus counter and histogram metrics #639

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Subsytem 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 Subsytem 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
Loading