Update couchbase-2.6 to new agent api

This commit is contained in:
Trask Stalnaker 2019-10-19 11:57:39 -07:00
parent d40c5dcbf2
commit 2c7609f7d7
2 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,7 @@
package datadog.trace.instrumentation.couchbase.client; package datadog.trace.instrumentation.couchbase.client;
import static datadog.trace.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.instrumentation.api.AgentTracer.activeSpan;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@ -11,9 +13,8 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.ContextStore; import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.InstrumentationContext; import datadog.trace.bootstrap.InstrumentationContext;
import io.opentracing.Scope; import datadog.trace.context.TraceScope;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.util.GlobalTracer;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice;
@ -36,7 +37,7 @@ public class CouchbaseCoreInstrumentation extends Instrumenter.Default {
@Override @Override
public Map<String, String> contextStore() { public Map<String, String> contextStore() {
return Collections.singletonMap( return Collections.singletonMap(
"com.couchbase.client.core.message.CouchbaseRequest", Span.class.getName()); "com.couchbase.client.core.message.CouchbaseRequest", AgentSpan.class.getName());
} }
@Override @Override
@ -54,18 +55,18 @@ public class CouchbaseCoreInstrumentation extends Instrumenter.Default {
@Advice.OnMethodExit(suppress = Throwable.class) @Advice.OnMethodExit(suppress = Throwable.class)
public static void addOperationIdToSpan(@Advice.Argument(0) final CouchbaseRequest request) { public static void addOperationIdToSpan(@Advice.Argument(0) final CouchbaseRequest request) {
final Scope scope = GlobalTracer.get().scopeManager().active(); final TraceScope scope = activeScope();
if (scope != null) { if (scope != null) {
// The scope from the initial rxJava subscribe is not available to the networking layer // The scope from the initial rxJava subscribe is not available to the networking layer
// To transfer the span, the span is added to the context store // To transfer the span, the span is added to the context store
final ContextStore<CouchbaseRequest, Span> contextStore = final ContextStore<CouchbaseRequest, AgentSpan> contextStore =
InstrumentationContext.get(CouchbaseRequest.class, Span.class); InstrumentationContext.get(CouchbaseRequest.class, AgentSpan.class);
Span span = contextStore.get(request); AgentSpan span = contextStore.get(request);
if (span == null) { if (span == null) {
span = GlobalTracer.get().activeSpan(); span = activeSpan();
contextStore.put(request, span); contextStore.put(request, span);
if (request.operationId() != null) { if (request.operationId() != null) {

View File

@ -13,7 +13,7 @@ import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter; import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.ContextStore; import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.InstrumentationContext; import datadog.trace.bootstrap.InstrumentationContext;
import io.opentracing.Span; import datadog.trace.instrumentation.api.AgentSpan;
import io.opentracing.tag.Tags; import io.opentracing.tag.Tags;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -37,7 +37,7 @@ public class CouchbaseNetworkInstrumentation extends Instrumenter.Default {
@Override @Override
public Map<String, String> contextStore() { public Map<String, String> contextStore() {
return Collections.singletonMap( return Collections.singletonMap(
"com.couchbase.client.core.message.CouchbaseRequest", Span.class.getName()); "com.couchbase.client.core.message.CouchbaseRequest", AgentSpan.class.getName());
} }
@Override @Override
@ -61,17 +61,18 @@ public class CouchbaseNetworkInstrumentation extends Instrumenter.Default {
@Advice.FieldValue("remoteSocket") final String remoteSocket, @Advice.FieldValue("remoteSocket") final String remoteSocket,
@Advice.FieldValue("localSocket") final String localSocket, @Advice.FieldValue("localSocket") final String localSocket,
@Advice.Argument(1) final CouchbaseRequest request) { @Advice.Argument(1) final CouchbaseRequest request) {
final ContextStore<CouchbaseRequest, Span> contextStore = final ContextStore<CouchbaseRequest, AgentSpan> contextStore =
InstrumentationContext.get(CouchbaseRequest.class, Span.class); InstrumentationContext.get(CouchbaseRequest.class, AgentSpan.class);
final Span span = contextStore.get(request); final AgentSpan span = contextStore.get(request);
if (span != null) { if (span != null) {
Tags.PEER_HOSTNAME.set(span, remoteHostname); span.setTag(Tags.PEER_HOSTNAME.getKey(), remoteHostname);
if (remoteSocket != null) { if (remoteSocket != null) {
final int splitIndex = remoteSocket.lastIndexOf(":"); final int splitIndex = remoteSocket.lastIndexOf(":");
if (splitIndex != -1) { if (splitIndex != -1) {
Tags.PEER_PORT.set(span, Integer.valueOf(remoteSocket.substring(splitIndex + 1))); span.setTag(
Tags.PEER_PORT.getKey(), Integer.parseInt(remoteSocket.substring(splitIndex + 1)));
} }
} }