Record Class MetricsSanitizer
java.lang.Object
java.lang.Record
com.loomcache.server.metrics.MetricsSanitizer
- Record Components:
maxLabelLength- Configuration
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 Summary
ConstructorsConstructorDescriptionMetricsSanitizer(int maxLabelLength, int maxMetricNameLength) Creates an instance of aMetricsSanitizerrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.booleanisHighCardinalityValue(@Nullable String labelValue) Check if a label value appears to be user-supplied high-cardinality data.intReturns the value of themaxLabelLengthrecord component.intReturns the value of themaxMetricNameLengthrecord component.sanitizeLabelName(String labelName) Sanitize a label name (key).sanitizeLabelValue(@Nullable String labelValue) Sanitize a label value.sanitizeMetricName(String metricName) Sanitize a metric name.io.micrometer.core.instrument.TagssanitizeTags(@Nullable io.micrometer.core.instrument.Tags tags) Sanitize a set of tags (label key-value pairs).toSafeCategory(@Nullable String labelValue) Convert a potentially high-cardinality value to a safe categorical label.final StringtoString()Returns a string representation of this record class.voidvalidateMetric(String metricName, @Nullable io.micrometer.core.instrument.Tags tags) Validate a complete metric and labels combination.
-
Constructor Details
-
MetricsSanitizer
public MetricsSanitizer(int maxLabelLength, int maxMetricNameLength) Creates an instance of aMetricsSanitizerrecord class.- Parameters:
maxLabelLength- the value for themaxLabelLengthrecord componentmaxMetricNameLength- the value for themaxMetricNameLengthrecord component
-
-
Method Details
-
sanitizeMetricName
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
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
-
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
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
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
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
-
hashCode
-
equals
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 thecomparemethod from their corresponding wrapper classes. -
maxLabelLength
public int maxLabelLength()Returns the value of themaxLabelLengthrecord component.- Returns:
- the value of the
maxLabelLengthrecord component
-
maxMetricNameLength
public int maxMetricNameLength()Returns the value of themaxMetricNameLengthrecord component.- Returns:
- the value of the
maxMetricNameLengthrecord component
-