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

otelgrpc: add custom attributes to the stats handler #5133

Merged
merged 2 commits into from
Aug 22, 2024

Conversation

inigohu
Copy link
Contributor

@inigohu inigohu commented Feb 19, 2024

Fixes #3894

@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 2 times, most recently from ce0e777 to aac80ca Compare February 20, 2024 10:52
@inigohu inigohu marked this pull request as ready for review February 20, 2024 10:54
@inigohu inigohu requested a review from a team February 20, 2024 10:54
@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 2 times, most recently from eacf0ac to d594e0c Compare May 9, 2024 15:52
@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch from d594e0c to 99ea5ca Compare June 3, 2024 10:57
@inigohu
Copy link
Contributor Author

inigohu commented Jun 3, 2024

Friendly ping @dashpole @hanyuancheung

@dashpole
Copy link
Contributor

dashpole commented Jun 3, 2024

This looks like it does a lot more than just add a labeler. Would you mind doing general refactoring in its own PR?

@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 2 times, most recently from e4f7d19 to c82b0f0 Compare June 4, 2024 09:41
@inigohu
Copy link
Contributor Author

inigohu commented Jun 4, 2024

This looks like it does a lot more than just add a labeler. Would you mind doing general refactoring in its own PR?

Sure. I have simplified this PR by adding only the labeler.

Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

can you add tests?

@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 2 times, most recently from 26b7c36 to 3d28af9 Compare June 5, 2024 11:07
Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

Can you test the span from the stats handler as well?

Copy link

codecov bot commented Jun 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 65.6%. Comparing base (57e55dc) to head (aba5258).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #5133   +/-   ##
=====================================
  Coverage   65.5%   65.6%           
=====================================
  Files        203     203           
  Lines      12936   12946   +10     
=====================================
+ Hits        8484    8494   +10     
  Misses      4198    4198           
  Partials     254     254           
Files Coverage Δ
...entation/google.golang.org/grpc/otelgrpc/config.go 90.0% <100.0%> (+1.0%) ⬆️
...n/google.golang.org/grpc/otelgrpc/stats_handler.go 100.0% <100.0%> (ø)

@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 2 times, most recently from 2d79ff4 to a6b8d67 Compare June 6, 2024 22:47
@inigohu
Copy link
Contributor Author

inigohu commented Jun 17, 2024

@dashpole PTAL

Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

I thought we were going to use a context-based pattern (similar to the otelhttp.Labeler) to support this? Am I misremembering?

@inigohu
Copy link
Contributor Author

inigohu commented Jun 18, 2024

I thought we were going to use a context-based pattern (similar to the otelhttp.Labeler) to support this? Am I misremembering?

Yes, sorry, I forgot to comment this last change. My first idea was to follow the otelhttp.Labeler strategy using the context to pass the labels, but when I was adding the tests I realised that maybe it doesn't make much sense to do it this way, because unlike http.Handler and grpc.UnaryInterceptors, you can't chain different grpc.StatsHandler to add labels and pass them through the context.

That's why I changed the approach. The behaviour differs from that of otelhttp.Labeler. That one is request/context based tagging and this one is server based tagging.

WDYT @dashpole ?

@dashpole
Copy link
Contributor

A few thoughts/ideas:

  • If we use constant labels as you propose here, what is the use-case? Are constant labels actually useful?
  • Should the Labeler take a stats.RPCTagInfo or stats.ConnTagInfo as an argument?
  • If we still used the Labeler approach, could users set the labeler in the context using an interceptor? (Not sure if interceptors run before the stats handler?)

@inigohu
Copy link
Contributor Author

inigohu commented Jun 19, 2024

If we use constant labels as you propose here, what is the use-case? Are constant labels actually useful?

Static attributes could be useful for tagging different servers running on the same service but on different ports, e.g. public, internal, backoffice, legacy...

Should the Labeler take a stats.RPCTagInfo or stats.ConnTagInfo as an argument?

These attributes are already being tagged in the spans/metrics.

If we still used the Labeler approach, could users set the labeler in the context using an interceptor? (Not sure if interceptors run before the stats handler?)

