Skip to content

http: add support for filtering /metrics by component label #3881

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

irateswami
Copy link

PR Description

Adds support for filtering metrics by component label via query parameters
on the /metrics endpoint.

This allows filtering the Prometheus exposition output by supplying one or
more component values via the ?component= query parameter.

Example:
GET /metrics?component=agent&component=otelcol

Only metrics that contain a component label matching one of the supplied
values will be returned.

Which issue(s) this PR fixes

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
@irateswami irateswami requested review from clayton-cornell and a team as code owners June 22, 2025 01:59
@CLAassistant
Copy link

CLAassistant commented Jun 22, 2025

CLA assistant check
All committers have signed the CLA.

@irateswami
Copy link
Author

My first PR on alloy, thanks for your time everybody! That was fun

@clayton-cornell clayton-cornell requested a review from a team June 24, 2025 17:53
@irateswami
Copy link
Author

@clayton-cornell I appreciate the feedback! I'll get those doc changes added.

@irateswami irateswami force-pushed the metrics-component-filter branch from 647ba79 to fc00b8a Compare June 24, 2025 20:52
Copy link
Contributor

@clayton-cornell clayton-cornell left a comment

Choose a reason for hiding this comment

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

Docs look good. I'll leave it now to the dev team to review the code.

@clayton-cornell clayton-cornell added the type/docs Docs Squad label across all Grafana Labs repos label Jun 24, 2025
@irateswami irateswami force-pushed the metrics-component-filter branch from 2d2760f to aff853e Compare June 25, 2025 15:48
This change wraps the Prometheus metrics handler with logic to parse
`component` query parameters and filter the resulting metrics output to
only include MetricFamilies that have at least one metric with a matching
`component` label.

Example:
  `GET /metrics?component=a&component=B`

The filtering is optional and backward-compatible:
- If no query param is supplied, all metrics are returned.
- Multiple component values are supported (?component=a&component=b).
- Unknown component values yield an empty response.

Includes:
- `filteredGatherer` implementation to filter prometheus.Gatherer results
- Updated HTTP handler in http.go
- Tests for expected behavior and edge cases

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
@irateswami irateswami force-pushed the metrics-component-filter branch from 77119d8 to a4d8576 Compare June 27, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/docs Docs Squad label across all Grafana Labs repos
3 participants