-
Notifications
You must be signed in to change notification settings - Fork 21
Create new span if parent span is missing and traceWithActiveSpanOnly = false #13
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
Merged
jpkrohling
merged 5 commits into
opentracing-contrib:master
from
malafeev:missing_parent
Apr 5, 2019
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8b394a
#6 Create new span if parent span is missing and traceWithActiveSpanO…
malafeev 14b0365
#6 extract scope creation to separate method
malafeev daeca06
#6 update test method to be more clear
malafeev 0cb9bca
#6 revert unneeded formatting
malafeev 300ecbc
#6 rename test method
malafeev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,3 +30,6 @@ target | |
| .classpath | ||
| .settings | ||
|
|
||
| # Intellij Idea | ||
| .idea/ | ||
| *.iml | ||
29 changes: 25 additions & 4 deletions
29
src/main/java/io/opentracing/contrib/concurrent/TracedExecutor.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 |
|---|---|---|
| @@ -1,27 +1,48 @@ | ||
| package io.opentracing.contrib.concurrent; | ||
|
|
||
| import java.util.concurrent.Executor; | ||
|
|
||
| import io.opentracing.Scope; | ||
| import io.opentracing.Tracer; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
| /** | ||
| * Executor which propagates span from parent thread to submitted {@link Runnable}. | ||
| * Optionally it creates parent span if traceWithActiveSpanOnly = false. | ||
| * | ||
| * @author Pavol Loffay | ||
| */ | ||
| public class TracedExecutor implements Executor { | ||
|
|
||
| protected final Tracer tracer; | ||
| private final Executor delegate; | ||
| private final boolean traceWithActiveSpanOnly; | ||
|
|
||
| public TracedExecutor(Executor executor, Tracer tracer) { | ||
| this(executor, tracer, true); | ||
| } | ||
|
|
||
| public TracedExecutor(Executor executor, Tracer tracer, boolean traceWithActiveSpanOnly) { | ||
| this.delegate = executor; | ||
| this.tracer = tracer; | ||
| this.traceWithActiveSpanOnly = traceWithActiveSpanOnly; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(Runnable runnable) { | ||
| delegate.execute(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer)); | ||
| Scope scope = createScope("execute"); | ||
| try { | ||
| delegate.execute(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer)); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Scope createScope(String operationName) { | ||
| if (tracer.activeSpan() == null && !traceWithActiveSpanOnly) { | ||
| return tracer.buildSpan(operationName).startActive(true); | ||
| } | ||
| return null; | ||
| } | ||
| } |
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
100 changes: 69 additions & 31 deletions
100
src/main/java/io/opentracing/contrib/concurrent/TracedScheduledExecutorService.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 |
|---|---|---|
| @@ -1,45 +1,83 @@ | ||
| package io.opentracing.contrib.concurrent; | ||
|
|
||
| import io.opentracing.Scope; | ||
| import io.opentracing.Tracer; | ||
|
|
||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.ScheduledFuture; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * @author Jose Montoya | ||
| * | ||
| * Executor which propagates span from parent thread to scheduled. | ||
| * Optionally it creates parent span if traceWithActiveSpanOnly = false. | ||
| */ | ||
| public class TracedScheduledExecutorService extends TracedExecutorService implements ScheduledExecutorService { | ||
|
|
||
| private final ScheduledExecutorService delegate; | ||
|
|
||
| public TracedScheduledExecutorService(ScheduledExecutorService delegate, Tracer tracer) { | ||
| super(delegate, tracer); | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit timeUnit) { | ||
| return delegate.schedule(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), delay, timeUnit); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit timeUnit) { | ||
| return delegate.schedule(tracer.activeSpan() == null ? callable : | ||
| new TracedCallable<T>(callable, tracer), delay, timeUnit); | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period, TimeUnit timeUnit) { | ||
| return delegate.scheduleAtFixedRate(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), initialDelay, period, timeUnit); | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long delay, TimeUnit timeUnit) { | ||
| return delegate.scheduleWithFixedDelay(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), initialDelay, delay, timeUnit); | ||
| } | ||
| private final ScheduledExecutorService delegate; | ||
|
|
||
| public TracedScheduledExecutorService(ScheduledExecutorService delegate, Tracer tracer) { | ||
| this(delegate, tracer, true); | ||
| } | ||
|
|
||
| public TracedScheduledExecutorService(ScheduledExecutorService delegate, Tracer tracer, | ||
| boolean traceWithActiveSpanOnly) { | ||
| super(delegate, tracer, traceWithActiveSpanOnly); | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit timeUnit) { | ||
| Scope scope = createScope("schedule"); | ||
| try { | ||
| return delegate.schedule(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), delay, timeUnit); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public <T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit timeUnit) { | ||
| Scope scope = createScope("schedule"); | ||
| try { | ||
| return delegate.schedule(tracer.activeSpan() == null ? callable : | ||
| new TracedCallable<T>(callable, tracer), delay, timeUnit); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period, | ||
| TimeUnit timeUnit) { | ||
| Scope scope = createScope("scheduleAtFixedRate"); | ||
| try { | ||
| return delegate.scheduleAtFixedRate(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), initialDelay, period, timeUnit); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long delay, | ||
| TimeUnit timeUnit) { | ||
| Scope scope = createScope("scheduleWithFixedDelay"); | ||
| try { | ||
| return delegate.scheduleWithFixedDelay(tracer.activeSpan() == null ? runnable : | ||
| new TracedRunnable(runnable, tracer), initialDelay, delay, timeUnit); | ||
| } finally { | ||
| if (scope != null) { | ||
| scope.close(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.