Skip to content

Commit

Permalink
Update the Propagators to be an actual container.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosalberto committed Nov 22, 2019
1 parent 70e0164 commit d6c9dfa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
36 changes: 11 additions & 25 deletions api/src/main/java/io/opentelemetry/OpenTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit d6c9dfa

Please sign in to comment.