Add default service provider for CorrelationContextManager. (#960)

Signed-off-by: Bogdan Cristian Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2020-03-04 13:38:31 -05:00 committed by GitHub
parent b20edbe043
commit a395039e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 12 deletions

View File

@ -18,6 +18,7 @@ package io.opentelemetry;
import io.opentelemetry.correlationcontext.CorrelationContextManager;
import io.opentelemetry.correlationcontext.DefaultCorrelationContextManager;
import io.opentelemetry.correlationcontext.DefaultCorrelationContextManagerProvider;
import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider;
import io.opentelemetry.metrics.DefaultMeterProvider;
import io.opentelemetry.metrics.DefaultMetricsProvider;
@ -118,7 +119,7 @@ public final class OpenTelemetry {
contextManager =
contextManagerProvider != null
? contextManagerProvider.create()
: DefaultCorrelationContextManager.getInstance();
: DefaultCorrelationContextManagerProvider.getInstance().create();
}
/**

View File

@ -43,7 +43,6 @@ public final class DefaultCorrelationContextManager implements CorrelationContex
*
* @return a {@code CorrelationContextManager} singleton that is the default implementation for
* {@link CorrelationContextManager}.
* @since 0.1.0
*/
public static CorrelationContextManager getInstance() {
return INSTANCE;

View File

@ -0,0 +1,43 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.correlationcontext;
import io.opentelemetry.correlationcontext.spi.CorrelationContextManagerProvider;
public final class DefaultCorrelationContextManagerProvider
implements CorrelationContextManagerProvider {
private static final CorrelationContextManagerProvider instance =
new DefaultCorrelationContextManagerProvider();
/**
* Returns a {@code CorrelationContextManagerProvider} singleton that is the default
* implementation for {@link CorrelationContextManager}.
*
* @return a {@code CorrelationContextManagerProvider} singleton that is the default
* implementation for {@link CorrelationContextManager}.
*/
public static CorrelationContextManagerProvider getInstance() {
return instance;
}
@Override
public CorrelationContextManager create() {
return DefaultCorrelationContextManager.getInstance();
}
private DefaultCorrelationContextManagerProvider() {}
}

View File

@ -37,7 +37,7 @@ import io.opentelemetry.metrics.LongObserver;
import io.opentelemetry.metrics.Meter;
import io.opentelemetry.metrics.MeterProvider;
import io.opentelemetry.metrics.spi.MetricsProvider;
import io.opentelemetry.trace.DefaultTracer;
import io.opentelemetry.trace.DefaultTracerProvider;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.Tracer;
@ -79,17 +79,15 @@ public class OpenTelemetryTest {
@Test
public void testDefault() {
assertThat(OpenTelemetry.getTracerProvider().get("testTracer"))
.isInstanceOf(DefaultTracer.getInstance().getClass());
assertThat(OpenTelemetry.getTracerProvider().get("testTracer"))
.isEqualTo(OpenTelemetry.getTracerProvider().get("testTracer"));
assertThat(OpenTelemetry.getMeterProvider())
.isInstanceOf(DefaultMeterProvider.getInstance().getClass());
assertThat(OpenTelemetry.getMeterProvider()).isEqualTo(OpenTelemetry.getMeterProvider());
assertThat(OpenTelemetry.getTracerProvider()).isInstanceOf(DefaultTracerProvider.class);
assertThat(OpenTelemetry.getTracerProvider())
.isSameInstanceAs(OpenTelemetry.getTracerProvider());
assertThat(OpenTelemetry.getMeterProvider()).isInstanceOf(DefaultMeterProvider.class);
assertThat(OpenTelemetry.getMeterProvider()).isSameInstanceAs(OpenTelemetry.getMeterProvider());
assertThat(OpenTelemetry.getCorrelationContextManager())
.isInstanceOf(DefaultCorrelationContextManager.getInstance().getClass());
.isInstanceOf(DefaultCorrelationContextManager.class);
assertThat(OpenTelemetry.getCorrelationContextManager())
.isEqualTo(OpenTelemetry.getCorrelationContextManager());
.isSameInstanceAs(OpenTelemetry.getCorrelationContextManager());
}
@Test