-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a built-in trace interceptor for keeping traces depending of thei…
…r latency (#8040) * add latency trace interceptor * Fix test * add comments * improve comments * changes after review * Add "experimental" in configuration key Co-authored-by: Brian Marks <[email protected]> * Add "experimental" in configuration key (for tests) Co-authored-by: Brian Marks <[email protected]> * spotlessApply --------- Co-authored-by: Cecile Terpin <“[email protected]”> Co-authored-by: Brian Marks <[email protected]>
- Loading branch information
1 parent
39e43da
commit 2b24697
Showing
7 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
dd-trace-core/src/main/java/datadog/trace/core/traceinterceptor/LatencyTraceInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package datadog.trace.core.traceinterceptor; | ||
|
||
import datadog.trace.api.Config; | ||
import datadog.trace.api.DDTags; | ||
import datadog.trace.api.interceptor.AbstractTraceInterceptor; | ||
import datadog.trace.api.interceptor.MutableSpan; | ||
import datadog.trace.api.interceptor.TraceInterceptor; | ||
import java.util.Collection; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* This trace latency interceptor is disabled by default. We can activate it by setting the value of | ||
* dd.trace.latency.interceptor.value to a positive value This value should be in milliseconds and | ||
* this interceptor will retain any local trace who has a root span duration greater than this | ||
* value. The activation of this interceptor is ignored if partial flush is enabled in order to | ||
* avoid incomplete local trace (incomplete chunk of trace). Note that since we're changing the | ||
* sampling priority at the end of local trace, there is no guarantee to get complete traces, since | ||
* the original sampling priority for this trace may have already been propagated. | ||
*/ | ||
public class LatencyTraceInterceptor extends AbstractTraceInterceptor { | ||
private static final Logger log = LoggerFactory.getLogger(LatencyTraceInterceptor.class); | ||
// duration configured in ms, need to be converted in nano seconds | ||
private static final long LATENCY = Config.get().getTraceKeepLatencyThreshold() * 1000000L; | ||
|
||
public static final TraceInterceptor INSTANCE = | ||
new LatencyTraceInterceptor(Priority.ROOT_SPAN_LATENCY); | ||
|
||
protected LatencyTraceInterceptor(Priority priority) { | ||
super(priority); | ||
} | ||
|
||
@Override | ||
public Collection<? extends MutableSpan> onTraceComplete( | ||
Collection<? extends MutableSpan> latencyTrace) { | ||
if (latencyTrace.isEmpty()) { | ||
return latencyTrace; | ||
} | ||
MutableSpan rootSpan = latencyTrace.iterator().next().getLocalRootSpan(); | ||
if (rootSpan != null && rootSpan.getDurationNano() > LATENCY) { | ||
rootSpan.setTag(DDTags.MANUAL_KEEP, true); | ||
} | ||
return latencyTrace; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...re/src/test/groovy/datadog/trace/core/traceinterceptor/LatencyTraceInterceptorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package datadog.trace.core.traceinterceptor | ||
|
||
import datadog.trace.api.DDTags | ||
import datadog.trace.common.writer.ListWriter | ||
|
||
import datadog.trace.core.test.DDCoreSpecification | ||
|
||
import spock.lang.Timeout | ||
|
||
@Timeout(10) | ||
class LatencyTraceInterceptorTest extends DDCoreSpecification { | ||
|
||
|
||
def "test set sampling priority according to latency"() { | ||
setup: | ||
|
||
injectSysConfig("trace.partial.flush.enabled", partialFlushEnabled) | ||
injectSysConfig("trace.experimental.keep.latency.threshold.ms", latencyThreshold) | ||
|
||
when: | ||
def writer = new ListWriter() | ||
def tracer = tracerBuilder().writer(writer).build() | ||
|
||
def spanSetup = tracer.buildSpan("test","my_operation_name").withTag(priorityTag, true).start() | ||
sleep(minDuration) | ||
spanSetup.finish() | ||
|
||
then: | ||
def trace = writer.firstTrace() | ||
trace.size() == 1 | ||
def span = trace[0] | ||
span.context().getSamplingPriority() == expected | ||
|
||
cleanup: | ||
tracer.close() | ||
|
||
where: | ||
partialFlushEnabled | latencyThreshold | priorityTag | minDuration | expected | ||
"true" | "200" | DDTags.MANUAL_KEEP | 10 | 2 | ||
"true" | "200" | DDTags.MANUAL_DROP | 10 | -1 | ||
"true" | "200" | DDTags.MANUAL_KEEP | 300 | 2 | ||
"true" | "200" | DDTags.MANUAL_DROP | 300 | -1 | ||
"false" | "200" | DDTags.MANUAL_KEEP | 10 | 2 | ||
"false" | "200" | DDTags.MANUAL_DROP | 10 | -1 | ||
"false" | "200" | DDTags.MANUAL_KEEP | 300 | 2 | ||
"false" | "200" | DDTags.MANUAL_DROP | 300 | 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters