Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ decorators:
setValue: aws-client
- type: ErrorFlag
- type: DBTypeDecorator
- type: DBStatementAsResourceName
- type: OperationDecorator
28 changes: 19 additions & 9 deletions dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import io.opentracing.tag.Tags;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.extern.slf4j.Slf4j;

/**
* SpanContext represents Span state that must propagate to descendant Spans and across process
Expand All @@ -19,6 +16,7 @@
* across Span boundaries and (2) any Datadog fields that are needed to identify or contextualize
* the associated Span instance
*/
@Slf4j
public class DDSpanContext implements io.opentracing.SpanContext {

public static final String LANGUAGE_FIELDNAME = "lang";
Expand Down Expand Up @@ -140,7 +138,7 @@ public void setSpanType(final String spanType) {

public void setBaggageItem(final String key, final String value) {
if (this.baggageItems.isEmpty()) {
this.baggageItems = new HashMap<String, String>();
this.baggageItems = new HashMap<>();
}
this.baggageItems.put(key, value);
}
Expand Down Expand Up @@ -190,21 +188,33 @@ public synchronized void setTag(final String tag, final Object value) {
}

if (this.tags.isEmpty()) {
this.tags = new HashMap<String, Object>();
this.tags = new HashMap<>();
}
this.tags.put(tag, value);

//Call decorators
final List<AbstractDecorator> decorators = tracer.getSpanContextDecorators(tag);
if (decorators != null) {
if (decorators != null && value != null) {
for (final AbstractDecorator decorator : decorators) {
decorator.afterSetTag(this, tag, value);
try {
decorator.afterSetTag(this, tag, value);
} catch (final Throwable ex) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I like that we have these try-catch statements. Mostly because it makes the code more stable especially if an underlying method fails. Rule #0: don't stop/crash the application

log.warn(
"Could not decorate the span decorator={}: {}",
decorator.getClass().getSimpleName(),
ex.getMessage());
}
}
}
//Error management
if (Tags.ERROR.getKey().equals(tag) && Boolean.TRUE.equals(value)) {
this.errorFlag = true;
}

// Remove null values
if (value == null) {
this.tags.remove(tag);
}
}

public synchronized Map<String, Object> getTags() {
Expand Down
1 change: 1 addition & 0 deletions dd-trace/src/main/java/com/datadoghq/trace/DDTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class DDTags {
public static final String RESOURCE_NAME = "resource-name";
public static final String THREAD_NAME = "thread-name";
public static final String THREAD_ID = "thread-id";
public static final String DB_STATEMENT = "sql.query";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datadoghq.trace.integration;

import com.datadoghq.trace.DDSpanContext;
import com.datadoghq.trace.DDTags;
import io.opentracing.tag.Tags;

Expand All @@ -10,4 +11,27 @@ public DBStatementAsResourceName() {
this.setMatchingTag(Tags.DB_STATEMENT.getKey());
this.setSetTag(DDTags.RESOURCE_NAME);
}

@Override
public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {

// Special case: Mongo
// Skip the decorators
if (context.getTags().containsKey(Tags.COMPONENT.getKey())
&& "java-mongo".equals(context.getTags().get(Tags.COMPONENT.getKey()))) {
return true;
}

//Assign service name
if (super.afterSetTag(context, tag, value)) {
// TODO: remove properly the tag (immutable at this time)
// the `db.statement` tag must be removed because it will be set
// by the Datadog Trace Agent as `sql.query`; here we're removing
// a duplicate that will not be obfuscated with the current Datadog
// Trace Agent version.
context.setTag(Tags.DB_STATEMENT.getKey(), null);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public DBTypeDecorator() {

@Override
public boolean afterSetTag(final DDSpanContext context, final String tag, final Object value) {

//Assign service name
if (super.afterSetTag(context, tag, value)) {
//Assign span type to DB
context.setSpanType("db");
// Special case: Mongo, set to mongodb
if ("mongo".equals(value)) {
context.setSpanType("mongodb");
} else {
context.setSpanType("sql");
}
// Works for: mongo, cassandra, jdbc
context.setOperationName(String.valueOf(value) + ".query");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,47 @@ class SpanDecoratorTest extends Specification {

then:
span.getOperationName() == type + ".query"
span.context().getSpanType() == "db"
span.context().getSpanType() == "sql"


when:
Tags.DB_TYPE.set(span, "mongo")

then:
span.getOperationName() == "mongo.query"
span.context().getSpanType() == "mongodb"


where:
type = "foo"


}

def "DBStatementAsResource should not interact on Mongo queries"() {
setup:
def tracer = new DDTracer()
def span = SpanFactory.newSpanOf(tracer)
tracer.addDecorator(new DBStatementAsResourceName())

when:
span.setResourceName("not-change-me")
Tags.COMPONENT.set(span, "java-mongo")
Tags.DB_STATEMENT.set(span, something)

then:
span.getResourceName() == "not-change-me"


when:
span.setResourceName("change-me")
Tags.COMPONENT.set(span, "other-contrib")
Tags.DB_STATEMENT.set(span, something)

then:
span.getResourceName() == something

where:
something = "fake-query"
}
}