feat: add MetricMetadata.Builder, deprecate wide constructors#2202
Open
zeitlinger wants to merge 3 commits into
Open
feat: add MetricMetadata.Builder, deprecate wide constructors#2202zeitlinger wants to merge 3 commits into
zeitlinger wants to merge 3 commits into
Conversation
The 4-arg and 5-arg constructors require callers to pre-compute expositionBaseName and originalName, leaking internal naming logic into every caller site. The new builder encapsulates that logic: - name(): base name (unit appended if absent) - unit(): appended to name when not already present - counterSuffix(true): sets expositionBaseName to name + "_total", so the exposition writer knows to preserve the suffix rather than double-append it (relevant for UTF-8 metric names) Existing callers (MetricMetadataSupport, MetricMetadata.escape) keep the deprecated constructors with @SuppressWarnings("deprecation"). Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
jaydeluca
reviewed
Jun 8, 2026
|
|
||
| /** Builder for {@link MetricMetadata}. */ | ||
| @StableApi | ||
| public static final class Builder { |
Collaborator
There was a problem hiding this comment.
will this solve the use cases in this repo that we now have the suppressions on? i'm not sure it allows us to actually fully deprecate the constructors as-is.
For example, for MetricWithFixedMetadata i think we'd need:
this.metadata = MetricMetadata.builder()
.name(name)
.expositionBaseName(expositionBaseName)
.originalName(originalName)
.help(builder.help)
.unit(builder.unit)
.build();It might be worth trying to migrate any of the ones where we suppress the deprecation warning, just to see if it's possible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MetricMetadata.builder()withname,help,unit,counterSuffixfields_totaltoexpositionBaseNamewhencounterSuffix=trueMetricMetadataSupport,MetricMetadata.escape) suppress the warningdocs/apidiffs/current_vs_latest/prometheus-metrics-model.txtMotivation
The OTel exporter (opentelemetry/opentelemetry-java#8346) needs to express per-strategy counter intent without pre-computing
expositionBaseNamemanually. The builder encapsulates that logic and provides a cleaner public API for any downstream adapter that constructsMetricMetadatadirectly.Test plan
MetricMetadataTest— 8 new builder tests covering: no unit, unit absent/present, counter suffix, counter + unit, UTF-8 name, non-counter, name-required validation@SuppressWarnings("deprecation")