Skip to content

Commit f8420e6

Browse files
authored
Adding trace-level log info to HTTP spans (#1425)
1 parent cea4237 commit f8420e6

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/transaction/TraceContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ <C> void propagateTraceContext(C carrier, TextHeaderSetter<C> headerSetter) {
539539
String tracestateHeaderValue = TextTracestateAppender.instance().join(tracestate, coreConfiguration.getTracestateSizeLimit());
540540
headerSetter.setHeader(TRACESTATE_HEADER_NAME, tracestateHeaderValue, carrier);
541541
}
542+
logger.trace("Trace context headers added to {}", carrier);
542543
}
543544

544545
/**

apm-agent-plugins/apm-httpclient-core/src/main/java/co/elastic/apm/agent/http/client/HttpClientHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@
2828
import co.elastic.apm.agent.impl.context.Destination;
2929
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
3030
import co.elastic.apm.agent.impl.transaction.Span;
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
3133

3234
import javax.annotation.Nullable;
3335
import java.net.URI;
3436

3537
public class HttpClientHelper {
38+
39+
private static final Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);
40+
3641
public static final String EXTERNAL_TYPE = "external";
3742
public static final String HTTP_SUBTYPE = "http";
3843

@@ -68,6 +73,9 @@ public static Span startHttpClientSpan(AbstractSpan<?> parent, String method, @N
6873
}
6974
setDestinationServiceDetails(span, scheme, hostName, port);
7075
}
76+
if (logger.isTraceEnabled()) {
77+
logger.trace("Created an HTTP exit span: {} for URI: {}. Parent span: {}", span, uri, parent);
78+
}
7179
return span;
7280
}
7381

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package co.elastic.apm.agent.resttemplate;
2+
3+
import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
4+
import co.elastic.apm.agent.http.client.HttpClientHelper;
5+
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
6+
import co.elastic.apm.agent.impl.transaction.Span;
7+
import net.bytebuddy.asm.Advice;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.http.client.ClientHttpRequest;
11+
import org.springframework.http.client.ClientHttpResponse;
12+
13+
import javax.annotation.Nullable;
14+
import java.io.IOException;
15+
import java.util.Objects;
16+
17+
public class SpringRestTemplateAdvice {
18+
19+
private static final Logger logger = LoggerFactory.getLogger(SpringRestTemplateAdvice.class);
20+
21+
@Nullable
22+
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
23+
public static Object beforeExecute(@Advice.This ClientHttpRequest request) {
24+
logger.trace("Enter advice for method {}#execute()", request.getClass().getName());
25+
if (TracerAwareInstrumentation.tracer.getActive() == null) {
26+
return null;
27+
}
28+
final AbstractSpan<?> parent = TracerAwareInstrumentation.tracer.getActive();
29+
Span span = HttpClientHelper.startHttpClientSpan(parent, Objects.toString(request.getMethod()), request.getURI(), request.getURI().getHost());
30+
if (span != null) {
31+
span.activate();
32+
span.propagateTraceContext(request, SpringRestRequestHeaderSetter.INSTANCE);
33+
return span;
34+
}
35+
return null;
36+
}
37+
38+
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
39+
public static void afterExecute(@Advice.Return @Nullable ClientHttpResponse clientHttpResponse,
40+
@Advice.Enter @Nullable Object spanObj,
41+
@Advice.Thrown @Nullable Throwable t) throws IOException {
42+
logger.trace("Exit advice for RestTemplate client execute() method, span object: {}", spanObj);
43+
if (spanObj instanceof Span) {
44+
Span span = (Span) spanObj;
45+
try {
46+
if (clientHttpResponse != null) {
47+
int statusCode = clientHttpResponse.getRawStatusCode();
48+
span.getContext().getHttp().withStatusCode(statusCode);
49+
}
50+
span.captureException(t);
51+
} finally {
52+
span.deactivate().end();
53+
}
54+
}
55+
}
56+
}

apm-agent-plugins/apm-spring-resttemplate-plugin/src/main/java/co/elastic/apm/agent/resttemplate/SpringRestTemplateInstrumentation.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,12 @@
2525
package co.elastic.apm.agent.resttemplate;
2626

2727
import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
28-
import co.elastic.apm.agent.http.client.HttpClientHelper;
29-
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
30-
import co.elastic.apm.agent.impl.transaction.Span;
31-
import net.bytebuddy.asm.Advice;
3228
import net.bytebuddy.description.method.MethodDescription;
3329
import net.bytebuddy.description.type.TypeDescription;
3430
import net.bytebuddy.matcher.ElementMatcher;
35-
import org.springframework.http.client.ClientHttpRequest;
36-
import org.springframework.http.client.ClientHttpResponse;
3731

38-
import javax.annotation.Nullable;
39-
import java.io.IOException;
4032
import java.util.Arrays;
4133
import java.util.Collection;
42-
import java.util.Objects;
4334

4435
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
4536
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
@@ -77,40 +68,4 @@ public Class<?> getAdviceClass() {
7768
public Collection<String> getInstrumentationGroupNames() {
7869
return Arrays.asList("http-client", "spring-resttemplate");
7970
}
80-
81-
public static class SpringRestTemplateAdvice {
82-
@Nullable
83-
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
84-
public static Object beforeExecute(@Advice.This ClientHttpRequest request) {
85-
if (tracer.getActive() == null) {
86-
return null;
87-
}
88-
final AbstractSpan<?> parent = tracer.getActive();
89-
Span span = HttpClientHelper.startHttpClientSpan(parent, Objects.toString(request.getMethod()), request.getURI(), request.getURI().getHost());
90-
if (span != null) {
91-
span.activate();
92-
span.propagateTraceContext(request, SpringRestRequestHeaderSetter.INSTANCE);
93-
return span;
94-
}
95-
return null;
96-
}
97-
98-
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
99-
public static void afterExecute(@Advice.Return @Nullable ClientHttpResponse clientHttpResponse,
100-
@Advice.Enter @Nullable Object spanObj,
101-
@Advice.Thrown @Nullable Throwable t) throws IOException {
102-
if (spanObj instanceof Span) {
103-
Span span = (Span) spanObj;
104-
try {
105-
if (clientHttpResponse != null) {
106-
int statusCode = clientHttpResponse.getRawStatusCode();
107-
span.getContext().getHttp().withStatusCode(statusCode);
108-
}
109-
span.captureException(t);
110-
} finally {
111-
span.deactivate().end();
112-
}
113-
}
114-
}
115-
}
11671
}

0 commit comments

Comments
 (0)