Class PrometheusNaming
Object
PrometheusNaming
Utility for Prometheus Metric and Label naming.
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
isValidLabelName
(String name) static boolean
isValidMetricName
(String name) Test if a metric name is valid.static boolean
isValidUnitName
(String name) Units may not have illegal characters, and they may not end with a reserved suffix like 'total'.static String
prometheusName
(String name) Get the metric or label name that is used in Prometheus exposition format.static String
sanitizeLabelName
(String labelName) Convert an arbitrary string to a name whereisValidLabelName(name)
is true.static String
sanitizeMetricName
(String metricName) Convert an arbitrary string to a name whereisValidMetricName(name)
is true.static String
sanitizeMetricName
(String metricName, Unit unit) LikesanitizeMetricName(String)
, but also makes sure that the unit is appended as a suffix if the unit is notnull
.static String
sanitizeUnitName
(String unitName) Convert an arbitrary string to a name whereisValidUnitName(name)
is true.static String
validateMetricName
(String name) Same asisValidMetricName(String)
, but produces an error message.static String
validateUnitName
(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. Rules:- The name must match
METRIC_NAME_PATTERN
. - The name MUST NOT end with one of the
RESERVED_METRIC_NAME_SUFFIXES
.
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-model
does 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_total
for the counter value, and the optionalprocessing_time_seconds_created
timestamp.Use
sanitizeMetricName(String)
to convert arbitrary Strings to valid metric names. - The name must match
-
validateMetricName
Same asisValidMetricName(String)
, but produces an error message.The name is valid if the error message is
null
. -
isValidLabelName
-
isValidUnitName
Units may not have illegal characters, and they may not end with a reserved suffix like 'total'. -
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 name whereisValidMetricName(name)
is true. -
sanitizeMetricName
LikesanitizeMetricName(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 name whereisValidUnitName(name)
is true.- Throws:
IllegalArgumentException
- if theunitName
cannot be converted, for example if you callsanitizeUnitName("total")
orsanitizeUnitName("")
.NullPointerException
- ifunitName
is null.
-