-
Notifications
You must be signed in to change notification settings - Fork 332
Cache span.kind as byte ordinal for fast isOutbound() #11116
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
Open
dougqh
wants to merge
8
commits into
master
Choose a base branch
from
dougqh/cache-span-kind-ordinal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f4d6a09
Cache span.kind as byte ordinal on DDSpanContext for fast isOutbound()
dougqh a8f31c4
Address review feedback on span.kind caching
dougqh 97e0894
Simplify span.kind ordinal mapping per review feedback
dougqh a77da4c
Extract getSpanKindTag helper and simplify benchmark setup
dougqh bd848b5
Remove isClientSpanKind helper, rename getSpanKindTag to getSpanKindS…
dougqh 632a5cd
Explicitly initialize spanKindOrdinal to SPAN_KIND_UNSET for clarity
dougqh 0913173
Add clarifying comment in removeTag about unsafeTags update
dougqh e497ad0
Merge branch 'master' into dougqh/cache-span-kind-ordinal
dougqh 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
69 changes: 69 additions & 0 deletions
69
dd-trace-core/src/jmh/java/datadog/trace/core/IsOutboundBenchmark.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,69 @@ | ||
| package datadog.trace.core; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
|
|
||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * Measures the cost of DDSpan.isOutbound(), which is called on every root span start and finish. | ||
| */ | ||
| @State(Scope.Thread) | ||
| @Warmup(iterations = 3, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(NANOSECONDS) | ||
| @Fork(value = 1) | ||
| public class IsOutboundBenchmark { | ||
|
|
||
| static final CoreTracer TRACER = CoreTracer.builder().build(); | ||
|
|
||
| private DDSpan clientSpan; | ||
| private DDSpan serverSpan; | ||
| private DDSpan unsetSpan; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| clientSpan = (DDSpan) TRACER.startSpan("benchmark", "client.op"); | ||
| clientSpan.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_CLIENT); | ||
|
|
||
| serverSpan = (DDSpan) TRACER.startSpan("benchmark", "server.op"); | ||
| serverSpan.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER); | ||
|
|
||
| unsetSpan = (DDSpan) TRACER.startSpan("benchmark", "unset.op"); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean isOutbound_client() { | ||
| return clientSpan.isOutbound(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean isOutbound_server() { | ||
| return serverSpan.isOutbound(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public boolean isOutbound_unset() { | ||
| return unsetSpan.isOutbound(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object getTag_spanKind_client() { | ||
| return clientSpan.getTag(Tags.SPAN_KIND); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object getTag_spanKind_unset() { | ||
| return unsetSpan.getTag(Tags.SPAN_KIND); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,7 @@ public boolean needsIntercept(String tag) { | |
| case HTTP_URL: | ||
| case ORIGIN_KEY: | ||
| case MEASURED: | ||
| case Tags.SPAN_KIND: | ||
| return true; | ||
|
|
||
| default: | ||
|
|
@@ -193,6 +194,11 @@ public boolean interceptTag(DDSpanContext span, String tag, Object value) { | |
| return interceptOrigin(span, value); | ||
| case MEASURED: | ||
| return interceptMeasured(span, value); | ||
| case Tags.SPAN_KIND: | ||
| // Cache the ordinal for fast isOutbound() checks. | ||
| // Return false so the value is still stored in unsafeTags for serialization. | ||
| span.setSpanKind(String.valueOf(value)); | ||
| return false; | ||
| default: | ||
| return intercept(span, tag, value); | ||
| } | ||
|
|
@@ -223,7 +229,7 @@ private static void setResourceFromUrl( | |
| path = uri == null ? null : uri.getPath(); | ||
| } | ||
| if (path != null) { | ||
| final boolean isClient = Tags.SPAN_KIND_CLIENT.equals(span.unsafeGetTag(Tags.SPAN_KIND)); | ||
| final boolean isClient = Tags.SPAN_KIND_CLIENT.equals(span.getSpanKindString()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I decided to keep the "enum" package visible for now, we cannot do a faster ordinal comparison here. I think that's okay. |
||
| Pair<CharSequence, Byte> normalized = | ||
| isClient | ||
| ? HttpResourceNames.computeForClient(method, path, false) | ||
|
|
||
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.