Skip to content

Commit

Permalink
Allow Prometheus exporter to add resource attributes to metric attrib…
Browse files Browse the repository at this point in the history
…utes (#6179)
  • Loading branch information
asafm committed Feb 8, 2024
1 parent 988dcca commit a5d2065
Show file tree
Hide file tree
Showing 10 changed files with 563 additions and 103 deletions.

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,10 @@ public static PrometheusHttpServerBuilder builder() {
int port,
@Nullable ExecutorService executor,
PrometheusRegistry prometheusRegistry,
boolean otelScopeEnabled) {
this.prometheusMetricReader = new PrometheusMetricReader(otelScopeEnabled);
boolean otelScopeEnabled,
@Nullable Predicate<String> allowedResourceAttributesFilter) {
this.prometheusMetricReader =
new PrometheusMetricReader(otelScopeEnabled, 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,7 @@ public final class PrometheusHttpServerBuilder {
private int port = DEFAULT_PORT;
private PrometheusRegistry prometheusRegistry = new PrometheusRegistry();
private boolean otelScopeEnabled = true;

@Nullable private Predicate<String> allowedResourceAttributesFilter;
@Nullable private ExecutorService executor;

/** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
Expand Down Expand Up @@ -60,12 +61,34 @@ 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);
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,
allowedResourceAttributesFilter);
}

PrometheusHttpServerBuilder() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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;
import javax.annotation.Nullable;

/**
* This is the bridge between Prometheus and OpenTelemetry.
Expand All @@ -26,9 +28,12 @@ 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);
// TODO: refactor to public static create or builder pattern to align with project style
/** See {@link Otel2PrometheusConverter#Otel2PrometheusConverter(boolean, Predicate)}. */
public PrometheusMetricReader(
boolean otelScopeEnabled, @Nullable Predicate<String> allowedResourceAttributesFilter) {
this.converter =
new Otel2PrometheusConverter(otelScopeEnabled, 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

0 comments on commit a5d2065

Please sign in to comment.