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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]According to OpenMetrics_countand_sum(and_gcount,_gsum) should also be reserved metric name suffixes. -
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) (package private) static booleanisValidLegacyChar(int c, int i) 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'.private static booleanisValidUtf8(String name) private static booleanisValidUtf8Char(int c) static booleanneedsEscaping(String name, EscapingScheme scheme) static StringprometheusName(String name) Get the metric or label name that is used in Prometheus exposition format.private static StringReturns a string with only valid unit name characters [a-zA-Z0-9_.:].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.static StringvalidateMetricName(String name) Same asisValidMetricName(String), but produces an error message.static StringvalidateUnitName(String name) Same asisValidUnitName(String)but returns an error message.
-
Field Details
-
RESERVED_METRIC_NAME_SUFFIXES
According to OpenMetrics_countand_sum(and_gcount,_gsum) should also be reserved metric name suffixes. However, popular instrumentation libraries have Gauges with names ending in_count. Examples:- Micrometer:
jvm_buffer_count - OpenTelemetry:
process_runtime_jvm_buffer_count
We do not treat
_countand_sumas reserved suffixes here for compatibility with these libraries. However, there is a risk of name conflict if someone creates a gauge namedmy_data_countand a histogram or summary namedmy_data, because the histogram or summary will implicitly have a sample namedmy_data_count. - Micrometer:
-
-
Constructor Details
-
PrometheusNaming
public PrometheusNaming()
-
-
Method Details
-
isValidMetricName
Test if a metric name is valid. Rules:- The name must match Metric names.
- The name MUST NOT end with one of the
RESERVED_METRIC_NAME_SUFFIXES.
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)to convert arbitrary Strings to valid metric names. -
validateMetricName
Same asisValidMetricName(String), but produces an error message.The name is valid if the error message is
null. -
isValidLegacyMetricName
-
isValidLabelName
-
isValidUtf8
-
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.
-
replaceIllegalCharsInUnitName
-
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
-
isValidLegacyChar
static boolean isValidLegacyChar(int c, int i) -
isValidUtf8Char
private static boolean isValidUtf8Char(int c)
-