Skip to content

feat: add MetricMetadata.Builder, deprecate wide constructors#2202

Open
zeitlinger wants to merge 3 commits into
mainfrom
feat/metric-metadata-builder
Open

feat: add MetricMetadata.Builder, deprecate wide constructors#2202
zeitlinger wants to merge 3 commits into
mainfrom
feat/metric-metadata-builder

Conversation

@zeitlinger

Copy link
Copy Markdown
Member

Summary

  • Adds MetricMetadata.builder() with name, help, unit, counterSuffix fields
  • Builder appends unit to the base name when absent, and appends _total to expositionBaseName when counterSuffix=true
  • Deprecates the 4-arg and 5-arg constructors; internal callers (MetricMetadataSupport, MetricMetadata.escape) suppress the warning
  • Updates docs/apidiffs/current_vs_latest/prometheus-metrics-model.txt

Motivation

The OTel exporter (opentelemetry/opentelemetry-java#8346) needs to express per-strategy counter intent without pre-computing expositionBaseName manually. The builder encapsulates that logic and provides a cleaner public API for any downstream adapter that constructs MetricMetadata directly.

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
  • Existing 4-arg/5-arg constructor tests annotated with @SuppressWarnings("deprecation")

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>
@zeitlinger zeitlinger marked this pull request as ready for review June 8, 2026 09:31

/** Builder for {@link MetricMetadata}. */
@StableApi
public static final class Builder {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

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