From 943061ea5918389998c0f9bd47dab8eba5d413fb Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 29 Mar 2018 10:05:07 -0700 Subject: [PATCH] Enabling NOOP classes to work with pre-existing production code (#263) In special circumstances, code may be written to automatically create spans, negating the need to always check if Tracer.activeSpan() is null. Under these circumstances, the Noop Tracer and ScopeManager will cause NPEs when they return null values when their `active` values are accessed from the GlobalTracer. This prevents code from running successfully when using Noop Tracers. This minor change allows users to depend that their code will genuinely Noop under all use cases. --- .../io/opentracing/noop/NoopScopeManager.java | 2 +- .../java/io/opentracing/noop/NoopTracer.java | 2 +- .../noop/NoopScopeManagerTest.java | 34 ++++++++++++++++++ .../io/opentracing/noop/NoopTracerTest.java | 35 +++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 opentracing-noop/src/test/java/io/opentracing/noop/NoopScopeManagerTest.java create mode 100644 opentracing-noop/src/test/java/io/opentracing/noop/NoopTracerTest.java 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 063cb61b..3f73b3a4 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopScopeManager.java @@ -36,7 +36,7 @@ public Scope activate(Span span, boolean finishOnClose) { @Override public Scope active() { - return null; + return NoopScope.INSTANCE; } static class NoopScopeImpl implements NoopScopeManager.NoopScope { 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 75770b85..9adf32c1 100644 --- a/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java +++ b/opentracing-noop/src/main/java/io/opentracing/noop/NoopTracer.java @@ -32,7 +32,7 @@ public ScopeManager scopeManager() { @Override public Span activeSpan() { - return null; + return NoopSpanImpl.INSTANCE; } @Override diff --git a/opentracing-noop/src/test/java/io/opentracing/noop/NoopScopeManagerTest.java b/opentracing-noop/src/test/java/io/opentracing/noop/NoopScopeManagerTest.java new file mode 100644 index 00000000..117027f7 --- /dev/null +++ b/opentracing-noop/src/test/java/io/opentracing/noop/NoopScopeManagerTest.java @@ -0,0 +1,34 @@ +/* + * 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 static org.junit.Assert.*; + +import org.junit.Test; + +import io.opentracing.Scope; + +public class NoopScopeManagerTest { + + @Test + public void activeValueToleratesUseTest() { + try{ + final Scope active = NoopScopeManager.INSTANCE.active(); + assertNotNull(active); + active.close(); + } catch (final NullPointerException e) { + fail("NoopScopeManagerImpl.active() should return a usable scope"); + } + } +} \ No newline at end of file diff --git a/opentracing-noop/src/test/java/io/opentracing/noop/NoopTracerTest.java b/opentracing-noop/src/test/java/io/opentracing/noop/NoopTracerTest.java new file mode 100644 index 00000000..cba9d779 --- /dev/null +++ b/opentracing-noop/src/test/java/io/opentracing/noop/NoopTracerTest.java @@ -0,0 +1,35 @@ +/* + * 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 static org.junit.Assert.*; + +import org.junit.Test; + +import io.opentracing.Span; +import io.opentracing.tag.Tags; + +public class NoopTracerTest { + + @Test + public void activeSpanValueToleratesUseTest() { + try { + final Span activeSpan = NoopTracerImpl.INSTANCE.activeSpan(); + assertNotNull(activeSpan); + Tags.ERROR.set(activeSpan, true); + } catch (final NullPointerException e) { + fail("NoopTracer.activeSpan() should return a usable span"); + } + } +} \ No newline at end of file