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

Extend prometheus declarative config support to include without_scope_info, with_resource_constant_labels #6840

Open
wants to merge 1 commit 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
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.util.StringJoiner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -38,13 +39,17 @@
*/
public final class PrometheusHttpServer implements MetricReader {

private final String host;
private final int port;
private final boolean otelScopeEnabled;
@Nullable private final Predicate<String> allowedResourceAttributesFilter;
private final MemoryMode memoryMode;
private final DefaultAggregationSelector defaultAggregationSelector;

private final PrometheusHttpServerBuilder builder;
private final HTTPServer httpServer;
private final PrometheusMetricReader prometheusMetricReader;
private final PrometheusRegistry prometheusRegistry;
private final String host;
private final MemoryMode memoryMode;
private final DefaultAggregationSelector defaultAggregationSelector;

/**
* Returns a new {@link PrometheusHttpServer} which can be registered to an {@link
Expand All @@ -71,11 +76,16 @@ public static PrometheusHttpServerBuilder builder() {
MemoryMode memoryMode,
@Nullable HttpHandler defaultHandler,
DefaultAggregationSelector defaultAggregationSelector) {
this.host = host;
this.port = port;
this.otelScopeEnabled = otelScopeEnabled;
this.allowedResourceAttributesFilter = allowedResourceAttributesFilter;
this.memoryMode = memoryMode;
this.defaultAggregationSelector = defaultAggregationSelector;

this.builder = builder;
this.prometheusMetricReader =
new PrometheusMetricReader(otelScopeEnabled, allowedResourceAttributesFilter);
this.host = host;
this.memoryMode = memoryMode;
this.prometheusRegistry = prometheusRegistry;
prometheusRegistry.register(prometheusMetricReader);
// When memory mode is REUSABLE_DATA, concurrent reads lead to data corruption. To prevent this,
Expand Down Expand Up @@ -103,7 +113,6 @@ public static PrometheusHttpServerBuilder builder() {
} catch (IOException e) {
throw new UncheckedIOException("Could not create Prometheus HTTP server", e);
}
this.defaultAggregationSelector = defaultAggregationSelector;
}

@Override
Expand Down Expand Up @@ -157,7 +166,16 @@ public void close() {

@Override
public String toString() {
return "PrometheusHttpServer{address=" + getAddress() + "}";
StringJoiner joiner = new StringJoiner(",", "PrometheusHttpServer{", "}");
joiner.add("host=" + host);
joiner.add("port=" + port);
joiner.add("otelScopeEnabled=" + otelScopeEnabled);
joiner.add("allowedResourceAttributesFilter=" + allowedResourceAttributesFilter);
joiner.add("memoryMode=" + memoryMode);
joiner.add(
"defaultAggregationSelector="
+ DefaultAggregationSelector.asString(defaultAggregationSelector));
return joiner.toString();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR also improves the PrometheusHttpServer#toString() implementation, which has been failing to include the various configurable parameters.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import io.opentelemetry.exporter.prometheus.PrometheusHttpServerBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.internal.IncludeExcludePredicate;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import java.util.List;

/**
* Declarative configuration SPI implementation for {@link PrometheusHttpServer}.
Expand Down Expand Up @@ -37,11 +39,28 @@ public MetricReader create(StructuredConfigProperties config) {
if (port != null) {
prometheusBuilder.setPort(port);
}

String host = config.getString("host");
if (host != null) {
prometheusBuilder.setHost(host);
}

Boolean withoutScopeInfo = config.getBoolean("without_scope_info");
if (withoutScopeInfo != null) {
prometheusBuilder.setOtelScopeEnabled(!withoutScopeInfo);
}

StructuredConfigProperties withResourceConstantLabels =
config.getStructured("with_resource_constant_labels");
if (withResourceConstantLabels != null) {
List<String> included = withResourceConstantLabels.getScalarList("included", String.class);
List<String> excluded = withResourceConstantLabels.getScalarList("excluded", String.class);
if (included != null || excluded != null) {
prometheusBuilder.setAllowedResourceAttributesFilter(
IncludeExcludePredicate.createPatternMatching(included, excluded));
}
}

return prometheusBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,15 @@ void fetch_DuplicateMetrics() {
@Test
void stringRepresentation() {
assertThat(prometheusServer.toString())
.isEqualTo("PrometheusHttpServer{address=" + prometheusServer.getAddress() + "}");
.isEqualTo(
"PrometheusHttpServer{"
+ "host=localhost,"
+ "port=0,"
+ "otelScopeEnabled=true,"
+ "allowedResourceAttributesFilter=null,"
+ "memoryMode=REUSABLE_DATA,"
+ "defaultAggregationSelector=DefaultAggregationSelector{COUNTER=default, UP_DOWN_COUNTER=default, HISTOGRAM=default, OBSERVABLE_COUNTER=default, OBSERVABLE_UP_DOWN_COUNTER=default, OBSERVABLE_GAUGE=default, GAUGE=default}"
+ "}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import static io.opentelemetry.sdk.internal.GlobUtil.toGlobPatternPredicate;

import io.opentelemetry.sdk.autoconfigure.ResourceConfiguration;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
Expand All @@ -17,6 +15,7 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeNameValueModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.DetectorAttributesModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.DetectorsModel;
import io.opentelemetry.sdk.internal.IncludeExcludePredicate;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceBuilder;
import java.io.Closeable;
Expand Down Expand Up @@ -147,47 +146,16 @@ private static boolean matchAll(String attributeKey) {

private static Predicate<String> detectorAttributeFilter(
@Nullable DetectorsModel detectorsModel) {
if (detectorsModel == null) {
return ResourceFactory::matchAll;
}
DetectorAttributesModel detectorAttributesModel = detectorsModel.getAttributes();
if (detectorAttributesModel == null) {
return ResourceFactory::matchAll;
}
List<String> included = detectorAttributesModel.getIncluded();
List<String> excluded = detectorAttributesModel.getExcluded();
if (included == null && excluded == null) {
return ResourceFactory::matchAll;
}
if (included == null) {
return excludedPredicate(excluded);
}
if (excluded == null) {
return includedPredicate(included);
}
return includedPredicate(included).and(excludedPredicate(excluded));
}

/**
* Returns a predicate which matches strings matching any of the {@code included} glob patterns.
*/
private static Predicate<String> includedPredicate(List<String> included) {
Predicate<String> result = attributeKey -> false;
for (String include : included) {
result = result.or(toGlobPatternPredicate(include));
}
return result;
}

/**
* Returns a predicate which matches strings NOT matching any of the {@code excluded} glob
* patterns.
*/
private static Predicate<String> excludedPredicate(List<String> excluded) {
Predicate<String> result = attributeKey -> true;
for (String exclude : excluded) {
result = result.and(toGlobPatternPredicate(exclude).negate());
if (detectorsModel != null) {
DetectorAttributesModel attributesModel = detectorsModel.getAttributes();
if (attributesModel != null) {
List<String> included = attributesModel.getIncluded();
List<String> excluded = attributesModel.getExcluded();
if (included != null || excluded != null) {
return IncludeExcludePredicate.createPatternMatching(included, excluded);
}
}
}
return result;
return ResourceFactory::matchAll;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.IncludeExcludeModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.StreamModel;
import io.opentelemetry.sdk.internal.IncludeExcludePredicate;
import io.opentelemetry.sdk.metrics.View;
import io.opentelemetry.sdk.metrics.ViewBuilder;
import java.io.Closeable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;

final class ViewFactory implements Factory<StreamModel, View> {

Expand All @@ -37,33 +35,16 @@ public View create(StreamModel model, SpiHelper spiHelper, List<Closeable> close
}
IncludeExcludeModel attributeKeys = model.getAttributeKeys();
if (attributeKeys != null) {
addAttributeKeyFilter(builder, attributeKeys.getIncluded(), attributeKeys.getExcluded());
List<String> included = attributeKeys.getIncluded();
List<String> excluded = attributeKeys.getExcluded();
if (included != null || excluded != null) {
builder.setAttributeFilter(IncludeExcludePredicate.createExactMatching(included, excluded));
}
}
if (model.getAggregation() != null) {
builder.setAggregation(
AggregationFactory.getInstance().create(model.getAggregation(), spiHelper, closeables));
}
return builder.build();
}

private static void addAttributeKeyFilter(
ViewBuilder builder, @Nullable List<String> included, @Nullable List<String> excluded) {
if (included == null && excluded == null) {
return;
}
if (included == null) {
Set<String> excludedKeys = new HashSet<>(excluded);
// TODO: set predicate with useful toString implementation
builder.setAttributeFilter(attributeKey -> !excludedKeys.contains(attributeKey));
return;
}
if (excluded == null) {
Set<String> includedKeys = new HashSet<>(included);
builder.setAttributeFilter(includedKeys);
return;
}
Set<String> includedKeys = new HashSet<>(included);
excluded.forEach(includedKeys::remove);
builder.setAttributeFilter(includedKeys);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.sdk.extension.incubator.fileconfig;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand All @@ -17,13 +18,15 @@
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.IncludeExcludeModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.MetricReaderModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OtlpMetricModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PeriodicMetricReaderModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PrometheusModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PullMetricExporterModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PullMetricReaderModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PushMetricExporterModel;
import io.opentelemetry.sdk.internal.IncludeExcludePredicate;
import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;
Expand Down Expand Up @@ -144,7 +147,14 @@ void create_PullPrometheusConfigured() throws IOException {
spiHelper = spy(spiHelper);
List<Closeable> closeables = new ArrayList<>();
PrometheusHttpServer expectedReader =
PrometheusHttpServer.builder().setHost("localhost").setPort(port).build();
PrometheusHttpServer.builder()
.setHost("localhost")
.setPort(port)
.setOtelScopeEnabled(false)
.setAllowedResourceAttributesFilter(
IncludeExcludePredicate.createPatternMatching(
singletonList("foo"), singletonList("bar")))
.build();
// Close the reader to avoid port conflict with the new instance created by MetricReaderFactory
expectedReader.close();

Expand All @@ -159,7 +169,14 @@ void create_PullPrometheusConfigured() throws IOException {
.withPrometheus(
new PrometheusModel()
.withHost("localhost")
.withPort(port)))),
.withPort(port)
.withWithResourceConstantLabels(
new IncludeExcludeModel()
.withIncluded(singletonList("foo"))
.withExcluded(singletonList("bar")))
.withWithoutScopeInfo(true)
.withWithoutTypeSuffix(true)
.withWithoutUnits(true)))),
spiHelper,
closeables);
cleanup.addCloseable(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.ExplicitBucketHistogramModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.IncludeExcludeModel;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.StreamModel;
import io.opentelemetry.sdk.internal.IncludeExcludePredicate;
import io.opentelemetry.sdk.metrics.View;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import org.junit.jupiter.api.Test;

class ViewFactoryTest {
Expand All @@ -41,7 +41,9 @@ void create() {
View.builder()
.setName("name")
.setDescription("description")
.setAttributeFilter(new HashSet<>(Arrays.asList("foo", "bar")))
.setAttributeFilter(
IncludeExcludePredicate.createExactMatching(
Arrays.asList("foo", "bar"), Collections.singletonList("baz")))
.setAggregation(
io.opentelemetry.sdk.metrics.Aggregation.explicitBucketHistogram(
Arrays.asList(1.0, 2.0)))
Expand All @@ -54,7 +56,9 @@ void create() {
.withName("name")
.withDescription("description")
.withAttributeKeys(
new IncludeExcludeModel().withIncluded(Arrays.asList("foo", "bar")))
new IncludeExcludeModel()
.withIncluded(Arrays.asList("foo", "bar"))
.withExcluded(Collections.singletonList("baz")))
.withAggregation(
new AggregationModel()
.withExplicitBucketHistogram(
Expand Down
Loading
Loading