Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exposes Tracer.currentSpanCustomizer() and Span.customizer() for safety
Eventhough we made a safe user type `SpanCustomizer`, we didn't provide a safe alternative to `Tracer.currentSpan()`. This led to routine leaking of `brave.Span` to users (even if they need to cast to discover this). While we did have `CurrentSpanCustomizer`, that relied on current context to work. Particularly the new servlet instrumentation uses explicit references, which are cheaper. This adds a couple utilities and uses them: `Tracer.currentSpanCustomizer()` - safer than `currentSpan()` `Span.customizer()` - safely masks lifecycle methods from parsers These are safer as they don't have the ability to abend the span, and have a chance of holding less state (ex NoopSpanCustomizer is constant). Future work will introduce `UnsafeSpan`, which can rely on these methods to ensure higher performance in single-threaded scenarios such as synchronous calls.
- Loading branch information
1 parent
101c6f9
commit 414922d
Showing
26 changed files
with
247 additions
and
72 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
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,22 @@ | ||
package brave; | ||
|
||
// Preferred to a constant NOOP in SpanCustomizer as the latter ends up in a hierachy including Span | ||
enum NoopSpanCustomizer implements SpanCustomizer { | ||
INSTANCE; | ||
|
||
@Override public SpanCustomizer name(String name) { | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer tag(String key, String value) { | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer annotate(String value) { | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer annotate(long timestamp, String value) { | ||
return this; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package brave; | ||
|
||
import brave.internal.recorder.Recorder; | ||
import brave.propagation.TraceContext; | ||
import com.google.auto.value.AutoValue; | ||
|
||
/** This wraps the public api and guards access to a mutable span. */ | ||
@AutoValue | ||
abstract class RealSpanCustomizer implements SpanCustomizer { | ||
|
||
abstract TraceContext context(); | ||
abstract Recorder recorder(); | ||
|
||
static RealSpanCustomizer create(TraceContext context, Recorder recorder) { | ||
return new AutoValue_RealSpanCustomizer(context, recorder); | ||
} | ||
|
||
@Override public SpanCustomizer name(String name) { | ||
recorder().name(context(), name); | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer annotate(String value) { | ||
recorder().annotate(context(), value); | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer annotate(long timestamp, String value) { | ||
recorder().annotate(context(), timestamp, value); | ||
return this; | ||
} | ||
|
||
@Override public SpanCustomizer tag(String key, String value) { | ||
recorder().tag(context(), key, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RealSpanCustomizer(" + context() + ")"; | ||
} | ||
} |
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
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 @@ | ||
package brave; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import zipkin2.Annotation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
|
||
public class RealSpanCustomizerTest { | ||
List<zipkin2.Span> spans = new ArrayList(); | ||
Tracer tracer = Tracing.newBuilder().spanReporter(spans::add).build().tracer(); | ||
Span span = tracer.newTrace(); | ||
SpanCustomizer spanCustomizer = span.customizer(); | ||
|
||
@After public void close() { | ||
Tracing.current().close(); | ||
} | ||
|
||
@Test public void name() { | ||
spanCustomizer.name("foo"); | ||
span.flush(); | ||
|
||
assertThat(spans).extracting(zipkin2.Span::name) | ||
.containsExactly("foo"); | ||
} | ||
|
||
@Test public void annotate() { | ||
spanCustomizer.annotate("foo"); | ||
span.flush(); | ||
|
||
assertThat(spans).flatExtracting(zipkin2.Span::annotations) | ||
.extracting(Annotation::value) | ||
.containsExactly("foo"); | ||
} | ||
|
||
@Test public void annotate_timestamp() { | ||
spanCustomizer.annotate(2, "foo"); | ||
span.flush(); | ||
|
||
assertThat(spans).flatExtracting(zipkin2.Span::annotations) | ||
.containsExactly(Annotation.create(2L, "foo")); | ||
} | ||
|
||
@Test public void tag() { | ||
spanCustomizer.tag("foo", "bar"); | ||
span.flush(); | ||
|
||
assertThat(spans).flatExtracting(s -> s.tags().entrySet()) | ||
.containsExactly(entry("foo", "bar")); | ||
} | ||
} |
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.