Skip to content

[instrumentation/google.golang.org/grpc/otelgrpc] Do not assume HandleRPC receives a gRPCContext - #4825

Merged
MadVikingGod merged 8 commits into
open-telemetry:mainfrom
mx-psi:mx-psi/grpc-safeguard
Jan 18, 2024
Merged

[instrumentation/google.golang.org/grpc/otelgrpc] Do not assume HandleRPC receives a gRPCContext#4825
MadVikingGod merged 8 commits into
open-telemetry:mainfrom
mx-psi:mx-psi/grpc-safeguard

Conversation

@mx-psi

@mx-psi mx-psi commented Jan 17, 2024

Copy link
Copy Markdown
Member

@codecov

codecov Bot commented Jan 17, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a28c68b) 81.3% compared to head (9be483c) 81.3%.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #4825   +/-   ##
=====================================
  Coverage   81.3%   81.3%           
=====================================
  Files        150     150           
  Lines      10885   10890    +5     
=====================================
+ Hits        8853    8858    +5     
  Misses      1873    1873           
  Partials     159     159           
Files Coverage Δ
...n/google.golang.org/grpc/otelgrpc/stats_handler.go 100.0% <100.0%> (ø)

Comment thread instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go
Comment thread instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go Outdated
@mx-psi mx-psi changed the title [instrumentation/google.golang.org/grpc/otelgrpc] Add safeguard to HandleRPC [instrumentation/google.golang.org/grpc/otelgrpc] Do not assume HandleRPC receives a gRPCContext Jan 17, 2024
@mx-psi
mx-psi requested a review from Aneurysm9 January 17, 2024 18:28
@pellared

pellared commented Jan 17, 2024

Copy link
Copy Markdown
Member

Is it not an effect of bug in https://github.com/grpc/grpc-go? If so could you additionally report it there?

References:

It would be also great to reproduce the issue and have a test that would help avoiding regression bugs.

Side note: I am not against having a "defensive" code.

Comment thread instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go
Comment thread instrumentation/google.golang.org/grpc/otelgrpc/stats_handler.go
gctx, ok := ctx.Value(gRPCContextKey{}).(*gRPCContext)
if ok && gctx != nil {
metricAttrs = make([]attribute.KeyValue, 0, len(gctx.metricAttrs)+1)
metricAttrs = append(metricAttrs, gctx.metricAttrs...)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use copy instead of you replace the zero on the preceding line with the length of the source slice. Alternately, consider using the slices.Clone function if you don't really need that extra slot in the backing array. It's implemented by append today, without the preceding call to make.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was already there and I am only moving it inside the if block, I would prefer to do this change on a separate PR if that's okay

@codeboten

Copy link
Copy Markdown
Contributor

Is it not an effect of bug in https://github.com/grpc/grpc-go? If so could you additionally report it there?

References:

* https://github.com/grpc/grpc-go/tree/master/examples/features/stats_monitoring

* https://pkg.go.dev/google.golang.org/grpc/stats#Handler

It would be also great to reproduce the issue and have a test that would help avoiding regression bugs.

Side note: I am not against having a "defensive" code.

It's not clear to me why this was not encountered before, but the examples in the grpc repo does suggest that defensive code is the way to go here: https://github.com/grpc/grpc-go/blob/ddd377f19841eae70862559c854d957d61b3b692/examples/features/stats_monitoring/statshandler/handler.go#L73

I'm not sure if this expected behaviour or a new behaviour though.

@pellared

pellared commented Jan 17, 2024

Copy link
Copy Markdown
Member

It's not clear to me why this was not encountered before, but the examples in the grpc repo does suggest that defensive code is the way to go here

I agree. Therefore, I am not against the change here.

Still, the docs suggests that it should not happen.
I think it might be worth reporting a bug. However, we should have repro steps...

@pellared pellared left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MadVikingGod IMO we can consider adding it to the upcoming release.

@mx-psi Can you please still follow-up and try to reproduce it and eventually create an issue in https://github.com/grpc/grpc-go?

@mx-psi

mx-psi commented Jan 18, 2024

Copy link
Copy Markdown
Member Author

Filed grpc/grpc-go/issues/6928. I still don't have a way to reproduce the issue, but I at least shared all we know so far. Feel free to comment if anything is missing there.

@MadVikingGod
MadVikingGod merged commit 40290ea into open-telemetry:main Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants