-
Notifications
You must be signed in to change notification settings - Fork 278
/
ApacheHttpClientInstrumentation.java
178 lines (157 loc) · 6.8 KB
/
ApacheHttpClientInstrumentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package datadog.trace.instrumentation.apachehttpclient5;
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
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.instrumentation.api.AgentScope;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.protocol.HttpContext;
@AutoService(InstrumenterModule.class)
public class ApacheHttpClientInstrumentation extends InstrumenterModule.Tracing
implements Instrumenter.CanShortcutTypeMatching {
public ApacheHttpClientInstrumentation() {
super(
"httpclient5",
"apache-httpclient5",
"apache-http-client5",
"httpclient",
"apache-httpclient",
"apache-http-client");
}
@Override
public boolean onlyMatchKnownTypes() {
return isShortcutMatchingEnabled(false);
}
@Override
public String[] knownMatchingTypes() {
return new String[] {
"org.apache.hc.client5.http.impl.classic.CloseableHttpClient",
"org.apache.hc.client5.http.impl.classic.MinimalHttpClient"
};
}
@Override
public String hierarchyMarkerType() {
return "org.apache.hc.client5.http.classic.HttpClient";
}
@Override
public ElementMatcher<TypeDescription> hierarchyMatcher() {
return implementsInterface(named(hierarchyMarkerType()));
}
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".ApacheHttpClientDecorator",
packageName + ".HttpHeadersInjectAdapter",
packageName + ".HostAndRequestAsHttpUriRequest",
packageName + ".HelperMethods",
packageName + ".WrappingStatusSettingResponseHandler",
};
}
@Override
public void methodAdvice(MethodTransformer transformer) {
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(takesArguments(1))
.and(takesArgument(0, named("org.apache.hc.core5.http.ClassicHttpRequest"))),
ApacheHttpClientInstrumentation.class.getName() + "$RequestAdvice");
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(takesArguments(2))
.and(takesArgument(0, named("org.apache.hc.core5.http.ClassicHttpRequest")))
.and(takesArgument(1, named("org.apache.hc.core5.http.protocol.HttpContext"))),
ApacheHttpClientInstrumentation.class.getName() + "$RequestAdvice");
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(takesArguments(3))
.and(takesArgument(0, named("org.apache.hc.core5.http.HttpHost")))
.and(takesArgument(1, named("org.apache.hc.core5.http.ClassicHttpRequest")))
.and(takesArgument(2, named("org.apache.hc.core5.http.protocol.HttpContext"))),
ApacheHttpClientInstrumentation.class.getName() + "$HostRequestAdvice");
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(takesArguments(2))
.and(takesArgument(0, named("org.apache.hc.core5.http.HttpHost")))
.and(takesArgument(1, named("org.apache.hc.core5.http.ClassicHttpRequest"))),
ApacheHttpClientInstrumentation.class.getName() + "$HostRequestAdvice");
transformer.applyAdvice(
isMethod()
.and(named("execute"))
.and(takesArguments(4))
.and(takesArgument(0, named("org.apache.hc.core5.http.HttpHost")))
.and(takesArgument(1, named("org.apache.hc.core5.http.ClassicHttpRequest")))
.and(takesArgument(2, named("org.apache.hc.core5.http.protocol.HttpContext")))
.and(takesArgument(3, named("org.apache.hc.core5.http.io.HttpClientResponseHandler"))),
ApacheHttpClientInstrumentation.class.getName() + "$ResponseHandlerAdvice");
}
public static class RequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(@Advice.Argument(0) final ClassicHttpRequest request) {
return HelperMethods.doMethodEnter(request);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Return final Object result,
@Advice.Thrown final Throwable throwable) {
HelperMethods.doMethodExit(scope, result, throwable);
}
}
public static class HostRequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(
@Advice.Argument(0) final HttpHost host,
@Advice.Argument(1) final ClassicHttpRequest request) {
return HelperMethods.doMethodEnter(host, request);
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Return final Object result,
@Advice.Thrown final Throwable throwable) {
HelperMethods.doMethodExit(scope, result, throwable);
}
}
public static class ResponseHandlerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(
@Advice.Argument(0) final HttpHost host,
@Advice.Argument(1) final ClassicHttpRequest request,
@Advice.Argument(2) final HttpContext context,
@Advice.Argument(
value = 3,
optional = true,
typing = Assigner.Typing.DYNAMIC,
readOnly = false)
Object handler) {
final AgentScope scope = HelperMethods.doMethodEnter(host, request);
// Wrap the handler so we capture the status code
if (null != scope && handler instanceof HttpClientResponseHandler) {
handler =
new WrappingStatusSettingResponseHandler(
scope.span(), (HttpClientResponseHandler) handler);
}
return scope;
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Return final Object result,
@Advice.Thrown final Throwable throwable) {
HelperMethods.doMethodExit(scope, result, throwable);
}
}
}