
io.opentracing.v_030.mock.MockTracer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentracing-v030 Show documentation
Show all versions of opentracing-v030 Show documentation
OpenTracing 0.30 compatibility package
The newest version!
/*
* 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.v_030.mock;
import io.opentracing.v_030.References;
import io.opentracing.v_030.propagation.Format;
import io.opentracing.v_030.propagation.TextMap;
import io.opentracing.v_030.ActiveSpan;
import io.opentracing.v_030.ActiveSpanSource;
import io.opentracing.v_030.BaseSpan;
import io.opentracing.v_030.Span;
import io.opentracing.v_030.SpanContext;
import io.opentracing.v_030.Tracer;
import io.opentracing.v_030.noop.NoopActiveSpanSource;
import io.opentracing.v_030.util.ThreadLocalActiveSpanSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* MockTracer makes it easy to test the semantics of OpenTracing instrumentation.
*
* By using a MockTracer as an io.opentracing.v_030.Tracer implementation for unittests, a developer can assert that Span
* properties and relationships with other Spans are defined as expected by instrumentation code.
*
* The MockTracerTest has simple usage examples.
*/
public class MockTracer implements Tracer {
private List finishedSpans = new ArrayList<>();
private final Propagator propagator;
private ActiveSpanSource spanSource;
public MockTracer() {
this(new ThreadLocalActiveSpanSource(), Propagator.PRINTER);
}
public MockTracer(ActiveSpanSource spanSource) {
this(spanSource, Propagator.PRINTER);
}
public MockTracer(ActiveSpanSource spanSource, Propagator propagator) {
this.propagator = propagator;
this.spanSource = spanSource;
}
/**
* Create a new MockTracer that passes through any calls to inject() and/or extract().
*/
public MockTracer(Propagator propagator) {
this(NoopActiveSpanSource.INSTANCE, propagator);
}
/**
* Clear the finishedSpans() queue.
*
* Note that this does *not* have any effect on Spans created by MockTracer that have not finish()ed yet; those
* will still be enqueued in finishedSpans() when they finish().
*/
public synchronized void reset() {
this.finishedSpans.clear();
}
/**
* @return a copy of all finish()ed MockSpans started by this MockTracer (since construction or the last call to
* MockTracer.reset()).
*
* @see MockTracer#reset()
*/
public synchronized List finishedSpans() {
return new ArrayList<>(this.finishedSpans);
}
/**
* Noop method called on {@link Span#finish()}.
*/
protected void onSpanFinished(MockSpan mockSpan) {
}
@Override
public ActiveSpan activeSpan() {
return spanSource.activeSpan();
}
@Override
public ActiveSpan makeActive(Span span) {
return spanSource.makeActive(span);
}
/**
* Propagator allows the developer to intercept and verify any calls to inject() and/or extract().
*
* By default, MockTracer uses Propagator.PRINTER which simply logs such calls to System.out.
*
* @see MockTracer#MockTracer(Propagator)
*/
public interface Propagator {
void inject(MockSpan.MockContext ctx, Format format, C carrier);
MockSpan.MockContext extract(Format format, C carrier);
Propagator PRINTER = new Propagator() {
@Override
public void inject(MockSpan.MockContext ctx, Format format, C carrier) {
System.out.println("inject(" + ctx + ", " + format + ", " + carrier + ")");
}
@Override
public MockSpan.MockContext extract(Format format, C carrier) {
System.out.println("extract(" + format + ", " + carrier + ")");
return null;
}
};
Propagator TEXT_MAP = new Propagator() {
public static final String SPAN_ID_KEY = "spanid";
public static final String TRACE_ID_KEY = "traceid";
public static final String BAGGAGE_KEY_PREFIX = "baggage-";
@Override
public void inject(MockSpan.MockContext ctx, Format format, C carrier) {
if (carrier instanceof TextMap) {
TextMap textMap = (TextMap) carrier;
for (Map.Entry entry : ctx.baggageItems()) {
textMap.put(BAGGAGE_KEY_PREFIX + entry.getKey(), entry.getValue());
}
textMap.put(SPAN_ID_KEY, String.valueOf(ctx.spanId()));
textMap.put(TRACE_ID_KEY, String.valueOf(ctx.traceId()));
} else {
throw new IllegalArgumentException("Unknown carrier");
}
}
@Override
public MockSpan.MockContext extract(Format format, C carrier) {
Long traceId = null;
Long spanId = null;
Map baggage = new HashMap<>();
if (carrier instanceof TextMap) {
TextMap textMap = (TextMap) carrier;
for (Map.Entry entry : textMap) {
if (TRACE_ID_KEY.equals(entry.getKey())) {
traceId = Long.valueOf(entry.getValue());
} else if (SPAN_ID_KEY.equals(entry.getKey())) {
spanId = Long.valueOf(entry.getValue());
} else if (entry.getKey().startsWith(BAGGAGE_KEY_PREFIX)){
String key = entry.getKey().substring((BAGGAGE_KEY_PREFIX.length()));
baggage.put(key, entry.getValue());
}
}
} else {
throw new IllegalArgumentException("Unknown carrier");
}
if (traceId != null && spanId != null) {
return new MockSpan.MockContext(traceId, spanId, baggage);
}
return null;
}
};
}
@Override
public SpanBuilder buildSpan(String operationName) {
return new SpanBuilder(operationName);
}
private SpanContext activeSpanContext() {
ActiveSpan handle = this.spanSource.activeSpan();
if (handle == null) {
return null;
}
return handle.context();
}
@Override
public void inject(SpanContext spanContext, Format format, C carrier) {
this.propagator.inject((MockSpan.MockContext)spanContext, format, carrier);
}
@Override
public SpanContext extract(Format format, C carrier) {
return this.propagator.extract(format, carrier);
}
synchronized void appendFinishedSpan(MockSpan mockSpan) {
this.finishedSpans.add(mockSpan);
this.onSpanFinished(mockSpan);
}
public final class SpanBuilder implements Tracer.SpanBuilder {
private final String operationName;
private long startMicros;
private MockSpan.MockContext firstParent;
private boolean ignoringActiveSpan;
private Map initialTags = new HashMap<>();
SpanBuilder(String operationName) {
this.operationName = operationName;
}
@Override
public SpanBuilder asChildOf(SpanContext parent) {
return addReference(References.CHILD_OF, parent);
}
@Override
public SpanBuilder asChildOf(BaseSpan parent) {
return addReference(References.CHILD_OF, parent.context());
}
@Override
public SpanBuilder ignoreActiveSpan() {
ignoringActiveSpan = true;
return this;
}
@Override
public SpanBuilder addReference(String referenceType, SpanContext referencedContext) {
if (firstParent == null && (
referenceType.equals(References.CHILD_OF) || referenceType.equals(References.FOLLOWS_FROM))) {
this.firstParent = (MockSpan.MockContext)referencedContext;
}
return this;
}
@Override
public SpanBuilder withTag(String key, String value) {
this.initialTags.put(key, value);
return this;
}
@Override
public SpanBuilder withTag(String key, boolean value) {
this.initialTags.put(key, value);
return this;
}
@Override
public SpanBuilder withTag(String key, Number value) {
this.initialTags.put(key, value);
return this;
}
@Override
public SpanBuilder withStartTimestamp(long microseconds) {
this.startMicros = microseconds;
return this;
}
@Override
public MockSpan start() {
return startManual();
}
@Override
public ActiveSpan startActive() {
MockSpan span = this.startManual();
return spanSource.makeActive(span);
}
@Override
public MockSpan startManual() {
if (this.startMicros == 0) {
this.startMicros = MockSpan.nowMicros();
}
if (firstParent == null && !ignoringActiveSpan) {
firstParent = (MockSpan.MockContext) activeSpanContext();
}
return new MockSpan(MockTracer.this, operationName, startMicros, initialTags, firstParent);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy