Skip to content

Commit af906ae

Browse files
committed
Get doc name to enrich resource name
1 parent 2003201 commit af906ae

File tree

8 files changed

+151
-26
lines changed

8 files changed

+151
-26
lines changed

dd-java-agent/instrumentation/mule-4/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ muzzle {
1515
versions = '[4.5.0,)'
1616
javaVersion = "17"
1717
excludeDependency 'com.google.code.findbugs:jsr305'
18+
additionalDependencies +="org.mule.runtime:mule-tracer-customization-impl:4.5.0"
19+
}
20+
pass {
21+
group = 'org.mule.runtime'
22+
module = 'mule-tracer-customization-impl'
23+
versions = '[4.5.0,)'
24+
javaVersion = "17"
25+
excludeDependency 'com.google.code.findbugs:jsr305'
26+
additionalDependencies +="org.mule.runtime:mule-core:4.5.0"
1827
}
1928
pass {
2029
name = 'before-4.5.0'
@@ -101,6 +110,7 @@ tasks.named("compileLatestDepForkedTestJava").configure {
101110

102111
dependencies {
103112
compileOnly group: 'org.mule.runtime', name: 'mule-core', version: muleVersion
113+
compileOnly group: 'org.mule.runtime', name: 'mule-tracer-customization-impl', version: muleVersion
104114

105115
testImplementation project(':dd-java-agent:instrumentation:aws-common')
106116
testImplementation project(':dd-java-agent:instrumentation:reactor-core-3.1')

dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/DDEventTracer.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
import java.util.Optional;
1010
import java.util.function.Supplier;
1111
import javax.annotation.Nonnull;
12+
import org.mule.runtime.api.component.Component;
1213
import org.mule.runtime.api.event.EventContext;
1314
import org.mule.runtime.api.message.Error;
1415
import org.mule.runtime.api.profiling.tracing.Span;
1516
import org.mule.runtime.core.api.event.CoreEvent;
1617
import org.mule.runtime.tracer.api.EventTracer;
1718
import org.mule.runtime.tracer.api.context.getter.DistributedTraceContextGetter;
1819
import org.mule.runtime.tracer.api.sniffer.SpanSnifferManager;
20+
import org.mule.runtime.tracer.api.span.info.EnrichedInitialSpanInfo;
1921
import org.mule.runtime.tracer.api.span.info.InitialSpanInfo;
2022
import org.mule.runtime.tracer.api.span.validation.Assertion;
23+
import org.mule.runtime.tracer.customization.impl.info.ExecutionInitialSpanInfo;
24+
import org.mule.runtime.tracer.customization.impl.provider.LazyInitialSpanInfo;
2125

2226
/**
2327
* This class is responsible for translating span reported by mule internal observability into DD
@@ -27,11 +31,16 @@ public class DDEventTracer implements EventTracer<CoreEvent> {
2731
/** Holds the link between mule event context <-> ddSpan */
2832
private final ContextStore<EventContext, SpanState> eventContextStore;
2933

34+
private final ContextStore<InitialSpanInfo, Component> componentContextStore;
35+
3036
private final EventTracer<CoreEvent> delegate;
3137

3238
public DDEventTracer(
33-
ContextStore<EventContext, SpanState> eventContextStore, EventTracer<CoreEvent> delegate) {
39+
ContextStore<EventContext, SpanState> eventContextStore,
40+
ContextStore<InitialSpanInfo, Component> componentContextStore,
41+
EventTracer<CoreEvent> delegate) {
3442
this.eventContextStore = eventContextStore;
43+
this.componentContextStore = componentContextStore;
3544
this.delegate = delegate;
3645
}
3746

@@ -51,6 +60,17 @@ private AgentSpan findParent(final EventContext eventContext) {
5160
return activeSpan();
5261
}
5362

63+
private Component findComponent(final InitialSpanInfo initialSpanInfo) {
64+
if (initialSpanInfo instanceof ExecutionInitialSpanInfo) {
65+
return componentContextStore.get(initialSpanInfo);
66+
} else if (initialSpanInfo instanceof LazyInitialSpanInfo) {
67+
return findComponent(((LazyInitialSpanInfo) initialSpanInfo).getDelegate());
68+
} else if (initialSpanInfo instanceof EnrichedInitialSpanInfo) {
69+
return findComponent(((EnrichedInitialSpanInfo) initialSpanInfo).getBaseInitialSpanInfo());
70+
}
71+
return null;
72+
}
73+
5474
private void activateOnContext(@Nonnull final EventContext eventContext, final AgentSpan span) {
5575
final SpanState previousState = eventContextStore.get(eventContext);
5676
final AgentSpan spanToActivate;
@@ -74,7 +94,8 @@ private void handleNewSpan(CoreEvent event, InitialSpanInfo spanInfo) {
7494

7595
final EventContext eventContext = event.getContext();
7696

77-
final AgentSpan span = DECORATE.onMuleSpan(findParent(eventContext), spanInfo);
97+
final AgentSpan span =
98+
DECORATE.onMuleSpan(findParent(eventContext), spanInfo, event, findComponent(spanInfo));
7899
activateOnContext(eventContext, span);
79100
}
80101

dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/EventTracerInstrumentation.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import datadog.trace.agent.tooling.InstrumenterModule;
66
import datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers;
77
import datadog.trace.bootstrap.InstrumentationContext;
8-
import java.util.Collections;
8+
import java.util.HashMap;
99
import java.util.Map;
1010
import net.bytebuddy.asm.Advice;
11+
import org.mule.runtime.api.component.Component;
1112
import org.mule.runtime.api.event.EventContext;
1213
import org.mule.runtime.core.api.event.CoreEvent;
1314
import org.mule.runtime.tracer.api.EventTracer;
15+
import org.mule.runtime.tracer.api.span.info.InitialSpanInfo;
1416

1517
@AutoService(InstrumenterModule.class)
1618
public class EventTracerInstrumentation extends InstrumenterModule.Tracing
@@ -26,8 +28,12 @@ protected boolean defaultEnabled() {
2628

2729
@Override
2830
public Map<String, String> contextStore() {
29-
return Collections.singletonMap(
30-
"org.mule.runtime.api.event.EventContext", packageName + ".SpanState");
31+
final Map<String, String> contextStore = new HashMap<>();
32+
contextStore.put("org.mule.runtime.api.event.EventContext", packageName + ".SpanState");
33+
contextStore.put(
34+
"org.mule.runtime.tracer.api.span.info.InitialSpanInfo",
35+
"org.mule.runtime.api.component.Component");
36+
return contextStore;
3137
}
3238

3339
@Override
@@ -59,7 +65,9 @@ public static void afterInit(
5965
EventTracer<CoreEvent> eventTracer) {
6066
eventTracer =
6167
new DDEventTracer(
62-
InstrumentationContext.get(EventContext.class, SpanState.class), eventTracer);
68+
InstrumentationContext.get(EventContext.class, SpanState.class),
69+
InstrumentationContext.get(InitialSpanInfo.class, Component.class),
70+
eventTracer);
6371
}
6472
}
6573
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package datadog.trace.instrumentation.mule4;
2+
3+
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
4+
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
5+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
6+
7+
import com.google.auto.service.AutoService;
8+
import datadog.trace.agent.tooling.Instrumenter;
9+
import datadog.trace.agent.tooling.InstrumenterModule;
10+
import datadog.trace.bootstrap.InstrumentationContext;
11+
import java.util.Collections;
12+
import java.util.Map;
13+
import net.bytebuddy.asm.Advice;
14+
import org.mule.runtime.api.component.Component;
15+
import org.mule.runtime.tracer.api.span.info.InitialSpanInfo;
16+
import org.mule.runtime.tracer.customization.impl.info.ExecutionInitialSpanInfo;
17+
18+
@AutoService(InstrumenterModule.class)
19+
public class ExecutionInitialSpanInfoInstrumentation extends InstrumenterModule.Tracing
20+
implements Instrumenter.ForSingleType {
21+
public ExecutionInitialSpanInfoInstrumentation() {
22+
super("mule");
23+
}
24+
25+
@Override
26+
public String instrumentedType() {
27+
return "org.mule.runtime.tracer.customization.impl.info.ExecutionInitialSpanInfo";
28+
}
29+
30+
@Override
31+
public Map<String, String> contextStore() {
32+
return Collections.singletonMap(
33+
"org.mule.runtime.tracer.api.span.info.InitialSpanInfo",
34+
"org.mule.runtime.api.component.Component");
35+
}
36+
37+
@Override
38+
public void methodAdvice(MethodTransformer transformer) {
39+
transformer.applyAdvice(
40+
isConstructor().and(takesArgument(0, named("org.mule.runtime.api.component.Component"))),
41+
getClass().getName() + "$StoreComponentAdvice");
42+
}
43+
44+
public static class StoreComponentAdvice {
45+
@Advice.OnMethodExit(suppress = Throwable.class)
46+
public static void afterConstruct(
47+
@Advice.This ExecutionInitialSpanInfo self, @Advice.Argument(0) final Component component) {
48+
InstrumentationContext.get(InitialSpanInfo.class, Component.class).put(self, component);
49+
}
50+
}
51+
}

dd-java-agent/instrumentation/mule-4/src/main/java/datadog/trace/instrumentation/mule4/MuleDecorator.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,36 @@
22

33
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
44

5+
import datadog.trace.api.Functions;
6+
import datadog.trace.api.cache.DDCache;
7+
import datadog.trace.api.cache.DDCaches;
58
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
69
import datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes;
710
import datadog.trace.bootstrap.instrumentation.api.Tags;
811
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
912
import datadog.trace.bootstrap.instrumentation.decorator.BaseDecorator;
13+
import java.util.function.Function;
14+
import org.mule.runtime.api.component.Component;
15+
import org.mule.runtime.core.api.event.CoreEvent;
1016
import org.mule.runtime.tracer.api.span.info.InitialSpanInfo;
1117

1218
public class MuleDecorator extends BaseDecorator {
1319
private static final CharSequence MULE = UTF8BytesString.create("mule");
1420
private static final CharSequence OPERATION_NAME = UTF8BytesString.create("mule.action");
1521
public static final MuleDecorator DECORATE = new MuleDecorator();
22+
private static final DDCache<CharSequence, String> TAG_CACHE = DDCaches.newFixedSizeCache(128);
23+
private static final Function<CharSequence, String> TAG_ADDER =
24+
new Functions.Prefix("mule.").andThen(new Functions.ToString<>());
25+
private static final DDCache<Component, String> COMPONENT_DOC_CACHE =
26+
DDCaches.newFixedSizeCache(1014);
27+
private static final Function<Component, String> COMPONENT_DOC_ADDER =
28+
component -> {
29+
final Object ret = component.getAnnotation(Component.Annotations.NAME_ANNOTATION_KEY);
30+
if (ret != null) {
31+
return ret.toString();
32+
}
33+
return null;
34+
};
1635

1736
@Override
1837
protected String[] instrumentationNames() {
@@ -36,7 +55,8 @@ public AgentSpan afterStart(final AgentSpan span) {
3655
return super.afterStart(span);
3756
}
3857

39-
public AgentSpan onMuleSpan(AgentSpan parentSpan, InitialSpanInfo spanInfo) {
58+
public AgentSpan onMuleSpan(
59+
AgentSpan parentSpan, InitialSpanInfo spanInfo, CoreEvent event, Component component) {
4060
// we stick with the same level of detail of OTEL exporter.
4161
// if not exportable we're not going to create a real span but we still need to track those
4262
// spans to keep a correct hierarchy.
@@ -50,9 +70,23 @@ public AgentSpan onMuleSpan(AgentSpan parentSpan, InitialSpanInfo spanInfo) {
5070
} else {
5171
span = startSpan(OPERATION_NAME, parentSpan.context());
5272
}
53-
span.setResourceName(spanInfo.getName());
54-
spanInfo.forEachAttribute(span::setTag);
55-
73+
// here we have to use the forEachAttribute since each specialized InitialSpanInfo class can add
74+
// different things through this method. Using the map version is not the same.
75+
spanInfo.forEachAttribute((s, s2) -> span.setTag(TAG_CACHE.computeIfAbsent(s, TAG_ADDER), s2));
76+
span.setTag("mule.correlation_id", event.getCorrelationId());
77+
// cache the resource name might be complex since it depends on a couple of keys
78+
String extraDetail = null;
79+
if (component != null) {
80+
extraDetail = COMPONENT_DOC_CACHE.computeIfAbsent(component, COMPONENT_DOC_ADDER);
81+
}
82+
if (extraDetail == null) {
83+
extraDetail = (String) span.getTag("mule.location");
84+
}
85+
if (extraDetail != null) {
86+
span.setResourceName(spanInfo.getName() + " " + extraDetail);
87+
} else {
88+
span.setResourceName(spanInfo.getName());
89+
}
5690
return afterStart(span);
5791
}
5892
}

dd-java-agent/instrumentation/mule-4/src/test/groovy/mule4/MuleForkedTest.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class MuleForkedTest extends WithHttpServer<MuleTestContainer> {
113113
defaultTags()
114114
}
115115
}
116-
muleSpan(it, "mule:flow")
117-
muleSpan(it, "http:request")
116+
muleSpan(it, "mule:flow", "MuleHttpServerClientTestFlow")
117+
muleSpan(it, "http:request", "Http Request")
118118
span {
119119
childOfPrevious()
120120
operationName "http.request"
@@ -182,15 +182,15 @@ class MuleForkedTest extends WithHttpServer<MuleTestContainer> {
182182
defaultTags()
183183
}
184184
}
185-
def flowParent = muleSpan(traceAssert, "mule:flow")
186-
def foreachParent = muleSpan(traceAssert, "mule:parallel-foreach", flowParent)
187-
muleSpan(traceAssert, "mule:set-payload", flowParent)
185+
def flowParent = muleSpan(traceAssert, "mule:flow", "MulePFETestFlow")
186+
def foreachParent = muleSpan(traceAssert, "mule:parallel-foreach", "PFE", flowParent)
187+
muleSpan(traceAssert, "mule:set-payload", "PFE Set Payload", flowParent)
188188
def iterationParents = []
189189
for (def pos = 1; pos <= names.size(); pos++) {
190-
iterationParents += muleSpan(traceAssert, "mule:parallel-foreach:iteration", foreachParent)
190+
iterationParents += muleSpan(traceAssert, "mule:parallel-foreach:iteration", "PFE", foreachParent)
191191
}
192192
iterationParents.each { parent ->
193-
muleSpan(traceAssert, "http:request", parent)
193+
muleSpan(traceAssert, "http:request", "PFE Request", parent)
194194
traceAssert.span {
195195
childOfPrevious()
196196
operationName "http.request"

dd-java-agent/instrumentation/mule-4/src/test/groovy/mule4/MuleHttpServerForkedTest.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class MuleHttpServerForkedTest extends HttpServerTest<MuleTestContainer> {
3535
@Override
3636
void controllerSpan(TraceAssert trace, ServerEndpoint endpoint = null) {
3737
def expectsError = endpoint == ServerEndpoint.EXCEPTION
38-
def flowSpan = muleSpan(trace, "mule:flow", null, expectsError)
39-
muleSpan(trace, "java:new", flowSpan)
40-
muleSpan(trace, "java:invoke", flowSpan, expectsError)
38+
def flowSpan = muleSpan(trace, "mule:flow", "MuleHttpServerTestFlow", null, expectsError)
39+
muleSpan(trace, "java:new", "Create Handler", flowSpan)
40+
muleSpan(trace, "java:invoke", "Handle Message", flowSpan, expectsError)
4141
super.controllerSpan(trace, endpoint)
4242
if (!expectsError) {
43-
muleSpan(trace, "mule:set-variable", flowSpan)
44-
muleSpan(trace, "mule:set-payload", flowSpan)
43+
muleSpan(trace, "mule:set-variable", "Set Response Code", flowSpan)
44+
muleSpan(trace, "mule:set-payload", "Set Response Body",flowSpan)
4545
} else {
46-
muleSpan(trace, "mule:on-error-propagate", flowSpan)
46+
muleSpan(trace, "mule:on-error-propagate", "unknown",flowSpan)
4747
}
4848
}
4949

dd-java-agent/instrumentation/mule-4/src/test/groovy/org/mule/runtime/api/util/MuleTestUtil.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import datadog.trace.bootstrap.instrumentation.api.Tags
77
import datadog.trace.core.DDSpan
88

99
class MuleTestUtil {
10-
static DDSpan muleSpan(TraceAssert traceAssert, String resource, DDSpan parent = null, boolean error = false) {
10+
static DDSpan muleSpan(TraceAssert traceAssert, String componentType, String componentName, DDSpan parent = null, boolean error = false) {
1111
def ret
1212
traceAssert.span {
1313
ret = it.span
1414
operationName "mule.action"
15-
resourceName resource
15+
resourceName "$componentType $componentName"
1616
if (parent != null) {
1717
childOf parent
1818
} else {
@@ -23,7 +23,8 @@ class MuleTestUtil {
2323
tags {
2424
"$Tags.COMPONENT" "mule"
2525
"$Tags.SPAN_KIND" "$Tags.SPAN_KIND_INTERNAL"
26-
"location" { String }
26+
"mule.location" { String }
27+
"mule.correlation_id" { String }
2728
if (error) {
2829
"$DDTags.ERROR_TYPE" { String }
2930
"$DDTags.ERROR_MSG" { String }

0 commit comments

Comments
 (0)