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

Commit

Permalink
Enabling NOOP classes to work with pre-existing production code (#263)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natehart authored and yurishkuro committed Mar 29, 2018
1 parent 806b026 commit 943061e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ScopeManager scopeManager() {

@Override
public Span activeSpan() {
return null;
return NoopSpanImpl.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}

0 comments on commit 943061e

Please sign in to comment.