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
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, and they may not end with a reserved suffix like 'total'.static booleanneedsEscaping(String name, EscapingScheme scheme) 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 name whereisValidMetricName(name)is true.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 name wherevalidateUnitName(String)isnull(i.e. the name is valid).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. 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-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)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. -
isValidLegacyMetricName
-
isValidLabelName
-
isValidLegacyLabelName
-
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 wherevalidateUnitName(String)isnull(i.e. the name is valid).- Throws:
IllegalArgumentException- if theunitNamecannot be converted, for example if you callsanitizeUnitName("total")orsanitizeUnitName("").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
-