Skip to content

add WithLabelsFromContext prometheus interceptor option #758

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

Conversation

benjibuiltit
Copy link
Contributor

Changes

This pull request introduces support for extracting and using dynamic labels from Go context / gRPC metadata in Prometheus metrics. The changes include updates to the server metrics configuration, enhancements to label extraction and handling, and the addition of comprehensive tests for the new functionality.

Enhancements to Prometheus Metrics:

  • Added support for dynamic labels extracted from request context / gRPC metadata by introducing the WithContextLabels and WithLabelsFromContext options in the providers/prometheus/options.go file. These allow configuring which labels will later be extracted dynamically, as well as a function to actually extract them.
  • Updated ServerMetrics to include contextLabelNames for storing dynamic label names and modified metric initialization to include these labels. This ensures that all metrics (e.g., counters, histograms) are pre-registered with the appropriate label names.
  • Enhanced the reporter logic to append dynamic labels to metrics during runtime, ensuring that the extracted labels are applied to counters, histograms, and other metric types.

Integration with Example Server:

  • Updated the example server (examples/server/main.go) to demonstrate the use of dynamic labels. This includes extracting the tenant_name label from metadata and configuring the server metrics to use it.

Verification

  • Added new test cases in providers/prometheus/context_labels_test.go to validate the functionality of dynamic labels. These tests cover scenarios such as missing labels, label extraction, and integration with histograms.
  • Tested manually by running an existing gRPC server, and using go mod replace to pull in the gRPC middleware prometheus package with my local changes. I then fired off requests with metadata to the server with a WithLabelsFromContext function configured to append that metadata to the metrics. Finally, I queried the prometheus metrics endpoint to view the raw metric values and confirmed that labels from context were being properly applied.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

I'd usually insist on an issue first, but this is well reasoned, with concrete use cases and even added examples. Thank you!

@johanbrandhorst
Copy link
Collaborator

I'd usually insist on an issue first, but this is well reasoned, with concrete use cases and even added examples. Thank you!

Ha, just realized there is an issue already, looks like you forgot to reference it? #445

@benjibuiltit
Copy link
Contributor Author

Ha, just realized there is an issue already, looks like you forgot to reference it? #445

Yep, apologies I did not realize linking was part of the process.

@benjibuiltit
Copy link
Contributor Author

@johanbrandhorst I managed to remove the toolchain directive, while still go mod tidy'ing to satisfy the linter. The patch version was still set in the go.mod file. I'm not sure what's needed to avoid this, so if this is still a blocker I'll take any guidance you have to offer.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

The go field update is fine :)

@johanbrandhorst johanbrandhorst merged commit 5304066 into grpc-ecosystem:main Jun 12, 2025
5 checks passed
@johanbrandhorst
Copy link
Collaborator

Thank you for your contribution!

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.

2 participants