Add the DefaultLogHandler implementation

This commit is contained in:
Marius Constantin 2020-02-27 11:04:52 +01:00
parent 6cd2d9f79b
commit b24ae8d638
5 changed files with 72 additions and 44 deletions

View File

@ -51,7 +51,7 @@ public class DDSpan implements Span, MutableSpan {
private final AtomicLong durationNano = new AtomicLong(); private final AtomicLong durationNano = new AtomicLong();
/** Delegates to for handling the logs if present. */ /** Delegates to for handling the logs if present. */
private final LogsHandler logsHandler; private final LogHandler logHandler;
/** Implementation detail. Stores the weak reference to this span. Used by TraceCollection. */ /** Implementation detail. Stores the weak reference to this span. Used by TraceCollection. */
volatile WeakReference<DDSpan> ref; volatile WeakReference<DDSpan> ref;
@ -63,7 +63,7 @@ public class DDSpan implements Span, MutableSpan {
* @param context the context used for the span * @param context the context used for the span
*/ */
DDSpan(final long timestampMicro, final DDSpanContext context) { DDSpan(final long timestampMicro, final DDSpanContext context) {
this(timestampMicro, context, null); this(timestampMicro, context, new DefaultLogHandler());
} }
/** /**
@ -71,11 +71,11 @@ public class DDSpan implements Span, MutableSpan {
* *
* @param timestampMicro if greater than zero, use this time instead of the current time * @param timestampMicro if greater than zero, use this time instead of the current time
* @param context the context used for the span * @param context the context used for the span
* @param logsHandler as the handler where to delegate the log actions * @param logHandler as the handler where to delegate the log actions
*/ */
DDSpan(final long timestampMicro, final DDSpanContext context, final LogsHandler logsHandler) { DDSpan(final long timestampMicro, final DDSpanContext context, final LogHandler logHandler) {
this.context = context; this.context = context;
this.logsHandler = logsHandler; this.logHandler = logHandler;
if (timestampMicro <= 0L) { if (timestampMicro <= 0L) {
// record the start time // record the start time
@ -242,11 +242,8 @@ public class DDSpan implements Span, MutableSpan {
*/ */
@Override @Override
public final DDSpan log(final Map<String, ?> map) { public final DDSpan log(final Map<String, ?> map) {
if (!extractError(map) && logsHandler != null) { extractError(map);
logsHandler.log(map); logHandler.log(map);
} else {
log.debug("`log` method is not implemented. Doing nothing");
}
return this; return this;
} }
@ -255,11 +252,8 @@ public class DDSpan implements Span, MutableSpan {
*/ */
@Override @Override
public final DDSpan log(final long l, final Map<String, ?> map) { public final DDSpan log(final long l, final Map<String, ?> map) {
if (!extractError(map) && logsHandler != null) { extractError(map);
logsHandler.log(l, map); logHandler.log(l, map);
} else {
log.debug("`log` method is not implemented. Doing nothing");
}
return this; return this;
} }
@ -268,11 +262,7 @@ public class DDSpan implements Span, MutableSpan {
*/ */
@Override @Override
public final DDSpan log(final String s) { public final DDSpan log(final String s) {
if (logsHandler != null) { logHandler.log(s);
logsHandler.log(s);
} else {
log.debug("`log` method is not implemented. Provided log: {}", s);
}
return this; return this;
} }
@ -281,11 +271,7 @@ public class DDSpan implements Span, MutableSpan {
*/ */
@Override @Override
public final DDSpan log(final long l, final String s) { public final DDSpan log(final long l, final String s) {
if (logsHandler != null) { logHandler.log(l, s);
logsHandler.log(l, s);
} else {
log.debug("`log` method is not implemented. Provided log: {}", s);
}
return this; return this;
} }

View File

@ -580,7 +580,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
private boolean errorFlag; private boolean errorFlag;
private String spanType; private String spanType;
private boolean ignoreScope = false; private boolean ignoreScope = false;
private LogsHandler logsHandler = null; private LogHandler logHandler = new DefaultLogHandler();
public DDSpanBuilder(final String operationName, final ScopeManager scopeManager) { public DDSpanBuilder(final String operationName, final ScopeManager scopeManager) {
this.operationName = operationName; this.operationName = operationName;
@ -594,7 +594,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
} }
private Span startSpan() { private Span startSpan() {
return new DDSpan(timestampMicro, buildSpanContext(), logsHandler); return new DDSpan(timestampMicro, buildSpanContext(), logHandler);
} }
@Override @Override
@ -671,8 +671,9 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
return parent.baggageItems(); return parent.baggageItems();
} }
public DDSpanBuilder withLogsHandler(LogsHandler logsHandler) { public DDSpanBuilder withLogHandler(final LogHandler logHandler) {
this.logsHandler = logsHandler; assert logHandler != null : "LogHandler must not be null";
this.logHandler = logHandler;
return this; return this;
} }

View File

@ -0,0 +1,28 @@
package datadog.opentracing;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
/** The default implementation of the LogHandler. */
@Slf4j
public class DefaultLogHandler implements LogHandler {
@Override
public void log(Map<String, ?> fields) {
log.debug("`log` method is not implemented. Doing nothing");
}
@Override
public void log(long timestampMicroseconds, Map<String, ?> fields) {
log.debug("`log` method is not implemented. Doing nothing");
}
@Override
public void log(String event) {
log.debug("`log` method is not implemented. Provided log: {}", event);
}
@Override
public void log(long timestampMicroseconds, String event) {
log.debug("`log` method is not implemented. Provided log: {}", event);
}
}

View File

@ -2,7 +2,7 @@ package datadog.opentracing;
import java.util.Map; import java.util.Map;
public interface LogsHandler { public interface LogHandler {
/** /**
* Handles the log implementation in the Span. * Handles the log implementation in the Span.

View File

@ -480,7 +480,7 @@ class DDSpanBuilderTest extends DDSpecification {
"a:1,b-c:d" | [a: "1", "b-c": "d"] "a:1,b-c:d" | [a: "1", "b-c": "d"]
} }
def "sanity test for logs if logsHandler is null"() { def "sanity test for logs if logHandler is null"() {
setup: setup:
final String expectedName = "fakeName" final String expectedName = "fakeName"
@ -502,13 +502,13 @@ class DDSpanBuilderTest extends DDSpecification {
def "should delegate simple logs to logHandler"() { def "should delegate simple logs to logHandler"() {
setup: setup:
final LogsHandler logsHandler = new TestLogsHandler() final LogHandler logHandler = new TestLogHandler()
final String expectedName = "fakeName" final String expectedName = "fakeName"
final DDSpan span = final DDSpan span =
tracer tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withLogsHandler(logsHandler) .withLogHandler(logHandler)
.withServiceName("foo") .withServiceName("foo")
.start() .start()
final String expectedLogEvent = "fakeEvent" final String expectedLogEvent = "fakeEvent"
@ -516,56 +516,56 @@ class DDSpanBuilderTest extends DDSpecification {
span.log(timeStamp, expectedLogEvent) span.log(timeStamp, expectedLogEvent)
expect: expect:
logsHandler.assertLogCalledWithArgs(timeStamp, expectedLogEvent) logHandler.assertLogCalledWithArgs(timeStamp, expectedLogEvent)
} }
def "should delegate simple logs with timestamp to logHandler"() { def "should delegate simple logs with timestamp to logHandler"() {
setup: setup:
final LogsHandler logsHandler = new TestLogsHandler() final LogHandler logHandler = new TestLogHandler()
final String expectedName = "fakeName" final String expectedName = "fakeName"
final DDSpan span = final DDSpan span =
tracer tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withLogsHandler(logsHandler) .withLogHandler(logHandler)
.withServiceName("foo") .withServiceName("foo")
.start() .start()
final String expectedLogEvent = "fakeEvent" final String expectedLogEvent = "fakeEvent"
span.log(expectedLogEvent) span.log(expectedLogEvent)
expect: expect:
logsHandler.assertLogCalledWithArgs(expectedLogEvent) logHandler.assertLogCalledWithArgs(expectedLogEvent)
} }
def "should delegate logs with fields to logHandler"() { def "should delegate logs with fields to logHandler"() {
setup: setup:
final LogsHandler logsHandler = new TestLogsHandler() final LogHandler logHandler = new TestLogHandler()
final String expectedName = "fakeName" final String expectedName = "fakeName"
final DDSpan span = final DDSpan span =
tracer tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withLogsHandler(logsHandler) .withLogHandler(logHandler)
.withServiceName("foo") .withServiceName("foo")
.start() .start()
final Map<String, String> fieldsMap = new HashMap<>() final Map<String, String> fieldsMap = new HashMap<>()
span.log(fieldsMap) span.log(fieldsMap)
expect: expect:
logsHandler.assertLogCalledWithArgs(fieldsMap) logHandler.assertLogCalledWithArgs(fieldsMap)
} }
def "should delegate logs with fields and timestamp to logHandler"() { def "should delegate logs with fields and timestamp to logHandler"() {
setup: setup:
final LogsHandler logsHandler = new TestLogsHandler() final LogHandler logHandler = new TestLogHandler()
final String expectedName = "fakeName" final String expectedName = "fakeName"
final DDSpan span = final DDSpan span =
tracer tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withLogsHandler(logsHandler) .withLogHandler(logHandler)
.withServiceName("foo") .withServiceName("foo")
.start() .start()
final Map<String, String> fieldsMap = new HashMap<>() final Map<String, String> fieldsMap = new HashMap<>()
@ -573,11 +573,24 @@ class DDSpanBuilderTest extends DDSpecification {
span.log(timeStamp, fieldsMap) span.log(timeStamp, fieldsMap)
expect: expect:
logsHandler.assertLogCalledWithArgs(timeStamp, fieldsMap) logHandler.assertLogCalledWithArgs(timeStamp, fieldsMap)
} }
private static class TestLogsHandler implements LogsHandler { def "test assertion not null LogHandler"() {
setup:
final String expectedName = "fakeName"
when:
tracer
.buildSpan(expectedName)
.withLogHandler(null)
then:
thrown(AssertionError)
}
private static class TestLogHandler implements LogHandler {
Object[] arguments = null Object[] arguments = null
@Override @Override