Class Labels

java.lang.Object
codes.grow.packages.loki.Labels

public final class Labels extends Object
Label helpers: validation, normalization and Prometheus label-set rendering.

Loki label names follow the Prometheus rules ([a-zA-Z_][a-zA-Z0-9_]*); names starting with __ are reserved.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final List<String>
    Keys the cardinality guard lets become index labels by default (coordinator decision 3).
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Renders an attribute value as a structured metadata string: strings as is, numbers and booleans as their text, lists and maps as JSON.
    static String
    Renders labels in Prometheus label-set syntax with sorted names and Go-quoted values: {a="b", c="d"} (the form protobuf push and listStreams() use).
    static boolean
    Returns whether name matches [a-zA-Z_][a-zA-Z0-9_]* and does not start with __.
    static String
    Normalizes an attribute key the way Loki does for OTLP attributes: every character outside [a-zA-Z0-9_] becomes _ (service.name → service_name) and a leading digit gets a _ prefix.
    static void
    validateLabel(String name, String value)
    Checks one index label: valid, non-reserved name and a non-empty value.
    static void
    Checks a label name.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_ALLOWED_LABELS

      public static final List<String> DEFAULT_ALLOWED_LABELS
      Keys the cardinality guard lets become index labels by default (coordinator decision 3). Every other attribute or resource key goes to structured metadata.
  • Method Details

    • isValidLabelName

      public static boolean isValidLabelName(String name)
      Returns whether name matches [a-zA-Z_][a-zA-Z0-9_]* and does not start with __.
      Parameters:
      name - label name
      Returns:
      true when Loki accepts it as an index label name
    • validateLabel

      public static void validateLabel(String name, String value)
      Checks one index label: valid, non-reserved name and a non-empty value.
      Parameters:
      name - label name
      value - label value
      Throws:
      ValidationException - when the label is invalid
    • validateLabelName

      public static void validateLabelName(String name)
      Checks a label name.
      Parameters:
      name - label name
      Throws:
      ValidationException - unless the name is valid and not reserved
    • normalizeLabelName

      public static String normalizeLabelName(String key)
      Normalizes an attribute key the way Loki does for OTLP attributes: every character outside [a-zA-Z0-9_] becomes _ (service.name → service_name) and a leading digit gets a _ prefix. An empty key stays empty.
      Parameters:
      key - attribute key
      Returns:
      the label / structured metadata name
    • formatLabelSet

      public static String formatLabelSet(Map<String,String> labels)
      Renders labels in Prometheus label-set syntax with sorted names and Go-quoted values: {a="b", c="d"} (the form protobuf push and listStreams() use).
      Parameters:
      labels - labels
      Returns:
      the label-set string
    • attrToString

      public static String attrToString(Object value)
      Renders an attribute value as a structured metadata string: strings as is, numbers and booleans as their text, lists and maps as JSON.
      Parameters:
      value - attribute value
      Returns:
      the string form ("" for null)