Skip to content

grpc/stats: Add support for custom labels in per call metrics (A108)#9008

Merged
mbissa merged 4 commits into
grpc:masterfrom
mbissa:a108-custom-label-changes
Mar 27, 2026
Merged

grpc/stats: Add support for custom labels in per call metrics (A108)#9008
mbissa merged 4 commits into
grpc:masterfrom
mbissa:a108-custom-label-changes

Conversation

@mbissa

@mbissa mbissa commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Related Issues/gRFC : A108.
It introduces a mechanism for application authors to inject a custom, request-scoped string label (grpc.client.call.custom) into their RPCs via context.Context, which is then extracted and propagated directly to client-side per-call metrics.

Added grpc.NewContextWithCustomLabel(ctx, label) and grpc.CustomLabelFromContext(ctx) to provide a type-safe, canonical way for clients to attach the label to metrics associated with their outgoing RPCs.

RELEASE NOTES:

  • grpc/stats : Add an API (grpc.NewContextWithCustomLabel(ctx, label) and grpc.CustomLabelFromContext(ctx)) to allow clients to attach grpc.client.call.custom label with specified value to per-call metrics as part of implementing A108.

@mbissa mbissa added this to the 1.81 Release milestone Mar 24, 2026
@mbissa mbissa added Type: Feature New features or improvements in behavior Area: Observability Includes Stats, Tracing, Channelz, Healthz, Binlog, Reflection, Admin, GCP Observability labels Mar 24, 2026
@codecov

codecov Bot commented Mar 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.96%. Comparing base (52bf4d9) to head (3073bca).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9008      +/-   ##
==========================================
- Coverage   83.05%   82.96%   -0.10%     
==========================================
  Files         411      411              
  Lines       32918    32960      +42     
==========================================
+ Hits        27340    27345       +5     
- Misses       4184     4209      +25     
- Partials     1394     1406      +12     
Files with missing lines Coverage Δ
balancer/rls/balancer.go 86.55% <ø> (ø)
balancer/rls/picker.go 92.59% <100.00%> (+0.13%) ⬆️
experimental/stats/metrics.go 52.94% <100.00%> (+19.60%) ⬆️
stats/opentelemetry/client_metrics.go 88.96% <100.00%> (+0.84%) ⬆️

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mbissa

mbissa commented Mar 24, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new API, grpc.NewContextWithCustomLabel, allowing users to attach a custom string label to a gRPC call's context. This custom label is then propagated and recorded as an optional attribute in OpenTelemetry client-side metrics and RLS load balancer metrics, enabling more granular metric analysis. Feedback includes addressing an inconsistency in optional label handling within client_metrics.go to ensure consistent metric attributes, and an optimization to fetch the custom label once in balancer/rls/picker.go.

Comment thread stats/opentelemetry/client_metrics.go
Comment thread balancer/rls/picker.go Outdated
@mbissa mbissa requested a review from easwars March 24, 2026 18:32

@easwars easwars left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Which of the existing tests verifies that the label is attached to all existing per-call metrics?

Comment thread rpc_util.go Outdated
Comment thread rpc_util.go Outdated
Comment thread rpc_util.go Outdated
Comment thread balancer/rls/picker.go
@easwars easwars assigned mbissa and unassigned easwars Mar 24, 2026
@mbissa

mbissa commented Mar 25, 2026

Copy link
Copy Markdown
Contributor Author

Which of the existing tests verifies that the label is attached to all existing per-call metrics?

There is no common test for verifying all labels for all metrics, instead there are tests for metrics that verify labels as well, I have added the check for custom labels there.

@mbissa mbissa assigned easwars and unassigned mbissa Mar 25, 2026
}
for _, o := range h.options.MetricsOptions.OptionalLabels {
if o == "grpc.client.call.custom" {
label := estats.CustomLabelFromContext(ctx)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we check for label != "" down below but not here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is correct, we should not be checking there as well. The fact that o == "grpc.client.call.custom" is true means the user has explicitly asked for this metric label to be present and even if the value is empty we should pass it through (considering the user has a use case when they want this value to be empty).

Also, in general in otel plugin also, if we have a optional label defined, we need to pass a value - even if empty string else it fails.

I have corrected it down below. Thanks for catching this!

@easwars easwars assigned mbissa and unassigned easwars Mar 27, 2026
@mbissa mbissa merged commit b71c262 into grpc:master Mar 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Observability Includes Stats, Tracing, Channelz, Healthz, Binlog, Reflection, Admin, GCP Observability Type: Feature New features or improvements in behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants