From 2934f4b1160aaae79fa2619887b119cc4f3c0c4e Mon Sep 17 00:00:00 2001 From: Nail Islamov Date: Mon, 12 Oct 2020 16:16:10 +1100 Subject: [PATCH 1/8] Add support for custom context storage --- .../io/opencensus/trace/ContextManager.java | 7 +++ .../main/java/io/opencensus/trace/Ctx.java | 6 +++ .../io/opencensus/trace/CurrentSpanUtils.java | 19 ++++---- .../trace/unsafe/ContextManagerImpl.java | 39 ++++++++++++++++ .../opencensus/trace/unsafe/ContextUtils.java | 2 +- .../io/opencensus/trace/unsafe/CtxImpl.java | 30 ++++++++++++ .../io/opencensus/trace/unsafe/CtxUtils.java | 46 +++++++++++++++++++ .../trace/CurrentSpanUtilsTest.java | 7 ++- .../trace/unsafe/ContextUtilsTest.java | 10 ++-- .../log4j2/ContextDataUtils.java | 5 +- .../OpenCensusTraceLoggingEnhancer.java | 5 +- .../OpenCensusSleuthSpanContextHolder.java | 7 +-- 12 files changed, 154 insertions(+), 29 deletions(-) create mode 100644 api/src/main/java/io/opencensus/trace/ContextManager.java create mode 100644 api/src/main/java/io/opencensus/trace/Ctx.java create mode 100644 api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java create mode 100644 api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java create mode 100644 api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java diff --git a/api/src/main/java/io/opencensus/trace/ContextManager.java b/api/src/main/java/io/opencensus/trace/ContextManager.java new file mode 100644 index 0000000000..42f4c7c07d --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/ContextManager.java @@ -0,0 +1,7 @@ +package io.opencensus.trace; + +public interface ContextManager { + Ctx currentContext(); + Ctx withValue(Ctx ctx, @javax.annotation.Nullable Span span); + Span getValue(Ctx ctx); +} diff --git a/api/src/main/java/io/opencensus/trace/Ctx.java b/api/src/main/java/io/opencensus/trace/Ctx.java new file mode 100644 index 0000000000..70f36dd742 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/Ctx.java @@ -0,0 +1,6 @@ +package io.opencensus.trace; + +public interface Ctx { + Ctx attach(); + void detach(Ctx ctx); +} diff --git a/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java b/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java index 19d6ac9978..a9df2c908e 100644 --- a/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java +++ b/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java @@ -16,9 +16,8 @@ package io.opencensus.trace; -import io.grpc.Context; import io.opencensus.common.Scope; -import io.opencensus.trace.unsafe.ContextUtils; +import io.opencensus.trace.unsafe.CtxUtils; import java.util.concurrent.Callable; import javax.annotation.Nullable; @@ -34,7 +33,7 @@ private CurrentSpanUtils() {} */ @Nullable static Span getCurrentSpan() { - return ContextUtils.getValue(Context.current()); + return CtxUtils.getValue(CtxUtils.currentContext()); } /** @@ -78,7 +77,7 @@ static Callable withSpan(Span span, boolean endSpan, Callable callable // Defines an arbitrary scope of code as a traceable operation. Supports try-with-resources idiom. private static final class ScopeInSpan implements Scope { - private final Context origContext; + private final Ctx origContext; private final Span span; private final boolean endSpan; @@ -90,12 +89,12 @@ private static final class ScopeInSpan implements Scope { private ScopeInSpan(Span span, boolean endSpan) { this.span = span; this.endSpan = endSpan; - origContext = ContextUtils.withValue(Context.current(), span).attach(); + origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); } @Override public void close() { - Context.current().detach(origContext); + CtxUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } @@ -116,7 +115,7 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { @Override public void run() { - Context origContext = ContextUtils.withValue(Context.current(), span).attach(); + Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); try { runnable.run(); } catch (Throwable t) { @@ -128,7 +127,7 @@ public void run() { } throw new RuntimeException("unexpected", t); } finally { - Context.current().detach(origContext); + CtxUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } @@ -149,7 +148,7 @@ private CallableInSpan(Span span, Callable callable, boolean endSpan) { @Override public V call() throws Exception { - Context origContext = ContextUtils.withValue(Context.current(), span).attach(); + Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); try { return callable.call(); } catch (Exception e) { @@ -162,7 +161,7 @@ public V call() throws Exception { } throw new RuntimeException("unexpected", t); } finally { - Context.current().detach(origContext); + CtxUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java new file mode 100644 index 0000000000..91b494a734 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java @@ -0,0 +1,39 @@ +package io.opencensus.trace.unsafe; + +import io.grpc.Context; +import io.opencensus.trace.ContextManager; +import io.opencensus.trace.Ctx; +import io.opencensus.trace.Span; +import javax.annotation.Nullable; + +/** + * Default {@code ContextManager} implementation using {@see io.grpc.Context} + */ +public class ContextManagerImpl implements ContextManager { + + @Override + public Ctx currentContext() { + return wrapContext(Context.current()); + } + + @Override + public Ctx withValue(Ctx ctx, @Nullable Span span) { + return wrapContext(ContextUtils.withValue(unwrapContext(ctx), span)); + } + + @Override + public Span getValue(Ctx ctx) { + return ContextUtils.getValue(unwrapContext(ctx)); + } + + private static Ctx wrapContext(Context context) { + return new CtxImpl(context); + } + + private static Context unwrapContext(Ctx ctx) { + return ((CtxImpl)ctx).getContext(); + } + + protected ContextManagerImpl() { + } +} diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java index 1026512eec..46ce2856ee 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java @@ -33,7 +33,7 @@ * * @since 0.5 */ -public final class ContextUtils { +final class ContextUtils { // No instance of this class. private ContextUtils() {} diff --git a/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java new file mode 100644 index 0000000000..a10a863d2b --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java @@ -0,0 +1,30 @@ +package io.opencensus.trace.unsafe; + +import io.grpc.Context; +import io.opencensus.trace.Ctx; + +/** + * {@code Ctx} implementation using {@see io.grpc.Context} + */ +class CtxImpl implements Ctx { + private final Context context; + + public CtxImpl(Context context) { + this.context = context; + } + + Context getContext() { + return context; + } + + @Override + public Ctx attach() { + return new CtxImpl(context.attach()); + } + + @Override + public void detach(Ctx ctx) { + CtxImpl impl = (CtxImpl) ctx; + context.detach(impl.context); + } +} diff --git a/api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java b/api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java new file mode 100644 index 0000000000..4425523146 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java @@ -0,0 +1,46 @@ +package io.opencensus.trace.unsafe; + +import io.opencensus.trace.ContextManager; +import io.opencensus.trace.Ctx; +import io.opencensus.trace.Span; + +public class CtxUtils { + // No instance of this class. + private CtxUtils() {} + + private static final ContextManager DEFAULT_CONTEXT_MANAGER = new ContextManagerImpl(); + private static ContextManager contextManager = DEFAULT_CONTEXT_MANAGER; + + /** + * Overrides context manager with a custom implementation + * @param cm custom {@code ContextManager} to be used instead of a default one. + */ + public static void setContextManager(ContextManager cm) { + contextManager = cm; + } + + public static Ctx currentContext() { + return contextManager.currentContext(); + } + + /** + * Creates a new {@code Ctx} with the given value set. + * + * @param context the parent {@code Ctx}. + * @param span the value to be set. + * @return a new context with the given value set. + */ + public static Ctx withValue(Ctx context, @javax.annotation.Nullable Span span) { + return contextManager.withValue(context, span); + } + + /** + * Returns the value from the specified {@code Ctx}. + * + * @param context the specified {@code Ctx}. + * @return the value from the specified {@code Ctx}. + */ + public static Span getValue(Ctx context) { + return contextManager.getValue(context); + } +} diff --git a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java index 4cc6fccbc1..cd56f344ce 100644 --- a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java @@ -21,9 +21,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; -import io.grpc.Context; import io.opencensus.common.Scope; -import io.opencensus.trace.unsafe.ContextUtils; +import io.opencensus.trace.unsafe.CtxUtils; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; @@ -74,12 +73,12 @@ public void getCurrentSpan_WhenNoContext() { @Test public void getCurrentSpan() { assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); - Context origContext = ContextUtils.withValue(Context.current(), span).attach(); + Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); // Make sure context is detached even if test fails. try { assertThat(CurrentSpanUtils.getCurrentSpan()).isSameInstanceAs(span); } finally { - Context.current().detach(origContext); + CtxUtils.currentContext().detach(origContext); } assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); } diff --git a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java index 0ad9feba3d..53cb0d023a 100644 --- a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java @@ -18,8 +18,8 @@ import static com.google.common.truth.Truth.assertThat; -import io.grpc.Context; import io.opencensus.trace.BlankSpan; +import io.opencensus.trace.Ctx; import io.opencensus.trace.Span; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,19 +31,19 @@ public class ContextUtilsTest { @Test public void testGetCurrentSpan_DefaultContext() { - Span span = ContextUtils.getValue(Context.current()); + Span span = CtxUtils.getValue(CtxUtils.currentContext()); assertThat(span).isEqualTo(BlankSpan.INSTANCE); } @Test public void testGetCurrentSpan_ContextSetToNull() { - Context orig = ContextUtils.withValue(Context.current(), null).attach(); + Ctx orig = CtxUtils.withValue(CtxUtils.currentContext(), null).attach(); try { - Span span = ContextUtils.getValue(Context.current()); + Span span = CtxUtils.getValue(CtxUtils.currentContext()); // ContextUtils.getValue always returns non-null. assertThat(span).isEqualTo(BlankSpan.INSTANCE); } finally { - Context.current().detach(orig); + CtxUtils.currentContext().detach(orig); } } } diff --git a/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java b/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java index d8bc5819ab..d182f53492 100644 --- a/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java +++ b/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java @@ -16,10 +16,9 @@ package io.opencensus.contrib.logcorrelation.log4j2; -import io.grpc.Context; import io.opencensus.trace.Span; import io.opencensus.trace.SpanContext; -import io.opencensus.trace.unsafe.ContextUtils; +import io.opencensus.trace.unsafe.CtxUtils; import java.util.Collection; import java.util.List; import javax.annotation.Nullable; @@ -81,7 +80,7 @@ static StringMap getContextAndTracingData() { } private static SpanContext getCurrentSpanContext() { - Span span = ContextUtils.getValue(Context.current()); + Span span = CtxUtils.getValue(CtxUtils.currentContext()); return span == null ? SpanContext.INVALID : span.getContext(); } } diff --git a/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java b/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java index bc3f2b2451..186c14d552 100644 --- a/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java +++ b/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java @@ -19,11 +19,10 @@ import com.google.cloud.ServiceOptions; import com.google.cloud.logging.LogEntry; import com.google.cloud.logging.LoggingEnhancer; -import io.grpc.Context; import io.opencensus.trace.Span; import io.opencensus.trace.SpanContext; import io.opencensus.trace.TraceId; -import io.opencensus.trace.unsafe.ContextUtils; +import io.opencensus.trace.unsafe.CtxUtils; import java.util.logging.LogManager; import javax.annotation.Nullable; @@ -99,7 +98,7 @@ public void enhanceLogEntry(LogEntry.Builder builder) { } private static SpanContext getCurrentSpanContext() { - Span span = ContextUtils.getValue(Context.current()); + Span span = CtxUtils.getValue(CtxUtils.currentContext()); return span == null ? SpanContext.INVALID : span.getContext(); } diff --git a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java index 332058d02a..5b77470629 100644 --- a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java +++ b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java @@ -18,7 +18,8 @@ import io.grpc.Context; import io.opencensus.common.ExperimentalApi; -import io.opencensus.trace.unsafe.ContextUtils; +import io.opencensus.trace.Ctx; +import io.opencensus.trace.unsafe.CtxUtils; import org.apache.commons.logging.Log; import org.springframework.cloud.sleuth.Span; import org.springframework.core.NamedThreadLocal; @@ -136,14 +137,14 @@ private static class SpanContext { final boolean autoClose; @javax.annotation.Nullable final SpanContext parent; final OpenCensusSleuthSpan ocSpan; - final Context ocCurrentContext; + final Ctx ocCurrentContext; private SpanContext(Span span, boolean autoClose) { this.span = span; this.autoClose = autoClose; this.parent = CURRENT_SPAN.get(); this.ocSpan = new OpenCensusSleuthSpan(span); - this.ocCurrentContext = ContextUtils.withValue(Context.current(), this.ocSpan); + this.ocCurrentContext = CtxUtils.withValue(CtxUtils.currentContext(), this.ocSpan); } } From a293f8aa6a17d82e4df4f90bfd9a37c794f4ebe7 Mon Sep 17 00:00:00 2001 From: zoercai Date: Thu, 15 Oct 2020 10:50:33 +1100 Subject: [PATCH 2/8] Add reflection for OpenTelemetry trace component --- .../main/java/io/opencensus/trace/Tracing.java | 17 ++++++++++++++++- .../trace/unsafe/ContextManagerImpl.java | 4 ++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/io/opencensus/trace/Tracing.java b/api/src/main/java/io/opencensus/trace/Tracing.java index f55cd7754f..26ee0fee03 100644 --- a/api/src/main/java/io/opencensus/trace/Tracing.java +++ b/api/src/main/java/io/opencensus/trace/Tracing.java @@ -32,6 +32,7 @@ * @since 0.5 */ public final class Tracing { + private static final Logger logger = Logger.getLogger(Tracing.class.getName()); private static final TraceComponent traceComponent = loadTraceComponent(TraceComponent.class.getClassLoader()); @@ -89,6 +90,19 @@ public static TraceConfig getTraceConfig() { // Any provider that may be used for TraceComponent can be added here. @DefaultVisibilityForTesting static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) { + try { + // Call Class.forName with literal string name of the class to help shading tools. + return Provider.createInstance( + Class.forName( + "migration.OpenTelemetryTraceComponentImpl", /*initialize=*/ true, classLoader), + TraceComponent.class); + } catch (ClassNotFoundException e) { + logger.log( + Level.FINE, + "Couldn't load full implementation for OpenTelemetry TraceComponent, now trying to load original" + + "implementation.", + e); + } try { // Call Class.forName with literal string name of the class to help shading tools. return Provider.createInstance( @@ -121,5 +135,6 @@ static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) { } // No instance of this class. - private Tracing() {} + private Tracing() { + } } diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java index 91b494a734..5d37abd406 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java @@ -7,7 +7,7 @@ import javax.annotation.Nullable; /** - * Default {@code ContextManager} implementation using {@see io.grpc.Context} + * Default {@code ContextManager} implementation using io.grpc.Context */ public class ContextManagerImpl implements ContextManager { @@ -31,7 +31,7 @@ private static Ctx wrapContext(Context context) { } private static Context unwrapContext(Ctx ctx) { - return ((CtxImpl)ctx).getContext(); + return ((CtxImpl) ctx).getContext(); } protected ContextManagerImpl() { From e25717d406783059b01c826fef5cf4d3077f3cbc Mon Sep 17 00:00:00 2001 From: zoercai Date: Mon, 19 Oct 2020 21:54:02 +1100 Subject: [PATCH 3/8] Review changes & Fix checkstyle --- .../io/opencensus/trace/ContextHandle.java | 24 ++++++++++ .../io/opencensus/trace/ContextManager.java | 25 ++++++++-- .../main/java/io/opencensus/trace/Ctx.java | 6 --- .../io/opencensus/trace/CurrentSpanUtils.java | 25 ++++++---- .../java/io/opencensus/trace/Tracing.java | 11 +++-- .../trace/unsafe/ContextHandleImpl.java | 47 +++++++++++++++++++ ...{CtxUtils.java => ContextHandleUtils.java} | 33 ++++++++++--- .../trace/unsafe/ContextManagerImpl.java | 36 ++++++++++---- .../io/opencensus/trace/unsafe/CtxImpl.java | 30 ------------ .../trace/CurrentSpanUtilsTest.java | 6 +-- .../trace/unsafe/ContextUtilsTest.java | 10 ++-- contrib/log_correlation/log4j2/README.md | 2 +- .../log4j2/ContextDataUtils.java | 4 +- .../OpenCensusTraceLoggingEnhancer.java | 4 +- .../OpenCensusSleuthSpanContextHolder.java | 8 ++-- 15 files changed, 184 insertions(+), 87 deletions(-) create mode 100644 api/src/main/java/io/opencensus/trace/ContextHandle.java delete mode 100644 api/src/main/java/io/opencensus/trace/Ctx.java create mode 100644 api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java rename api/src/main/java/io/opencensus/trace/unsafe/{CtxUtils.java => ContextHandleUtils.java} (50%) delete mode 100644 api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java diff --git a/api/src/main/java/io/opencensus/trace/ContextHandle.java b/api/src/main/java/io/opencensus/trace/ContextHandle.java new file mode 100644 index 0000000000..985d261310 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/ContextHandle.java @@ -0,0 +1,24 @@ +/* + * Copyright 2016-17, OpenCensus 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.opencensus.trace; + +public interface ContextHandle { + + ContextHandle attach(); + + void detach(ContextHandle contextHandle); +} diff --git a/api/src/main/java/io/opencensus/trace/ContextManager.java b/api/src/main/java/io/opencensus/trace/ContextManager.java index 42f4c7c07d..c13ecc012e 100644 --- a/api/src/main/java/io/opencensus/trace/ContextManager.java +++ b/api/src/main/java/io/opencensus/trace/ContextManager.java @@ -1,7 +1,26 @@ +/* + * Copyright 2016-17, OpenCensus 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.opencensus.trace; public interface ContextManager { - Ctx currentContext(); - Ctx withValue(Ctx ctx, @javax.annotation.Nullable Span span); - Span getValue(Ctx ctx); + + ContextHandle currentContext(); + + ContextHandle withValue(ContextHandle contextHandle, @javax.annotation.Nullable Span span); + + Span getValue(ContextHandle contextHandle); } diff --git a/api/src/main/java/io/opencensus/trace/Ctx.java b/api/src/main/java/io/opencensus/trace/Ctx.java deleted file mode 100644 index 70f36dd742..0000000000 --- a/api/src/main/java/io/opencensus/trace/Ctx.java +++ /dev/null @@ -1,6 +0,0 @@ -package io.opencensus.trace; - -public interface Ctx { - Ctx attach(); - void detach(Ctx ctx); -} diff --git a/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java b/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java index a9df2c908e..c09c975ea5 100644 --- a/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java +++ b/api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java @@ -17,12 +17,13 @@ package io.opencensus.trace; import io.opencensus.common.Scope; -import io.opencensus.trace.unsafe.CtxUtils; +import io.opencensus.trace.unsafe.ContextHandleUtils; import java.util.concurrent.Callable; import javax.annotation.Nullable; /** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */ final class CurrentSpanUtils { + // No instance of this class. private CurrentSpanUtils() {} @@ -33,7 +34,7 @@ private CurrentSpanUtils() {} */ @Nullable static Span getCurrentSpan() { - return CtxUtils.getValue(CtxUtils.currentContext()); + return ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); } /** @@ -77,7 +78,8 @@ static Callable withSpan(Span span, boolean endSpan, Callable callable // Defines an arbitrary scope of code as a traceable operation. Supports try-with-resources idiom. private static final class ScopeInSpan implements Scope { - private final Ctx origContext; + + private final ContextHandle origContext; private final Span span; private final boolean endSpan; @@ -89,12 +91,13 @@ private static final class ScopeInSpan implements Scope { private ScopeInSpan(Span span, boolean endSpan) { this.span = span; this.endSpan = endSpan; - origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); + origContext = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); } @Override public void close() { - CtxUtils.currentContext().detach(origContext); + ContextHandleUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } @@ -102,6 +105,7 @@ public void close() { } private static final class RunnableInSpan implements Runnable { + // TODO(bdrutu): Investigate if the extra private visibility increases the generated bytecode. private final Span span; private final Runnable runnable; @@ -115,7 +119,8 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) { @Override public void run() { - Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); + ContextHandle origContext = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); try { runnable.run(); } catch (Throwable t) { @@ -127,7 +132,7 @@ public void run() { } throw new RuntimeException("unexpected", t); } finally { - CtxUtils.currentContext().detach(origContext); + ContextHandleUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } @@ -136,6 +141,7 @@ public void run() { } private static final class CallableInSpan implements Callable { + private final Span span; private final Callable callable; private final boolean endSpan; @@ -148,7 +154,8 @@ private CallableInSpan(Span span, Callable callable, boolean endSpan) { @Override public V call() throws Exception { - Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); + ContextHandle origContext = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); try { return callable.call(); } catch (Exception e) { @@ -161,7 +168,7 @@ public V call() throws Exception { } throw new RuntimeException("unexpected", t); } finally { - CtxUtils.currentContext().detach(origContext); + ContextHandleUtils.currentContext().detach(origContext); if (endSpan) { span.end(); } diff --git a/api/src/main/java/io/opencensus/trace/Tracing.java b/api/src/main/java/io/opencensus/trace/Tracing.java index 26ee0fee03..9de1530307 100644 --- a/api/src/main/java/io/opencensus/trace/Tracing.java +++ b/api/src/main/java/io/opencensus/trace/Tracing.java @@ -94,13 +94,15 @@ static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) { // Call Class.forName with literal string name of the class to help shading tools. return Provider.createInstance( Class.forName( - "migration.OpenTelemetryTraceComponentImpl", /*initialize=*/ true, classLoader), + "io.opentelemetry.opencensusshim.OpenTelemetryTraceComponentImpl", + /*initialize=*/ true, + classLoader), TraceComponent.class); } catch (ClassNotFoundException e) { logger.log( Level.FINE, - "Couldn't load full implementation for OpenTelemetry TraceComponent, now trying to load original" - + "implementation.", + "Couldn't load full implementation for OpenTelemetry TraceComponent, now trying to load " + + "original implementation.", e); } try { @@ -135,6 +137,5 @@ static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) { } // No instance of this class. - private Tracing() { - } + private Tracing() {} } diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java new file mode 100644 index 0000000000..f16848c111 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-17, OpenCensus 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.opencensus.trace.unsafe; + +import io.grpc.Context; +import io.opencensus.trace.ContextHandle; + +/** + * {@code Ctx} implementation using {@see io.grpc.Context}. + */ +class ContextHandleImpl implements ContextHandle { + + private final Context context; + + public ContextHandleImpl(Context context) { + this.context = context; + } + + Context getContext() { + return context; + } + + @Override + public ContextHandle attach() { + return new ContextHandleImpl(context.attach()); + } + + @Override + public void detach(ContextHandle contextHandle) { + ContextHandleImpl impl = (ContextHandleImpl) contextHandle; + context.detach(impl.context); + } +} diff --git a/api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java similarity index 50% rename from api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java rename to api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java index 4425523146..1dc8d41011 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/CtxUtils.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java @@ -1,25 +1,43 @@ +/* + * Copyright 2016-17, OpenCensus 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.opencensus.trace.unsafe; +import io.opencensus.trace.ContextHandle; import io.opencensus.trace.ContextManager; -import io.opencensus.trace.Ctx; import io.opencensus.trace.Span; -public class CtxUtils { +public class ContextHandleUtils { + // No instance of this class. - private CtxUtils() {} + private ContextHandleUtils() {} private static final ContextManager DEFAULT_CONTEXT_MANAGER = new ContextManagerImpl(); private static ContextManager contextManager = DEFAULT_CONTEXT_MANAGER; /** - * Overrides context manager with a custom implementation + * Overrides context manager with a custom implementation. + * * @param cm custom {@code ContextManager} to be used instead of a default one. */ public static void setContextManager(ContextManager cm) { contextManager = cm; } - public static Ctx currentContext() { + public static ContextHandle currentContext() { return contextManager.currentContext(); } @@ -30,7 +48,8 @@ public static Ctx currentContext() { * @param span the value to be set. * @return a new context with the given value set. */ - public static Ctx withValue(Ctx context, @javax.annotation.Nullable Span span) { + public static ContextHandle withValue( + ContextHandle context, @javax.annotation.Nullable Span span) { return contextManager.withValue(context, span); } @@ -40,7 +59,7 @@ public static Ctx withValue(Ctx context, @javax.annotation.Nullable Span span) { * @param context the specified {@code Ctx}. * @return the value from the specified {@code Ctx}. */ - public static Span getValue(Ctx context) { + public static Span getValue(ContextHandle context) { return contextManager.getValue(context); } } diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java index 5d37abd406..c307f67c67 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java @@ -1,8 +1,24 @@ +/* + * Copyright 2016-17, OpenCensus 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.opencensus.trace.unsafe; import io.grpc.Context; +import io.opencensus.trace.ContextHandle; import io.opencensus.trace.ContextManager; -import io.opencensus.trace.Ctx; import io.opencensus.trace.Span; import javax.annotation.Nullable; @@ -12,26 +28,26 @@ public class ContextManagerImpl implements ContextManager { @Override - public Ctx currentContext() { + public ContextHandle currentContext() { return wrapContext(Context.current()); } @Override - public Ctx withValue(Ctx ctx, @Nullable Span span) { - return wrapContext(ContextUtils.withValue(unwrapContext(ctx), span)); + public ContextHandle withValue(ContextHandle contextHandle, @Nullable Span span) { + return wrapContext(ContextUtils.withValue(unwrapContext(contextHandle), span)); } @Override - public Span getValue(Ctx ctx) { - return ContextUtils.getValue(unwrapContext(ctx)); + public Span getValue(ContextHandle contextHandle) { + return ContextUtils.getValue(unwrapContext(contextHandle)); } - private static Ctx wrapContext(Context context) { - return new CtxImpl(context); + private static ContextHandle wrapContext(Context context) { + return new ContextHandleImpl(context); } - private static Context unwrapContext(Ctx ctx) { - return ((CtxImpl) ctx).getContext(); + private static Context unwrapContext(ContextHandle contextHandle) { + return ((ContextHandleImpl) contextHandle).getContext(); } protected ContextManagerImpl() { diff --git a/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java deleted file mode 100644 index a10a863d2b..0000000000 --- a/api/src/main/java/io/opencensus/trace/unsafe/CtxImpl.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.opencensus.trace.unsafe; - -import io.grpc.Context; -import io.opencensus.trace.Ctx; - -/** - * {@code Ctx} implementation using {@see io.grpc.Context} - */ -class CtxImpl implements Ctx { - private final Context context; - - public CtxImpl(Context context) { - this.context = context; - } - - Context getContext() { - return context; - } - - @Override - public Ctx attach() { - return new CtxImpl(context.attach()); - } - - @Override - public void detach(Ctx ctx) { - CtxImpl impl = (CtxImpl) ctx; - context.detach(impl.context); - } -} diff --git a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java index cd56f344ce..34a0d24667 100644 --- a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; import io.opencensus.common.Scope; -import io.opencensus.trace.unsafe.CtxUtils; +import io.opencensus.trace.unsafe.ContextHandleUtils; import java.util.concurrent.Callable; import org.junit.Before; import org.junit.Test; @@ -73,12 +73,12 @@ public void getCurrentSpan_WhenNoContext() { @Test public void getCurrentSpan() { assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); - Ctx origContext = CtxUtils.withValue(CtxUtils.currentContext(), span).attach(); + ContextHandle origContext = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); // Make sure context is detached even if test fails. try { assertThat(CurrentSpanUtils.getCurrentSpan()).isSameInstanceAs(span); } finally { - CtxUtils.currentContext().detach(origContext); + ContextHandleUtils.currentContext().detach(origContext); } assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); } diff --git a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java index 53cb0d023a..3059e00b10 100644 --- a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java @@ -19,7 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import io.opencensus.trace.BlankSpan; -import io.opencensus.trace.Ctx; +import io.opencensus.trace.ContextHandle; import io.opencensus.trace.Span; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,19 +31,19 @@ public class ContextUtilsTest { @Test public void testGetCurrentSpan_DefaultContext() { - Span span = CtxUtils.getValue(CtxUtils.currentContext()); + Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); assertThat(span).isEqualTo(BlankSpan.INSTANCE); } @Test public void testGetCurrentSpan_ContextSetToNull() { - Ctx orig = CtxUtils.withValue(CtxUtils.currentContext(), null).attach(); + ContextHandle orig = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), null).attach(); try { - Span span = CtxUtils.getValue(CtxUtils.currentContext()); + Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); // ContextUtils.getValue always returns non-null. assertThat(span).isEqualTo(BlankSpan.INSTANCE); } finally { - CtxUtils.currentContext().detach(orig); + ContextHandleUtils.currentContext().detach(orig); } } } diff --git a/contrib/log_correlation/log4j2/README.md b/contrib/log_correlation/log4j2/README.md index 2827950d02..a528584b72 100644 --- a/contrib/log_correlation/log4j2/README.md +++ b/contrib/log_correlation/log4j2/README.md @@ -55,7 +55,7 @@ context: These values can be accessed from layouts with [Context Map Lookup](http://logging.apache.org/log4j/2.x/manual/lookups.html#ContextMapLookup). For -example, the trace ID can be accessed with `$${ctx:traceId}`. The values can also be accessed with +example, the trace ID can be accessed with `$${contextHandle:traceId}`. The values can also be accessed with the `X` conversion character in [`PatternLayout`](http://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout), for example, `%X{traceId}`. diff --git a/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java b/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java index d182f53492..4fb052073d 100644 --- a/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java +++ b/contrib/log_correlation/log4j2/src/main/java/io/opencensus/contrib/logcorrelation/log4j2/ContextDataUtils.java @@ -18,7 +18,7 @@ import io.opencensus.trace.Span; import io.opencensus.trace.SpanContext; -import io.opencensus.trace.unsafe.CtxUtils; +import io.opencensus.trace.unsafe.ContextHandleUtils; import java.util.Collection; import java.util.List; import javax.annotation.Nullable; @@ -80,7 +80,7 @@ static StringMap getContextAndTracingData() { } private static SpanContext getCurrentSpanContext() { - Span span = CtxUtils.getValue(CtxUtils.currentContext()); + Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); return span == null ? SpanContext.INVALID : span.getContext(); } } diff --git a/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java b/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java index 186c14d552..95f72d0ae3 100644 --- a/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java +++ b/contrib/log_correlation/stackdriver/src/main/java/io/opencensus/contrib/logcorrelation/stackdriver/OpenCensusTraceLoggingEnhancer.java @@ -22,7 +22,7 @@ import io.opencensus.trace.Span; import io.opencensus.trace.SpanContext; import io.opencensus.trace.TraceId; -import io.opencensus.trace.unsafe.CtxUtils; +import io.opencensus.trace.unsafe.ContextHandleUtils; import java.util.logging.LogManager; import javax.annotation.Nullable; @@ -98,7 +98,7 @@ public void enhanceLogEntry(LogEntry.Builder builder) { } private static SpanContext getCurrentSpanContext() { - Span span = CtxUtils.getValue(CtxUtils.currentContext()); + Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); return span == null ? SpanContext.INVALID : span.getContext(); } diff --git a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java index 5b77470629..3ed9a855c7 100644 --- a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java +++ b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java @@ -18,8 +18,8 @@ import io.grpc.Context; import io.opencensus.common.ExperimentalApi; -import io.opencensus.trace.Ctx; -import io.opencensus.trace.unsafe.CtxUtils; +import io.opencensus.trace.ContextHandle; +import io.opencensus.trace.unsafe.ContextHandleUtils; import org.apache.commons.logging.Log; import org.springframework.cloud.sleuth.Span; import org.springframework.core.NamedThreadLocal; @@ -137,14 +137,14 @@ private static class SpanContext { final boolean autoClose; @javax.annotation.Nullable final SpanContext parent; final OpenCensusSleuthSpan ocSpan; - final Ctx ocCurrentContext; + final ContextHandle ocCurrentContext; private SpanContext(Span span, boolean autoClose) { this.span = span; this.autoClose = autoClose; this.parent = CURRENT_SPAN.get(); this.ocSpan = new OpenCensusSleuthSpan(span); - this.ocCurrentContext = CtxUtils.withValue(CtxUtils.currentContext(), this.ocSpan); + this.ocCurrentContext = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), this.ocSpan); } } From 4dce999d6746ecaec04319a7f6a4f3acea5f198a Mon Sep 17 00:00:00 2001 From: zoercai Date: Mon, 19 Oct 2020 22:09:33 +1100 Subject: [PATCH 4/8] Fix test checkstyle --- .../test/java/io/opencensus/trace/CurrentSpanUtilsTest.java | 3 ++- .../test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java index 34a0d24667..d186197e82 100644 --- a/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/CurrentSpanUtilsTest.java @@ -73,7 +73,8 @@ public void getCurrentSpan_WhenNoContext() { @Test public void getCurrentSpan() { assertThat(CurrentSpanUtils.getCurrentSpan()).isEqualTo(BlankSpan.INSTANCE); - ContextHandle origContext = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); + ContextHandle origContext = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach(); // Make sure context is detached even if test fails. try { assertThat(CurrentSpanUtils.getCurrentSpan()).isSameInstanceAs(span); diff --git a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java index 3059e00b10..a9da128fea 100644 --- a/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java +++ b/api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java @@ -37,7 +37,8 @@ public void testGetCurrentSpan_DefaultContext() { @Test public void testGetCurrentSpan_ContextSetToNull() { - ContextHandle orig = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), null).attach(); + ContextHandle orig = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), null).attach(); try { Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext()); // ContextUtils.getValue always returns non-null. From 55cfe19cb69db81773a8a758367097014bc7fa9d Mon Sep 17 00:00:00 2001 From: zoercai Date: Tue, 20 Oct 2020 15:52:33 +1100 Subject: [PATCH 5/8] Fix verify google java format --- .../java/io/opencensus/trace/unsafe/ContextHandleImpl.java | 4 +--- .../io/opencensus/trace/unsafe/ContextManagerImpl.java | 7 ++----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java index f16848c111..6ba88a819b 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java @@ -19,9 +19,7 @@ import io.grpc.Context; import io.opencensus.trace.ContextHandle; -/** - * {@code Ctx} implementation using {@see io.grpc.Context}. - */ +/** {@code Ctx} implementation using {@see io.grpc.Context}. */ class ContextHandleImpl implements ContextHandle { private final Context context; diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java index c307f67c67..f46d6e0e6f 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java @@ -22,9 +22,7 @@ import io.opencensus.trace.Span; import javax.annotation.Nullable; -/** - * Default {@code ContextManager} implementation using io.grpc.Context - */ +/** Default {@code ContextManager} implementation using io.grpc.Context */ public class ContextManagerImpl implements ContextManager { @Override @@ -50,6 +48,5 @@ private static Context unwrapContext(ContextHandle contextHandle) { return ((ContextHandleImpl) contextHandle).getContext(); } - protected ContextManagerImpl() { - } + protected ContextManagerImpl() {} } From 5e8df764d80d95e881ae4378140b41436491de87 Mon Sep 17 00:00:00 2001 From: zoercai Date: Tue, 20 Oct 2020 16:11:02 +1100 Subject: [PATCH 6/8] Fix more verify google java format --- .../spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java index 3ed9a855c7..37bf2360f2 100644 --- a/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java +++ b/contrib/spring_sleuth_v1x/src/main/java/io/opencensus/contrib/spring/sleuth/v1x/OpenCensusSleuthSpanContextHolder.java @@ -144,7 +144,8 @@ private SpanContext(Span span, boolean autoClose) { this.autoClose = autoClose; this.parent = CURRENT_SPAN.get(); this.ocSpan = new OpenCensusSleuthSpan(span); - this.ocCurrentContext = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), this.ocSpan); + this.ocCurrentContext = + ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), this.ocSpan); } } From 0b78da616be6aa033877c47e9e7d80dd4ae59ed2 Mon Sep 17 00:00:00 2001 From: zoercai Date: Tue, 20 Oct 2020 16:44:09 +1100 Subject: [PATCH 7/8] Allow migration library context manager to be loaded via reflection automatically --- .../trace/unsafe/ContextHandleUtils.java | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java index 1dc8d41011..65405517cb 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java @@ -16,29 +16,43 @@ package io.opencensus.trace.unsafe; +import io.opencensus.internal.Provider; import io.opencensus.trace.ContextHandle; import io.opencensus.trace.ContextManager; import io.opencensus.trace.Span; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.Nullable; public class ContextHandleUtils { // No instance of this class. private ContextHandleUtils() {} - private static final ContextManager DEFAULT_CONTEXT_MANAGER = new ContextManagerImpl(); - private static ContextManager contextManager = DEFAULT_CONTEXT_MANAGER; + private static final Logger LOGGER = Logger.getLogger(ContextHandleUtils.class.getName()); + private static final ContextManager CONTEXT_MANAGER = + loadContextManager(ContextManager.class.getClassLoader()); - /** - * Overrides context manager with a custom implementation. - * - * @param cm custom {@code ContextManager} to be used instead of a default one. - */ - public static void setContextManager(ContextManager cm) { - contextManager = cm; + private static ContextManager loadContextManager(@Nullable ClassLoader classLoader) { + try { + return Provider.createInstance( + Class.forName( + "io.opentelemetry.opencensusshim.OpenTelemetryContextManager", + /*initialize=*/ true, + classLoader), + ContextManager.class); + } catch (ClassNotFoundException e) { + LOGGER.log( + Level.FINE, + "Couldn't load full implementation for OpenTelemetry context manager, now loading " + + "original implementation.", + e); + } + return new ContextManagerImpl(); } public static ContextHandle currentContext() { - return contextManager.currentContext(); + return CONTEXT_MANAGER.currentContext(); } /** @@ -50,7 +64,7 @@ public static ContextHandle currentContext() { */ public static ContextHandle withValue( ContextHandle context, @javax.annotation.Nullable Span span) { - return contextManager.withValue(context, span); + return CONTEXT_MANAGER.withValue(context, span); } /** @@ -60,6 +74,6 @@ public static ContextHandle withValue( * @return the value from the specified {@code Ctx}. */ public static Span getValue(ContextHandle context) { - return contextManager.getValue(context); + return CONTEXT_MANAGER.getValue(context); } } From cfd0ab2a398d09a50d34cf0f436d677c4d462b06 Mon Sep 17 00:00:00 2001 From: zoercai Date: Wed, 21 Oct 2020 09:07:29 +1100 Subject: [PATCH 8/8] Review changes --- .../io/opencensus/trace/unsafe/ContextHandleImpl.java | 2 +- .../io/opencensus/trace/unsafe/ContextHandleUtils.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java index 6ba88a819b..89b733dcb7 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleImpl.java @@ -19,7 +19,7 @@ import io.grpc.Context; import io.opencensus.trace.ContextHandle; -/** {@code Ctx} implementation using {@see io.grpc.Context}. */ +/** {@code ContextHandle} implementation using {@see io.grpc.Context}. */ class ContextHandleImpl implements ContextHandle { private final Context context; diff --git a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java index 65405517cb..3d14b9460a 100644 --- a/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java +++ b/api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java @@ -56,9 +56,9 @@ public static ContextHandle currentContext() { } /** - * Creates a new {@code Ctx} with the given value set. + * Creates a new {@code ContextHandle} with the given value set. * - * @param context the parent {@code Ctx}. + * @param context the parent {@code ContextHandle}. * @param span the value to be set. * @return a new context with the given value set. */ @@ -68,10 +68,10 @@ public static ContextHandle withValue( } /** - * Returns the value from the specified {@code Ctx}. + * Returns the value from the specified {@code ContextHandle}. * - * @param context the specified {@code Ctx}. - * @return the value from the specified {@code Ctx}. + * @param context the specified {@code ContextHandle}. + * @return the value from the specified {@code ContextHandle}. */ public static Span getValue(ContextHandle context) { return CONTEXT_MANAGER.getValue(context);