-
Notifications
You must be signed in to change notification settings - Fork 322
Multi-tracing support for Couchbase 3.2+ #10147
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
ValentinZakharov
wants to merge
3
commits into
master
Choose a base branch
from
vzakharov/couchbase-multitrace
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
3 commits
Select commit
Hold shift + click to select a range
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
38 changes: 38 additions & 0 deletions
38
.../trace/instrumentation/couchbase_32/client/CoreEnvironmentBuilderRequestTracerAdvice.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,38 @@ | ||
| package datadog.trace.instrumentation.couchbase_32.client; | ||
|
|
||
| import com.couchbase.client.core.Core; | ||
| import com.couchbase.client.core.cnc.RequestTracer; | ||
| import datadog.trace.bootstrap.ContextStore; | ||
| import datadog.trace.bootstrap.InstrumentationContext; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| public class CoreEnvironmentBuilderRequestTracerAdvice { | ||
| @Advice.OnMethodEnter(suppress = Throwable.class) | ||
| public static void onEnter( | ||
| @Advice.Argument(value = 0, readOnly = false) RequestTracer requestTracer) { | ||
|
|
||
| // already a delegating tracer | ||
| if (requestTracer instanceof DelegatingRequestTracer) { | ||
| return; | ||
| } | ||
|
|
||
| // already a datadog tracer | ||
| if (requestTracer instanceof DatadogRequestTracer) { | ||
| return; | ||
| } | ||
|
|
||
| ContextStore<Core, String> coreContext = InstrumentationContext.get(Core.class, String.class); | ||
|
|
||
| DatadogRequestTracer datadogTracer = new DatadogRequestTracer(AgentTracer.get(), coreContext); | ||
|
|
||
| // if the app didn't set a custom tracer, use only datadog tracer | ||
| if (requestTracer == null) { | ||
| requestTracer = datadogTracer; | ||
| return; | ||
| } | ||
|
|
||
| // Wrap custom datadog and cnc tracers into a delegating | ||
| requestTracer = new DelegatingRequestTracer(datadogTracer, requestTracer); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
...rc/main/java/datadog/trace/instrumentation/couchbase_32/client/DelegatingRequestSpan.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,72 @@ | ||
| package datadog.trace.instrumentation.couchbase_32.client; | ||
|
|
||
| import com.couchbase.client.core.cnc.RequestSpan; | ||
| import com.couchbase.client.core.msg.RequestContext; | ||
| import java.time.Instant; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| /** RequestSpan, which delegates all calls to two other RequestSpans */ | ||
| public class DelegatingRequestSpan implements RequestSpan { | ||
|
|
||
| private final RequestSpan ddSpan; | ||
| private final RequestSpan cncSpan; | ||
|
|
||
| public DelegatingRequestSpan(@Nonnull RequestSpan ddSpan, @Nonnull RequestSpan cncSpan) { | ||
| this.ddSpan = ddSpan; | ||
| this.cncSpan = cncSpan; | ||
| } | ||
|
|
||
| public RequestSpan getDatadogSpan() { | ||
| return ddSpan; | ||
| } | ||
|
|
||
| public RequestSpan getCncSpan() { | ||
| return cncSpan; | ||
| } | ||
|
|
||
| @Override | ||
| public void attribute(String key, String value) { | ||
| ddSpan.attribute(key, value); | ||
| cncSpan.attribute(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void attribute(String key, boolean value) { | ||
| ddSpan.attribute(key, value); | ||
| cncSpan.attribute(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void attribute(String key, long value) { | ||
| ddSpan.attribute(key, value); | ||
| cncSpan.attribute(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public void event(String name, Instant timestamp) { | ||
| ddSpan.event(name, timestamp); | ||
| cncSpan.event(name, timestamp); | ||
| } | ||
|
|
||
| @Override | ||
| public void status(StatusCode status) { | ||
| ddSpan.status(status); | ||
| cncSpan.status(status); | ||
| } | ||
|
|
||
| @Override | ||
| public void end() { | ||
| try { | ||
| ddSpan.end(); | ||
| } finally { | ||
| // guarantee cnc spans get ended even if ddSpan.end() throws exception | ||
| cncSpan.end(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void requestContext(RequestContext requestContext) { | ||
| ddSpan.requestContext(requestContext); | ||
| cncSpan.requestContext(requestContext); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
.../main/java/datadog/trace/instrumentation/couchbase_32/client/DelegatingRequestTracer.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,70 @@ | ||
| package datadog.trace.instrumentation.couchbase_32.client; | ||
|
|
||
| import com.couchbase.client.core.cnc.RequestSpan; | ||
| import com.couchbase.client.core.cnc.RequestTracer; | ||
| import com.couchbase.client.core.cnc.tracing.NoopRequestSpan; | ||
| import java.time.Duration; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| public class DelegatingRequestTracer implements RequestTracer { | ||
|
|
||
| private final DatadogRequestTracer ddTracer; | ||
| private final RequestTracer cncTracer; | ||
|
|
||
| public DelegatingRequestTracer(DatadogRequestTracer ddTracer, RequestTracer cncTracer) { | ||
| this.ddTracer = ddTracer; | ||
| this.cncTracer = cncTracer; | ||
| } | ||
|
|
||
| @Override | ||
| public RequestSpan requestSpan(String name, RequestSpan parent) { | ||
| RequestSpan ddParentSpan = unwrapDatadogParentSpan(parent); | ||
| RequestSpan cncParentSpan = unwrapCncParentSpan(parent); | ||
|
|
||
| RequestSpan ddSpan = ddTracer != null ? ddTracer.requestSpan(name, ddParentSpan) : null; | ||
| RequestSpan cncSpan = cncTracer != null ? cncTracer.requestSpan(name, cncParentSpan) : null; | ||
|
|
||
| // no tracers are present - return noop span | ||
| if (ddSpan == null && cncSpan == null) { | ||
| return NoopRequestSpan.INSTANCE; | ||
| } | ||
|
|
||
| // only one tracer is present - no need to delegate | ||
| if (ddSpan == null) { | ||
| return cncSpan; | ||
| } | ||
| if (cncSpan == null) { | ||
| return ddSpan; | ||
| } | ||
|
|
||
| return new DelegatingRequestSpan(ddSpan, cncSpan); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Void> start() { | ||
| Mono<Void> primary = ddTracer != null ? ddTracer.start() : Mono.empty(); | ||
| Mono<Void> secondary = cncTracer != null ? cncTracer.start() : Mono.empty(); | ||
| return Mono.when(primary, secondary); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<Void> stop(Duration timeout) { | ||
| Mono<Void> primary = ddTracer != null ? ddTracer.stop(timeout) : Mono.empty(); | ||
| Mono<Void> secondary = cncTracer != null ? cncTracer.stop(timeout) : Mono.empty(); | ||
| return Mono.when(primary, secondary); | ||
| } | ||
|
|
||
| private static RequestSpan unwrapDatadogParentSpan(RequestSpan parent) { | ||
| if (parent instanceof DelegatingRequestSpan) { | ||
| return ((DelegatingRequestSpan) parent).getDatadogSpan(); | ||
| } | ||
| return parent; | ||
| } | ||
|
|
||
| private static RequestSpan unwrapCncParentSpan(RequestSpan parent) { | ||
| if (parent instanceof DelegatingRequestSpan) { | ||
| return ((DelegatingRequestSpan) parent).getCncSpan(); | ||
| } | ||
| return parent; | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "cnc" stand for?