Skip to content

Commit a5d2065

Browse files
authored
Allow Prometheus exporter to add resource attributes to metric attributes (#6179)
1 parent 988dcca commit a5d2065

File tree

10 files changed

+563
-103
lines changed

10 files changed

+563
-103
lines changed

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java

Lines changed: 140 additions & 51 deletions
Large diffs are not rendered by default.

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.InetSocketAddress;
2424
import java.util.concurrent.ExecutorService;
2525
import java.util.concurrent.TimeUnit;
26+
import java.util.function.Predicate;
2627
import javax.annotation.Nullable;
2728

2829
/**
@@ -55,8 +56,10 @@ public static PrometheusHttpServerBuilder builder() {
5556
int port,
5657
@Nullable ExecutorService executor,
5758
PrometheusRegistry prometheusRegistry,
58-
boolean otelScopeEnabled) {
59-
this.prometheusMetricReader = new PrometheusMetricReader(otelScopeEnabled);
59+
boolean otelScopeEnabled,
60+
@Nullable Predicate<String> allowedResourceAttributesFilter) {
61+
this.prometheusMetricReader =
62+
new PrometheusMetricReader(otelScopeEnabled, allowedResourceAttributesFilter);
6063
this.host = host;
6164
this.prometheusRegistry = prometheusRegistry;
6265
prometheusRegistry.register(prometheusMetricReader);

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerBuilder.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import io.prometheus.metrics.model.registry.PrometheusRegistry;
1212
import java.util.concurrent.ExecutorService;
13+
import java.util.function.Predicate;
1314
import javax.annotation.Nullable;
1415

1516
/** A builder for {@link PrometheusHttpServer}. */
@@ -22,7 +23,7 @@ public final class PrometheusHttpServerBuilder {
2223
private int port = DEFAULT_PORT;
2324
private PrometheusRegistry prometheusRegistry = new PrometheusRegistry();
2425
private boolean otelScopeEnabled = true;
25-
26+
@Nullable private Predicate<String> allowedResourceAttributesFilter;
2627
@Nullable private ExecutorService executor;
2728

2829
/** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
@@ -60,12 +61,34 @@ public PrometheusHttpServerBuilder setOtelScopeEnabled(boolean otelScopeEnabled)
6061
return this;
6162
}
6263

64+
/**
65+
* Set if the resource attributes should be added as labels on each exported metric.
66+
*
67+
* <p>If set, resource attributes will be added as labels on each exported metric if their key
68+
* tests positive (true) when passed through {@code resourceAttributesFilter}.
69+
*
70+
* @param resourceAttributesFilter a predicate that returns true if the resource attribute should
71+
* be added as a label on each exported metric. The predicates input is the resource attribute
72+
* key.
73+
*/
74+
public PrometheusHttpServerBuilder setAllowedResourceAttributesFilter(
75+
Predicate<String> resourceAttributesFilter) {
76+
this.allowedResourceAttributesFilter = requireNonNull(resourceAttributesFilter);
77+
return this;
78+
}
79+
6380
/**
6481
* Returns a new {@link PrometheusHttpServer} with the configuration of this builder which can be
6582
* registered with a {@link io.opentelemetry.sdk.metrics.SdkMeterProvider}.
6683
*/
6784
public PrometheusHttpServer build() {
68-
return new PrometheusHttpServer(host, port, executor, prometheusRegistry, otelScopeEnabled);
85+
return new PrometheusHttpServer(
86+
host,
87+
port,
88+
executor,
89+
prometheusRegistry,
90+
otelScopeEnabled,
91+
allowedResourceAttributesFilter);
6992
}
7093

7194
PrometheusHttpServerBuilder() {}

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/PrometheusMetricReader.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import io.opentelemetry.sdk.metrics.export.MetricReader;
1313
import io.prometheus.metrics.model.registry.MultiCollector;
1414
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
15+
import java.util.function.Predicate;
16+
import javax.annotation.Nullable;
1517

1618
/**
1719
* This is the bridge between Prometheus and OpenTelemetry.
@@ -26,9 +28,12 @@ public class PrometheusMetricReader implements MetricReader, MultiCollector {
2628
private volatile CollectionRegistration collectionRegistration = CollectionRegistration.noop();
2729
private final Otel2PrometheusConverter converter;
2830

29-
/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean)}. */
30-
public PrometheusMetricReader(boolean otelScopeEnabled) {
31-
this.converter = new Otel2PrometheusConverter(otelScopeEnabled);
31+
// TODO: refactor to public static create or builder pattern to align with project style
32+
/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean, Predicate)}. */
33+
public PrometheusMetricReader(
34+
boolean otelScopeEnabled, @Nullable Predicate<String> allowedResourceAttributesFilter) {
35+
this.converter =
36+
new Otel2PrometheusConverter(otelScopeEnabled, allowedResourceAttributesFilter);
3237
}
3338

3439
@Override

exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/internal/PrometheusMetricReaderProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public MetricReader createMetricReader(ConfigProperties config) {
3131
if (host != null) {
3232
prometheusBuilder.setHost(host);
3333
}
34+
3435
return prometheusBuilder.build();
3536
}
3637

0 commit comments

Comments
 (0)