Skip to content

Commit 49d9c0d

Browse files
author
Aliaksandr Mianzhynski
authored
openmetrics: forward server context (#434)
1 parent 709d415 commit 49d9c0d

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

providers/openmetrics/reporter.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ type reportable struct {
6969
}
7070

7171
func (rep *reportable) ServerReporter(ctx context.Context, _ interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) {
72-
return rep.reporter(rep.serverMetrics, nil, typ, service, method, KindServer)
72+
return rep.reporter(ctx, rep.serverMetrics, nil, typ, service, method, KindServer)
7373
}
7474

7575
func (rep *reportable) ClientReporter(ctx context.Context, _ interface{}, typ interceptors.GRPCType, service string, method string) (interceptors.Reporter, context.Context) {
76-
return rep.reporter(nil, rep.clientMetrics, typ, service, method, KindClient)
76+
return rep.reporter(ctx, nil, rep.clientMetrics, typ, service, method, KindClient)
7777
}
7878

79-
func (rep *reportable) reporter(sm *ServerMetrics, cm *ClientMetrics, rpcType interceptors.GRPCType, service, method string, kind Kind) (interceptors.Reporter, context.Context) {
79+
func (rep *reportable) reporter(ctx context.Context, sm *ServerMetrics, cm *ClientMetrics, rpcType interceptors.GRPCType, service, method string, kind Kind) (interceptors.Reporter, context.Context) {
8080
r := &reporter{
8181
clientMetrics: cm,
8282
serverMetrics: sm,
@@ -110,7 +110,5 @@ func (rep *reportable) reporter(sm *ServerMetrics, cm *ClientMetrics, rpcType in
110110
}
111111
r.serverMetrics.serverStartedCounter.WithLabelValues(string(r.typ), r.service, r.method).Inc()
112112
}
113-
114-
// TODO: @yashrsharma44 - What should we use instead of the context.Background()?
115-
return r, context.Background()
113+
return r, ctx
116114
}

providers/openmetrics/server_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package metrics
66
import (
77
"bufio"
88
"context"
9+
"errors"
910
"fmt"
1011
"io"
1112
"net"
@@ -242,6 +243,9 @@ func (s *testService) PingEmpty(ctx context.Context, _ *pb_testproto.PingEmptyRe
242243

243244
func (s *testService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
244245
// Send user trailers and headers.
246+
if _, ok := grpc.Method(ctx); !ok {
247+
return nil, errors.New("cannot retrieve method name")
248+
}
245249
return &pb_testproto.PingResponse{Value: ping.Value, Counter: 42}, nil
246250
}
247251

@@ -251,6 +255,9 @@ func (s *testService) PingError(ctx context.Context, ping *pb_testproto.PingErro
251255
}
252256

253257
func (s *testService) PingList(ping *pb_testproto.PingListRequest, stream pb_testproto.TestService_PingListServer) error {
258+
if _, ok := grpc.Method(stream.Context()); !ok {
259+
return errors.New("cannot retrieve method name")
260+
}
254261
if ping.ErrorCodeReturned != 0 {
255262
return status.Error(codes.Code(ping.ErrorCodeReturned), "foobar")
256263
}

0 commit comments

Comments
 (0)