Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion dd-java-agent/instrumentation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ TaskProvider<JavaExec> registerIndexTask(String indexTaskName, String indexer, S
}

instrumentationNaming {
exclusions = ["org-json-20230227"] // org-json does not use semver
exclusions = [
"org-json-20230227",
// org-json does not use semver
"feign-10.8-generated", // research convention: toolkit-generated side-by-side eval module
]
suffixes = ["-common", "-stubs", "-iast"]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
muzzle {
pass {
group = "io.github.openfeign"
module = "feign-core"
versions = "[10.8,)"
}
}

apply from: "$rootDir/gradle/java.gradle"

addTestSuiteForDir('latestDepTest', 'test')

dependencies {
compileOnly(group: 'io.github.openfeign', name: 'feign-core', version: '10.8')

testImplementation(group: 'io.github.openfeign', name: 'feign-core', version: '10.8')

latestDepTestImplementation group: 'io.github.openfeign', name: 'feign-core', version: '10+'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package datadog.trace.instrumentation.feign;

import static datadog.context.Context.current;
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.DECORATE;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.FEIGN;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.feign.RequestHeaderInjectAdapter.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import feign.AsyncClient;
import feign.Request;
import feign.Response;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumenterModule.class)
public class FeignAsyncClientInstrumentation extends InstrumenterModule.Tracing
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice {

public FeignAsyncClientInstrumentation() {
super("feign", "feign-10.8");
}

@Override
public String hierarchyMarkerType() {
return "feign.AsyncClient";
}

@Override
public ElementMatcher<TypeDescription> hierarchyMatcher() {
return implementsInterface(named(hierarchyMarkerType()));
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".FeignClientDecorator",
packageName + ".RequestHeaderInjectAdapter",
packageName + ".SpanFinishingCallback",
};
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(isPublic())
.and(takesArguments(3))
.and(takesArgument(0, named("feign.Request")))
.and(takesArgument(1, named("feign.Request$Options"))),
FeignAsyncClientInstrumentation.class.getName() + "$AsyncExecuteAdvice");
}

public static class AsyncExecuteAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(
@Advice.Argument(value = 0, readOnly = false) Request request) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(AsyncClient.class);
if (callDepth > 0) {
return null;
}

final AgentSpan span = startSpan(FEIGN.toString(), HTTP_REQUEST);
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);

final AgentScope scope = activateSpan(span);

// Inject trace context into request headers
Map<String, Collection<String>> injectedHeaders = new LinkedHashMap<>();
DECORATE.injectContext(current(), injectedHeaders, SETTER);
request = RequestHeaderInjectAdapter.inject(request, injectedHeaders);

return scope;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Return(readOnly = false) CompletableFuture<Response> future,
@Advice.Thrown final Throwable throwable) {
if (scope == null) {
return;
}
final AgentSpan span = scope.span();
scope.close();
CallDepthThreadLocalMap.reset(AsyncClient.class);

if (throwable != null) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
span.finish();
return;
}