The HandleRPC of the StatsHandler is executed several times, but to summarise this would be the order:

HandleRPC(begin) -> Interceptors(pre) -> handler logic -> Interceptors(post) -> HandleRPC(end).

The problem is that despite modifying the context in an interceptor, the interceptor does not propagate it outwards, so it never reaches the end of the HandleRPC.

@jklipp
Copy link

jklipp commented Jun 28, 2024

Hi, I've ended up here after trying to figure out how to attach custom (static) labels to otelgrpc stats. Just wanted to add our concrete use case if it's helpful to demonstrate the need.

We've got a central cluster that talks to the same service on multiple remote clusters, and I'd like to be able to label and differentiate remote clusters so I can examine their latency metrics separately.

From what I've been able to determine poking around the codebases, attaching a different client handler with a different set of static labels/attributes on it to each of our outbound grpc client connections would let us distinguish between the connections after everything rolls up to the client latency metric.

The only other way I can see to achieve that is to have different MeterProviders with different labels, but then I believe they would conflict on the metric names if I were to try and publish the metrics through the same exporter (the otel prometheus exporter in this case).

@dashpole
Copy link
Contributor

dashpole commented Jul 9, 2024

I think this change is a good idea. @hanyuancheung any thoughts?

Copy link
Contributor

@dashpole dashpole left a comment

Choose a reason for hiding this comment

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

please add a changelog entry

@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch from 65ac7d6 to 4249103 Compare July 10, 2024 14:46
CHANGELOG.md Outdated Show resolved Hide resolved
@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch 4 times, most recently from 985c8c4 to 7d7a5b3 Compare August 2, 2024 21:32
@inigohu inigohu force-pushed the feature/otelgrpc-custom-attr branch from 7d7a5b3 to f622caa Compare August 2, 2024 21:35
@zchee
Copy link
Contributor

zchee commented Aug 22, 2024

@dmathieu @dashpole Sorry to interrupt, but when will this change be released (merged)?

@dmathieu dmathieu merged commit 18eaff6 into open-telemetry:main Aug 22, 2024
32 checks passed
MrAlias added a commit that referenced this pull request Aug 23, 2024
This release is the last to support [Go 1.21]. The next release will
require at least [Go 1.22].

### Added

- Add the `WithSpanAttributes` and `WithMetricAttributes` methods to set
custom attributes to the stats handler in
`go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`.
(#5133)
- The `go.opentelemetry.io/contrib/bridges/otelzap` module. This module
provides an OpenTelemetry logging bridge for `go.uber.org/zap`. (#5191)
- Support for the `OTEL_HTTP_CLIENT_COMPATIBILITY_MODE=http/dup`
environment variable in
`go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` to emit
attributes for both the v1.20.0 and v1.26.0 semantic conventions.
(#5401)
- The `go.opentelemetry.io/contrib/bridges/otelzerolog` module. This
module provides an OpenTelemetry logging bridge for
`github.com/rs/zerolog`. (#5405)
- Add `WithGinFilter` filter parameter in
`go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`
to allow filtering requests with `*gin.Context`. (#5743)
- Support for stdoutlog exporter in
`go.opentelemetry.io/contrib/config`. (#5850)
- Add macOS ARM64 platform to the compatibility testing suite. (#5868)
- Add new runtime metrics to
`go.opentelemetry.io/contrib/instrumentation/runtime`, which are still
disabled by default. (#5870)
- Add the `WithMetricsAttributesFn` option to allow setting dynamic,
per-request metric attributes in
`go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
- The `go.opentelemetry.io/contrib/config` package supports configuring
`with_resource_constant_labels` for the prometheus exporter. (#5890)
- Support [Go 1.23]. (#6017)

### Removed

- The deprecated `go.opentelemetry.io/contrib/processors/baggagecopy`
package is removed. (#5853)

### Fixed

- Race condition when reading the HTTP body and writing the response in
`go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5916)

[Go 1.23]: https://go.dev/doc/go1.23
[Go 1.22]: https://go.dev/doc/go1.22
[Go 1.21]: https://go.dev/doc/go1.21
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.

otelgrpc: Allow adding custom attributes to otelgrpc metrics
6 participants