-
Notifications
You must be signed in to change notification settings - Fork 858
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
Stabilize MetricProducer, allow custom MetricReaders #5835
Changes from 2 commits
c3f769f
dd4af4b
374206a
ee329c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
Comparing source compatibility of against | ||
No changes. | ||
*** MODIFIED INTERFACE: PUBLIC ABSTRACT io.opentelemetry.sdk.metrics.export.CollectionRegistration (not serializable) | ||
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0 | ||
+++ NEW METHOD: PUBLIC(+) java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData> collectAllMetrics() | ||
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.export.CollectionRegistration noop() | ||
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.export.MetricProducer (not serializable) | ||
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a. | ||
+++ NEW SUPERCLASS: java.lang.Object | ||
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) java.util.Collection<io.opentelemetry.sdk.metrics.data.MetricData> produce(io.opentelemetry.sdk.resources.Resource) | ||
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder (not serializable) | ||
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0 | ||
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder registerMetricProducer(io.opentelemetry.sdk.metrics.export.MetricProducer) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.opencensusshim; | ||
|
||
import io.opencensus.metrics.Metrics; | ||
import io.opencensus.metrics.export.MetricProducerManager; | ||
import io.opentelemetry.opencensusshim.internal.metrics.MetricAdapter; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
import io.opentelemetry.sdk.metrics.data.MetricData; | ||
import io.opentelemetry.sdk.metrics.export.MetricProducer; | ||
import io.opentelemetry.sdk.metrics.export.MetricReader; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link MetricProducer} for OpenCensus metrics, which allows {@link MetricReader}s to read from | ||
* both OpenTelemetry and OpenCensus metrics. | ||
* | ||
* <p>To use, register with {@link SdkMeterProviderBuilder#registerMetricProducer(MetricProducer)}. | ||
*/ | ||
public final class OpenCensusMetricProducer implements MetricProducer { | ||
private final MetricProducerManager openCensusMetricStorage; | ||
|
||
OpenCensusMetricProducer(MetricProducerManager openCensusMetricStorage) { | ||
this.openCensusMetricStorage = openCensusMetricStorage; | ||
} | ||
|
||
/** | ||
* Constructs a new {@link OpenCensusMetricProducer} that reports against the given {@link | ||
* Resource}. | ||
*/ | ||
public static MetricProducer create() { | ||
return new OpenCensusMetricProducer(Metrics.getExportComponent().getMetricProducerManager()); | ||
} | ||
|
||
@Override | ||
public Collection<MetricData> produce(Resource resource) { | ||
List<MetricData> result = new ArrayList<>(); | ||
openCensusMetricStorage | ||
.getAllMetricProducer() | ||
.forEach( | ||
producer -> | ||
producer | ||
.getMetrics() | ||
.forEach(metric -> result.add(MetricAdapter.convert(resource, metric)))); | ||
return result; | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import io.opencensus.stats.Stats; | ||
import io.opencensus.stats.StatsRecorder; | ||
import io.opencensus.stats.View; | ||
import io.opentelemetry.opencensusshim.OpenCensusMetricProducer; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; | ||
import java.time.Duration; | ||
|
@@ -26,7 +27,10 @@ class OpenCensusMetricsTest { | |
void capturesOpenCensusAndOtelMetrics() throws InterruptedException { | ||
InMemoryMetricReader reader = InMemoryMetricReader.create(); | ||
SdkMeterProvider otelMetrics = | ||
SdkMeterProvider.builder().registerMetricReader(OpenCensusMetrics.attachTo(reader)).build(); | ||
SdkMeterProvider.builder() | ||
.registerMetricReader(reader) | ||
.registerMetricProducer(OpenCensusMetricProducer.create()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This demonstrates how you would register a custom metric producer with SdkMeterProvider. Nice and tidy. All the MetricData produced flow to any registered MetricReaders. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks great. |
||
.build(); | ||
// Record an otel metric. | ||
otelMetrics.meterBuilder("otel").build().counterBuilder("otel.sum").build().add(1); | ||
// Record an OpenCensus metric. | ||
|
@@ -47,7 +51,7 @@ void capturesOpenCensusAndOtelMetrics() throws InterruptedException { | |
.untilAsserted( | ||
() -> | ||
assertThat(reader.collectAllMetrics()) | ||
.satisfiesExactly( | ||
.satisfiesExactlyInAnyOrder( | ||
metric -> | ||
assertThat(metric).hasName("otel.sum").hasLongSumSatisfying(sum -> {}), | ||
metric -> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it currently possible to use the OC bridge with the Prometheus exporter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure - the OC metrics will go to any registered MetricReader. Built in MetricReaders include PrometheusHttpServer, PeriodicMetricReader (to support any push based MetricExporter), and InMemoryMetricReader for testing.