if (future != null) {
future = future.whenComplete(new SpanFinishingCallback(span));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve cancellation on async Feign futures

When an AsyncClient implementation returns a cancellable CompletableFuture (for example Feign's async clients use cancellation to abort in-flight requests), replacing it with the dependent stage from whenComplete breaks that contract: cancelling the future returned to user code only cancels the dependent stage and leaves the original request running. Register the callback without assigning it back, or otherwise propagate cancellation to the original future.

Useful? React with 👍 / 👎.

} else {
DECORATE.beforeFinish(span);
span.finish();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package datadog.trace.instrumentation.feign;

import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.bootstrap.instrumentation.decorator.HttpClientDecorator;
import feign.Request;
import feign.Response;
import java.net.URI;
import java.net.URISyntaxException;

public class FeignClientDecorator extends HttpClientDecorator<Request, Response> {

public static final CharSequence FEIGN = UTF8BytesString.create("feign");
public static final FeignClientDecorator DECORATE = new FeignClientDecorator();

public static final CharSequence HTTP_REQUEST = UTF8BytesString.create(DECORATE.operationName());

@Override
protected String[] instrumentationNames() {
return new String[] {"feign", "feign-10.8"};
}

@Override
protected CharSequence component() {
return FEIGN;
}

@Override
protected String method(final Request request) {
return request.httpMethod().name();
}

@Override
protected URI url(final Request request) {
try {
return new URI(request.url());
} catch (URISyntaxException e) {
return null;
}
}

@Override
protected int status(final Response response) {
return response.status();
}

@Override
protected String getRequestHeader(Request request, String headerName) {
java.util.Collection<String> values = request.headers().get(headerName);
if (values != null && !values.isEmpty()) {
return values.iterator().next();
}
return null;
}

@Override
protected String getResponseHeader(Response response, String headerName) {
java.util.Collection<String> values = response.headers().get(headerName);
if (values != null && !values.isEmpty()) {
return values.iterator().next();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package datadog.trace.instrumentation.feign;

import static datadog.context.Context.current;
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.DECORATE;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.FEIGN;
import static datadog.trace.instrumentation.feign.FeignClientDecorator.HTTP_REQUEST;
import static datadog.trace.instrumentation.feign.RequestHeaderInjectAdapter.SETTER;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import feign.Client;
import feign.Request;
import feign.Response;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(InstrumenterModule.class)
public class FeignClientInstrumentation extends InstrumenterModule.Tracing
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice {

public FeignClientInstrumentation() {
super("feign", "feign-10.8");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep generated Feign instrumentation opt-in

This generated/eval module is documented in the new metadata as disabled by default, but InstrumenterModule.Tracing uses defaultEnabled() from the base class unless it is overridden, so this constructor enables the sync and async Feign instrumentations for every Feign 10.8+ application by default. If this module is meant to be opt-in, override defaultEnabled() in both instrumentation classes; otherwise the metadata default is misleading and users will get spans unless they explicitly opt out.

Useful? React with 👍 / 👎.

}

@Override
public String hierarchyMarkerType() {
return "feign.Client";
}

@Override
public ElementMatcher<TypeDescription> hierarchyMatcher() {
return implementsInterface(named(hierarchyMarkerType()));
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".FeignClientDecorator", packageName + ".RequestHeaderInjectAdapter",
};
}

@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(isPublic())
.and(takesArguments(2))
.and(takesArgument(0, named("feign.Request")))
.and(takesArgument(1, named("feign.Request$Options"))),
FeignClientInstrumentation.class.getName() + "$ExecuteAdvice");
}

public static class ExecuteAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(
@Advice.Argument(value = 0, readOnly = false) Request request) {
final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Client.class);
if (callDepth > 0) {
return null;
}

final AgentSpan span = startSpan(FEIGN.toString(), HTTP_REQUEST);
DECORATE.afterStart(span);
DECORATE.onRequest(span, request);

final AgentScope scope = activateSpan(span);

// Inject trace context into request headers
Map<String, Collection<String>> injectedHeaders = new LinkedHashMap<>();
DECORATE.injectContext(current(), injectedHeaders, SETTER);
request = RequestHeaderInjectAdapter.inject(request, injectedHeaders);

return scope;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Return final Response response,
@Advice.Thrown final Throwable throwable) {
if (scope == null) {
return;
}
final AgentSpan span = scope.span();
try {
if (response != null) {
DECORATE.onResponse(span, response);
}
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
} finally {
scope.close();
span.finish();
CallDepthThreadLocalMap.reset(Client.class);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package datadog.trace.instrumentation.feign;

import datadog.context.propagation.CarrierSetter;
import feign.Request;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
public class RequestHeaderInjectAdapter implements CarrierSetter<Map<String, Collection<String>>> {

public static final RequestHeaderInjectAdapter SETTER = new RequestHeaderInjectAdapter();

@Override
public void set(
final Map<String, Collection<String>> carrier, final String key, final String value) {
Collection<String> values = new ArrayList<>(1);
values.add(value);
carrier.put(key, values);
}

/**
* Feign Request objects are immutable — headers cannot be modified after creation. This method
* creates a new Request with the trace context headers injected.
*/
public static Request inject(
final Request original, final Map<String, Collection<String>> injectedHeaders) {
Map<String, Collection<String>> merged = new LinkedHashMap<>(original.headers());
merged.putAll(injectedHeaders);
return Request.create(
original.httpMethod(), original.url(), merged, original.body(), StandardCharsets.UTF_8);
}
}
Loading
Loading