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 @@ -68,6 +68,13 @@ public interface CoreSpan<T extends CoreSpan<T>> {

CharSequence getType();

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

void processTagsAndBaggage(MetadataConsumer consumer);

T setSamplingPriority(int samplingPriority, int samplingMechanism);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,12 @@ void write(final List<DDSpan> trace) {
if (writtenTrace.isEmpty()) {
return;
}

// run early tag postprocessors before publishing to the metrics writer since peer / base
// service are needed
for (DDSpan span : writtenTrace) {
span.processServiceTags();
}
boolean forceKeep = metricsAggregator.publish(writtenTrace);

TraceCollector traceCollector = writtenTrace.get(0).context().getTraceCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public String getSpanType() {

@Override
public TagMap getTags() {
// This is an imutable copy of the tags
// This is an immutable copy of the tags
return context.getTags();
}

Expand All @@ -703,6 +703,11 @@ public CharSequence getType() {
return context.getSpanType();
}

@Override
public void processServiceTags() {
context.earlyProcessTags(links);
}

@Override
public void processTagsAndBaggage(final MetadataConsumer consumer) {
context.processTagsAndBaggage(consumer, longRunningVersion, links);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,11 +921,17 @@ public <T> void setMetaStruct(final String field, final T value) {
}
}

public void earlyProcessTags(List<AgentSpanLink> links) {
synchronized (unsafeTags) {
TagsPostProcessorFactory.eagerProcessor().processTags(unsafeTags, this, links);
}
}

public void processTagsAndBaggage(
final MetadataConsumer consumer, int longRunningVersion, List<AgentSpanLink> links) {
synchronized (unsafeTags) {
// Tags
TagsPostProcessorFactory.instance().processTags(unsafeTags, this, links);
TagsPostProcessorFactory.lazyProcessor().processTags(unsafeTags, this, links);

String linksTag = DDSpanLink.toTag(links);
if (linksTag != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ public final class TagsPostProcessorFactory {
private static boolean addRemoteHostname = true;

private static class Lazy {
private static TagsPostProcessor create() {
final List<TagsPostProcessor> processors = new ArrayList<>(7);
private static TagsPostProcessor eagerProcessor = createEagerChain();
private static TagsPostProcessor lazyProcessor = createLazyChain();

private static TagsPostProcessor createEagerChain() {
final List<TagsPostProcessor> processors = new ArrayList<>(2);
processors.add(new PeerServiceCalculator());
if (addBaseService) {
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
}
return new PostProcessorChain(processors.toArray(new TagsPostProcessor[0]));
}

private static TagsPostProcessor createLazyChain() {
final List<TagsPostProcessor> processors = new ArrayList<>(7);

processors.add(new QueryObfuscator(Config.get().getObfuscationQueryRegexp()));
if (addRemoteHostname) {
processors.add(new RemoteHostnameAdder(Config.get().getHostNameSupplier()));
Expand All @@ -36,12 +45,14 @@ private static TagsPostProcessor create() {
return new PostProcessorChain(
processors.toArray(processors.toArray(new TagsPostProcessor[0])));
}
}

private static TagsPostProcessor instance = create();
public static TagsPostProcessor eagerProcessor() {
return Lazy.eagerProcessor;
}

public static TagsPostProcessor instance() {
return Lazy.instance;
public static TagsPostProcessor lazyProcessor() {
return Lazy.lazyProcessor;
}

/**
Expand All @@ -51,7 +62,7 @@ public static TagsPostProcessor instance() {
*/
public static void withAddBaseService(boolean enabled) {
addBaseService = enabled;
Lazy.instance = Lazy.create();
Lazy.eagerProcessor = Lazy.createEagerChain();
}

/**
Expand All @@ -61,7 +72,7 @@ public static void withAddBaseService(boolean enabled) {
*/
public static void withAddRemoteHostname(boolean enabled) {
addRemoteHostname = enabled;
Lazy.instance = Lazy.create();
Lazy.lazyProcessor = Lazy.createLazyChain();
}

/** Used for testing purposes. It reset the singleton and restore default options */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ class SimpleSpan implements CoreSpan<SimpleSpan> {
return type
}

@Override
void processServiceTags() {}

@Override
void processTagsAndBaggage(MetadataConsumer consumer) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ class TraceGenerator {
return this.type
}

@Override
void processServiceTags() {}

@Override
void processTagsAndBaggage(MetadataConsumer consumer) {
consumer.accept(metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ class TraceGenerator {
return type
}

@Override
void processServiceTags() {}

@Override
void processTagsAndBaggage(MetadataConsumer consumer) {
consumer.accept(metadata)
Expand Down