Skip to content

Commit 50ad603

Browse files
mhliddPerfectSlayersmola
authored
Migrating all HttpServer Instrumentations to Extract full Context (#8820)
* supporting baggage in weblog related instrumentations resolve merge conflicts * simplifying startSpan * adding support for AzureFunctionsInstrumentation * cleanup * updateing grizzly instrumentations * updating PR comments and addressing failing unit tests * fixing ambiguous overloading tests * logging * logging 2 * spotless * spotlesS * updating tests * updating akkahttp * migrating all http server instrumentations to use Context and ContextScope * update spray compilation error * feat: Add more helpers to bytecode bridge * applying advice to static calls to fromContext * adding import * final import * change name to onrequest * adding casting to null * casting all null context calls to onRequest * final casting * fixing scala syntax * fixing null * casting null parameters in server decorator test * fix appsec jetty visitor * modifying jetty-common Method insn * spotless * responding PR comments * removing null reference * updating jetty visitors * checking for NPE --------- Co-authored-by: Bruce Bujon <[email protected]> Co-authored-by: Santiago Mola <[email protected]> Co-authored-by: Bruce Bujon <[email protected]>
1 parent 81ef2be commit 50ad603

File tree

43 files changed

+301
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+301
-259
lines changed

dd-java-agent/agent-bootstrap/src/jmh/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecoratorBenchmark.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static java.util.concurrent.TimeUnit.MICROSECONDS;
44
import static java.util.concurrent.TimeUnit.SECONDS;
55

6+
import datadog.context.Context;
67
import datadog.trace.api.GlobalTracer;
78
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
89
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
10+
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
911
import datadog.trace.bootstrap.instrumentation.api.ContextVisitors;
1012
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
1113
import datadog.trace.bootstrap.instrumentation.api.URIDefaultDataAdapter;
@@ -56,12 +58,12 @@ public void setUp() {
5658
.build();
5759
GlobalTracer.forceRegister(tracer);
5860
decorator = new BenchmarkHttpServerDecorator();
59-
span = decorator.startSpan(Collections.emptyMap(), null);
61+
span = decorator.startSpan(Collections.emptyMap(), (Context) null);
6062
}
6163

6264
@Benchmark
6365
public AgentSpan onRequest() {
64-
return decorator.onRequest(span, null, request, null);
66+
return decorator.onRequest(span, null, request, (AgentSpanContext.Extracted) null);
6567
}
6668

6769
public static class Request {

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/api/Java8BytecodeBridge.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public static Context getContextFrom(Object carrier) {
2929
public static Context detachContextFrom(Object carrier) {
3030
return Context.detachFrom(carrier);
3131
}
32+
33+
/** @see AgentSpan#fromContext(Context) */
34+
public static AgentSpan spanFromContext(Context context) {
35+
return AgentSpan.fromContext(context);
36+
}
3237
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/BaseDecorator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static datadog.trace.api.cache.RadixTreeCache.PORTS;
44
import static datadog.trace.api.cache.RadixTreeCache.UNSET_PORT;
55

6+
import datadog.context.ContextScope;
67
import datadog.trace.api.Config;
78
import datadog.trace.api.DDTags;
89
import datadog.trace.api.Functions;
@@ -105,6 +106,11 @@ public AgentSpan onError(final AgentSpan span, final Throwable throwable, byte e
105106
return span;
106107
}
107108

109+
public ContextScope onError(final ContextScope scope, final Throwable throwable) {
110+
onError(AgentSpan.fromContext(scope.context()), throwable);
111+
return scope;
112+
}
113+
108114
public AgentSpan onPeerConnection(
109115
final AgentSpan span, final InetSocketAddress remoteConnection) {
110116
if (remoteConnection != null) {

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecorator.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package datadog.trace.bootstrap.instrumentation.decorator;
22

33
import static datadog.context.Context.root;
4-
import static datadog.context.propagation.Propagators.defaultPropagator;
54
import static datadog.trace.api.cache.RadixTreeCache.UNSET_STATUS;
65
import static datadog.trace.api.datastreams.DataStreamsContext.fromTags;
76
import static datadog.trace.api.gateway.Events.EVENTS;
@@ -11,6 +10,7 @@
1110

1211
import datadog.appsec.api.blocking.BlockingException;
1312
import datadog.context.Context;
13+
import datadog.context.propagation.Propagators;
1414
import datadog.trace.api.Config;
1515
import datadog.trace.api.DDTags;
1616
import datadog.trace.api.function.TriConsumer;
@@ -137,17 +137,17 @@ public AgentSpanContext.Extracted extract(REQUEST_CARRIER carrier) {
137137
}
138138

139139
/**
140-
* Will be renamed to #extract(REQUEST_CARRIER) when refactoring of instrumentation's is complete
140+
* Will be renamed to #extract(REQUEST_CARRIER) when refactoring of instrumentations is complete
141141
*/
142142
public Context extractContext(REQUEST_CARRIER carrier) {
143143
AgentPropagation.ContextVisitor<REQUEST_CARRIER> getter = getter();
144144
if (null == carrier || null == getter) {
145145
return root();
146146
}
147-
return defaultPropagator().extract(root(), carrier, getter);
147+
return Propagators.defaultPropagator().extract(root(), carrier, getter);
148148
}
149149

150-
/** Deprecated. Use {@link #startSpanFromContext(String, Object, Context)} instead. */
150+
/** Deprecated. Use {@link #startSpan(Object, Context)} instead. */
151151
@Deprecated
152152
public AgentSpan startSpan(REQUEST_CARRIER carrier, AgentSpanContext.Extracted context) {
153153
return startSpan("http-server", carrier, context);
@@ -170,20 +170,23 @@ public AgentSpan startSpan(
170170
return span;
171171
}
172172

173-
/**
174-
* Will be renamed to #startSpan(String, REQUEST_CARRIER, Context) when refactoring of
175-
* instrumentation's is complete
176-
*/
177-
public AgentSpan startSpanFromContext(
178-
String instrumentationName, REQUEST_CARRIER carrier, Context context) {
179-
return startSpan(instrumentationName, carrier, getSpanContext(context));
173+
public AgentSpan startSpan(REQUEST_CARRIER carrier, Context context) {
174+
return startSpan("http-server", carrier, getExtractedSpanContext(context));
180175
}
181176

182-
public AgentSpanContext.Extracted getSpanContext(Context context) {
177+
public AgentSpanContext.Extracted getExtractedSpanContext(Context context) {
183178
AgentSpan extractedSpan = AgentSpan.fromContext(context);
184179
return extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
185180
}
186181

182+
public AgentSpan onRequest(
183+
final AgentSpan span,
184+
final CONNECTION connection,
185+
final REQUEST request,
186+
final Context context) {
187+
return onRequest(span, connection, request, getExtractedSpanContext(context));
188+
}
189+
187190
public AgentSpan onRequest(
188191
final AgentSpan span,
189192
final CONNECTION connection,

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecoratorTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
5151
def decorator = newDecorator()
5252

5353
when:
54-
decorator.onRequest(this.span, null, req, null)
54+
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)
5555

5656
then:
5757
if (req) {
@@ -84,7 +84,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
8484
def decorator = newDecorator()
8585

8686
when:
87-
decorator.onRequest(this.span, null, req, null)
87+
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)
8888

8989
then:
9090
if (expectedUrl) {
@@ -135,7 +135,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
135135
def decorator = newDecorator()
136136

137137
when:
138-
decorator.onRequest(this.span, null, req, null)
138+
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)
139139

140140
then:
141141
1 * this.span.setTag(Tags.HTTP_URL, {it.toString() == expectedUrl})
@@ -165,7 +165,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
165165
def decorator = newDecorator()
166166

167167
when:
168-
decorator.onRequest(this.span, null, [url: new URI('http://host/p%20ath')], null)
168+
decorator.onRequest(this.span, null, [url: new URI('http://host/p%20ath')], (AgentSpanContext.Extracted) null)
169169

170170
then:
171171
1 * this.span.setResourceName({ it as String == '/path' }, ResourceNamePriorities.HTTP_PATH_NORMALIZER)

dd-java-agent/instrumentation/akka-http/akka-http-10.0/src/main/java/datadog/trace/instrumentation/akkahttp/DatadogAsyncHandlerWrapper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import akka.http.scaladsl.model.HttpResponse;
55
import akka.http.scaladsl.util.FastFuture$;
66
import akka.stream.Materializer;
7+
import datadog.context.Context;
8+
import datadog.context.ContextScope;
79
import datadog.trace.api.gateway.Flow;
8-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
910
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1011
import datadog.trace.instrumentation.akkahttp.appsec.BlockingResponseHelper;
1112
import scala.Function1;
@@ -26,8 +27,9 @@ public DatadogAsyncHandlerWrapper(
2627

2728
@Override
2829
public Future<HttpResponse> apply(final HttpRequest request) {
29-
final AgentScope scope = DatadogWrapperHelper.createSpan(request);
30-
AgentSpan span = scope.span();
30+
final ContextScope scope = DatadogWrapperHelper.createSpan(request);
31+
Context context = scope.context();
32+
final AgentSpan span = AgentSpan.fromContext(context);
3133
Future<HttpResponse> futureResponse;
3234

3335
// handle blocking in the beginning of the request

dd-java-agent/instrumentation/akka-http/akka-http-10.0/src/main/java/datadog/trace/instrumentation/akkahttp/DatadogServerRequestResponseFlowWrapper.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import akka.stream.stage.AbstractOutHandler;
1414
import akka.stream.stage.GraphStage;
1515
import akka.stream.stage.GraphStageLogic;
16+
import datadog.context.ContextScope;
1617
import datadog.trace.api.gateway.RequestContext;
17-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1818
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1919
import datadog.trace.instrumentation.akkahttp.appsec.BlockingResponseHelper;
2020
import java.util.Queue;
@@ -56,7 +56,7 @@ public GraphStageLogic createLogic(final Attributes inheritedAttributes) throws
5656
// that this connection was created with. This means that we can safely
5757
// close the span at the front of the queue when we receive the response
5858
// from the user code, since it will match up to the request for that span.
59-
final Queue<AgentScope> scopes = new ArrayBlockingQueue<>(pipeliningLimit);
59+
final Queue<ContextScope> scopes = new ArrayBlockingQueue<>(pipeliningLimit);
6060
boolean[] skipNextPull = new boolean[] {false};
6161

6262
// This is where the request comes in from the server and TCP layer
@@ -66,8 +66,8 @@ public GraphStageLogic createLogic(final Attributes inheritedAttributes) throws
6666
@Override
6767
public void onPush() throws Exception {
6868
final HttpRequest request = grab(requestInlet);
69-
final AgentScope scope = DatadogWrapperHelper.createSpan(request);
70-
AgentSpan span = scope.span();
69+
final ContextScope scope = DatadogWrapperHelper.createSpan(request);
70+
final AgentSpan span = AgentSpan.fromContext(scope.context());
7171
RequestContext requestContext = span.getRequestContext();
7272
if (requestContext != null) {
7373
HttpResponse response =
@@ -77,7 +77,7 @@ public void onPush() throws Exception {
7777
skipNextPull[0] = true;
7878
requestContext.getTraceSegment().effectivelyBlocked();
7979
emit(responseOutlet, response);
80-
DatadogWrapperHelper.finishSpan(scope.span(), response);
80+
DatadogWrapperHelper.finishSpan(span, response);
8181
pull(requestInlet);
8282
scope.close();
8383
return;
@@ -131,9 +131,9 @@ public void onDownstreamFinish() throws Exception {
131131
@Override
132132
public void onPush() throws Exception {
133133
HttpResponse response = grab(responseInlet);
134-
final AgentScope scope = scopes.poll();
134+
final ContextScope scope = scopes.poll();
135135
if (scope != null) {
136-
AgentSpan span = scope.span();
136+
AgentSpan span = AgentSpan.fromContext(scope.context());
137137
HttpResponse newResponse =
138138
BlockingResponseHelper.handleFinishForWaf(span, response);
139139
if (newResponse != response) {
@@ -146,7 +146,7 @@ public void onPush() throws Exception {
146146
// and close it. If it's not, then it will be cleaned up actor message
147147
// processing instrumentation that drives this state machine
148148
AgentSpan activeSpan = activeSpan();
149-
if (activeSpan == scope.span()) {
149+
if (activeSpan == span) {
150150
scope.close();
151151
}
152152
}
@@ -157,26 +157,27 @@ public void onPush() throws Exception {
157157
public void onUpstreamFinish() throws Exception {
158158
// We will not receive any more responses from the user code, so clean up any
159159
// remaining spans
160-
AgentScope scope = scopes.poll();
160+
ContextScope scope = scopes.poll();
161161
while (scope != null) {
162-
scope.span().finish();
162+
AgentSpan.fromContext(scope.context()).finish();
163163
scope = scopes.poll();
164164
}
165165
completeStage();
166166
}
167167

168168
@Override
169169
public void onUpstreamFailure(final Throwable ex) throws Exception {
170-
AgentScope scope = scopes.poll();
170+
ContextScope scope = scopes.poll();
171+
AgentSpan span = AgentSpan.fromContext(scope.context());
171172
if (scope != null) {
172173
// Mark the span as failed
173-
DatadogWrapperHelper.finishSpan(scope.span(), ex);
174+
DatadogWrapperHelper.finishSpan(span, ex);
174175
}
175176
// We will not receive any more responses from the user code, so clean up any
176177
// remaining spans
177178
scope = scopes.poll();
178179
while (scope != null) {
179-
scope.span().finish();
180+
span.finish();
180181
scope = scopes.poll();
181182
}
182183
fail(responseOutlet, ex);

dd-java-agent/instrumentation/akka-http/akka-http-10.0/src/main/java/datadog/trace/instrumentation/akkahttp/DatadogWrapperHelper.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package datadog.trace.instrumentation.akkahttp;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
43
import static datadog.trace.instrumentation.akkahttp.AkkaHttpServerDecorator.DECORATE;
54

65
import akka.http.scaladsl.model.HttpRequest;
76
import akka.http.scaladsl.model.HttpResponse;
8-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
7+
import datadog.context.Context;
8+
import datadog.context.ContextScope;
99
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
10-
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
1110

1211
public class DatadogWrapperHelper {
13-
public static AgentScope createSpan(final HttpRequest request) {
14-
final AgentSpanContext.Extracted extractedContext = DECORATE.extract(request);
12+
public static ContextScope createSpan(final HttpRequest request) {
13+
final Context extractedContext = DECORATE.extractContext(request);
1514
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
1615
DECORATE.afterStart(span);
1716
DECORATE.onRequest(span, request, request, extractedContext);
1817

19-
return activateSpan(span);
18+
return extractedContext.with(span).attach();
2019
}
2120

2221
public static void finishSpan(final AgentSpan span, final HttpResponse response) {

dd-java-agent/instrumentation/axway-api/src/main/java/datadog/trace/instrumentation/axway/HTTPPluginAdvice.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import datadog.trace.bootstrap.InstrumentationContext;
99
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1010
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
11+
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
1112
import net.bytebuddy.asm.Advice;
1213

1314
public class HTTPPluginAdvice {
@@ -17,7 +18,8 @@ public static AgentScope onEnter(@Advice.Argument(value = 2) final Object server
1718
final AgentSpan span = startSpan(DECORATE.spanName()).setMeasured(true);
1819
DECORATE.afterStart(span);
1920
// serverTransaction is like request + connection in one object:
20-
DECORATE.onRequest(span, serverTransaction, serverTransaction, null);
21+
DECORATE.onRequest(
22+
span, serverTransaction, serverTransaction, (AgentSpanContext.Extracted) null);
2123
final AgentScope scope = activateSpan(span);
2224
return scope;
2325
}

dd-java-agent/instrumentation/azure-functions/src/main/java/datadog/trace/instrumentation/azure/functions/AzureFunctionsInstrumentation.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.declaresMethod;
44
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.isAnnotatedWith;
55
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
6-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
6+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
77
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
88
import static datadog.trace.instrumentation.azurefunctions.AzureFunctionsDecorator.DECORATE;
99
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -14,11 +14,11 @@
1414
import com.microsoft.azure.functions.ExecutionContext;
1515
import com.microsoft.azure.functions.HttpRequestMessage;
1616
import com.microsoft.azure.functions.HttpResponseMessage;
17+
import datadog.context.Context;
18+
import datadog.context.ContextScope;
1719
import datadog.trace.agent.tooling.Instrumenter;
1820
import datadog.trace.agent.tooling.InstrumenterModule;
19-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2021
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
21-
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
2222
import net.bytebuddy.asm.Advice;
2323
import net.bytebuddy.description.type.TypeDescription;
2424
import net.bytebuddy.matcher.ElementMatcher;
@@ -64,24 +64,24 @@ public void methodAdvice(MethodTransformer transformer) {
6464

6565
public static class AzureFunctionsAdvice {
6666
@Advice.OnMethodEnter(suppress = Throwable.class)
67-
public static AgentScope methodEnter(
67+
public static ContextScope methodEnter(
6868
@Advice.Argument(0) final HttpRequestMessage request,
6969
@Advice.Argument(1) final ExecutionContext context) {
70-
final AgentSpanContext.Extracted extractedContext = DECORATE.extract(request);
70+
final Context extractedContext = DECORATE.extractContext(request);
7171
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
7272
DECORATE.afterStart(span, context.getFunctionName());
7373
DECORATE.onRequest(span, request, request, extractedContext);
7474
HTTP_RESOURCE_DECORATOR.withRoute(
7575
span, request.getHttpMethod().name(), request.getUri().getPath());
76-
return activateSpan(span);
76+
return extractedContext.with(span).attach();
7777
}
7878

7979
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
8080
public static void methodExit(
81-
@Advice.Enter final AgentScope scope,
81+
@Advice.Enter final ContextScope scope,
8282
@Advice.Return final HttpResponseMessage response,
8383
@Advice.Thrown final Throwable throwable) {
84-
final AgentSpan span = scope.span();
84+
final AgentSpan span = spanFromContext(scope.context());
8585
DECORATE.onError(span, throwable);
8686
DECORATE.onResponse(span, response);
8787
DECORATE.beforeFinish(span);

0 commit comments

Comments
 (0)