Skip to content

Commit 02a3250

Browse files
Move base and peer service adder before span is published (#9408)
* Move base and peer service adder before span is published * Change method signature to fix ugly groovy issues * feat(tracer): Introduce eager span tag processors --------- Co-authored-by: Bruce Bujon <bruce.bujon@datadoghq.com>
1 parent 6ce5559 commit 02a3250

8 files changed

Lines changed: 53 additions & 9 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/CoreSpan.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ public interface CoreSpan<T extends CoreSpan<T>> {
6868

6969
CharSequence getType();
7070

71+
/**
72+
* Runs early {@link datadog.trace.core.tagprocessor.TagsPostProcessor} like base service and peer
73+
* service computation. Such tags are needed before span serialization so they can’t be processed
74+
* lazily as part of the {@link #processTagsAndBaggage(MetadataConsumer)} API.
75+
*/
76+
void processServiceTags();
77+
7178
void processTagsAndBaggage(MetadataConsumer consumer);
7279

7380
T setSamplingPriority(int samplingPriority, int samplingMechanism);

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,12 @@ void write(final List<DDSpan> trace) {
11091109
if (writtenTrace.isEmpty()) {
11101110
return;
11111111
}
1112+
1113+
// run early tag postprocessors before publishing to the metrics writer since peer / base
1114+
// service are needed
1115+
for (DDSpan span : writtenTrace) {
1116+
span.processServiceTags();
1117+
}
11121118
boolean forceKeep = metricsAggregator.publish(writtenTrace);
11131119

11141120
TraceCollector traceCollector = writtenTrace.get(0).context().getTraceCollector();

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ public String getSpanType() {
694694

695695
@Override
696696
public TagMap getTags() {
697-
// This is an imutable copy of the tags
697+
// This is an immutable copy of the tags
698698
return context.getTags();
699699
}
700700

@@ -703,6 +703,11 @@ public CharSequence getType() {
703703
return context.getSpanType();
704704
}
705705

706+
@Override
707+
public void processServiceTags() {
708+
context.earlyProcessTags(links);
709+
}
710+
706711
@Override
707712
public void processTagsAndBaggage(final MetadataConsumer consumer) {
708713
context.processTagsAndBaggage(consumer, longRunningVersion, links);

dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,11 +921,17 @@ public <T> void setMetaStruct(final String field, final T value) {
921921
}
922922
}
923923

924+
public void earlyProcessTags(List<AgentSpanLink> links) {
925+
synchronized (unsafeTags) {
926+
TagsPostProcessorFactory.eagerProcessor().processTags(unsafeTags, this, links);
927+
}
928+
}
929+
924930
public void processTagsAndBaggage(
925931
final MetadataConsumer consumer, int longRunningVersion, List<AgentSpanLink> links) {
926932
synchronized (unsafeTags) {
927933
// Tags
928-
TagsPostProcessorFactory.instance().processTags(unsafeTags, this, links);
934+
TagsPostProcessorFactory.lazyProcessor().processTags(unsafeTags, this, links);
929935

930936
String linksTag = DDSpanLink.toTag(links);
931937
if (linksTag != null) {

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/TagsPostProcessorFactory.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ public final class TagsPostProcessorFactory {
99
private static boolean addRemoteHostname = true;
1010

1111
private static class Lazy {
12-
private static TagsPostProcessor create() {
13-
final List<TagsPostProcessor> processors = new ArrayList<>(7);
12+
private static TagsPostProcessor eagerProcessor = createEagerChain();
13+
private static TagsPostProcessor lazyProcessor = createLazyChain();
14+
15+
private static TagsPostProcessor createEagerChain() {
16+
final List<TagsPostProcessor> processors = new ArrayList<>(2);
1417
processors.add(new PeerServiceCalculator());
1518
if (addBaseService) {
1619
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
1720
}
21+
return new PostProcessorChain(processors.toArray(new TagsPostProcessor[0]));
22+
}
23+
24+
private static TagsPostProcessor createLazyChain() {
25+
final List<TagsPostProcessor> processors = new ArrayList<>(7);
26+
1827
processors.add(new QueryObfuscator(Config.get().getObfuscationQueryRegexp()));
1928
if (addRemoteHostname) {
2029
processors.add(new RemoteHostnameAdder(Config.get().getHostNameSupplier()));
@@ -36,12 +45,14 @@ private static TagsPostProcessor create() {
3645
return new PostProcessorChain(
3746
processors.toArray(processors.toArray(new TagsPostProcessor[0])));
3847
}
48+
}
3949

40-
private static TagsPostProcessor instance = create();
50+
public static TagsPostProcessor eagerProcessor() {
51+
return Lazy.eagerProcessor;
4152
}
4253

43-
public static TagsPostProcessor instance() {
44-
return Lazy.instance;
54+
public static TagsPostProcessor lazyProcessor() {
55+
return Lazy.lazyProcessor;
4556
}
4657

4758
/**
@@ -51,7 +62,7 @@ public static TagsPostProcessor instance() {
5162
*/
5263
public static void withAddBaseService(boolean enabled) {
5364
addBaseService = enabled;
54-
Lazy.instance = Lazy.create();
65+
Lazy.eagerProcessor = Lazy.createEagerChain();
5566
}
5667

5768
/**
@@ -61,7 +72,7 @@ public static void withAddBaseService(boolean enabled) {
6172
*/
6273
public static void withAddRemoteHostname(boolean enabled) {
6374
addRemoteHostname = enabled;
64-
Lazy.instance = Lazy.create();
75+
Lazy.lazyProcessor = Lazy.createLazyChain();
6576
}
6677

6778
/** Used for testing purposes. It reset the singleton and restore default options */

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/SimpleSpan.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ class SimpleSpan implements CoreSpan<SimpleSpan> {
206206
return type
207207
}
208208

209+
@Override
210+
void processServiceTags() {}
211+
209212
@Override
210213
void processTagsAndBaggage(MetadataConsumer consumer) {}
211214

dd-trace-core/src/test/groovy/datadog/trace/common/writer/TraceGenerator.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ class TraceGenerator {
333333
return this.type
334334
}
335335

336+
@Override
337+
void processServiceTags() {}
338+
336339
@Override
337340
void processTagsAndBaggage(MetadataConsumer consumer) {
338341
consumer.accept(metadata)

dd-trace-core/src/traceAgentTest/groovy/TraceGenerator.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ class TraceGenerator {
309309
return type
310310
}
311311

312+
@Override
313+
void processServiceTags() {}
314+
312315
@Override
313316
void processTagsAndBaggage(MetadataConsumer consumer) {
314317
consumer.accept(metadata)

0 commit comments

Comments
 (0)