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

[PoC] Implement Metric Producer specification #5678

Closed
wants to merge 8 commits into from
Closed
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
21 changes: 20 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
Comparing source compatibility of against
No changes.
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.data.ScopeMetricData (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.data.Data<?> getData()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String getDescription()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.GaugeData<io.opentelemetry.sdk.metrics.data.DoublePointData> getDoubleGaugeData()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.SumData<io.opentelemetry.sdk.metrics.data.DoublePointData> getDoubleSumData()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.ExponentialHistogramData getExponentialHistogramData()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.HistogramData getHistogramData()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.common.InstrumentationScopeInfo getInstrumentationScopeInfo()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.GaugeData<io.opentelemetry.sdk.metrics.data.LongPointData> getLongGaugeData()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.SumData<io.opentelemetry.sdk.metrics.data.LongPointData> getLongSumData()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String getName()
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.data.SummaryData getSummaryData()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.data.MetricDataType getType()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.lang.String getUnit()
+++ NEW METHOD: PUBLIC(+) boolean isEmpty()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.export.PeriodicMetricReaderBuilder addMetricProducer(io.opentelemetry.sdk.metrics.internal.export.MetricProducer)
17 changes: 16 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-testing.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert assertThat(io.opentelemetry.sdk.metrics.data.ScopeMetricData)
+++ NEW CLASS: PUBLIC(+) FINAL(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasDescription(java.lang.String)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasDoubleGaugeSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.DoubleGaugeAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasDoubleSumSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.DoubleSumAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasExponentialHistogramSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.ExponentialHistogramAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasHistogramSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.HistogramAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasInstrumentationScope(io.opentelemetry.sdk.common.InstrumentationScopeInfo)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasLongGaugeSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.LongGaugeAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasLongSumSatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.LongSumAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasName(java.lang.String)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasSummarySatisfying(java.util.function.Consumer<io.opentelemetry.sdk.testing.assertj.SummaryAssert>)
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.testing.assertj.ScopeMetricAssert hasUnit(java.lang.String)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.CollectionRegistration;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.export.MetricProducer;
import io.opentelemetry.sdk.metrics.internal.export.SdkMetricProducer;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
Expand All @@ -42,6 +45,7 @@
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;

Expand All @@ -59,7 +63,8 @@ public final class PrometheusHttpServer implements MetricReader {

private final HttpServer server;
private final ExecutorService executor;
private volatile MetricProducer metricProducer = MetricProducer.noop();
private volatile SdkMetricProducer sdkMetricProducer = SdkMetricProducer.noop();
private final List<MetricProducer> registeredProducers;

/**
* Returns a new {@link PrometheusHttpServer} which can be registered to an {@link
Expand All @@ -75,25 +80,37 @@ public static PrometheusHttpServerBuilder builder() {
return new PrometheusHttpServerBuilder();
}

PrometheusHttpServer(String host, int port, ExecutorService executor) {
PrometheusHttpServer(
String host, int port, ExecutorService executor, List<MetricProducer> registeredProducers) {
try {
server = HttpServer.create(new InetSocketAddress(host, port), 3);
} catch (IOException e) {
throw new UncheckedIOException("Could not create Prometheus HTTP server", e);
}
MetricsHandler metricsHandler =
new MetricsHandler(() -> getMetricProducer().collectAllMetrics());
MetricsHandler metricsHandler = new MetricsHandler(() -> getMetricData());
server.createContext("/", metricsHandler);
server.createContext("/metrics", metricsHandler);
server.createContext("/-/healthy", HealthHandler.INSTANCE);
this.executor = executor;
server.setExecutor(executor);
this.registeredProducers = registeredProducers;

start();
}

private MetricProducer getMetricProducer() {
return metricProducer;
private Collection<MetricData> getMetricData() {
Collection<MetricData> metricData = sdkMetricProducer.collectAllMetrics();
Resource resource = sdkMetricProducer.getResource();
for (MetricProducer metricProducer : registeredProducers) {
metricData.addAll(
metricProducer.produce().stream()
.map(
scopeMetricData -> {
return ImmutableMetricData.createFromScopeMetricData(resource, scopeMetricData);
})
.collect(Collectors.toList()));
}
return metricData;
}

private void start() {
Expand All @@ -114,7 +131,7 @@ private void start() {

@Override
public void register(CollectionRegistration registration) {
this.metricProducer = MetricProducer.asMetricProducer(registration);
this.sdkMetricProducer = SdkMetricProducer.asSdkMetricProducer(registration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import static java.util.Objects.requireNonNull;

import io.opentelemetry.sdk.internal.DaemonThreadFactory;
import io.opentelemetry.sdk.metrics.internal.export.MetricProducer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
Expand All @@ -24,6 +28,8 @@ public final class PrometheusHttpServerBuilder {

@Nullable private ExecutorService executor;

private final List<MetricProducer> registeredProducers = new ArrayList<>();

/** Sets the host to bind to. If unset, defaults to {@value #DEFAULT_HOST}. */
public PrometheusHttpServerBuilder setHost(String host) {
requireNonNull(host, "host");
Expand All @@ -46,6 +52,13 @@ public PrometheusHttpServerBuilder setExecutor(ExecutorService executor) {
return this;
}

/** Adds a {@link MetricProducer} to schedule reads on. */
public PrometheusHttpServerBuilder addMetricProducer(MetricProducer producer) {
Objects.requireNonNull(producer, "producer");
this.registeredProducers.add(producer);
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}.
Expand All @@ -55,7 +68,7 @@ public PrometheusHttpServer build() {
if (executorService == null) {
executorService = getDefaultExecutor();
}
return new PrometheusHttpServer(host, port, executorService);
return new PrometheusHttpServer(host, port, executorService, registeredProducers);
}

PrometheusHttpServerBuilder() {}
Expand Down
Loading