diff --git a/interceptors/client.go b/interceptors/client.go index 120914225..86c51a079 100644 --- a/interceptors/client.go +++ b/interceptors/client.go @@ -22,7 +22,6 @@ func UnaryClientInterceptor(reportable ClientReportable) grpc.UnaryClientInterce reporter.PostMsgSend(req, nil, time.Since(r.startTime)) err := invoker(newCtx, method, req, reply, cc, opts...) reporter.PostMsgReceive(reply, err, time.Since(r.startTime)) - reporter.PostCall(err, time.Since(r.startTime)) return err } diff --git a/interceptors/logging/logging.go b/interceptors/logging/logging.go index 62611172a..1bf043d03 100644 --- a/interceptors/logging/logging.go +++ b/interceptors/logging/logging.go @@ -58,7 +58,7 @@ func newCommonFields(kind string, c interceptors.CallMeta) Fields { // Iter returns FieldsIterator. func (f Fields) Iter() FieldsIterator { // We start from -2 as we iterate over two items per iteration and first iteration will advance iterator to 0. - return iter{i: -2, f: f} + return &iter{i: -2, f: f} } // FieldsIterator is an interface allowing to iterate over fields. @@ -72,7 +72,7 @@ type iter struct { i int } -func (i iter) Next() bool { +func (i *iter) Next() bool { if i.i >= len(i.f) { return false } @@ -81,7 +81,7 @@ func (i iter) Next() bool { return i.i < len(i.f) } -func (i iter) At() (k, v string) { +func (i *iter) At() (k, v string) { if i.i < 0 || i.i >= len(i.f) { return "", "" } diff --git a/providers/openmetrics/client_test.go b/providers/openmetrics/client_test.go index 1289440ef..e0cda5712 100644 --- a/providers/openmetrics/client_test.go +++ b/providers/openmetrics/client_test.go @@ -4,11 +4,8 @@ package metrics import ( - "context" "io" - "net" "testing" - "time" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -16,60 +13,29 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2/testproto/v1" + "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" ) func TestClientInterceptorSuite(t *testing.T) { - suite.Run(t, &ClientInterceptorTestSuite{}) + c := NewClientMetrics(WithClientHandlingTimeHistogram()) + suite.Run(t, &ClientInterceptorTestSuite{ + InterceptorTestSuite: &testpb.InterceptorTestSuite{ + TestService: &testpb.TestPingService{T: t}, + ClientOpts: []grpc.DialOption{ + grpc.WithUnaryInterceptor(UnaryClientInterceptor(c)), + grpc.WithStreamInterceptor(StreamClientInterceptor(c)), + }, + }, + clientMetrics: c, + }) } type ClientInterceptorTestSuite struct { - suite.Suite - - serverListener net.Listener - server *grpc.Server - clientConn *grpc.ClientConn - testClient pb_testproto.TestServiceClient - ctx context.Context - cancel context.CancelFunc - clientMetrics *ClientMetrics -} - -func (s *ClientInterceptorTestSuite) SetupSuite() { - var err error - - // Make all RPC calls last at most 2 sec, meaning all async issues or deadlock will not kill tests. - s.ctx, s.cancel = context.WithTimeout(context.TODO(), 2*time.Second) - - s.clientMetrics = NewClientMetrics(WithClientHandlingTimeHistogram()) - - s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") - require.NoError(s.T(), err, "must be able to allocate a port for serverListener") - - // This is the point where we hook up the interceptor - s.server = grpc.NewServer() - pb_testproto.RegisterTestServiceServer(s.server, &testService{t: s.T()}) - - go func() { - err = s.server.Serve(s.serverListener) - require.NoError(s.T(), err, "must not error on server listening") - }() - - s.clientConn, err = grpc.DialContext( - s.ctx, - s.serverListener.Addr().String(), - grpc.WithInsecure(), - grpc.WithBlock(), - grpc.WithUnaryInterceptor(UnaryClientInterceptor(s.clientMetrics)), - grpc.WithStreamInterceptor(StreamClientInterceptor(s.clientMetrics)), - ) - require.NoError(s.T(), err, "must not error on client Dial") - s.testClient = pb_testproto.NewTestServiceClient(s.clientConn) + *testpb.InterceptorTestSuite + clientMetrics *ClientMetrics } func (s *ClientInterceptorTestSuite) SetupTest() { - - // Make sure every test starts with same fresh, intialized metric state. s.clientMetrics.clientStartedCounter.Reset() s.clientMetrics.clientHandledCounter.Reset() s.clientMetrics.clientHandledHistogram.Reset() @@ -77,47 +43,33 @@ func (s *ClientInterceptorTestSuite) SetupTest() { s.clientMetrics.clientStreamMsgSent.Reset() } -func (s *ClientInterceptorTestSuite) TearDownSuite() { - if s.clientConn != nil { - s.clientConn.Close() - } - if s.serverListener != nil { - s.server.Stop() - s.T().Logf("stopped grpc.Server at: %v", s.serverListener.Addr().String()) - s.serverListener.Close() - } -} - -func (s *ClientInterceptorTestSuite) TearDownTest() { - s.cancel() -} - func (s *ClientInterceptorTestSuite) TestUnaryIncrementsMetrics() { - _, err := s.testClient.PingEmpty(s.ctx, &pb_testproto.PingEmptyRequest{}) // should return with code=OK + _, err := s.Client.PingEmpty(s.SimpleCtx(), &testpb.PingEmptyRequest{}) require.NoError(s.T(), err) - requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty")) - requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty", "OK")) - requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty")) - _, err = s.testClient.PingError(s.ctx, &pb_testproto.PingErrorRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty")) + requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty", "OK")) + requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty")) + + _, err = s.Client.PingError(s.SimpleCtx(), &testpb.PingErrorRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) require.Error(s.T(), err) - requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError")) - requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError", "FailedPrecondition")) - requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError")) + requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingError")) + requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingError", "FailedPrecondition")) + requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "PingError")) } func (s *ClientInterceptorTestSuite) TestStartedStreamingIncrementsStarted() { - _, err := s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{}) + _, err := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{}) require.NoError(s.T(), err) - requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) - _, err = s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + _, err = s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) require.NoError(s.T(), err, "PingList must not fail immediately") - requireValue(s.T(), 2, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValue(s.T(), 2, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) } func (s *ClientInterceptorTestSuite) TestStreamingIncrementsMetrics() { - ss, err := s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{}) // should return with code=OK + ss, err := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{}) require.NoError(s.T(), err) // Do a read, just for kicks. count := 0 @@ -129,15 +81,15 @@ func (s *ClientInterceptorTestSuite) TestStreamingIncrementsMetrics() { require.NoError(s.T(), err, "reading pingList shouldn't fail") count++ } - require.EqualValues(s.T(), countListResponses, count, "Number of received msg on the wire must match") + require.EqualValues(s.T(), testpb.ListResponseCount, count, "Number of received msg on the wire must match") - requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList", "OK")) - requireValue(s.T(), countListResponses+1 /* + EOF */, s.clientMetrics.clientStreamMsgReceived.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValue(s.T(), 1, s.clientMetrics.clientStreamMsgSent.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValue(s.T(), 1, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList", "OK")) + requireValue(s.T(), testpb.ListResponseCount+1 /* + EOF */, s.clientMetrics.clientStreamMsgReceived.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValue(s.T(), 1, s.clientMetrics.clientStreamMsgSent.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValueHistCount(s.T(), 1, s.clientMetrics.clientHandledHistogram.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) - ss, err = s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + ss, err = s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) require.NoError(s.T(), err, "PingList must not fail immediately") // Do a read, just to progate errors. @@ -145,7 +97,7 @@ func (s *ClientInterceptorTestSuite) TestStreamingIncrementsMetrics() { st, _ := status.FromError(err) require.Equal(s.T(), codes.FailedPrecondition, st.Code(), "Recv must return FailedPrecondition, otherwise the test is wrong") - requireValue(s.T(), 2, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValue(s.T(), 1, s.clientMetrics.clientHandledCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList", "FailedPrecondition")) - requireValueHistCount(s.T(), 2, s.clientMetrics.clientHandledHistogram.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValue(s.T(), 2, s.clientMetrics.clientStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + 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")) } diff --git a/providers/openmetrics/reporter.go b/providers/openmetrics/reporter.go index 4aa364340..dd66811ae 100644 --- a/providers/openmetrics/reporter.go +++ b/providers/openmetrics/reporter.go @@ -68,12 +68,12 @@ type reportable struct { serverMetrics *ServerMetrics } -func (rep *reportable) ServerReporter(ctx context.Context, _ interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) { - return rep.reporter(ctx, rep.serverMetrics, nil, typ, service, method, KindServer) +func (rep *reportable) ServerReporter(ctx context.Context, meta interceptors.CallMeta) (interceptors.Reporter, context.Context) { + return rep.reporter(ctx, rep.serverMetrics, nil, meta.Typ, meta.Service, meta.Method, KindServer) } -func (rep *reportable) ClientReporter(ctx context.Context, _ interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) { - return rep.reporter(ctx, nil, rep.clientMetrics, typ, service, method, KindClient) +func (rep *reportable) ClientReporter(ctx context.Context, meta interceptors.CallMeta) (interceptors.Reporter, context.Context) { + return rep.reporter(ctx, nil, rep.clientMetrics, meta.Typ, meta.Service, meta.Method, KindClient) } func (rep *reportable) reporter(ctx context.Context, sm *ServerMetrics, cm *ClientMetrics, rpcType interceptors.GRPCType, service, method string, kind Kind) (interceptors.Reporter, context.Context) { diff --git a/providers/openmetrics/server_test.go b/providers/openmetrics/server_test.go index 090a6d461..16a08174c 100644 --- a/providers/openmetrics/server_test.go +++ b/providers/openmetrics/server_test.go @@ -6,10 +6,8 @@ package metrics import ( "bufio" "context" - "errors" "fmt" "io" - "net" "net/http" "net/http/httptest" "reflect" @@ -26,87 +24,37 @@ import ( "github.com/stretchr/testify/suite" "google.golang.org/grpc" "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" - pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2/testproto/v1" -) - -const ( - pingDefaultValue = "I like kittens." - countListResponses = 20 + "github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb" ) func TestServerInterceptorSuite(t *testing.T) { - suite.Run(t, &ServerInterceptorTestSuite{}) + s := NewServerMetrics(WithServerHandlingTimeHistogram()) + suite.Run(t, &ServerInterceptorTestSuite{ + InterceptorTestSuite: &testpb.InterceptorTestSuite{ + TestService: &testpb.TestPingService{T: t}, + ServerOpts: []grpc.ServerOption{ + grpc.StreamInterceptor(StreamServerInterceptor(s)), + grpc.UnaryInterceptor(UnaryServerInterceptor(s)), + }, + }, + serverMetrics: s, + }) } type ServerInterceptorTestSuite struct { - suite.Suite - - serverListener net.Listener - server *grpc.Server - clientConn *grpc.ClientConn - testClient pb_testproto.TestServiceClient - ctx context.Context - cancel context.CancelFunc + *testpb.InterceptorTestSuite serverMetrics *ServerMetrics } -func (s *ServerInterceptorTestSuite) SetupSuite() { - var err error - - // Make all RPC calls last at most 2 sec, meaning all async issues or deadlock will not kill tests. - s.ctx, s.cancel = context.WithTimeout(context.TODO(), 2*time.Second) - - s.serverMetrics = NewServerMetrics(WithServerHandlingTimeHistogram()) - - s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") - require.NoError(s.T(), err, "must be able to allocate a port for serverListener") - - // This is the point where we hook up the interceptor - s.server = grpc.NewServer( - grpc.StreamInterceptor(StreamServerInterceptor(s.serverMetrics)), - grpc.UnaryInterceptor(UnaryServerInterceptor(s.serverMetrics)), - ) - pb_testproto.RegisterTestServiceServer(s.server, &testService{t: s.T()}) - - go func() { - err = s.server.Serve(s.serverListener) - require.NoError(s.T(), err, "must not error on server listening") - }() - - s.clientConn, err = grpc.DialContext(s.ctx, s.serverListener.Addr().String(), grpc.WithInsecure(), grpc.WithBlock()) - require.NoError(s.T(), err, "must not error on client Dial") - s.testClient = pb_testproto.NewTestServiceClient(s.clientConn) -} - func (s *ServerInterceptorTestSuite) SetupTest() { - // Make all RPC calls last at most 2 sec, meaning all async issues or deadlock will not kill tests. - s.ctx, s.cancel = context.WithTimeout(context.TODO(), 2*time.Second) - - // Make sure every test starts with same fresh, intialized metric state. s.serverMetrics.serverStartedCounter.Reset() s.serverMetrics.serverHandledCounter.Reset() s.serverMetrics.serverHandledHistogram.Reset() s.serverMetrics.serverStreamMsgReceived.Reset() s.serverMetrics.serverStreamMsgSent.Reset() - s.serverMetrics.InitializeMetrics(s.server) -} - -func (s *ServerInterceptorTestSuite) TearDownSuite() { - if s.clientConn != nil { - s.clientConn.Close() - } - if s.serverListener != nil { - s.server.Stop() - s.T().Logf("stopped grpc.Server at: %v", s.serverListener.Addr().String()) - s.serverListener.Close() - } -} - -func (s *ServerInterceptorTestSuite) TearDownTest() { - s.cancel() + s.serverMetrics.InitializeMetrics(s.Server) } func (s *ServerInterceptorTestSuite) TestRegisterPresetsStuff() { @@ -119,16 +67,16 @@ func (s *ServerInterceptorTestSuite) TestRegisterPresetsStuff() { existingLabels []string }{ // Order of label is irrelevant. - {"grpc_server_started_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingEmpty", "unary"}}, - {"grpc_server_started_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingList", "server_stream"}}, - {"grpc_server_msg_received_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingList", "server_stream"}}, - {"grpc_server_msg_sent_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingEmpty", "unary"}}, - {"grpc_server_handling_seconds_sum", []string{"providers.openmetrics.testproto.v1.TestService", "PingEmpty", "unary"}}, - {"grpc_server_handling_seconds_count", []string{"providers.openmetrics.testproto.v1.TestService", "PingList", "server_stream"}}, - {"grpc_server_handled_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingList", "server_stream", "OutOfRange"}}, - {"grpc_server_handled_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingList", "server_stream", "Aborted"}}, - {"grpc_server_handled_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingEmpty", "unary", "FailedPrecondition"}}, - {"grpc_server_handled_total", []string{"providers.openmetrics.testproto.v1.TestService", "PingEmpty", "unary", "ResourceExhausted"}}, + {"grpc_server_started_total", []string{testpb.TestServiceFullName, "PingEmpty", "unary"}}, + {"grpc_server_started_total", []string{testpb.TestServiceFullName, "PingList", "server_stream"}}, + {"grpc_server_msg_received_total", []string{testpb.TestServiceFullName, "PingList", "server_stream"}}, + {"grpc_server_msg_sent_total", []string{testpb.TestServiceFullName, "PingEmpty", "unary"}}, + {"grpc_server_handling_seconds_sum", []string{testpb.TestServiceFullName, "PingEmpty", "unary"}}, + {"grpc_server_handling_seconds_count", []string{testpb.TestServiceFullName, "PingList", "server_stream"}}, + {"grpc_server_handled_total", []string{testpb.TestServiceFullName, "PingList", "server_stream", "OutOfRange"}}, + {"grpc_server_handled_total", []string{testpb.TestServiceFullName, "PingList", "server_stream", "Aborted"}}, + {"grpc_server_handled_total", []string{testpb.TestServiceFullName, "PingEmpty", "unary", "FailedPrecondition"}}, + {"grpc_server_handled_total", []string{testpb.TestServiceFullName, "PingEmpty", "unary", "ResourceExhausted"}}, } { lineCount := len(fetchPrometheusLines(s.T(), registry, testCase.metricName, testCase.existingLabels...)) assert.NotZero(s.T(), lineCount, "metrics must exist for test case %d", testID) @@ -136,33 +84,33 @@ func (s *ServerInterceptorTestSuite) TestRegisterPresetsStuff() { } func (s *ServerInterceptorTestSuite) TestUnaryIncrementsMetrics() { - _, err := s.testClient.PingEmpty(s.ctx, &pb_testproto.PingEmptyRequest{}) // should return with code=OK + _, err := s.Client.PingEmpty(s.SimpleCtx(), &testpb.PingEmptyRequest{}) require.NoError(s.T(), err) - requireValue(s.T(), 1, s.serverMetrics.serverStartedCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty")) - requireValue(s.T(), 1, s.serverMetrics.serverHandledCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty", "OK")) - requireValueHistCount(s.T(), 1, s.serverMetrics.serverHandledHistogram.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingEmpty")) + requireValue(s.T(), 1, s.serverMetrics.serverStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty")) + requireValue(s.T(), 1, s.serverMetrics.serverHandledCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty", "OK")) + requireValueHistCount(s.T(), 1, s.serverMetrics.serverHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "PingEmpty")) - _, err = s.testClient.PingError(s.ctx, &pb_testproto.PingErrorRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + _, err = s.Client.PingError(s.SimpleCtx(), &testpb.PingErrorRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) require.Error(s.T(), err) - requireValue(s.T(), 1, s.serverMetrics.serverStartedCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError")) - requireValue(s.T(), 1, s.serverMetrics.serverHandledCounter.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError", "FailedPrecondition")) - requireValueHistCount(s.T(), 1, s.serverMetrics.serverHandledHistogram.WithLabelValues("unary", "providers.openmetrics.testproto.v1.TestService", "PingError")) + requireValue(s.T(), 1, s.serverMetrics.serverStartedCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingError")) + requireValue(s.T(), 1, s.serverMetrics.serverHandledCounter.WithLabelValues("unary", testpb.TestServiceFullName, "PingError", "FailedPrecondition")) + requireValueHistCount(s.T(), 1, s.serverMetrics.serverHandledHistogram.WithLabelValues("unary", testpb.TestServiceFullName, "PingError")) } func (s *ServerInterceptorTestSuite) TestStartedStreamingIncrementsStarted() { - _, err := s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{}) + _, err := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{}) require.NoError(s.T(), err) - requireValueWithRetry(s.ctx, s.T(), 1, - s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) - _, err = s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + _, err = s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) require.NoError(s.T(), err, "PingList must not fail immediately") - requireValueWithRetry(s.ctx, s.T(), 2, - s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 2, + s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) } func (s *ServerInterceptorTestSuite) TestStreamingIncrementsMetrics() { - ss, _ := s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{}) // should return with code=OK + ss, _ := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{}) // Do a read, just for kicks. count := 0 for { @@ -173,28 +121,28 @@ func (s *ServerInterceptorTestSuite) TestStreamingIncrementsMetrics() { require.NoError(s.T(), err, "reading pingList shouldn't fail") count++ } - require.EqualValues(s.T(), countListResponses, count, "Number of received msg on the wire must match") - - requireValueWithRetry(s.ctx, s.T(), 1, - s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValueWithRetry(s.ctx, s.T(), 1, - s.serverMetrics.serverHandledCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList", "OK")) - requireValueWithRetry(s.ctx, s.T(), countListResponses, - s.serverMetrics.serverStreamMsgSent.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValueWithRetry(s.ctx, s.T(), 1, - s.serverMetrics.serverStreamMsgReceived.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValueWithRetryHistCount(s.ctx, s.T(), 1, - s.serverMetrics.serverHandledHistogram.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - - _, err := s.testClient.PingList(s.ctx, &pb_testproto.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition + require.EqualValues(s.T(), testpb.ListResponseCount, count, "Number of received msg on the wire must match") + + requireValueWithRetry(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverHandledCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList", "OK")) + requireValueWithRetry(s.SimpleCtx(), s.T(), testpb.ListResponseCount, + s.serverMetrics.serverStreamMsgSent.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverStreamMsgReceived.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValueWithRetryHistCount(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverHandledHistogram.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + + _, err := s.Client.PingList(s.SimpleCtx(), &testpb.PingListRequest{ErrorCodeReturned: uint32(codes.FailedPrecondition)}) // should return with code=FailedPrecondition require.NoError(s.T(), err, "PingList must not fail immediately") - requireValueWithRetry(s.ctx, s.T(), 2, - s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) - requireValueWithRetry(s.ctx, s.T(), 1, - s.serverMetrics.serverHandledCounter.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList", "FailedPrecondition")) - requireValueWithRetryHistCount(s.ctx, s.T(), 2, - s.serverMetrics.serverHandledHistogram.WithLabelValues("server_stream", "providers.openmetrics.testproto.v1.TestService", "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 2, + s.serverMetrics.serverStartedCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) + requireValueWithRetry(s.SimpleCtx(), s.T(), 1, + s.serverMetrics.serverHandledCounter.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList", "FailedPrecondition")) + requireValueWithRetryHistCount(s.SimpleCtx(), s.T(), 2, + s.serverMetrics.serverHandledHistogram.WithLabelValues("server_stream", testpb.TestServiceFullName, "PingList")) } // fetchPrometheusLines does mocked HTTP GET request against real prometheus handler to get the same view that Prometheus @@ -233,42 +181,6 @@ func fetchPrometheusLines(t *testing.T, reg prometheus.Gatherer, metricName stri return ret } -type testService struct { - t *testing.T -} - -func (s *testService) PingEmpty(ctx context.Context, _ *pb_testproto.PingEmptyRequest) (*pb_testproto.PingEmptyResponse, error) { - return &pb_testproto.PingEmptyResponse{Value: pingDefaultValue, Counter: 42}, nil -} - -func (s *testService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) { - // Send user trailers and headers. - if _, ok := grpc.Method(ctx); !ok { - return nil, errors.New("cannot retrieve method name") - } - return &pb_testproto.PingResponse{Value: ping.Value, Counter: 42}, nil -} - -func (s *testService) PingError(ctx context.Context, ping *pb_testproto.PingErrorRequest) (*pb_testproto.PingErrorResponse, error) { - code := codes.Code(ping.ErrorCodeReturned) - return nil, status.Error(code, "Userspace error.") -} - -func (s *testService) PingList(ping *pb_testproto.PingListRequest, stream pb_testproto.TestService_PingListServer) error { - if _, ok := grpc.Method(stream.Context()); !ok { - return errors.New("cannot retrieve method name") - } - if ping.ErrorCodeReturned != 0 { - return status.Error(codes.Code(ping.ErrorCodeReturned), "foobar") - } - // Send user trailers and headers. - for i := 0; i < countListResponses; i++ { - err := stream.Send(&pb_testproto.PingListResponse{Value: ping.Value, Counter: int32(i)}) - require.NoError(s.t, err, "must not error on server sending streaming values") - } - return nil -} - // toFloat64HistCount does the same thing as prometheus go client testutil.ToFloat64, but for histograms. // TODO(bwplotka): Upstream this function to prometheus client. func toFloat64HistCount(h prometheus.Observer) uint64 {