Record Class MetricsSanitizer

java.lang.Object
java.lang.Record
com.loomcache.server.metrics.MetricsSanitizer
Record Components:
maxLabelLength - Configuration

public record MetricsSanitizer(int maxLabelLength, int maxMetricNameLength) extends Record
Sanitizes metric names and label values to prevent cardinality explosion.

Responsibilities: - Validate metric names and labels against naming conventions - Truncate overly long labels to a maximum length - Replace invalid characters in labels - Normalize label names (lowercase, underscore-separated) - Prevent user-supplied data from becoming unbounded labels

Thread-safe implementation using immutable return values.

  • Constructor Details

    • MetricsSanitizer

      public MetricsSanitizer(int maxLabelLength, int maxMetricNameLength)
      Creates an instance of a MetricsSanitizer record class.
      Parameters:
      maxLabelLength - the value for the maxLabelLength record component
      maxMetricNameLength - the value for the maxMetricNameLength record component
  • Method Details

    • sanitizeMetricName

      public String sanitizeMetricName(String metricName)
      Sanitize a metric name. Validates against conventions and truncates if necessary.
      Parameters:
      metricName - the metric name to sanitize (must not be null or empty)
      Returns:
      the sanitized metric name
      Throws:
      IllegalArgumentException - if the metric name is null, empty, or invalid after sanitization
    • sanitizeLabelName

      public String sanitizeLabelName(String labelName)
      Sanitize a label name (key). Converts to lowercase, replaces invalid chars with underscores.
      Parameters:
      labelName - the label name to sanitize (must not be null or empty)
      Returns:
      the sanitized label name
      Throws:
      IllegalArgumentException - if labelName is null or empty
    • sanitizeLabelValue

      public String sanitizeLabelValue(@Nullable String labelValue)
      Sanitize a label value. Truncates if necessary, removes invalid characters.
      Parameters:
      labelValue - the label value to sanitize (may be null)
      Returns:
      the sanitized label value
    • sanitizeTags

      public io.micrometer.core.instrument.Tags sanitizeTags(@Nullable io.micrometer.core.instrument.Tags tags)
      Sanitize a set of tags (label key-value pairs). Returns a new Tags object with sanitized keys and values.
      Parameters:
      tags - the tags to sanitize (may be null or empty)
      Returns:
      sanitized Tags object
    • isHighCardinalityValue

      public boolean isHighCardinalityValue(@Nullable String labelValue)
      Check if a label value appears to be user-supplied high-cardinality data. Heuristics: - Very long values (> threshold) - UUID-like patterns - Hexadecimal strings - High entropy strings
      Parameters:
      labelValue - the label value to check (may be null)
      Returns:
      true if this looks like potentially high-cardinality data
    • toSafeCategory

      public String toSafeCategory(@Nullable String labelValue)
      Convert a potentially high-cardinality value to a safe categorical label. Examples: - UUID -> "uuid" - Long hex string -> "hex" - Random high-entropy string -> "unknown"
      Parameters:
      labelValue - the label value (may be null)
      Returns:
      a safe categorical representation
    • validateMetric

      public void validateMetric(String metricName, @Nullable io.micrometer.core.instrument.Tags tags)
      Validate a complete metric and labels combination. Useful for early validation before registration.
      Parameters:
      metricName - the metric name (must not be null)
      tags - the labels (may be null)
      Throws:
      IllegalArgumentException - if validation fails
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • maxLabelLength

      public int maxLabelLength()
      Returns the value of the maxLabelLength record component.
      Returns:
      the value of the maxLabelLength record component
    • maxMetricNameLength

      public int maxMetricNameLength()
      Returns the value of the maxMetricNameLength record component.
      Returns:
      the value of the maxMetricNameLength record component