diff --git a/api/src/main/java/io/opentelemetry/OpenTelemetry.java b/api/src/main/java/io/opentelemetry/OpenTelemetry.java index 7f7f591add0..9a7b416bea2 100644 --- a/api/src/main/java/io/opentelemetry/OpenTelemetry.java +++ b/api/src/main/java/io/opentelemetry/OpenTelemetry.java @@ -18,8 +18,7 @@ import io.opentelemetry.context.propagation.DefaultHttpExtractor; import io.opentelemetry.context.propagation.DefaultHttpInjector; -import io.opentelemetry.context.propagation.HttpExtractor; -import io.opentelemetry.context.propagation.HttpInjector; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.CorrelationContextManager; import io.opentelemetry.distributedcontext.DefaultCorrelationContextManager; import io.opentelemetry.distributedcontext.spi.CorrelationContextManagerProvider; @@ -53,29 +52,8 @@ public final class OpenTelemetry { private final TracerFactory tracerFactory; private final Meter meter; private final CorrelationContextManager contextManager; - - public static final class Propagators { - private static volatile HttpInjector httpInjector = new DefaultHttpInjector(); - private static volatile HttpExtractor httpExtractor = new DefaultHttpExtractor(); - - public static HttpExtractor getHttpExtractor() { - return httpExtractor; - } - - public static void setHttpExtractor(HttpExtractor httpExtractor) { - Propagators.httpExtractor = httpExtractor; - } - - public static HttpInjector getHttpInjector() { - return httpInjector; - } - - public static void setHttpInjector(HttpInjector httpInjector) { - Propagators.httpInjector = httpInjector; - } - - private Propagators() {} - } + private volatile Propagators propagators = + Propagators.create(new DefaultHttpInjector(), new DefaultHttpExtractor()); /** * Returns a singleton {@link TracerFactory}. @@ -113,6 +91,14 @@ public static CorrelationContextManager getCorrelationContextManager() { return getInstance().contextManager; } + public static Propagators getPropagators() { + return getInstance().propagators; + } + + public static void setPropagators(Propagators propagators) { + getInstance().propagators = propagators; + } + /** Lazy loads an instance. */ private static OpenTelemetry getInstance() { if (instance == null) { diff --git a/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java new file mode 100644 index 00000000000..2b76983e115 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/context/propagation/Propagators.java @@ -0,0 +1,39 @@ +/* + * Copyright 2019, 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.context.propagation; + +public final class Propagators { + private final HttpInjector injector; + private final HttpExtractor extractor; + + public static Propagators create(HttpInjector injector, HttpExtractor extractor) { + return new Propagators(injector, extractor); + } + + private Propagators(HttpInjector injector, HttpExtractor extractor) { + this.injector = injector; + this.extractor = extractor; + } + + public HttpExtractor getHttpExtractor() { + return extractor; + } + + public HttpInjector getHttpInjector() { + return injector; + } +} diff --git a/contrib/web_servlet_filter/src/main/java/io/opentelemetry/contrib/web/servlet/filter/TracingFilter.java b/contrib/web_servlet_filter/src/main/java/io/opentelemetry/contrib/web/servlet/filter/TracingFilter.java index f6da859b7dc..54eef8c7b96 100644 --- a/contrib/web_servlet_filter/src/main/java/io/opentelemetry/contrib/web/servlet/filter/TracingFilter.java +++ b/contrib/web_servlet_filter/src/main/java/io/opentelemetry/contrib/web/servlet/filter/TracingFilter.java @@ -20,6 +20,8 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.ChainedPropagators; +import io.opentelemetry.context.propagation.DefaultHttpInjector; +import io.opentelemetry.context.propagation.Propagators; import io.opentelemetry.distributedcontext.propagation.DefaultCorrelationContextExtractor; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.SpanContext; @@ -145,10 +147,13 @@ public void init(FilterConfig filterConfig) throws ServletException { skipPattern = (Pattern) contextAttribute; } - // Initialize the extractor. - OpenTelemetry.Propagators.setHttpExtractor( - ChainedPropagators.chain( - new HttpTraceContextExtractor(), new DefaultCorrelationContextExtractor())); + // Initialize the propagators. + Propagators propagators = + Propagators.create( + new DefaultHttpInjector(), + ChainedPropagators.chain( + new HttpTraceContextExtractor(), new DefaultCorrelationContextExtractor())); + OpenTelemetry.setPropagators(propagators); } @Override @@ -177,7 +182,8 @@ public void doFilter( * instance. */ Context ctx = - OpenTelemetry.Propagators.getHttpExtractor() + OpenTelemetry.getPropagators() + .getHttpExtractor() .extract(Context.current(), httpRequest, HttpServletRequestGetter.getInstance()); SpanContext extractedContext = ctx.getValue(ContextKeys.getSpanContextKey());