Deprecate metrics from opentelemetry-api, add a global version in metrics api (#2279)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2020-12-15 15:14:50 -08:00 committed by GitHub
parent eccbfebf38
commit dbb1ae91f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 169 additions and 56 deletions

View File

@ -42,6 +42,7 @@ public class DefaultOpenTelemetry implements OpenTelemetry {
}
@Override
@Deprecated
public MeterProvider getMeterProvider() {
return meterProvider;
}

View File

@ -30,6 +30,7 @@ public class DefaultOpenTelemetryBuilder
}
@Override
@Deprecated
public DefaultOpenTelemetryBuilder setMeterProvider(MeterProvider meterProvider) {
requireNonNull(meterProvider, "meterProvider");
this.meterProvider = meterProvider;

View File

@ -115,7 +115,12 @@ public final class GlobalOpenTelemetry {
return get().getTracer(instrumentationName, instrumentationVersion);
}
/** Returns the globally registered {@link MeterProvider}. */
/**
* Returns the globally registered {@link MeterProvider}.
*
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static MeterProvider getMeterProvider() {
return get().getMeterProvider();
}
@ -128,7 +133,9 @@ public final class GlobalOpenTelemetry {
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static Meter getMeter(String instrumentationName) {
return get().getMeter(instrumentationName);
}
@ -144,7 +151,9 @@ public final class GlobalOpenTelemetry {
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static Meter getMeter(String instrumentationName, String instrumentationVersion) {
return get().getMeter(instrumentationName, instrumentationVersion);
}

View File

@ -88,7 +88,7 @@ public interface OpenTelemetry {
/**
* Returns the globally registered {@link MeterProvider}.
*
* @deprecated use {@link GlobalOpenTelemetry#getMeterProvider()}
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
static MeterProvider getGlobalMeterProvider() {
@ -98,7 +98,7 @@ public interface OpenTelemetry {
/**
* Gets or creates a named meter instance from the globally registered {@link MeterProvider}.
*
* @deprecated use {@link GlobalOpenTelemetry#getMeter(String)}
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
static Meter getGlobalMeter(String instrumentationName) {
@ -109,7 +109,7 @@ public interface OpenTelemetry {
* Gets or creates a named and versioned meter instance from the globally registered {@link
* MeterProvider}.
*
* @deprecated use {@link GlobalOpenTelemetry#getMeter(String, String)}
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
static Meter getGlobalMeter(String instrumentationName, String instrumentationVersion) {
@ -173,17 +173,21 @@ public interface OpenTelemetry {
return getTracerProvider().get(instrumentationName, instrumentationVersion);
}
/** Returns the {@link MeterProvider} for this {@link OpenTelemetry}. */
/**
* Returns the {@link MeterProvider} for this {@link OpenTelemetry}.
*
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
MeterProvider getMeterProvider();
/**
* Gets or creates a named meter instance from the {@link MeterProvider} in this {@link
* OpenTelemetry}.
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
default Meter getMeter(String instrumentationName) {
return getMeterProvider().get(instrumentationName);
}
@ -192,11 +196,9 @@ public interface OpenTelemetry {
* Gets or creates a named and versioned meter instance from the {@link MeterProvider} in this
* {@link OpenTelemetry}.
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
default Meter getMeter(String instrumentationName, String instrumentationVersion) {
return getMeterProvider().get(instrumentationName, instrumentationVersion);
}

View File

@ -21,7 +21,12 @@ public interface OpenTelemetryBuilder<T extends OpenTelemetryBuilder<T>> {
/** Sets the {@link TracerProvider} to use. */
T setTracerProvider(TracerProvider tracerProvider);
/** Sets the {@link MeterProvider} to use. */
/**
* Sets the {@link MeterProvider} to use.
*
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
T setMeterProvider(MeterProvider meterProvider);
/** Sets the {@link ContextPropagators} to use. */

View File

@ -41,6 +41,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@SuppressWarnings("deprecation") // Testing deprecated code
class OpenTelemetryTest {
@BeforeAll

View File

@ -0,0 +1,93 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.api.metrics;
import io.opentelemetry.spi.metrics.MeterProviderFactory;
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
/**
* IMPORTANT: This is a temporary class, and solution for the metrics package until it will be
* marked as stable.
*/
public class GlobalMetricsProvider {
private static final Object mutex = new Object();
private static final AtomicReference<MeterProvider> globalMeterProvider = new AtomicReference<>();
private GlobalMetricsProvider() {}
/** Returns the globally registered {@link MeterProvider}. */
public static MeterProvider get() {
MeterProvider meterProvider = globalMeterProvider.get();
if (meterProvider == null) {
synchronized (mutex) {
if (globalMeterProvider.get() == null) {
MeterProviderFactory meterProviderFactory = loadSpi();
if (meterProviderFactory != null) {
meterProvider = meterProviderFactory.create();
} else {
meterProvider = MeterProvider.getDefault();
}
globalMeterProvider.compareAndSet(null, meterProvider);
}
}
}
return meterProvider;
}
/**
* Gets or creates a named meter instance from the globally registered {@link MeterProvider}.
*
* <p>This is a shortcut method for {@code getGlobalMeterProvider().get(instrumentationName)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @return a tracer instance.
*/
public static Meter getMeter(String instrumentationName) {
return get().get(instrumentationName);
}
/**
* Gets or creates a named and versioned meter instance from the globally registered {@link
* MeterProvider}.
*
* <p>This is a shortcut method for {@code getGlobalMeterProvider().get(instrumentationName,
* instrumentationVersion)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
* @return a tracer instance.
*/
public static Meter getMeter(String instrumentationName, String instrumentationVersion) {
return get().get(instrumentationName, instrumentationVersion);
}
/**
* Load provider class via {@link ServiceLoader}. A specific provider class can be requested via
* setting a system property with FQCN.
*
* @return a provider or null if not found
* @throws IllegalStateException if a specified provider is not found
*/
@Nullable
private static MeterProviderFactory loadSpi() {
String specifiedProvider = System.getProperty(MeterProviderFactory.class.getName());
ServiceLoader<MeterProviderFactory> providers = ServiceLoader.load(MeterProviderFactory.class);
for (MeterProviderFactory provider : providers) {
if (specifiedProvider == null || specifiedProvider.equals(provider.getClass().getName())) {
return provider;
}
}
if (specifiedProvider != null) {
throw new IllegalStateException(
String.format("Service provider %s not found", specifiedProvider));
}
return null;
}
}

View File

@ -5,7 +5,6 @@
package io.opentelemetry.spi.metrics;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.metrics.MeterProvider;
import javax.annotation.concurrent.ThreadSafe;
@ -16,8 +15,6 @@ import javax.annotation.concurrent.ThreadSafe;
* <br>
* A specific implementation can be selected by a system property {@code
* io.opentelemetry.metrics.spi.MeterProviderFactory} with value of fully qualified class name.
*
* @see OpenTelemetry
*/
@ThreadSafe
public interface MeterProviderFactory {

View File

@ -10,9 +10,10 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.ConnectivityState;
import io.grpc.ManagedChannel;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Labels;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceResponse;
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
@ -76,16 +77,9 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
private final TraceServiceFutureStub traceService;
private final ManagedChannel managedChannel;
private final long deadlineMs;
private final LongCounter spansSeen =
GlobalOpenTelemetry.getMeter("io.opentelemetry.exporters.otlp")
.longCounterBuilder("spansSeenByExporter")
.build();
private final LongCounter spansExported =
GlobalOpenTelemetry.getMeter("io.opentelemetry.exporters.otlp")
.longCounterBuilder("spansExportedByExporter")
.build();
private final LongCounter.BoundLongCounter spansSeen;
private final LongCounter.BoundLongCounter spansExportedSuccess;
private final LongCounter.BoundLongCounter spansExportedFailure;
/**
* Creates a new OTLP gRPC Span Reporter with the given name, using the given channel.
@ -95,6 +89,12 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
* or to a negative value, the exporter will wait indefinitely.
*/
OtlpGrpcSpanExporter(ManagedChannel channel, long deadlineMs) {
Meter meter = GlobalMetricsProvider.getMeter("io.opentelemetry.exporters.otlp");
this.spansSeen =
meter.longCounterBuilder("spansSeenByExporter").build().bind(EXPORTER_NAME_LABELS);
LongCounter spansExportedCounter = meter.longCounterBuilder("spansExportedByExporter").build();
this.spansExportedSuccess = spansExportedCounter.bind(EXPORT_SUCCESS_LABELS);
this.spansExportedFailure = spansExportedCounter.bind(EXPORT_FAILURE_LABELS);
this.managedChannel = channel;
this.deadlineMs = deadlineMs;
this.traceService = TraceServiceGrpc.newFutureStub(channel);
@ -108,7 +108,7 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
*/
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
spansSeen.add(spans.size(), EXPORTER_NAME_LABELS);
spansSeen.add(spans.size());
ExportTraceServiceRequest exportTraceServiceRequest =
ExportTraceServiceRequest.newBuilder()
.addAllResourceSpans(SpanAdapter.toProtoResourceSpans(spans))
@ -128,13 +128,13 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
new FutureCallback<ExportTraceServiceResponse>() {
@Override
public void onSuccess(@Nullable ExportTraceServiceResponse response) {
spansExported.add(spans.size(), EXPORT_SUCCESS_LABELS);
spansExportedSuccess.add(spans.size());
result.succeed();
}
@Override
public void onFailure(Throwable t) {
spansExported.add(spans.size(), EXPORT_FAILURE_LABELS);
spansExportedFailure.add(spans.size());
logger.log(Level.WARNING, "Failed to export spans. Error message: " + t.getMessage());
logger.log(Level.FINEST, "Failed to export spans. Details follow: " + t);
result.fail();
@ -181,15 +181,11 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
@Override
public CompletableResultCode shutdown() {
final CompletableResultCode result = new CompletableResultCode();
managedChannel.notifyWhenStateChanged(
ConnectivityState.SHUTDOWN,
new Runnable() {
@Override
public void run() {
result.succeed();
}
});
managedChannel.notifyWhenStateChanged(ConnectivityState.SHUTDOWN, result::succeed);
managedChannel.shutdown();
this.spansSeen.unbind();
this.spansExportedSuccess.unbind();
this.spansExportedFailure.unbind();
return result;
}
}

View File

@ -12,11 +12,13 @@ import eu.rekawek.toxiproxy.model.ToxicList;
import eu.rekawek.toxiproxy.model.toxic.Timeout;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Labels;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.exporter.otlp.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.MeterSdkProvider;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricData.LongPoint;
import io.opentelemetry.sdk.metrics.data.MetricData.Point;
@ -240,15 +242,13 @@ public class OtlpPipelineStressTest {
// set up the metric exporter and wire it into the SDK and a timed reader.
IntervalMetricReader intervalMetricReader =
IntervalMetricReader.builder()
.setMetricExporter(metricExporter)
.setMetricProducers(
Collections.singleton(
OpenTelemetrySdk.getGlobalMeterProvider().getMetricProducer()))
.setExportIntervalMillis(1000)
.build();
return intervalMetricReader;
return IntervalMetricReader.builder()
.setMetricExporter(metricExporter)
.setMetricProducers(
Collections.singleton(
((MeterSdkProvider) GlobalMetricsProvider.get()).getMetricProducer()))
.setExportIntervalMillis(1000)
.build();
}
private static void addOtlpSpanExporter() {

View File

@ -5,9 +5,9 @@
package io.opentelemetry.sdk.logging.export;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Labels;
import io.opentelemetry.api.internal.Utils;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongCounter.BoundLongCounter;
import io.opentelemetry.api.metrics.Meter;
@ -70,7 +70,7 @@ public class BatchLogProcessor implements LogProcessor {
private static class Worker implements Runnable {
static {
Meter meter = GlobalOpenTelemetry.getMeter("io.opentelemetry.sdk.logging");
Meter meter = GlobalMetricsProvider.getMeter("io.opentelemetry.sdk.logging");
LongCounter logRecordsProcessed =
meter
.longCounterBuilder("logRecordsProcessed")

View File

@ -6,9 +6,10 @@
package io.opentelemetry.sdk.trace.export;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.MeterSdkProvider;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricData.LongPoint;
import io.opentelemetry.sdk.metrics.data.MetricData.Point;
@ -55,7 +56,7 @@ public class BatchSpanProcessorDroppedSpansBenchmark {
@State(Scope.Benchmark)
public static class BenchmarkState {
private final MetricProducer metricProducer =
OpenTelemetrySdk.getGlobalMeterProvider().getMetricProducer();
((MeterSdkProvider) GlobalMetricsProvider.get()).getMetricProducer();
private BatchSpanProcessor processor;
private Tracer tracer;
private Collection<MetricData> allMetrics;

View File

@ -57,7 +57,12 @@ public final class OpenTelemetrySdk extends DefaultOpenTelemetry {
return (SdkTracerProvider) ((ObfuscatedTracerProvider) tracerProvider).unobfuscate();
}
/** Returns the global {@link MeterSdkProvider}. */
/**
* Returns the global {@link MeterSdkProvider}.
*
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static MeterSdkProvider getGlobalMeterProvider() {
return (MeterSdkProvider) GlobalOpenTelemetry.get().getMeterProvider();
}
@ -130,6 +135,7 @@ public final class OpenTelemetrySdk extends DefaultOpenTelemetry {
* @see MeterSdkProvider#builder()
*/
@Override
@Deprecated
public Builder setMeterProvider(MeterProvider meterProvider) {
if (!(meterProvider instanceof MeterSdkProvider)) {
throw new IllegalArgumentException(

View File

@ -40,6 +40,7 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
@SuppressWarnings("deprecation") // Testing deprecated code
class OpenTelemetrySdkTest {
@Mock private SdkTracerProvider tracerProvider;

View File

@ -7,7 +7,7 @@ package io.opentelemetry.sdk.metrics.spi;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.sdk.metrics.MeterSdkProvider;
import org.junit.jupiter.api.Test;
@ -15,6 +15,6 @@ import org.junit.jupiter.api.Test;
class MeterProviderFactorySdkTest {
@Test
void testDefault() {
assertThat(GlobalOpenTelemetry.getMeterProvider()).isInstanceOf(MeterSdkProvider.class);
assertThat(GlobalMetricsProvider.get()).isInstanceOf(MeterSdkProvider.class);
}
}

View File

@ -5,8 +5,8 @@
package io.opentelemetry.sdk.trace.export;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.Labels;
import io.opentelemetry.api.metrics.GlobalMetricsProvider;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongCounter.BoundLongCounter;
import io.opentelemetry.api.metrics.Meter;
@ -169,7 +169,7 @@ public final class BatchSpanProcessor implements SpanProcessor {
this.maxExportBatchSize = maxExportBatchSize;
this.exporterTimeoutMillis = exporterTimeoutMillis;
this.queue = queue;
Meter meter = GlobalOpenTelemetry.getMeter("io.opentelemetry.sdk.trace");
Meter meter = GlobalMetricsProvider.getMeter("io.opentelemetry.sdk.trace");
meter
.longValueObserverBuilder("queueSize")
.setDescription("The number of spans queued")