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

Allow Prometheus exporter to add resource attributes to metric attributes #6179

Merged

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -55,8 +56,12 @@ public static PrometheusHttpServerBuilder builder() {
int port,
@Nullable ExecutorService executor,
PrometheusRegistry prometheusRegistry,
boolean otelScopeEnabled) {
this.prometheusMetricReader = new PrometheusMetricReader(otelScopeEnabled);
boolean otelScopeEnabled,
boolean addResourceAttributesAsLabels,
Predicate<String> allowedResourceAttributesFilter) {
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
this.prometheusMetricReader =
new PrometheusMetricReader(
otelScopeEnabled, addResourceAttributesAsLabels, allowedResourceAttributesFilter);
this.host = host;
this.prometheusRegistry = prometheusRegistry;
prometheusRegistry.register(prometheusMetricReader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.prometheus.metrics.model.registry.PrometheusRegistry;
import java.util.concurrent.ExecutorService;
import java.util.function.Predicate;
import javax.annotation.Nullable;

/** A builder for {@link PrometheusHttpServer}. */
Expand All @@ -22,7 +23,8 @@ public final class PrometheusHttpServerBuilder {
private int port = DEFAULT_PORT;
private PrometheusRegistry prometheusRegistry = new PrometheusRegistry();
private boolean otelScopeEnabled = true;

private boolean addResourceAttributesAsLabels = false;
private Predicate<String> allowedResourceAttributesFilter = attributeKey -> true;
@Nullable private ExecutorService executor;

/** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
Expand Down Expand Up @@ -60,12 +62,36 @@ public PrometheusHttpServerBuilder setOtelScopeEnabled(boolean otelScopeEnabled)
return this;
}

/**
* Set if the resource attributes should be added as labels on each exported metric.
*
* <p>If set, resource attributes will be added as labels on each exported metric if their key
* tests positive (true) when passed through {@code resourceAttributesFilter}.
*
* @param resourceAttributesFilter a predicate that returns true if the resource attribute should
* be added as a label on each exported metric. The predicates input is the resource attribute
* key.
*/
public PrometheusHttpServerBuilder setAllowedResourceAttributesFilter(
Predicate<String> resourceAttributesFilter) {
this.allowedResourceAttributesFilter = requireNonNull(resourceAttributesFilter);
this.addResourceAttributesAsLabels = true;
return this;
}

/**
* Returns a new {@link PrometheusHttpServer} with the configuration of this builder which can be
* registered with a {@link io.opentelemetry.sdk.metrics.SdkMeterProvider}.
*/
public PrometheusHttpServer build() {
return new PrometheusHttpServer(host, port, executor, prometheusRegistry, otelScopeEnabled);
return new PrometheusHttpServer(
host,
port,
executor,
prometheusRegistry,
otelScopeEnabled,
addResourceAttributesAsLabels,
allowedResourceAttributesFilter);
}

PrometheusHttpServerBuilder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.util.function.Predicate;

/**
* This is the bridge between Prometheus and OpenTelemetry.
Expand All @@ -26,9 +27,14 @@ public class PrometheusMetricReader implements MetricReader, MultiCollector {
private volatile CollectionRegistration collectionRegistration = CollectionRegistration.noop();
private final Otel2PrometheusConverter converter;

/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean)}. */
public PrometheusMetricReader(boolean otelScopeEnabled) {
this.converter = new Otel2PrometheusConverter(otelScopeEnabled);
/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean, boolean, Predicate)}. */
public PrometheusMetricReader(
boolean otelScopeEnabled,
boolean addResourceAttributesAsLabels,
Predicate<String> allowedResourceAttributesFilter) {
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
this.converter =
new Otel2PrometheusConverter(
otelScopeEnabled, addResourceAttributesAsLabels, allowedResourceAttributesFilter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MetricReader createMetricReader(ConfigProperties config) {
if (host != null) {
prometheusBuilder.setHost(host);
}

return prometheusBuilder.build();
}

Expand Down