Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTF-8 support in metric and label names #1255

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.prometheus.metrics.config;

import java.util.Map;

public class NamingProperties {

private static final String VALIDATION_SCHEME = "validationScheme";
private final String validationScheme;

private NamingProperties(String validation) {
this.validationScheme = validation;
}

public String getValidationScheme() {
return validationScheme;
}

static NamingProperties load(String prefix, Map<Object, Object> properties) throws PrometheusPropertiesException {
String validationScheme = Util.loadString(prefix + "." + VALIDATION_SCHEME, properties);
return new NamingProperties(validationScheme);
}

public static Builder builder() {
return new Builder();
}

public static class Builder {

private String validationScheme;

private Builder() {}

public Builder validation(String validationScheme) {
this.validationScheme = validationScheme;
return this;
}

public NamingProperties build() {
return new NamingProperties(validationScheme);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PrometheusProperties {
private final ExporterHttpServerProperties exporterHttpServerProperties;
private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties;
private final ExporterPushgatewayProperties exporterPushgatewayProperties;
private final NamingProperties namingProperties;

/**
* Get the properties instance. When called for the first time, {@code get()} loads the properties
Expand All @@ -44,7 +45,8 @@ public PrometheusProperties(
ExporterFilterProperties exporterFilterProperties,
ExporterHttpServerProperties httpServerConfig,
ExporterPushgatewayProperties pushgatewayProperties,
ExporterOpenTelemetryProperties otelConfig) {
ExporterOpenTelemetryProperties otelConfig,
NamingProperties namingProperties) {
this.defaultMetricsProperties = defaultMetricsProperties;
this.metricProperties.putAll(metricProperties);
this.exemplarProperties = exemplarProperties;
Expand All @@ -53,6 +55,7 @@ public PrometheusProperties(
this.exporterHttpServerProperties = httpServerConfig;
this.exporterPushgatewayProperties = pushgatewayProperties;
this.exporterOpenTelemetryProperties = otelConfig;
this.namingProperties = namingProperties;
}

/**
Expand Down Expand Up @@ -95,4 +98,8 @@ public ExporterPushgatewayProperties getExporterPushgatewayProperties() {
public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
return exporterOpenTelemetryProperties;
}

public NamingProperties getNamingProperties() {
return namingProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.rmi.Naming;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -42,6 +43,7 @@ public static PrometheusProperties load(Map<Object, Object> externalProperties)
ExporterPushgatewayProperties.load(properties);
ExporterOpenTelemetryProperties exporterOpenTelemetryProperties =
ExporterOpenTelemetryProperties.load(properties);
NamingProperties namingProperties = NamingProperties.load("io.prometheus.naming", properties);
validateAllPropertiesProcessed(properties);
return new PrometheusProperties(
defaultMetricsProperties,
Expand All @@ -51,7 +53,8 @@ public static PrometheusProperties load(Map<Object, Object> externalProperties)
exporterFilterProperties,
exporterHttpServerProperties,
exporterPushgatewayProperties,
exporterOpenTelemetryProperties);
exporterOpenTelemetryProperties,
namingProperties);
}

// This will remove entries from properties when they are processed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.core.metrics;

import static io.prometheus.metrics.core.metrics.TestUtil.assertExemplarEquals;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.nameEscapingScheme;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.data.Offset.offset;
Expand All @@ -11,12 +12,7 @@
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_29_3.Metrics;
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
import io.prometheus.metrics.model.snapshots.ClassicHistogramBucket;
import io.prometheus.metrics.model.snapshots.Exemplar;
import io.prometheus.metrics.model.snapshots.Exemplars;
import io.prometheus.metrics.model.snapshots.HistogramSnapshot;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.*;
import io.prometheus.metrics.tracer.common.SpanContext;
import io.prometheus.metrics.tracer.initializer.SpanContextSupplier;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -946,6 +942,7 @@ public void testDefaults() throws IOException {
// text
ByteArrayOutputStream out = new ByteArrayOutputStream();
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(false, true);
nameEscapingScheme = EscapingScheme.NO_ESCAPING;
writer.write(out, MetricSnapshots.of(snapshot));
assertThat(out).hasToString(expectedTextFormat);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.prometheus.metrics.core.metrics;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.nameEscapingScheme;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_29_3.Metrics;
import io.prometheus.metrics.expositionformats.internal.PrometheusProtobufWriterImpl;
import io.prometheus.metrics.expositionformats.internal.ProtobufUtil;
import io.prometheus.metrics.model.snapshots.EscapingScheme;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.Unit;
Expand Down Expand Up @@ -121,6 +123,7 @@ public void testConstLabelsDuplicate2() {
private void assertTextFormat(String expected, Info info) throws IOException {
OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(true, true);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
nameEscapingScheme = EscapingScheme.NO_ESCAPING;
writer.write(outputStream, MetricSnapshots.of(info.collect()));
String result = outputStream.toString(StandardCharsets.UTF_8.name());
if (!result.contains(expected)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.prometheus.metrics.expositionformats.ExpositionFormats;
import io.prometheus.metrics.model.registry.MetricNameFilter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.EscapingScheme;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -19,6 +20,8 @@
import java.util.function.Predicate;
import java.util.zip.GZIPOutputStream;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.nameEscapingScheme;

/** Prometheus scrape endpoint. */
public class PrometheusScrapeHandler {

Expand Down Expand Up @@ -54,12 +57,13 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
try {
PrometheusHttpRequest request = exchange.getRequest();
MetricSnapshots snapshots = scrape(request);
String acceptHeader = request.getHeader("Accept");
nameEscapingScheme = EscapingScheme.fromAcceptHeader(acceptHeader);
if (writeDebugResponse(snapshots, exchange)) {
return;
}
ByteArrayOutputStream responseBuffer =
new ByteArrayOutputStream(lastResponseSize.get() + 1024);
String acceptHeader = request.getHeader("Accept");
ExpositionFormatWriter writer = expositionFormats.findWriter(acceptHeader);
writer.write(responseBuffer, snapshots);
lastResponseSize.set(responseBuffer.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.prometheus.metrics.exporter.pushgateway;

import static io.prometheus.metrics.exporter.pushgateway.Scheme.HTTP;
import static io.prometheus.metrics.model.snapshots.PrometheusNaming.*;

import io.prometheus.metrics.config.ExporterPushgatewayProperties;
import io.prometheus.metrics.config.PrometheusProperties;
Expand All @@ -11,6 +12,8 @@
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.EscapingScheme;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -200,6 +203,7 @@ private void doRequest(PrometheusRegistry registry, String method) throws IOExce
try {
if (!method.equals("DELETE")) {
OutputStream outputStream = connection.getOutputStream();
nameEscapingScheme = EscapingScheme.NO_ESCAPING;
writer.write(outputStream, registry.scrape());
outputStream.flush();
outputStream.close();
Expand Down Expand Up @@ -430,11 +434,11 @@ private URL makeUrl(ExporterPushgatewayProperties properties)
if (groupingKey != null) {
for (Map.Entry<String, String> entry : groupingKey.entrySet()) {
if (entry.getValue().isEmpty()) {
url += "/" + entry.getKey() + "@base64/=";
url += "/" + escapeName(entry.getKey(), EscapingScheme.VALUE_ENCODING_ESCAPING) + "@base64/=";
} else if (entry.getValue().contains("/")) {
url += "/" + entry.getKey() + "@base64/" + base64url(entry.getValue());
url += "/" + escapeName(entry.getKey(), EscapingScheme.VALUE_ENCODING_ESCAPING) + "@base64/" + base64url(entry.getValue());
} else {
url += "/" + entry.getKey() + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");
url += "/" + escapeName(entry.getKey(), EscapingScheme.VALUE_ENCODING_ESCAPING) + "/" + URLEncoder.encode(entry.getValue(), "UTF-8");
}
}
}
Expand Down
Loading