Class MetricWithFixedMetadata

Object
Metric
MetricWithFixedMetadata
All Implemented Interfaces:
Collector
Direct Known Subclasses:
CounterWithCallback, GaugeWithCallback, Info, StatefulMetric, SummaryWithCallback

public abstract class MetricWithFixedMetadata extends Metric
Almost all metrics have fixed metadata, i.e. the metric name is known when the metric is created.

An exception would be a metric that is a bridge to a 3rd party metric library, where the metric name has to be retrieved from the 3rd party metric library at scrape time.

  • Method Details

    • getMetadata

      Description copied from interface: Collector
      Returns the metric metadata (name, help, unit) for registration-time validation.

      When non-null, the registry uses this to validate that metrics with the same name have consistent help and unit. Returning null means help/unit validation is skipped for this collector.

      Returns:
      the metric metadata, or null to skip help/unit validation
    • getPrometheusName

      Description copied from interface: Collector
      This is called in two places:
      1. During registration to check if a metric with that name already exists.
      2. During scrape to check if this collector can be skipped because a name filter is present and the metric name is excluded.

      Returning null means checks are omitted (registration the metric always succeeds), and the collector is always scraped (the result is dropped after scraping if a name filter is present and the metric name is excluded).

      If your metric has a name that does not change at runtime it is a good idea to overwrite this and return the name.

      All metrics in prometheus-metrics-core override this.

    • getLabelNames

      Description copied from interface: Collector
      Returns the complete set of label names for this metric.

      This includes both dynamic label names (specified in labelNames()) and constant label names (specified in constLabels()). Label names are normalized using Prometheus naming conventions.

      This is used for registration-time validation to prevent duplicate label schemas for the same metric name. Two collectors with the same name and type can coexist if they have different label name sets.

      Returning null is treated as an empty label set: the registry normalizes it to Collections.emptySet() and performs full label-schema validation and duplicate detection. Two collectors with the same name, type, and null (or empty) label names are considered duplicate and registration of the second will fail.

      Returns:
      the set of all label names, or null (treated as empty) for a metric with no labels