-
Notifications
You must be signed in to change notification settings - Fork 303
Add a built-in trace interceptor for keeping traces depending of their latency #8040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3fefc0d
add latency trace interceptor
c1a7978
Fix text
ea65fd8
add comments
955e382
improve comments
f127aa7
changes after review
16496e9
Add "experimental" in configuration key
cecile75 a287c8d
Add "experimental" in configuration key (for tests)
cecile75 d63fbf5
spotlessApply
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.keep.latency.threshold.ms", latencyThreshold) | ||
cecile75 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.