Class Labels

java.lang.Object
io.prometheus.metrics.model.snapshots.Labels
All Implemented Interfaces:
Comparable<Labels>, Iterable<Label>

public final class Labels extends Object implements Comparable<Labels>, Iterable<Label>
Immutable set of name/value pairs, sorted by name.
  • Field Details

    • EMPTY

      public static final Labels EMPTY
    • prometheusNames

      private final String[] prometheusNames
    • names

      private final String[] names
    • values

      private final String[] values
  • Constructor Details

  • Method Details

    • isEmpty

      public boolean isEmpty()
    • of

      public static Labels of(String... keyValuePairs)
      Create a new Labels instance. You can either create Labels with one of the static Labels.of(...) methods, or you can use the builder().
      Parameters:
      keyValuePairs - as in {name1, value1, name2, value2}. Length must be even. PrometheusNaming.isValidLabelName(String) must be true for each name. Use PrometheusNaming.sanitizeLabelName(String) to convert arbitrary strings to valid label names. Label names must be unique (no duplicate label names).
    • of

      public static Labels of(List<String> names, List<String> values)
      Create a new Labels instance. You can either create Labels with one of the static Labels.of(...) methods, or you can use the builder().
      Parameters:
      names - label names. PrometheusNaming.isValidLabelName(String) must be true for each name. Use PrometheusNaming.sanitizeLabelName(String) to convert arbitrary strings to valid label names. Label names must be unique (no duplicate label names).
      values - label values. names.size() must be equal to values.size().
    • of

      public static Labels of(String[] names, String[] values)
      Create a new Labels instance. You can either create Labels with one of the static Labels.of(...) methods, or you can use the builder().
      Parameters:
      names - label names. PrometheusNaming.isValidLabelName(String) must be true for each name. Use PrometheusNaming.sanitizeLabelName(String) to convert arbitrary strings to valid label names. Label names must be unique (no duplicate label names).
      values - label values. names.length must be equal to values.length.
    • makePrometheusNames

      static String[] makePrometheusNames(String[] names)
    • contains

      public boolean contains(String labelName)
      Test if these labels contain a specific label name.

      Dots are treated as underscores, so contains("my.label") and contains("my_label") are the same.

    • get

      public String get(String labelName)
      Get the label value for a given label name.

      Returns null if the labelName is not found.

      Dots are treated as underscores, so get("my.label") and get("my_label") are the same.

    • sortAndValidate

      private static void sortAndValidate(String[] names, String[] prometheusNames, String[] values)
    • validateNames

      private static void validateNames(String[] names, String[] prometheusNames)
    • sort

      private static void sort(String[] names, String[] prometheusNames, String[] values)
    • swap

      private static void swap(int i, int j, String[] names, String[] prometheusNames, String[] values)
    • iterator

      public Iterator<Label> iterator()
      Specified by:
      iterator in interface Iterable<Label>
    • stream

      public Stream<Label> stream()
    • size

      public int size()
    • getName

      public String getName(int i)
    • getPrometheusName

      public String getPrometheusName(int i)
      Like getName(int), but dots are replaced with underscores.

      This is used by Prometheus exposition formats.

    • getValue

      public String getValue(int i)
    • merge

      public Labels merge(Labels other)
      Create a new Labels instance containing the labels of this and the labels of other. This and other must not contain the same label name.
    • merge

      public Labels merge(String[] names, String[] values)
      Create a new Labels instance containing the labels of this and the labels passed as names and values. The new label names must not already be contained in this Labels instance.
    • add

      public Labels add(String name, String value)
      Create a new Labels instance containing the labels of this and the label passed as name and value. The label name must not already be contained in this Labels instance.
    • hasSameNames

      public boolean hasSameNames(Labels other)
    • hasSameValues

      public boolean hasSameValues(Labels other)
    • compareTo

      public int compareTo(Labels other)
      Specified by:
      compareTo in interface Comparable<Labels>
    • compare

      private int compare(String[] array1, String[] array2)
    • asList

      private List<Label> asList()
    • toString

      public String toString()
      This must not be used in Prometheus exposition formats because names may contain dots.

      However, for debugging it's better to show the original names rather than the Prometheus names.

      Overrides:
      toString in class Object
    • appendEscapedLabelValue

      private void appendEscapedLabelValue(StringBuilder b, String value)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • builder

      public static Labels.Builder builder()