public class MockTracer extends Object implements Tracer
| Modifier and Type | Class and Description |
|---|---|
static interface |
MockTracer.Propagator
Propagator allows the developer to intercept and verify any calls to inject() and/or extract().
|
class |
MockTracer.SpanBuilder |
| Constructor and Description |
|---|
MockTracer() |
MockTracer(ActiveSpanSource spanSource) |
MockTracer(ActiveSpanSource spanSource,
MockTracer.Propagator propagator) |
MockTracer(MockTracer.Propagator propagator)
Create a new MockTracer that passes through any calls to inject() and/or extract().
|
| Modifier and Type | Method and Description |
|---|---|
ActiveSpan |
activeSpan()
Return the
active span. |
MockTracer.SpanBuilder |
buildSpan(String operationName)
Return a new SpanBuilder for a Span with the given `operationName`.
|
<C> SpanContext |
extract(Format<C> format,
C carrier)
Extract a SpanContext from a `carrier` of a given type, presumably after propagation across a process boundary.
|
List<MockSpan> |
finishedSpans() |
<C> void |
inject(SpanContext spanContext,
Format<C> format,
C carrier)
Inject a SpanContext into a `carrier` of a given type, presumably for propagation across process boundaries.
|
ActiveSpan |
makeActive(Span span)
Wrap and "make active" a
Span by encapsulating it – and any active state (e.g., MDC state) in the
current thread – in a new ActiveSpan. |
protected void |
onSpanFinished(MockSpan mockSpan)
Noop method called on
Span.finish(). |
void |
reset()
Clear the finishedSpans() queue.
|
public MockTracer()
public MockTracer(ActiveSpanSource spanSource)
public MockTracer(ActiveSpanSource spanSource, MockTracer.Propagator propagator)
public MockTracer(MockTracer.Propagator propagator)
public void reset()
public List<MockSpan> finishedSpans()
reset()protected void onSpanFinished(MockSpan mockSpan)
Span.finish().public ActiveSpan activeSpan()
ActiveSpanSourceactive span. This does not affect the internal reference count for the
ActiveSpan.
If there is an active span, it becomes an implicit parent of any newly-created
span at Tracer.SpanBuilder.startActive() time (rather than at
Tracer.buildSpan(String) time).
activeSpan in interface ActiveSpanSourceactive span, or null if none could be found.public ActiveSpan makeActive(Span span)
ActiveSpanSourceSpan by encapsulating it – and any active state (e.g., MDC state) in the
current thread – in a new ActiveSpan.makeActive in interface ActiveSpanSourcespan - the Span to wrap in an ActiveSpanActiveSpan that encapsulates the given Span and any other
ActiveSpanSource-specific context (e.g., the MDC context map)public MockTracer.SpanBuilder buildSpan(String operationName)
TracerYou can override the operationName later via BaseSpan.setOperationName(String).
A contrived example:
Tracer tracer = ...
// Note: if there is a `tracer.activeSpan()`, it will be used as the target of an implicit CHILD_OF
// Reference for "workSpan" when `startActive()` is invoked.
try (ActiveSpan workSpan = tracer.buildSpan("DoWork").startActive()) {
workSpan.setTag("...", "...");
// etc, etc
}
// It's also possible to create Spans manually, bypassing the ActiveSpanSource activation.
Span http = tracer.buildSpan("HandleHTTPRequest")
.asChildOf(rpcSpanContext) // an explicit parent
.withTag("user_agent", req.UserAgent)
.withTag("lucky_number", 42)
.startManual();
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier)
TracerExample:
Tracer tracer = ...
Span clientSpan = ...
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest);
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, httpHeadersCarrier);
inject in interface TracerC - the carrier type, which also parametrizes the Format.spanContext - the SpanContext instance to inject into the carrierformat - the Format of the carriercarrier - the carrier for the SpanContext state. All Tracer.inject() implementations must support
io.opentracing.propagation.TextMap and java.nio.ByteBuffer.Format,
Format.Builtinpublic <C> SpanContext extract(Format<C> format, C carrier)
TracerExample:
Tracer tracer = ...
TextMap httpHeadersCarrier = new AnHttpHeaderCarrier(httpRequest);
SpanContext spanCtx = tracer.extract(Format.Builtin.HTTP_HEADERS, httpHeadersCarrier);
... = tracer.buildSpan('...').asChildOf(spanCtx).startActive();
If the span serialized state is invalid (corrupt, wrong version, etc) inside the carrier this will result in an
IllegalArgumentException.extract in interface TracerC - the carrier type, which also parametrizes the Format.format - the Format of the carriercarrier - the carrier for the SpanContext state. All Tracer.extract() implementations must support
io.opentracing.propagation.TextMap and java.nio.ByteBuffer.Format,
Format.BuiltinCopyright © 2016–2018 OpenTracing. All rights reserved.