Skip to content

Commit

Permalink
Allow clients to enable or disable all jdbc traces (#56) (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
osvaldopina committed Mar 12, 2020
1 parent b134aab commit 3f4c6e1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static Span buildSpan(String operationName,
boolean withActiveSpanOnly,
Set<String> ignoredStatements,
Tracer tracer) {
if (withActiveSpanOnly && tracer.activeSpan() == null) {
if (!TracingDriver.isTraceEnabled() || (withActiveSpanOnly && tracer.activeSpan() == null)) {
return NoopSpan.INSTANCE;
} else if (ignoredStatements != null && ignoredStatements.contains(sql)) {
return NoopSpan.INSTANCE;
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/opentracing/contrib/jdbc/TracingDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,22 @@ public synchronized static Driver load() {
}
}

private static boolean interceptorMode = false;
private static boolean traceEnabled = true;

/**
* Sets the {@code traceEnabled} property to enable or disable traces.
*
* @param traceEnabled The {@code traceEnabled} value.
*/
public static void setTraceEnabled(boolean traceEnabled) {
TracingDriver.traceEnabled = traceEnabled;
}

public static boolean isTraceEnabled() {
return TracingDriver.traceEnabled;
}

private static boolean interceptorMode = false;

/**
* Turns "interceptor mode" on or off.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017-2020 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.contrib.jdbc;

import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import io.opentracing.util.GlobalTracerTestUtil;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import static io.opentracing.contrib.jdbc.TestUtil.checkNoEmptyTags;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class JdbcTracingUtilsTest {

private static final MockTracer mockTracer = new MockTracer();

@BeforeClass
public static void init() {
GlobalTracerTestUtil.setGlobalTracerUnconditionally(mockTracer);
}

@Before
public void before() {
mockTracer.reset();
}

@AfterClass
public static void afterClass() {
TracingDriver.setTraceEnabled(true);
}

@Test
public void buildSpanWithTracedEnabled() throws Exception {
TracingDriver.setInterceptorMode(false);
TracingDriver.setTraceEnabled(true);
try (Connection connection = DriverManager.getConnection("jdbc:tracing:h2:mem:jdbc")) {
Statement statement = connection.createStatement();
statement.executeUpdate("CREATE TABLE employer (id INTEGER)");
}

List<MockSpan> spans = mockTracer.finishedSpans();
assertEquals(2, spans.size());
checkNoEmptyTags(spans);
}

@Test
public void buildSpanWithoutTraceEnabled() throws Exception {
TracingDriver.setInterceptorMode(false);
TracingDriver.setTraceEnabled(false);
try (Connection connection = DriverManager.getConnection("jdbc:tracing:h2:mem:jdbc")) {
Statement statement = connection.createStatement();
statement.executeUpdate("CREATE TABLE employer (id INTEGER)");
}

assertTrue(mockTracer.finishedSpans().isEmpty());
}
}

0 comments on commit 3f4c6e1

Please sign in to comment.