Class PrometheusNaming
Note that this library allows dots in metric and label names. Dots will automatically be replaced with underscores in Prometheus exposition formats. However, if metrics are exposed in OpenTelemetry format the dots are retained.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringescapeName(String name, EscapingScheme scheme) Escapes the incoming name according to the provided escaping scheme.static booleanisValidLabelName(String name) static booleanisValidLegacyLabelName(String name) static booleanstatic booleanisValidMetricName(String name) Test if a metric name is valid.static booleanisValidUnitName(String name) Units may not have illegal characters.static booleanneedsEscaping(String name, EscapingScheme scheme) static StringnormalizeMetricName(String metricName) Convert an arbitrary string to a valid metric name without stripping reserved suffixes.static StringnormalizeMetricName(String metricName, Unit unit) LikenormalizeMetricName(String), but also makes sure that the unit is appended as a suffix if the unit is notnull.static StringprometheusName(String name) Get the metric or label name that is used in Prometheus exposition format.static StringsanitizeLabelName(String labelName) Convert an arbitrary string to a name whereisValidLabelName(name)is true.static StringsanitizeMetricName(String metricName) Convert an arbitrary string to a valid metric name.static StringsanitizeMetricName(String metricName, Unit unit) LikesanitizeMetricName(String), but also makes sure that the unit is appended as a suffix if the unit is notnull.static StringsanitizeUnitName(String unitName) Convert an arbitrary string to a valid unit name by replacing illegal characters.static StringvalidateMetricName(String name) Same asisValidMetricName(String), but produces an error message.static StringvalidateUnitName(String name) Same asisValidUnitName(String)but returns an error message.
-
Constructor Details
-
PrometheusNaming
public PrometheusNaming()
-
-
Method Details
-
isValidMetricName
Test if a metric name is valid. Any non-empty valid UTF-8 string is accepted.Collision detection for suffixes like
_total,_info,_bucket, etc. is handled at registration time by thePrometheusRegistry, not by name validation.If a metric has a
Unit, the metric name SHOULD end with the unit as a suffix. Note that OpenMetrics requires metric names to have their unit as suffix, and we implement this inprometheus-metrics-core. However,prometheus-metrics-modeldoes not enforce Unit suffixes.Example: If you create a Counter for a processing time with Unit
SECONDS, the name should beprocessing_time_seconds. When exposed in OpenMetrics Text format, this will be represented as two values:processing_time_seconds_totalfor the counter value, and the optionalprocessing_time_seconds_createdtimestamp.Use
sanitizeMetricName(String)for compatibility-preserving sanitization that strips reserved suffixes, ornormalizeMetricName(String)for permissive normalization that keeps the original suffixes intact. -
validateMetricName
Same asisValidMetricName(String), but produces an error message.The name is valid if the error message is
null. -
isValidLegacyMetricName
-
isValidLabelName
-
isValidLegacyLabelName
-
isValidUnitName
Units may not have illegal characters. -
validateUnitName
Same asisValidUnitName(String)but returns an error message. -
prometheusName
Get the metric or label name that is used in Prometheus exposition format.- Parameters:
name- must be a valid metric or label name, i.e.isValidMetricName(name)orisValidLabelName(name)must be true.- Returns:
- the name with dots replaced by underscores.
-
sanitizeMetricName
Convert an arbitrary string to a valid metric name.Reserved metric name suffixes (
_total,_created,_bucket,_infoand their dot variants) are stripped. These suffixes are appended automatically by Prometheus exposition format writers, so including them in a base metric name would result in double-suffixing or unintended type inference. For example, a JMX attribute namedRequestTotalwould be sanitized fromkafka_consumer_request_totaltokafka_consumer_request, and the counter writer would add_totalback at scrape time.This behaviour was present in client_java 1.5.x and is restored here to fix a regression introduced in 1.6.0 that affected downstream tools (e.g. the JMX Exporter and the simpleclient bridge) which relied on
sanitizeMetricNameto strip these suffixes before passing names to the snapshot builders.If you want permissive normalization that keeps reserved suffixes intact, use
normalizeMetricName(String)instead.- Throws:
IllegalArgumentException- if the input is empty
-
sanitizeMetricName
LikesanitizeMetricName(String), but also makes sure that the unit is appended as a suffix if the unit is notnull. -
normalizeMetricName
Convert an arbitrary string to a valid metric name without stripping reserved suffixes.Any non-empty valid UTF-8 string is accepted and returned unchanged. This is the permissive normalization behavior introduced in 1.6.0. Use this method for new integrations that want to preserve the original metric name and rely on registration-time collision detection instead of suffix stripping.
- Throws:
IllegalArgumentException- if the input is empty
-
normalizeMetricName
LikenormalizeMetricName(String), but also makes sure that the unit is appended as a suffix if the unit is notnull. -
sanitizeLabelName
Convert an arbitrary string to a name whereisValidLabelName(name)is true. -
sanitizeUnitName
Convert an arbitrary string to a valid unit name by replacing illegal characters.- Throws:
IllegalArgumentException- if theunitNamecannot be converted, e.g. if you callsanitizeUnitName("").NullPointerException- ifunitNameis null.
-
escapeName
Escapes the incoming name according to the provided escaping scheme. Depending on the rules of escaping, this may cause no change in the string that is returned (especially NO_ESCAPING, which by definition is a noop). This method does not do any validation of the name. -
needsEscaping
-