2
2
3
3
import static datadog .trace .agent .tooling .bytebuddy .matcher .NameMatchers .named ;
4
4
import static datadog .trace .agent .tooling .bytebuddy .matcher .NameMatchers .namedOneOf ;
5
- import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .activateSpan ;
6
- import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .propagate ;
7
- import static datadog .trace .bootstrap .instrumentation .api .AgentTracer .startSpan ;
8
- import static datadog .trace .bootstrap .instrumentation .decorator .HttpServerDecorator .DD_SPAN_ATTRIBUTE ;
9
5
import static datadog .trace .bootstrap .instrumentation .java .concurrent .ExcludeFilter .ExcludeType .RUNNABLE ;
10
- import static datadog .trace .instrumentation .jetty11 .JettyDecorator .DECORATE ;
11
- import static datadog .trace .instrumentation .jetty11 .JettyDecorator .SERVLET_REQUEST ;
12
- import static datadog .trace .instrumentation .jetty11 .RequestExtractAdapter .GETTER ;
13
6
import static net .bytebuddy .matcher .ElementMatchers .declaresMethod ;
14
7
import static net .bytebuddy .matcher .ElementMatchers .takesNoArguments ;
15
8
16
9
import com .google .auto .service .AutoService ;
17
10
import datadog .trace .agent .tooling .ExcludeFilterProvider ;
18
11
import datadog .trace .agent .tooling .Instrumenter ;
19
- import datadog .trace .api .CorrelationIdentifier ;
20
- import datadog .trace .api .GlobalTracer ;
21
- import datadog .trace .bootstrap .instrumentation .api .AgentScope ;
22
- import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
23
12
import datadog .trace .bootstrap .instrumentation .java .concurrent .ExcludeFilter ;
24
13
import java .util .Arrays ;
25
14
import java .util .Collection ;
26
15
import java .util .Collections ;
27
16
import java .util .Map ;
28
- import net .bytebuddy .asm .Advice ;
29
17
import net .bytebuddy .description .method .MethodDescription ;
30
18
import net .bytebuddy .description .type .TypeDescription ;
31
19
import net .bytebuddy .matcher .ElementMatcher ;
32
- import org .eclipse .jetty .server .HttpChannel ;
33
- import org .eclipse .jetty .server .Request ;
34
20
35
21
@ AutoService (Instrumenter .class )
36
22
public final class JettyServerInstrumentation extends Instrumenter .Tracing
@@ -51,6 +37,9 @@ public String[] helperClassNames() {
51
37
packageName + ".JettyDecorator" ,
52
38
packageName + ".RequestExtractAdapter" ,
53
39
packageName + ".RequestURIDataAdapter" ,
40
+ packageName + ".JettyServerAdvice" ,
41
+ packageName + ".JettyServerAdvice$HandleAdvice" ,
42
+ packageName + ".JettyServerAdvice$ResetAdvice" ,
54
43
};
55
44
}
56
45
@@ -74,11 +63,11 @@ public boolean matches(MethodDescription target) {
74
63
.matches (target .getDeclaringType ().asErasure ());
75
64
}
76
65
}))),
77
- JettyServerInstrumentation . class . getName () + "$HandleAdvice" );
66
+ packageName + ".JettyServerAdvice $HandleAdvice" );
78
67
transformation .applyAdvice (
79
68
// name changed to recycle in 9.3.0
80
69
namedOneOf ("reset" , "recycle" ).and (takesNoArguments ()),
81
- JettyServerInstrumentation . class . getName () + "$ResetAdvice" );
70
+ packageName + ".JettyServerAdvice $ResetAdvice" );
82
71
}
83
72
84
73
@ Override
@@ -92,58 +81,4 @@ public boolean matches(MethodDescription target) {
92
81
"org.eclipse.jetty.util.thread.TimerScheduler" ,
93
82
"org.eclipse.jetty.util.thread.TimerScheduler$SimpleTask" ));
94
83
}
95
-
96
- public static class HandleAdvice {
97
-
98
- @ Advice .OnMethodEnter (suppress = Throwable .class )
99
- public static AgentScope onEnter (@ Advice .This final HttpChannel channel ) {
100
- Request req = channel .getRequest ();
101
-
102
- Object existingSpan = req .getAttribute (DD_SPAN_ATTRIBUTE );
103
- if (existingSpan instanceof AgentSpan ) {
104
- // Request already gone through initial processing, so just activate the span.
105
- return activateSpan ((AgentSpan ) existingSpan );
106
- }
107
-
108
- final AgentSpan .Context .Extracted extractedContext = propagate ().extract (req , GETTER );
109
-
110
- final AgentSpan span = startSpan (SERVLET_REQUEST , extractedContext ).setMeasured (true );
111
- DECORATE .afterStart (span );
112
- DECORATE .onRequest (span , req , req , extractedContext );
113
-
114
- final AgentScope scope = activateSpan (span );
115
- scope .setAsyncPropagation (true );
116
- req .setAttribute (DD_SPAN_ATTRIBUTE , span );
117
- req .setAttribute (CorrelationIdentifier .getTraceIdKey (), GlobalTracer .get ().getTraceId ());
118
- req .setAttribute (CorrelationIdentifier .getSpanIdKey (), GlobalTracer .get ().getSpanId ());
119
- return scope ;
120
- }
121
-
122
- @ Advice .OnMethodExit (suppress = Throwable .class , onThrowable = Throwable .class )
123
- public static void closeScope (@ Advice .Enter final AgentScope scope ) {
124
- scope .close ();
125
- }
126
- }
127
-
128
- /**
129
- * Jetty ensures that connections are reset immediately after the response is sent. This provides
130
- * a reliable point to finish the server span at the last possible moment.
131
- */
132
- public static class ResetAdvice {
133
- @ Advice .OnMethodEnter (suppress = Throwable .class )
134
- public static void stopSpan (@ Advice .This final HttpChannel channel ) {
135
- Request req = channel .getRequest ();
136
- Object spanObj = req .getAttribute (DD_SPAN_ATTRIBUTE );
137
- if (spanObj instanceof AgentSpan ) {
138
- final AgentSpan span = (AgentSpan ) spanObj ;
139
- DECORATE .onResponse (span , channel );
140
- DECORATE .beforeFinish (span );
141
- span .finish ();
142
- }
143
- }
144
-
145
- private void muzzleCheck (HttpChannel connection ) {
146
- connection .run ();
147
- }
148
- }
149
84
}
0 commit comments