From deeecd077417ed1306dc1b8c0e85fef7c98baf47 Mon Sep 17 00:00:00 2001 From: Carlos Alberto Cortez Date: Fri, 16 Nov 2018 17:04:36 +0100 Subject: [PATCH] Initial implementation of a no-op active Span. --- .../java/io/opentracing/mock/MockTracer.java | 10 +++--- .../io/opentracing/mock/MockTracerTest.java | 9 +++--- .../java/io/opentracing/noop/NoopScope.java | 31 +++++++++++++++++++ .../io/opentracing/noop/NoopScopeManager.java | 14 --------- .../java/io/opentracing/noop/NoopSpan.java | 2 +- .../io/opentracing/noop/NoopSpanBuilder.java | 2 +- .../io/opentracing/noop/NoopSpanContext.java | 2 +- .../java/io/opentracing/noop/NoopTracer.java | 2 +- .../util/ThreadLocalScopeManager.java | 8 +++-- .../util/ThreadLocalScopeManagerTest.java | 18 ++++++----- .../util/ThreadLocalScopeTest.java | 3 +- 11 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 opentracing-noop/src/main/java/io/opentracing/noop/NoopScope.java diff --git a/opentracing-mock/src/main/java/io/opentracing/mock/MockTracer.java b/opentracing-mock/src/main/java/io/opentracing/mock/MockTracer.java index ac078fe5..02afc9d9 100644 --- a/opentracing-mock/src/main/java/io/opentracing/mock/MockTracer.java +++ b/opentracing-mock/src/main/java/io/opentracing/mock/MockTracer.java @@ -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; @@ -268,8 +268,7 @@ public SpanContext extract(Format format, C carrier) { @Override public Span activeSpan() { - Scope scope = this.scopeManager.active(); - return scope == null ? null : scope.span(); + return this.scopeManager.active().span(); } @Override @@ -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); } @@ -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); diff --git a/opentracing-mock/src/test/java/io/opentracing/mock/MockTracerTest.java b/opentracing-mock/src/test/java/io/opentracing/mock/MockTracerTest.java index 99fbc2fc..45ad6ef2 100644 --- a/opentracing-mock/src/test/java/io/opentracing/mock/MockTracerTest.java +++ b/opentracing-mock/src/test/java/io/opentracing/mock/MockTracerTest.java @@ -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; @@ -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 { @@ -274,7 +275,7 @@ public void testActiveSpanFinish() { scope.close(); } - Assert.assertNull(mockTracer.activeSpan()); + Assert.assertEquals(NoopSpan.INSTANCE, mockTracer.activeSpan()); Assert.assertFalse(mockTracer.finishedSpans().isEmpty()); } diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopScope.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopScope.java new file mode 100644 index 00000000..04caf68b --- /dev/null +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopScope.java @@ -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; + } +} diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java index 911ac5a1..f76a85fe 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java @@ -19,10 +19,6 @@ public interface NoopScopeManager extends ScopeManager { NoopScopeManager INSTANCE = new NoopScopeManagerImpl(); - - interface NoopScope extends Scope { - NoopScope INSTANCE = new NoopScopeManagerImpl.NoopScopeImpl(); - } } /** @@ -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; - } - } } diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpan.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpan.java index f32bb769..af11ec05 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpan.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpan.java @@ -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() {} diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanBuilder.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanBuilder.java index b3d7f1c2..0ba1af74 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanBuilder.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanBuilder.java @@ -70,7 +70,7 @@ public Tracer.SpanBuilder withStartTimestamp(long microseconds) { @Override public Scope startActive(boolean finishOnClose) { - return NoopScopeManager.NoopScope.INSTANCE; + return NoopScope.INSTANCE; } @Override diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanContext.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanContext.java index 186a0517..88e2ff3b 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanContext.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopSpanContext.java @@ -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() { diff --git a/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java b/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java index abed3ad4..0e839d23 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java @@ -38,7 +38,7 @@ public Span activeSpan() { @Override public Scope activateSpan(Span span) { - return NoopScopeManager.NoopScope.INSTANCE; + return NoopScope.INSTANCE; } @Override diff --git a/opentracing-util/src/main/java/io/opentracing/util/ThreadLocalScopeManager.java b/opentracing-util/src/main/java/io/opentracing/util/ThreadLocalScopeManager.java index 433f4bc0..74d52dc9 100644 --- a/opentracing-util/src/main/java/io/opentracing/util/ThreadLocalScopeManager.java +++ b/opentracing-util/src/main/java/io/opentracing/util/ThreadLocalScopeManager.java @@ -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. @@ -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(); } } diff --git a/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeManagerTest.java b/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeManagerTest.java index 26e88c0f..8d2cfa1f 100644 --- a/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeManagerTest.java +++ b/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeManagerTest.java @@ -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; @@ -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 @@ -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 @@ -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 @@ -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()); } } diff --git a/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeTest.java b/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeTest.java index 367f7a49..1f0b17c4 100644 --- a/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeTest.java +++ b/opentracing-util/src/test/java/io/opentracing/util/ThreadLocalScopeTest.java @@ -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; @@ -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