Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Initial implementation of a no-op active Span. #320

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopScopeManager;
import io.opentracing.noop.NoopSpanContext;
import io.opentracing.propagation.Binary;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
Expand Down Expand Up @@ -268,8 +268,7 @@ public <C> SpanContext extract(Format<C> format, C carrier) {

@Override
public Span activeSpan() {
Scope scope = this.scopeManager.active();
return scope == null ? null : scope.span();
return this.scopeManager.active().span();
}

@Override
Expand Down Expand Up @@ -304,6 +303,9 @@ public final class SpanBuilder implements Tracer.SpanBuilder {

@Override
public SpanBuilder asChildOf(SpanContext parent) {
if (parent == NoopSpanContext.INSTANCE) {
return this;
}
return addReference(References.CHILD_OF, parent);
}

Expand Down Expand Up @@ -375,7 +377,7 @@ public MockSpan startManual() {
this.startMicros = MockSpan.nowMicros();
}
SpanContext activeSpanContext = activeSpanContext();
if(references.isEmpty() && !ignoringActiveSpan && activeSpanContext != null) {
if(references.isEmpty() && !ignoringActiveSpan && activeSpanContext != NoopSpanContext.INSTANCE) {
references.add(new MockSpan.Reference((MockSpan.MockContext) activeSpanContext, References.CHILD_OF));
}
return new MockSpan(MockTracer.this, operationName, startMicros, initialTags, references);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopSpan;
import io.opentracing.propagation.Binary;
import io.opentracing.propagation.BinaryAdapters;
import io.opentracing.propagation.Format;
Expand Down Expand Up @@ -250,21 +251,21 @@ public void testBinaryPropagatorExtractError() {
@Test
public void testActiveSpan() {
MockTracer mockTracer = new MockTracer();
Assert.assertNull(mockTracer.activeSpan());
Assert.assertEquals(NoopSpan.INSTANCE, mockTracer.activeSpan());

Span span = mockTracer.buildSpan("foo").start();
try (Scope scope = mockTracer.activateSpan(span)) {
Assert.assertEquals(mockTracer.scopeManager().activeSpan(), mockTracer.activeSpan());
}

Assert.assertNull(mockTracer.activeSpan());
Assert.assertEquals(NoopSpan.INSTANCE, mockTracer.activeSpan());
Assert.assertTrue(mockTracer.finishedSpans().isEmpty());
}

@Test
public void testActiveSpanFinish() {
MockTracer mockTracer = new MockTracer();
Assert.assertNull(mockTracer.activeSpan());
Assert.assertEquals(NoopSpan.INSTANCE, mockTracer.activeSpan());

Scope scope = null;
try {
Expand All @@ -274,7 +275,7 @@ public void testActiveSpanFinish() {
scope.close();
}

Assert.assertNull(mockTracer.activeSpan());
Assert.assertEquals(NoopSpan.INSTANCE, mockTracer.activeSpan());
Assert.assertFalse(mockTracer.finishedSpans().isEmpty());
}

Expand Down
31 changes: 31 additions & 0 deletions opentracing-noop/src/main/java/io/opentracing/noop/NoopScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2016-2018 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.noop;

import io.opentracing.Scope;
import io.opentracing.Span;

public interface NoopScope extends Scope {
NoopScope INSTANCE = new NoopScopeImpl();
}

class NoopScopeImpl implements NoopScope {
@Override
public void close() {}

@Override
public Span span() {
return NoopSpan.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

public interface NoopScopeManager extends ScopeManager {
NoopScopeManager INSTANCE = new NoopScopeManagerImpl();

interface NoopScope extends Scope {
NoopScope INSTANCE = new NoopScopeManagerImpl.NoopScopeImpl();
}
}

/**
Expand All @@ -48,14 +44,4 @@ public Scope active() {
public Span activeSpan() {
return NoopSpan.INSTANCE;
}

static class NoopScopeImpl implements NoopScopeManager.NoopScope {
@Override
public void close() {}

@Override
public Span span() {
return NoopSpan.INSTANCE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface NoopSpan extends Span {
final class NoopSpanImpl implements NoopSpan {

@Override
public SpanContext context() { return NoopSpanContextImpl.INSTANCE; }
public SpanContext context() { return NoopSpanContext.INSTANCE; }

@Override
public void finish() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Tracer.SpanBuilder withStartTimestamp(long microseconds) {

@Override
public Scope startActive(boolean finishOnClose) {
return NoopScopeManager.NoopScope.INSTANCE;
return NoopScope.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@


public interface NoopSpanContext extends SpanContext {
static final NoopSpanContextImpl INSTANCE = new NoopSpanContextImpl();
}

final class NoopSpanContextImpl implements NoopSpanContext {
static final NoopSpanContextImpl INSTANCE = new NoopSpanContextImpl();

@Override
public String toTraceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Span activeSpan() {

@Override
public Scope activateSpan(Span span) {
return NoopScopeManager.NoopScope.INSTANCE;
return NoopScope.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.noop.NoopScope;
import io.opentracing.noop.NoopSpan;

/**
* A simple {@link ScopeManager} implementation built on top of Java's thread-local storage primitive.
Expand All @@ -37,12 +39,12 @@ public Scope activate(Span span) {

@Override
public Scope active() {
return tlsScope.get();
Scope scope = tlsScope.get();
return scope == null ? NoopScope.INSTANCE : scope;
}

@Override
public Span activeSpan() {
Scope scope = tlsScope.get();
return scope == null ? null : scope.span();
return active().span();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.noop.NoopScope;
import io.opentracing.noop.NoopSpan;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -35,13 +37,13 @@ public void before() throws Exception {
@Test
public void missingActiveScope() throws Exception {
Scope missingScope = source.active();
assertNull(missingScope);
assertEquals(NoopScope.INSTANCE, missingScope);
}

@Test
public void missingActiveSpan() throws Exception {
Span missingSpan = source.activeSpan();
assertNull(missingSpan);
assertEquals(NoopSpan.INSTANCE, missingSpan);
}

@Test
Expand All @@ -65,10 +67,10 @@ public void defaultActivate() throws Exception {

// And now Scope/Span are gone:
Scope missingScope = source.active();
assertNull(missingScope);
assertEquals(NoopScope.INSTANCE, missingScope);

Span missingSpan = source.activeSpan();
assertNull(missingSpan);
assertEquals(NoopSpan.INSTANCE, missingSpan);
}

@Test
Expand All @@ -89,8 +91,8 @@ public void finishSpanClose() throws Exception {
verify(span, times(1)).finish();

// Verify Scope/Span are gone.
assertNull(source.active());
assertNull(source.activeSpan());
assertEquals(NoopScope.INSTANCE, source.active());
assertEquals(NoopSpan.INSTANCE, source.activeSpan());
}

@Test
Expand All @@ -111,7 +113,7 @@ public void dontFinishSpanNoClose() throws Exception {
verify(span, never()).finish();

// Verify Scope/Span are gone.
assertNull(source.active());
assertNull(source.activeSpan());
assertEquals(NoopScope.INSTANCE, source.active());
assertEquals(NoopSpan.INSTANCE, source.activeSpan());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.noop.NoopScope;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -64,7 +65,7 @@ public void implicitSpanStack() throws Exception {

// And now nothing is active.
Scope missingSpan = scopeManager.active();
assertNull(missingSpan);
assertEquals(NoopScope.INSTANCE, missingSpan);
}

@Test
Expand Down