Skip to content

Commit cd40150

Browse files
authored
Tracing instrumentation: Make contextmanager tracing disabled by default (#72)
* Tracing instrumentation: Make contextmanager tracing disabled by default Add unit test that illustrates how it can be enabled. * Little simplification of MockSpanMatcher
1 parent 23d2214 commit cd40150

File tree

3 files changed

+199
-8
lines changed

3 files changed

+199
-8
lines changed

opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/OpentracingContextTimer.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
* @author Sjoerd Talsma
3636
*/
3737
public class OpentracingContextTimer implements ContextTimer {
38-
private static final String SYS_DISABLED = "opentracing.contexttimer.disabled";
39-
private static final String ENV_DISABLED = SYS_DISABLED.toUpperCase().replace('.', '_');
38+
private static final String SYS_ENABLED = "opentracing.trace.contextmanager";
39+
private static final String ENV_ENABLED = SYS_ENABLED.toUpperCase().replace('.', '_');
4040
private static final String LOG_FIELD_THREAD = "context.thread";
4141

4242
@Override
@@ -61,11 +61,13 @@ public void update(Class<?> type, String method, long duration, TimeUnit unit) {
6161
}
6262

6363
private static boolean reportContextSwitchesFor(Class<?> type) {
64-
final String disabled = System.getProperty(SYS_DISABLED, System.getenv(ENV_DISABLED));
65-
if ("1".equals(disabled) || "true".equalsIgnoreCase(disabled)) return false;
66-
67-
// Only report spans for entire snapshots, not individual context managers
68-
// Could be made configurable if somebody ever asks for it..
69-
return ContextManagers.class.equals(type) || ContextSnapshot.class.equals(type);
64+
boolean enableTracing = false;
65+
final String prop = System.getProperty(SYS_ENABLED, System.getenv(ENV_ENABLED));
66+
if ("1".equals(prop) || "true".equalsIgnoreCase(prop) || "enabled".equalsIgnoreCase(prop)) {
67+
// Only report spans for entire snapshots, not individual context managers
68+
// Could be made configurable if somebody ever asks for it..
69+
enableTracing = ContextManagers.class.equals(type) || ContextSnapshot.class.equals(type);
70+
}
71+
return enableTracing;
7072
}
7173
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2016-2018 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.opentracing;
17+
18+
import io.opentracing.mock.MockSpan;
19+
import org.hamcrest.BaseMatcher;
20+
import org.hamcrest.Description;
21+
import org.hamcrest.Matcher;
22+
23+
import java.util.ArrayList;
24+
import java.util.Collection;
25+
26+
import static org.hamcrest.Matchers.hasItem;
27+
import static org.hamcrest.Matchers.is;
28+
import static org.junit.Assert.fail;
29+
30+
/**
31+
* Hamcrest {@linkplain Matcher} for {@link MockSpan} objects.
32+
*
33+
* @author Sjoerd Talsma
34+
*/
35+
public class MockSpanMatcher extends BaseMatcher<MockSpan> {
36+
37+
private Collection<Matcher<String>> operationNameMatchers = new ArrayList<Matcher<String>>();
38+
private Collection<Matcher<Iterable<? super MockSpan.Reference>>> referenceMatchers =
39+
new ArrayList<Matcher<Iterable<? super MockSpan.Reference>>>();
40+
41+
private MockSpanMatcher() {
42+
}
43+
44+
public static MockSpanMatcher withOperationName(String operationName) {
45+
return withOperationName(is(operationName));
46+
}
47+
48+
public static MockSpanMatcher withOperationName(Matcher<String> operationName) {
49+
return new MockSpanMatcher().andOperationName(operationName);
50+
}
51+
52+
public static MockSpanMatcher withReference(Matcher<MockSpan.Reference> reference) {
53+
return new MockSpanMatcher().andReference(reference);
54+
}
55+
56+
public MockSpanMatcher andOperationName(String operationName) {
57+
return andOperationName(is(operationName));
58+
}
59+
60+
public MockSpanMatcher andOperationName(Matcher<String> operationName) {
61+
if (operationName == null) fail("Operation name matcher is <null>.");
62+
this.operationNameMatchers.add(operationName);
63+
return this;
64+
}
65+
66+
public MockSpanMatcher andReference(Matcher<MockSpan.Reference> reference) {
67+
if (reference == null) fail("Refrence matcher is <null>.");
68+
this.referenceMatchers.add(hasItem(reference));
69+
return this;
70+
}
71+
72+
@Override
73+
public boolean matches(Object item) {
74+
if (item == null) return true;
75+
else if (!(item instanceof MockSpan)) return false;
76+
final MockSpan mockSpan = (MockSpan) item;
77+
for (Matcher<String> operationName : operationNameMatchers) {
78+
if (!operationName.matches(mockSpan.operationName())) return false;
79+
}
80+
for (Matcher<Iterable<? super MockSpan.Reference>> reference : referenceMatchers) {
81+
if (!reference.matches(mockSpan.references())) return false;
82+
}
83+
return true;
84+
}
85+
86+
@Override
87+
public void describeTo(Description description) {
88+
description.appendText("MockSpan");
89+
String sep = " with ";
90+
for (Matcher<String> operationName : operationNameMatchers) {
91+
operationName.describeTo(description.appendText(sep).appendText("operationName "));
92+
sep = " and ";
93+
}
94+
for (Matcher<Iterable<? super MockSpan.Reference>> reference : referenceMatchers) {
95+
reference.describeTo(description.appendText(sep).appendText("references "));
96+
sep = " and ";
97+
}
98+
}
99+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2016-2018 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.opentracing;
17+
18+
import io.opentracing.mock.MockTracer;
19+
import io.opentracing.util.GlobalTracer;
20+
import io.opentracing.util.GlobalTracerTestUtil;
21+
import nl.talsmasoftware.context.ContextManagers;
22+
import org.junit.After;
23+
import org.junit.AfterClass;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
import static nl.talsmasoftware.context.opentracing.MockSpanMatcher.withOperationName;
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
import static org.hamcrest.Matchers.empty;
31+
import static org.hamcrest.Matchers.hasItem;
32+
import static org.hamcrest.Matchers.is;
33+
34+
/**
35+
* Test and demonstration of the tracing of context switches themselves.
36+
*
37+
* @author Sjoerd Talsma
38+
*/
39+
public class OpentracingContextTimerTest {
40+
41+
private static final String PROPERTY_NAME = "opentracing.trace.contextmanager";
42+
private static final String oldSystemProperty = System.getProperty(PROPERTY_NAME);
43+
44+
static MockTracer tracer = new MockTracer();
45+
46+
@BeforeClass
47+
public static void initGlobalTracer() {
48+
GlobalTracerTestUtil.resetGlobalTracer();
49+
GlobalTracer.register(tracer);
50+
}
51+
52+
@AfterClass
53+
public static void resetGlobalTracer() {
54+
GlobalTracerTestUtil.resetGlobalTracer();
55+
}
56+
57+
@Before
58+
public void setup() {
59+
System.clearProperty(PROPERTY_NAME);
60+
tracer.reset();
61+
}
62+
63+
@After
64+
public void restoreSystemproperty() {
65+
if (oldSystemProperty == null) System.clearProperty(PROPERTY_NAME);
66+
else System.setProperty(PROPERTY_NAME, oldSystemProperty);
67+
}
68+
69+
@Test
70+
public void testDisabledByDefault() {
71+
ContextManagers.createContextSnapshot().reactivate().close();
72+
73+
assertThat(tracer.finishedSpans(), is(empty()));
74+
}
75+
76+
@Test
77+
public void testTraceCreateContextSnapshot() {
78+
System.setProperty(PROPERTY_NAME, "true");
79+
ContextManagers.createContextSnapshot();
80+
assertThat(tracer.finishedSpans(), hasItem(withOperationName("ContextManagers.createContextSnapshot")));
81+
}
82+
83+
@Test
84+
public void testTraceReactivateContextSnapshot() {
85+
System.setProperty(PROPERTY_NAME, "true");
86+
ContextManagers.createContextSnapshot().reactivate().close();
87+
assertThat(tracer.finishedSpans(), hasItem(withOperationName("ContextSnapshot.reactivate")));
88+
}
89+
90+
}

0 commit comments

Comments
 (0)