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

Commit f9f8ce7

Browse files
committed
Merge branch 'master' of github.com:census-instrumentation/opencensus-java into v0.28.x
2 parents b45afba + f8ea9bb commit f9f8ce7

File tree

17 files changed

+305
-42
lines changed

17 files changed

+305
-42
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2016-17, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace;
18+
19+
public interface ContextHandle {
20+
21+
ContextHandle attach();
22+
23+
void detach(ContextHandle contextHandle);
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016-17, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace;
18+
19+
public interface ContextManager {
20+
21+
ContextHandle currentContext();
22+
23+
ContextHandle withValue(ContextHandle contextHandle, @javax.annotation.Nullable Span span);
24+
25+
Span getValue(ContextHandle contextHandle);
26+
}

api/src/main/java/io/opencensus/trace/CurrentSpanUtils.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
package io.opencensus.trace;
1818

19-
import io.grpc.Context;
2019
import io.opencensus.common.Scope;
21-
import io.opencensus.trace.unsafe.ContextUtils;
20+
import io.opencensus.trace.unsafe.ContextHandleUtils;
2221
import java.util.concurrent.Callable;
2322
import javax.annotation.Nullable;
2423

2524
/** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */
2625
final class CurrentSpanUtils {
26+
2727
// No instance of this class.
2828
private CurrentSpanUtils() {}
2929

@@ -34,7 +34,7 @@ private CurrentSpanUtils() {}
3434
*/
3535
@Nullable
3636
static Span getCurrentSpan() {
37-
return ContextUtils.getValue(Context.current());
37+
return ContextHandleUtils.getValue(ContextHandleUtils.currentContext());
3838
}
3939

4040
/**
@@ -78,7 +78,8 @@ static <C> Callable<C> withSpan(Span span, boolean endSpan, Callable<C> callable
7878

7979
// Defines an arbitrary scope of code as a traceable operation. Supports try-with-resources idiom.
8080
private static final class ScopeInSpan implements Scope {
81-
private final Context origContext;
81+
82+
private final ContextHandle origContext;
8283
private final Span span;
8384
private final boolean endSpan;
8485

@@ -90,19 +91,21 @@ private static final class ScopeInSpan implements Scope {
9091
private ScopeInSpan(Span span, boolean endSpan) {
9192
this.span = span;
9293
this.endSpan = endSpan;
93-
origContext = ContextUtils.withValue(Context.current(), span).attach();
94+
origContext =
95+
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
9496
}
9597

9698
@Override
9799
public void close() {
98-
Context.current().detach(origContext);
100+
ContextHandleUtils.currentContext().detach(origContext);
99101
if (endSpan) {
100102
span.end();
101103
}
102104
}
103105
}
104106

105107
private static final class RunnableInSpan implements Runnable {
108+
106109
// TODO(bdrutu): Investigate if the extra private visibility increases the generated bytecode.
107110
private final Span span;
108111
private final Runnable runnable;
@@ -116,7 +119,8 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) {
116119

117120
@Override
118121
public void run() {
119-
Context origContext = ContextUtils.withValue(Context.current(), span).attach();
122+
ContextHandle origContext =
123+
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
120124
try {
121125
runnable.run();
122126
} catch (Throwable t) {
@@ -128,7 +132,7 @@ public void run() {
128132
}
129133
throw new RuntimeException("unexpected", t);
130134
} finally {
131-
Context.current().detach(origContext);
135+
ContextHandleUtils.currentContext().detach(origContext);
132136
if (endSpan) {
133137
span.end();
134138
}
@@ -137,6 +141,7 @@ public void run() {
137141
}
138142

139143
private static final class CallableInSpan<V> implements Callable<V> {
144+
140145
private final Span span;
141146
private final Callable<V> callable;
142147
private final boolean endSpan;
@@ -149,7 +154,8 @@ private CallableInSpan(Span span, Callable<V> callable, boolean endSpan) {
149154

150155
@Override
151156
public V call() throws Exception {
152-
Context origContext = ContextUtils.withValue(Context.current(), span).attach();
157+
ContextHandle origContext =
158+
ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), span).attach();
153159
try {
154160
return callable.call();
155161
} catch (Exception e) {
@@ -162,7 +168,7 @@ public V call() throws Exception {
162168
}
163169
throw new RuntimeException("unexpected", t);
164170
} finally {
165-
Context.current().detach(origContext);
171+
ContextHandleUtils.currentContext().detach(origContext);
166172
if (endSpan) {
167173
span.end();
168174
}

api/src/main/java/io/opencensus/trace/Tracing.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @since 0.5
3333
*/
3434
public final class Tracing {
35+
3536
private static final Logger logger = Logger.getLogger(Tracing.class.getName());
3637
private static final TraceComponent traceComponent =
3738
loadTraceComponent(TraceComponent.class.getClassLoader());
@@ -89,6 +90,21 @@ public static TraceConfig getTraceConfig() {
8990
// Any provider that may be used for TraceComponent can be added here.
9091
@DefaultVisibilityForTesting
9192
static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) {
93+
try {
94+
// Call Class.forName with literal string name of the class to help shading tools.
95+
return Provider.createInstance(
96+
Class.forName(
97+
"io.opentelemetry.opencensusshim.OpenTelemetryTraceComponentImpl",
98+
/*initialize=*/ true,
99+
classLoader),
100+
TraceComponent.class);
101+
} catch (ClassNotFoundException e) {
102+
logger.log(
103+
Level.FINE,
104+
"Couldn't load full implementation for OpenTelemetry TraceComponent, now trying to load "
105+
+ "original implementation.",
106+
e);
107+
}
92108
try {
93109
// Call Class.forName with literal string name of the class to help shading tools.
94110
return Provider.createInstance(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016-17, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace.unsafe;
18+
19+
import io.grpc.Context;
20+
import io.opencensus.trace.ContextHandle;
21+
22+
/** {@code ContextHandle} implementation using {@see io.grpc.Context}. */
23+
class ContextHandleImpl implements ContextHandle {
24+
25+
private final Context context;
26+
27+
public ContextHandleImpl(Context context) {
28+
this.context = context;
29+
}
30+
31+
Context getContext() {
32+
return context;
33+
}
34+
35+
@Override
36+
public ContextHandle attach() {
37+
return new ContextHandleImpl(context.attach());
38+
}
39+
40+
@Override
41+
public void detach(ContextHandle contextHandle) {
42+
ContextHandleImpl impl = (ContextHandleImpl) contextHandle;
43+
context.detach(impl.context);
44+
}
45+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2016-17, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace.unsafe;
18+
19+
import io.opencensus.internal.Provider;
20+
import io.opencensus.trace.ContextHandle;
21+
import io.opencensus.trace.ContextManager;
22+
import io.opencensus.trace.Span;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import javax.annotation.Nullable;
26+
27+
public class ContextHandleUtils {
28+
29+
// No instance of this class.
30+
private ContextHandleUtils() {}
31+
32+
private static final Logger LOGGER = Logger.getLogger(ContextHandleUtils.class.getName());
33+
private static final ContextManager CONTEXT_MANAGER =
34+
loadContextManager(ContextManager.class.getClassLoader());
35+
36+
private static ContextManager loadContextManager(@Nullable ClassLoader classLoader) {
37+
try {
38+
return Provider.createInstance(
39+
Class.forName(
40+
"io.opentelemetry.opencensusshim.OpenTelemetryContextManager",
41+
/*initialize=*/ true,
42+
classLoader),
43+
ContextManager.class);
44+
} catch (ClassNotFoundException e) {
45+
LOGGER.log(
46+
Level.FINE,
47+
"Couldn't load full implementation for OpenTelemetry context manager, now loading "
48+
+ "original implementation.",
49+
e);
50+
}
51+
return new ContextManagerImpl();
52+
}
53+
54+
public static ContextHandle currentContext() {
55+
return CONTEXT_MANAGER.currentContext();
56+
}
57+
58+
/**
59+
* Creates a new {@code ContextHandle} with the given value set.
60+
*
61+
* @param context the parent {@code ContextHandle}.
62+
* @param span the value to be set.
63+
* @return a new context with the given value set.
64+
*/
65+
public static ContextHandle withValue(
66+
ContextHandle context, @javax.annotation.Nullable Span span) {
67+
return CONTEXT_MANAGER.withValue(context, span);
68+
}
69+
70+
/**
71+
* Returns the value from the specified {@code ContextHandle}.
72+
*
73+
* @param context the specified {@code ContextHandle}.
74+
* @return the value from the specified {@code ContextHandle}.
75+
*/
76+
public static Span getValue(ContextHandle context) {
77+
return CONTEXT_MANAGER.getValue(context);
78+
}
79+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2016-17, OpenCensus Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opencensus.trace.unsafe;
18+
19+
import io.grpc.Context;
20+
import io.opencensus.trace.ContextHandle;
21+
import io.opencensus.trace.ContextManager;
22+
import io.opencensus.trace.Span;
23+
import javax.annotation.Nullable;
24+
25+
/** Default {@code ContextManager} implementation using io.grpc.Context */
26+
public class ContextManagerImpl implements ContextManager {
27+
28+
@Override
29+
public ContextHandle currentContext() {
30+
return wrapContext(Context.current());
31+
}
32+
33+
@Override
34+
public ContextHandle withValue(ContextHandle contextHandle, @Nullable Span span) {
35+
return wrapContext(ContextUtils.withValue(unwrapContext(contextHandle), span));
36+
}
37+
38+
@Override
39+
public Span getValue(ContextHandle contextHandle) {
40+
return ContextUtils.getValue(unwrapContext(contextHandle));
41+
}
42+
43+
private static ContextHandle wrapContext(Context context) {
44+
return new ContextHandleImpl(context);
45+
}
46+
47+
private static Context unwrapContext(ContextHandle contextHandle) {
48+
return ((ContextHandleImpl) contextHandle).getContext();
49+
}
50+
51+
protected ContextManagerImpl() {}
52+
}

api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*
3434
* @since 0.5
3535
*/
36-
public final class ContextUtils {
36+
final class ContextUtils {
3737
// No instance of this class.
3838
private ContextUtils() {}
3939

0 commit comments

Comments
 (0)