Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Add Support for Opencensus to OpenTelemetry migration #2059

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions 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);
}
26 changes: 26 additions & 0 deletions api/src/main/java/io/opencensus/trace/ContextManager.java
@@ -0,0 +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 {

ContextHandle currentContext();

ContextHandle withValue(ContextHandle contextHandle, @javax.annotation.Nullable Span span);

Span getValue(ContextHandle contextHandle);
}
26 changes: 16 additions & 10 deletions api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java
Expand Up @@ -16,14 +16,14 @@

package io.opencensus.trace;

import io.grpc.Context;
import io.opencensus.common.Scope;
import io.opencensus.trace.unsafe.ContextUtils;
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() {}

Expand All @@ -34,7 +34,7 @@ private CurrentSpanUtils() {}
*/
@Nullable
static Span getCurrentSpan() {
return ContextUtils.getValue(Context.current());
return ContextHandleUtils.getValue(ContextHandleUtils.currentContext());
}

/**
Expand Down Expand Up @@ -78,7 +78,8 @@ static <C> Callable<C> withSpan(Span span, boolean endSpan, Callable<C> 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 ContextHandle origContext;
private final Span span;
private final boolean endSpan;

Expand All @@ -90,19 +91,21 @@ 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 =
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
}

@Override
public void close() {
Context.current().detach(origContext);
ContextHandleUtils.currentContext().detach(origContext);
if (endSpan) {
span.end();
}
}
}

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;
Expand All @@ -116,7 +119,8 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) {

@Override
public void run() {
Context origContext = ContextUtils.withValue(Context.current(), span).attach();
ContextHandle origContext =
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
try {
runnable.run();
} catch (Throwable t) {
Expand All @@ -128,7 +132,7 @@ public void run() {
}
throw new RuntimeException("unexpected", t);
} finally {
Context.current().detach(origContext);
ContextHandleUtils.currentContext().detach(origContext);
if (endSpan) {
span.end();
}
Expand All @@ -137,6 +141,7 @@ public void run() {
}

private static final class CallableInSpan<V> implements Callable<V> {

private final Span span;
private final Callable<V> callable;
private final boolean endSpan;
Expand All @@ -149,7 +154,8 @@ private CallableInSpan(Span span, Callable<V> callable, boolean endSpan) {

@Override
public V call() throws Exception {
Context origContext = ContextUtils.withValue(Context.current(), span).attach();
ContextHandle origContext =
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
try {
return callable.call();
} catch (Exception e) {
Expand All @@ -162,7 +168,7 @@ public V call() throws Exception {
}
throw new RuntimeException("unexpected", t);
} finally {
Context.current().detach(origContext);
ContextHandleUtils.currentContext().detach(origContext);
if (endSpan) {
span.end();
}
Expand Down
16 changes: 16 additions & 0 deletions api/src/main/java/io/opencensus/trace/Tracing.java
Expand Up @@ -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());
Expand Down Expand Up @@ -89,6 +90,21 @@ 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(
"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.",
e);
}
try {
// Call Class.forName with literal string name of the class to help shading tools.
return Provider.createInstance(
Expand Down
@@ -0,0 +1,45 @@
/*
* 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 ContextHandle} 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);
}
}
@@ -0,0 +1,79 @@
/*
* 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.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 Logger LOGGER = Logger.getLogger(ContextHandleUtils.class.getName());
private static final ContextManager CONTEXT_MANAGER =
loadContextManager(ContextManager.class.getClassLoader());

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();
}
zoercai marked this conversation as resolved.
Show resolved Hide resolved

public static ContextHandle currentContext() {
return CONTEXT_MANAGER.currentContext();
}

/**
* Creates a new {@code ContextHandle} with the given value set.
*
* @param context the parent {@code ContextHandle}.
* @param span the value to be set.
* @return a new context with the given value set.
*/
public static ContextHandle withValue(
ContextHandle context, @javax.annotation.Nullable Span span) {
return CONTEXT_MANAGER.withValue(context, span);
}

/**
* Returns the value from the specified {@code ContextHandle}.
*
* @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);
}
}
@@ -0,0 +1,52 @@
/*
* 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.Span;
import javax.annotation.Nullable;

/** Default {@code ContextManager} implementation using io.grpc.Context */
public class ContextManagerImpl implements ContextManager {

@Override
public ContextHandle currentContext() {
return wrapContext(Context.current());
}

@Override
public ContextHandle withValue(ContextHandle contextHandle, @Nullable Span span) {
return wrapContext(ContextUtils.withValue(unwrapContext(contextHandle), span));
}

@Override
public Span getValue(ContextHandle contextHandle) {
return ContextUtils.getValue(unwrapContext(contextHandle));
}

private static ContextHandle wrapContext(Context context) {
return new ContextHandleImpl(context);
}

private static Context unwrapContext(ContextHandle contextHandle) {
return ((ContextHandleImpl) contextHandle).getContext();
}

protected ContextManagerImpl() {}
}
Expand Up @@ -33,7 +33,7 @@
*
* @since 0.5
*/
public final class ContextUtils {
final class ContextUtils {
// No instance of this class.
private ContextUtils() {}

Expand Down