forked from opentracing-contrib/java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Bhs/scopes update #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlosalberto
wants to merge
20
commits into
bhs:bhs/scopes
Choose a base branch
from
carlosalberto:bhs/scopes-update
base: bhs/scopes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
43db7b5
add 'common handler' test
malafeev 26b97cd
Add an example for multiple callbacks for an active span.
carlosalberto 661a6ff
apply received feedback
malafeev 1930580
fix thread name
malafeev 6b4e518
Let the Logger format the message instead of concatenating values.
carlosalberto 9d9e3f5
Reverse the sleep time for the threads in TestMultipleCallbacks.
carlosalberto 697321c
Merge pull request #1 from carlosalberto/multiple_callbacks
malafeev 7b0720a
Add an example of nested callbacks.
carlosalberto 8205f1f
Merge pull request #2 from carlosalberto/nested_callbacks
malafeev 6ba0b1c
Add a late-finish Span example.
carlosalberto c1390b0
Add an example for an active span being temporary replaced.
carlosalberto 0904ea2
Merge pull request #3 from carlosalberto/actives_replacement
malafeev dcbaab7
Merge pull request #4 from carlosalberto/late_span_finish
malafeev 211e8be
one more check
malafeev bf5ac9a
use one Random, return Future
malafeev e925d86
refactoring + one more case for parent-span issue
malafeev 5209880
Make all tests pass with the Scope API
bhs f91856c
Update the latest examples with the Scope prototype.
carlosalberto f62b2e8
Update the README with instructions to build/test the PR.
carlosalberto a144e92
Clarify the PR installation step/need.
carlosalberto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
83 changes: 83 additions & 0 deletions
83
...va/io/opentracing/contrib/examples/active_span_replacement/TestActiveSpanReplacement.java
This file contains hidden or 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,83 @@ | ||
| package io.opentracing.contrib.examples.active_span_replacement; | ||
|
|
||
| import static com.jayway.awaitility.Awaitility.await; | ||
| import static io.opentracing.contrib.examples.TestUtils.reportedSpansSize; | ||
| import static io.opentracing.contrib.examples.TestUtils.sleep; | ||
| import static org.hamcrest.core.IsEqual.equalTo; | ||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| import io.opentracing.Scope; | ||
| import io.opentracing.Scope.Observer; | ||
| import io.opentracing.Span; | ||
| import io.opentracing.mock.MockSpan; | ||
| import io.opentracing.mock.MockTracer; | ||
| import io.opentracing.mock.MockTracer.Propagator; | ||
| import io.opentracing.util.ThreadLocalScopeManager; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ExecutorService; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestActiveSpanReplacement { | ||
|
|
||
| private final MockTracer tracer = new MockTracer(Propagator.TEXT_MAP); | ||
|
|
||
| private final ExecutorService executor = Executors.newCachedThreadPool(); | ||
|
|
||
| @Before | ||
| public void before() { | ||
| tracer.reset(); | ||
| tracer.setScopeManager(new ThreadLocalScopeManager()); | ||
| } | ||
|
|
||
| @Test | ||
| public void test() throws Exception { | ||
| /* Start an isolated task and query for its result -and finish it- in another task/thread */ | ||
| Span span = tracer.buildSpan("initial").startManual(); | ||
| submitAnotherTask(span); | ||
|
|
||
| await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(tracer), equalTo(3)); | ||
|
|
||
| List<MockSpan> spans = tracer.finishedSpans(); | ||
| assertEquals(3, spans.size()); | ||
| assertEquals("initial", spans.get(0).operationName()); /* Isolated task. */ | ||
| assertEquals("subtask", spans.get(1).operationName()); | ||
| assertEquals("task", spans.get(2).operationName()); | ||
|
|
||
| /* task/subtask are part of the same trace, | ||
| * and subtask is a child of task */ | ||
| assertEquals(spans.get(1).context().traceId(), spans.get(2).context().traceId()); | ||
| assertEquals(spans.get(2).context().spanId(), spans.get(1).parentId()); | ||
|
|
||
| /* initial task is not related in any way to those two tasks */ | ||
| assertNotEquals(spans.get(0).context().traceId(), spans.get(1).context().traceId()); | ||
| assertEquals(0, spans.get(0).parentId()); | ||
|
|
||
| assertNull(tracer.scopeManager().active()); | ||
| } | ||
|
|
||
| private void submitAnotherTask(final Span span) { | ||
|
|
||
| executor.submit(new Runnable() { | ||
| @Override | ||
| public void run () { | ||
| /* Create a new Span for this task */ | ||
| try (Scope taskSpan = tracer.buildSpan("task").startActive(Observer.FINISH_ON_CLOSE)) { | ||
|
|
||
| /* Simulate work strictly related to the initial Span. */ | ||
| try (Scope initialSpan = span.activate(Observer.FINISH_ON_CLOSE)) { | ||
| sleep(50); | ||
| } | ||
|
|
||
| /* Restore the span for this task and create a subspan */ | ||
| try (Scope subTask = tracer.buildSpan("subtask").startActive(Observer.FINISH_ON_CLOSE)) { | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
casting doesn't look nice. What if it's not
AutoFinishScope?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey!
So, if you are talking about the chance of
Scopenot being aAutoFinishScopehere, I can add the check for sanity purposes (even though we know that for this example, we always get an Auto one :) )But if you are talking about doing such cast overall - that's a reason to try to include the
deferandContinuationapi into the main interface (i.e.io.opentracing.Scope)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, my concern about generic use case