Class PrometheusNaming

Object
PrometheusNaming

public class PrometheusNaming extends Object
Utility for Prometheus Metric and Label naming.

Note that this library allows dots in metric and label names. Dots will automatically be replaced with underscores in Prometheus exposition formats. However, if metrics are exposed in OpenTelemetry format the dots are retained.

  • Constructor Details

  • Method Details

    • isValidMetricName

      public static boolean isValidMetricName(String name)
      Test if a metric name is valid. Rules:
      • The name must match METRIC_NAME_PATTERN.
      • The name MUST NOT end with one of the RESERVED_METRIC_NAME_SUFFIXES.
      If a metric has a Unit, the metric name SHOULD end with the unit as a suffix. Note that OpenMetrics requires metric names to have their unit as suffix, and we implement this in prometheus-metrics-core. However, prometheus-metrics-model does not enforce Unit suffixes.

      Example: If you create a Counter for a processing time with Unit SECONDS, the name should be processing_time_seconds. When exposed in OpenMetrics Text format, this will be represented as two values: processing_time_seconds_total for the counter value, and the optional processing_time_seconds_created timestamp.

      Use sanitizeMetricName(String) to convert arbitrary Strings to valid metric names.

    • isValidLabelName

      public static boolean isValidLabelName(String name)
    • prometheusName

      public static String prometheusName(String name)
      Get the metric or label name that is used in Prometheus exposition format.
      Parameters:
      name - must be a valid metric or label name, i.e. isValidMetricName(name) or isValidLabelName(name) must be true.
      Returns:
      the name with dots replaced by underscores.
    • sanitizeMetricName

      public static String sanitizeMetricName(String metricName)
      Convert an arbitrary string to a name where isValidMetricName(name) is true.
    • sanitizeLabelName

      public static String sanitizeLabelName(String labelName)
      Convert an arbitrary string to a name where isValidLabelName(name) is true.