Skip to content

Commit bbff530

Browse files
committed
[instrumentation/google.golang.org/grpc/otelgrpc] Add safeguard to HandleRPC
To help prevent crashes such as open-telemetry/opentelemetry-collector/issues/9296, check if the context is nil before proceeding. This is inspired by: https://github.com/grpc/grpc-go/blob/ddd377f19841eae70862559c854d957d61b3b692/stats/opencensus/opencensus.go#L204-L207
1 parent 96790b3 commit bbff530

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (h *clientHandler) HandleConn(context.Context, stats.ConnStats) {
136136
func (c *config) handleRPC(ctx context.Context, rs stats.RPCStats, isServer bool) { // nolint: revive // isServer is not a control flag.
137137
span := trace.SpanFromContext(ctx)
138138
gctx, _ := ctx.Value(gRPCContextKey{}).(*gRPCContext)
139+
if gctx == nil {
140+
// Shouldn't happen because TagRPC populates this information.
141+
return
142+
}
139143
var messageId int64
140144
metricAttrs := make([]attribute.KeyValue, 0, len(gctx.metricAttrs)+1)
141145
metricAttrs = append(metricAttrs, gctx.metricAttrs...)

0 commit comments

Comments
 (0)