Handle case where SQL is not saved in map properly.

Otherwise this could result in an NPE.
This commit is contained in:
Tyler Benson 2017-11-08 13:17:49 -05:00
parent e21ed1052f
commit f61935452b
3 changed files with 16 additions and 8 deletions

View File

@ -23,6 +23,7 @@ import net.bytebuddy.asm.Advice;
@AutoService(Instrumenter.class) @AutoService(Instrumenter.class)
public final class PreparedStatementInstrumentation implements Instrumenter { public final class PreparedStatementInstrumentation implements Instrumenter {
private static final String UNKNOWN_QUERY = "Unknown Query";
@Override @Override
public AgentBuilder instrument(final AgentBuilder agentBuilder) { public AgentBuilder instrument(final AgentBuilder agentBuilder) {
@ -61,7 +62,7 @@ public final class PreparedStatementInstrumentation implements Instrumenter {
Tags.COMPONENT.set(span, "java-jdbc-prepared_statement"); Tags.COMPONENT.set(span, "java-jdbc-prepared_statement");
span.setTag(DDTags.SERVICE_NAME, dbInfo.getType()); span.setTag(DDTags.SERVICE_NAME, dbInfo.getType());
span.setTag(DDTags.RESOURCE_NAME, sql); span.setTag(DDTags.RESOURCE_NAME, sql == null ? UNKNOWN_QUERY : sql);
span.setTag(DDTags.SPAN_TYPE, "sql"); span.setTag(DDTags.SPAN_TYPE, "sql");
span.setTag("span.origin.type", statement.getClass().getName()); span.setTag("span.origin.type", statement.getClass().getName());
span.setTag("db.jdbc.url", dbInfo.getUrl()); span.setTag("db.jdbc.url", dbInfo.getUrl());

View File

@ -176,9 +176,13 @@ public class DDSpanContext implements io.opentracing.SpanContext {
* Add a tag to the span. Tags are not propagated to the children * Add a tag to the span. Tags are not propagated to the children
* *
* @param tag the tag-name * @param tag the tag-name
* @param value the value of the value * @param value the value of the tag. tags with null values are ignored.
*/ */
public synchronized void setTag(final String tag, final Object value) { public synchronized void setTag(final String tag, final Object value) {
if (value == null) {
return;
}
if (tag.equals(DDTags.SERVICE_NAME)) { if (tag.equals(DDTags.SERVICE_NAME)) {
setServiceName(value.toString()); setServiceName(value.toString());
return; return;
@ -210,14 +214,10 @@ public class DDSpanContext implements io.opentracing.SpanContext {
} }
} }
// Error management // Error management
if (Tags.ERROR.getKey().equals(tag) && Boolean.TRUE.equals(value)) { if (Tags.ERROR.getKey().equals(tag)
&& Boolean.TRUE.equals(value instanceof String ? Boolean.valueOf((String) value) : value)) {
this.errorFlag = true; this.errorFlag = true;
} }
// Remove null values
if (value == null) {
this.tags.remove(tag);
}
} }
public synchronized Map<String, Object> getTags() { public synchronized Map<String, Object> getTags() {

View File

@ -0,0 +1,7 @@
package com.datadoghq.trace
import spock.lang.Specification
class DDSpanContextTest extends Specification {
}