forked from opentracing/opentracing-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in-process propagation resolves opentracing/specification#23
- Loading branch information
1 parent
bc1983e
commit 8cf3178
Showing
16 changed files
with
789 additions
and
19 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
70 changes: 70 additions & 0 deletions
70
opentracing-api/src/main/java/io/opentracing/SpanManager.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,70 @@ | ||
/** | ||
* Copyright 2016-2017 The OpenTracing 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.opentracing; | ||
|
||
/** | ||
* @author Pavol Loffay | ||
*/ | ||
public interface SpanManager { | ||
|
||
/** | ||
* @param span span to bundle into visibility, there is always only one visibility per span | ||
* @return visibility | ||
*/ | ||
Visibility bundle(Span span); | ||
|
||
/** | ||
* @return not finished active span or null | ||
*/ | ||
VisibilityContext active(); | ||
|
||
interface Visibility { | ||
/** | ||
* @return visibility context which is used to activate/deactivate span | ||
*/ | ||
VisibilityContext capture(); | ||
|
||
/** | ||
* @return associated span or null if visibility is marked as finished. | ||
*/ | ||
Span span(); | ||
/** | ||
* @return always spanContext | ||
*/ | ||
SpanContext context(); | ||
|
||
/** | ||
* Mark associated span as finished. | ||
* | ||
* Should be called by {@link Span#finish()} or directly if one does not want to expose span. | ||
* This method should be idempotent. | ||
* | ||
* review note: reverse operation should not be allowed. | ||
*/ | ||
void hideSpan(); | ||
} | ||
|
||
interface VisibilityContext { | ||
/** | ||
* on/activate - {@link SpanManager#active()} will return this object. | ||
*/ | ||
VisibilityContext on(); | ||
/** | ||
* off/deactivate - {@link SpanManager#active()} will not return this object. | ||
*/ | ||
VisibilityContext off(); | ||
|
||
Visibility visibility(); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
opentracing-api/src/main/java/io/opentracing/ThreadLocalSpanManager.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,95 @@ | ||
/** | ||
* Copyright 2016-2017 The OpenTracing 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.opentracing; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* @author Pavol Loffay | ||
*/ | ||
public class ThreadLocalSpanManager implements SpanManager { | ||
|
||
private final ThreadLocal<SimpleLinkedVisibilityContext> activeContext = new ThreadLocal<SimpleLinkedVisibilityContext>(); | ||
|
||
@Override | ||
public Visibility bundle(Span span) { | ||
return span.visibility() == null ? new SimpleVisibility(span) : span.visibility(); | ||
} | ||
|
||
@Override | ||
public SimpleLinkedVisibilityContext active() { | ||
return activeContext.get(); | ||
} | ||
|
||
class SimpleVisibility implements Visibility { | ||
private final Span span; | ||
private AtomicBoolean hideSpan = new AtomicBoolean(false); | ||
|
||
public SimpleVisibility(Span span) { | ||
this.span = span; | ||
} | ||
|
||
@Override | ||
public Span span() { | ||
return hideSpan.get() ? null : span; | ||
} | ||
|
||
@Override | ||
public SpanContext context() { | ||
return span.context(); | ||
} | ||
|
||
@Override | ||
public SimpleLinkedVisibilityContext capture() { | ||
return new SimpleLinkedVisibilityContext(this); | ||
} | ||
|
||
@Override | ||
public void hideSpan() { | ||
hideSpan.set(true); | ||
} | ||
} | ||
|
||
class SimpleLinkedVisibilityContext implements SpanManager.VisibilityContext { | ||
|
||
private final SimpleVisibility visibility; | ||
private SimpleLinkedVisibilityContext previous; | ||
|
||
public SimpleLinkedVisibilityContext(SimpleVisibility visibility) { | ||
this.visibility = visibility; | ||
} | ||
|
||
@Override | ||
public SimpleLinkedVisibilityContext on() { | ||
previous = activeContext.get(); | ||
activeContext.set(this); | ||
return this; | ||
} | ||
|
||
@Override | ||
public SimpleLinkedVisibilityContext off() { | ||
if (this == activeContext.get()) { | ||
activeContext.set(previous); | ||
} | ||
// else should not happen | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public Visibility visibility() { | ||
return visibility; | ||
} | ||
} | ||
} |
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
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.