This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a ActiveSpanSource backed TraceContext (#266)
- use `ActiveSpanSource` for propagation instead of `ThreadLocalTraceContext`.
- Loading branch information
Showing
17 changed files
with
291 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
jaeger-context/src/main/java/com/uber/jaeger/context/ActiveSpanSourceTraceContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* 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 com.uber.jaeger.context; | ||
|
||
import io.opentracing.ActiveSpan; | ||
import io.opentracing.ActiveSpanSource; | ||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.util.GlobalTracer; | ||
import io.opentracing.util.ThreadLocalActiveSpan; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* This is a {@link TraceContext} that relies on the {@link ActiveSpanSource} registered with {@link | ||
* GlobalTracer}. | ||
*/ | ||
public class ActiveSpanSourceTraceContext implements TraceContext { | ||
|
||
private final ActiveSpanSource activeSpanSource; | ||
/** | ||
* This is a hack to retrieve the span wrapped by the {@link ThreadLocalActiveSpan} implementation | ||
* to shoehorn into the {@link TraceContext} implementation. This is being done so that | ||
* instrumentation relying on {@link Tracer} is consistent with instrumentation using {@link | ||
* TraceContext}. We expect to remove this when opentracing-api version 0.31 is released. | ||
*/ | ||
private static final Field wrappedSpan; | ||
|
||
static { | ||
try { | ||
wrappedSpan = ThreadLocalActiveSpan.class.getDeclaredField("wrapped"); | ||
wrappedSpan.setAccessible(true); | ||
} catch (NoSuchFieldException e) { | ||
throw new RuntimeException("Unable to access ThreadLocalActiveSpan.wrapped reflectively.", e); | ||
} | ||
} | ||
|
||
public ActiveSpanSourceTraceContext(ActiveSpanSource activeSpanSource) { | ||
this.activeSpanSource = activeSpanSource; | ||
} | ||
|
||
/** Makes the span active. */ | ||
@Override | ||
public void push(Span span) { | ||
activeSpanSource.makeActive(span); | ||
} | ||
|
||
/** Deactivates the current active span. */ | ||
@Override | ||
public Span pop() { | ||
ActiveSpan activeSpan = activeSpanSource.activeSpan(); | ||
Span span = getSpan(activeSpan); | ||
activeSpan.deactivate(); | ||
return span; | ||
} | ||
|
||
/** Retrieves the current active span. */ | ||
@Override | ||
public Span getCurrentSpan() { | ||
ActiveSpan activeSpan = activeSpanSource.activeSpan(); | ||
return getSpan(activeSpan); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return activeSpanSource.activeSpan() == null; | ||
} | ||
|
||
private Span getSpan(ActiveSpan activeSpan) { | ||
Span span; | ||
try { | ||
span = (Span) wrappedSpan.get(activeSpan); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return span; | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
jaeger-context/src/main/java/com/uber/jaeger/context/ThreadLocalTraceContext.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
jaeger-context/src/test/java/com/uber/jaeger/context/ActiveSpanSourceTraceContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2017, The Jaeger 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 com.uber.jaeger.context; | ||
|
||
import io.opentracing.ActiveSpanSource; | ||
import io.opentracing.Span; | ||
import io.opentracing.util.ThreadLocalActiveSpanSource; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ActiveSpanSourceTraceContextTest { | ||
|
||
private ActiveSpanSource activeSpanSource = new ThreadLocalActiveSpanSource(); | ||
|
||
@Mock private Span span; | ||
|
||
private ActiveSpanSourceTraceContext traceContext = | ||
new ActiveSpanSourceTraceContext(activeSpanSource); | ||
|
||
@Test | ||
public void pushAndPop() throws Exception { | ||
traceContext.push(span); | ||
Span actual = traceContext.pop(); | ||
Assert.assertEquals(span, actual); | ||
} | ||
|
||
@Test | ||
public void getCurrentSpan() throws Exception { | ||
traceContext.push(span); | ||
Span actual = traceContext.getCurrentSpan(); | ||
Assert.assertEquals(span, actual); | ||
} | ||
|
||
@Test | ||
public void isEmpty() throws Exception { | ||
Assert.assertTrue(traceContext.isEmpty()); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
jaeger-context/src/test/java/com/uber/jaeger/context/TracingUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2017, The Jaeger 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 com.uber.jaeger.context; | ||
|
||
import com.uber.jaeger.Configuration; | ||
import com.uber.jaeger.utils.TestUtils; | ||
import io.opentracing.Tracer; | ||
import java.util.concurrent.Executors; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TracingUtilsTest { | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
TestUtils.resetGlobalTracer(); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void getTraceContextWithoutGlobalTracer() throws Exception { | ||
TracingUtils.getTraceContext(); | ||
} | ||
|
||
@Test | ||
public void getTraceContext() { | ||
Tracer tracer = new Configuration("boop").getTracer(); | ||
Assert.assertNotNull(tracer); | ||
Assert.assertNotNull(TracingUtils.getTraceContext()); | ||
} | ||
|
||
@Test(expected = IllegalStateException.class) | ||
public void tracedExecutorWithoutGlobalTracer() throws Exception { | ||
TracingUtils.tracedExecutor(null); | ||
} | ||
|
||
@Test() | ||
public void tracedExecutor() throws Exception { | ||
Tracer tracer = new Configuration("boop").getTracer(); | ||
Assert.assertNotNull(tracer); | ||
Assert.assertNotNull(TracingUtils.tracedExecutor(Executors.newSingleThreadExecutor())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.