Object
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

  • 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.
    • 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.

    • 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>
    • 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
    • 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()