Skip to content

Commit

Permalink
Allow changing slowQueryThresholdMs in code (#111)
Browse files Browse the repository at this point in the history
Fixes #107
  • Loading branch information
asvanberg committed Feb 23, 2021
1 parent 6b7c2b6 commit e6b2ef9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -164,7 +164,7 @@ public class OpenTracingConfig {
Span is marked by tag `slow=true` if duration exceed `slowQueryThresholdMs`.
`slowQueryThresholdMs` defaults to `0` which means disabled, can be enabled in two ways:
1. Passing system property, E.g. `-Dio.opentracing.contrib.jdbc.slowQueryThresholdMs=100`
2. Modified static field value by code, E.g. `io.opentracing.contrib.jdbc.JdbcTracingUtils.slowQueryThresholdMs=100`
2. Modify value by code, E.g. `io.opentracing.contrib.jdbc.JdbcTracing.setSlowQueryThresholdMs(100)`

## Troubleshooting
In case of _Unable to find a driver_ error the database driver should be registered before configuring
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/io/opentracing/contrib/jdbc/JdbcTracing.java
Expand Up @@ -28,4 +28,18 @@ public static void setTraceEnabled(boolean traceEnabled) {
public static boolean isTraceEnabled() {
return JdbcTracing.traceEnabled;
}

/**
* can be modified by application code
*/
private static int slowQueryThresholdMs = Integer.getInteger("io.opentracing.contrib.jdbc.slowQueryThresholdMs", 0);

public static int getSlowQueryThresholdMs() {
return slowQueryThresholdMs;
}

public static void setSlowQueryThresholdMs(final int slowQueryThresholdMs) {
JdbcTracing.slowQueryThresholdMs = slowQueryThresholdMs;
}

}
13 changes: 4 additions & 9 deletions src/main/java/io/opentracing/contrib/jdbc/JdbcTracingUtils.java
Expand Up @@ -36,11 +36,6 @@ class JdbcTracingUtils {

static final BooleanTag SLOW = new BooleanTag("slow");

/**
* can be modified by application code
*/
public static int slowQueryThresholdMs = Integer.getInteger("io.opentracing.contrib.jdbc.slowQueryThresholdMs", 0);

static Span buildSpan(String operationName,
String sql,
ConnectionInfo connectionInfo,
Expand Down Expand Up @@ -76,14 +71,14 @@ static <E extends Exception> void execute(String operationName,

final Span span = buildSpan(operationName, sql, connectionInfo, withActiveSpanOnly,
ignoreStatements, tracer);
long time = slowQueryThresholdMs > 0 ? System.nanoTime() : 0;
long time = JdbcTracing.getSlowQueryThresholdMs() > 0 ? System.nanoTime() : 0;
try (Scope ignored = tracer.activateSpan(span)) {
runnable.run();
} catch (Exception e) {
JdbcTracingUtils.onError(e, span);
throw e;
} finally {
if (slowQueryThresholdMs > 0 && System.nanoTime() - time > TimeUnit.MILLISECONDS.toNanos(slowQueryThresholdMs)) {
if (JdbcTracing.getSlowQueryThresholdMs() > 0 && System.nanoTime() - time > TimeUnit.MILLISECONDS.toNanos(JdbcTracing.getSlowQueryThresholdMs())) {
SLOW.set(span, true);
}
span.finish();
Expand All @@ -103,14 +98,14 @@ static <T, E extends Exception> T call(String operationName,

final Span span = buildSpan(operationName, sql, connectionInfo, withActiveSpanOnly,
ignoreStatements, tracer);
long time = slowQueryThresholdMs > 0 ? System.nanoTime() : 0;
long time = JdbcTracing.getSlowQueryThresholdMs() > 0 ? System.nanoTime() : 0;
try (Scope ignored = tracer.activateSpan(span)) {
return callable.call();
} catch (Exception e) {
JdbcTracingUtils.onError(e, span);
throw e;
} finally {
if (slowQueryThresholdMs > 0 && System.nanoTime() - time > TimeUnit.MILLISECONDS.toNanos(slowQueryThresholdMs)) {
if (JdbcTracing.getSlowQueryThresholdMs() > 0 && System.nanoTime() - time > TimeUnit.MILLISECONDS.toNanos(JdbcTracing.getSlowQueryThresholdMs())) {
SLOW.set(span, true);
}
span.finish();
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -41,11 +42,13 @@ public static void init() {
@Before
public void before() {
mockTracer.reset();
JdbcTracing.setSlowQueryThresholdMs(0);
}

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

@Test
Expand Down Expand Up @@ -73,4 +76,24 @@ public void buildSpanWithoutTraceEnabled() throws Exception {

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

@Test
public void setSlowTagCorrectly() throws Exception {
final int slowQueryThresholdMs = 100;
JdbcTracing.setSlowQueryThresholdMs(slowQueryThresholdMs);

JdbcTracingUtils.execute(
"SlowQuery",
() -> Thread.sleep(slowQueryThresholdMs * 2),
null,
ConnectionInfo.UNKNOWN_CONNECTION_INFO,
false,
Collections.emptySet(),
mockTracer);

final List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals("Should have traced a query execution", 1, finishedSpans.size());
final MockSpan slowQuerySpan = finishedSpans.get(0);
assertTrue("Span should be tagged slow", slowQuerySpan.tags().containsKey(JdbcTracingUtils.SLOW.getKey()));
}
}

0 comments on commit e6b2ef9

Please sign in to comment.