Skip to content

Migrating all HttpServer Instrumentations to Extract full Context #8820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
54cdafc
supporting baggage in weblog related instrumentations
mhlidd May 6, 2025
16094c9
simplifying startSpan
mhlidd May 6, 2025
ed234d3
adding support for AzureFunctionsInstrumentation
mhlidd May 6, 2025
a7a35a6
cleanup
mhlidd May 6, 2025
8a5c90f
updateing grizzly instrumentations
mhlidd May 6, 2025
aacc0a0
updating PR comments and addressing failing unit tests
mhlidd May 7, 2025
2d3aed6
fixing ambiguous overloading tests
mhlidd May 7, 2025
0968d40
logging
mhlidd May 8, 2025
7a956f7
logging 2
mhlidd May 8, 2025
e2fb44c
spotless
mhlidd May 8, 2025
6b9b6e1
spotlesS
mhlidd May 12, 2025
0bb7df6
updating tests
mhlidd May 12, 2025
e4a63f2
updating akkahttp
mhlidd May 13, 2025
ebdeafd
migrating all http server instrumentations to use Context and Context…
mhlidd May 13, 2025
187f65c
update spray compilation error
mhlidd May 13, 2025
44188ec
Merge branch 'master' into mhlidd/migrate_http_server_instrumentations
mhlidd May 14, 2025
7f36c74
feat: Add more helpers to bytecode bridge
PerfectSlayer May 15, 2025
f36ba8d
applying advice to static calls to fromContext
mhlidd May 15, 2025
ddf872c
adding import
mhlidd May 15, 2025
4e94f29
final import
mhlidd May 15, 2025
2fb5557
change name to onrequest
mhlidd May 19, 2025
5d4f0a8
adding casting to null
mhlidd May 19, 2025
0120c2b
casting all null context calls to onRequest
mhlidd May 19, 2025
2ce18b1
final casting
mhlidd May 19, 2025
cfa274d
fixing scala syntax
mhlidd May 19, 2025
6cb87c5
fixing null
mhlidd May 19, 2025
ed2de2b
Merge branch 'master' into mhlidd/migrate_http_server_instrumentations
mhlidd May 20, 2025
712c64b
casting null parameters in server decorator test
mhlidd May 20, 2025
16b77ab
Merge branch 'master' into mhlidd/migrate_http_server_instrumentations
mhlidd May 20, 2025
80529d3
fix appsec jetty visitor
smola May 20, 2025
8893c3f
modifying jetty-common Method insn
mhlidd May 20, 2025
51f7ad5
Merge branch 'master' into mhlidd/migrate_http_server_instrumentations
mhlidd May 20, 2025
c355d84
spotless
mhlidd May 20, 2025
f0967eb
Merge branch 'master' into mhlidd/migrate_http_server_instrumentations
mhlidd May 21, 2025
4a43c8f
responding PR comments
mhlidd May 21, 2025
daebabd
removing null reference
mhlidd May 21, 2025
27d88b2
updating jetty visitors
mhlidd May 21, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import datadog.context.Context;
import datadog.trace.api.GlobalTracer;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.bootstrap.instrumentation.api.ContextVisitors;
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
import datadog.trace.bootstrap.instrumentation.api.URIDefaultDataAdapter;
Expand Down Expand Up @@ -56,12 +58,12 @@ public void setUp() {
.build();
GlobalTracer.forceRegister(tracer);
decorator = new BenchmarkHttpServerDecorator();
span = decorator.startSpan(Collections.emptyMap(), null);
span = decorator.startSpan(Collections.emptyMap(), (Context) null);
}

@Benchmark
public AgentSpan onRequest() {
return decorator.onRequest(span, null, request, null);
return decorator.onRequest(span, null, request, (AgentSpanContext.Extracted) null);
}

public static class Request {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public static Context getContextFrom(Object carrier) {
public static Context detachContextFrom(Object carrier) {
return Context.detachFrom(carrier);
}

/** @see AgentSpan#fromContext(Context) */
public static AgentSpan spanFromContext(Context context) {
return AgentSpan.fromContext(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static datadog.trace.api.cache.RadixTreeCache.PORTS;
import static datadog.trace.api.cache.RadixTreeCache.UNSET_PORT;

import datadog.context.ContextScope;
import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.Functions;
Expand Down Expand Up @@ -105,6 +106,11 @@ public AgentSpan onError(final AgentSpan span, final Throwable throwable, byte e
return span;
}

public ContextScope onError(final ContextScope scope, final Throwable throwable) {
onError(AgentSpan.fromContext(scope.context()), throwable);
return scope;
}

public AgentSpan onPeerConnection(
final AgentSpan span, final InetSocketAddress remoteConnection) {
if (remoteConnection != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datadog.trace.bootstrap.instrumentation.decorator;

import static datadog.context.Context.root;
import static datadog.context.propagation.Propagators.defaultPropagator;
import static datadog.trace.api.cache.RadixTreeCache.UNSET_STATUS;
import static datadog.trace.api.datastreams.DataStreamsContext.fromTags;
import static datadog.trace.api.gateway.Events.EVENTS;
Expand All @@ -11,6 +10,7 @@

import datadog.appsec.api.blocking.BlockingException;
import datadog.context.Context;
import datadog.context.propagation.Propagators;
import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.function.TriConsumer;
Expand Down Expand Up @@ -137,17 +137,17 @@ public AgentSpanContext.Extracted extract(REQUEST_CARRIER carrier) {
}

/**
* Will be renamed to #extract(REQUEST_CARRIER) when refactoring of instrumentation's is complete
* Will be renamed to #extract(REQUEST_CARRIER) when refactoring of instrumentations is complete
*/
public Context extractContext(REQUEST_CARRIER carrier) {
AgentPropagation.ContextVisitor<REQUEST_CARRIER> getter = getter();
if (null == carrier || null == getter) {
return root();
}
return defaultPropagator().extract(root(), carrier, getter);
return Propagators.defaultPropagator().extract(root(), carrier, getter);
}

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

/**
* Will be renamed to #startSpan(String, REQUEST_CARRIER, Context) when refactoring of
* instrumentation's is complete
*/
public AgentSpan startSpanFromContext(
String instrumentationName, REQUEST_CARRIER carrier, Context context) {
return startSpan(instrumentationName, carrier, getSpanContext(context));
public AgentSpan startSpan(REQUEST_CARRIER carrier, Context context) {
return startSpan("http-server", carrier, getExtractedSpanContext(context));
}

public AgentSpanContext.Extracted getSpanContext(Context context) {
public AgentSpanContext.Extracted getExtractedSpanContext(Context context) {
AgentSpan extractedSpan = AgentSpan.fromContext(context);
return extractedSpan == null ? null : (AgentSpanContext.Extracted) extractedSpan.context();
}

public AgentSpan onRequest(
final AgentSpan span,
final CONNECTION connection,
final REQUEST request,
final Context context) {
return onRequest(span, connection, request, getExtractedSpanContext(context));
}

public AgentSpan onRequest(
final AgentSpan span,
final CONNECTION connection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
def decorator = newDecorator()

when:
decorator.onRequest(this.span, null, req, null)
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)

then:
if (req) {
Expand Down Expand Up @@ -84,7 +84,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
def decorator = newDecorator()

when:
decorator.onRequest(this.span, null, req, null)
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)

then:
if (expectedUrl) {
Expand Down Expand Up @@ -135,7 +135,7 @@ class HttpServerDecoratorTest extends ServerDecoratorTest {
def decorator = newDecorator()

when:
decorator.onRequest(this.span, null, req, null)
decorator.onRequest(this.span, null, req, (AgentSpanContext.Extracted) null)

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

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

then:
1 * this.span.setResourceName({ it as String == '/path' }, ResourceNamePriorities.HTTP_PATH_NORMALIZER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import akka.http.scaladsl.model.HttpResponse;
import akka.http.scaladsl.util.FastFuture$;
import akka.stream.Materializer;
import datadog.context.Context;
import datadog.context.ContextScope;
import datadog.trace.api.gateway.Flow;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.instrumentation.akkahttp.appsec.BlockingResponseHelper;
import scala.Function1;
Expand All @@ -26,8 +27,9 @@ public DatadogAsyncHandlerWrapper(

@Override
public Future<HttpResponse> apply(final HttpRequest request) {
final AgentScope scope = DatadogWrapperHelper.createSpan(request);
AgentSpan span = scope.span();
final ContextScope scope = DatadogWrapperHelper.createSpan(request);
Context context = scope.context();
final AgentSpan span = AgentSpan.fromContext(context);
Future<HttpResponse> futureResponse;

// handle blocking in the beginning of the request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import akka.stream.stage.AbstractOutHandler;
import akka.stream.stage.GraphStage;
import akka.stream.stage.GraphStageLogic;
import datadog.context.ContextScope;
import datadog.trace.api.gateway.RequestContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.instrumentation.akkahttp.appsec.BlockingResponseHelper;
import java.util.Queue;
Expand Down Expand Up @@ -56,7 +56,7 @@ public GraphStageLogic createLogic(final Attributes inheritedAttributes) throws
// that this connection was created with. This means that we can safely
// close the span at the front of the queue when we receive the response
// from the user code, since it will match up to the request for that span.
final Queue<AgentScope> scopes = new ArrayBlockingQueue<>(pipeliningLimit);
final Queue<ContextScope> scopes = new ArrayBlockingQueue<>(pipeliningLimit);
boolean[] skipNextPull = new boolean[] {false};

// This is where the request comes in from the server and TCP layer
Expand All @@ -66,8 +66,8 @@ public GraphStageLogic createLogic(final Attributes inheritedAttributes) throws
@Override
public void onPush() throws Exception {
final HttpRequest request = grab(requestInlet);
final AgentScope scope = DatadogWrapperHelper.createSpan(request);
AgentSpan span = scope.span();
final ContextScope scope = DatadogWrapperHelper.createSpan(request);
final AgentSpan span = AgentSpan.fromContext(scope.context());
RequestContext requestContext = span.getRequestContext();
if (requestContext != null) {
HttpResponse response =
Expand All @@ -77,7 +77,7 @@ public void onPush() throws Exception {
skipNextPull[0] = true;
requestContext.getTraceSegment().effectivelyBlocked();
emit(responseOutlet, response);
DatadogWrapperHelper.finishSpan(scope.span(), response);
DatadogWrapperHelper.finishSpan(span, response);
pull(requestInlet);
scope.close();
return;
Expand Down Expand Up @@ -131,9 +131,9 @@ public void onDownstreamFinish() throws Exception {
@Override
public void onPush() throws Exception {
HttpResponse response = grab(responseInlet);
final AgentScope scope = scopes.poll();
final ContextScope scope = scopes.poll();
if (scope != null) {
AgentSpan span = scope.span();
AgentSpan span = AgentSpan.fromContext(scope.context());
HttpResponse newResponse =
BlockingResponseHelper.handleFinishForWaf(span, response);
if (newResponse != response) {
Expand All @@ -146,7 +146,7 @@ public void onPush() throws Exception {
// and close it. If it's not, then it will be cleaned up actor message
// processing instrumentation that drives this state machine
AgentSpan activeSpan = activeSpan();
if (activeSpan == scope.span()) {
if (activeSpan == span) {
scope.close();
}
}
Expand All @@ -157,26 +157,27 @@ public void onPush() throws Exception {
public void onUpstreamFinish() throws Exception {
// We will not receive any more responses from the user code, so clean up any
// remaining spans
AgentScope scope = scopes.poll();
ContextScope scope = scopes.poll();
while (scope != null) {
scope.span().finish();
AgentSpan.fromContext(scope.context()).finish();
scope = scopes.poll();
}
completeStage();
}

@Override
public void onUpstreamFailure(final Throwable ex) throws Exception {
AgentScope scope = scopes.poll();
ContextScope scope = scopes.poll();
AgentSpan span = AgentSpan.fromContext(scope.context());
if (scope != null) {
// Mark the span as failed
DatadogWrapperHelper.finishSpan(scope.span(), ex);
DatadogWrapperHelper.finishSpan(span, ex);
}
// We will not receive any more responses from the user code, so clean up any
// remaining spans
scope = scopes.poll();
while (scope != null) {
scope.span().finish();
span.finish();
scope = scopes.poll();
}
fail(responseOutlet, ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package datadog.trace.instrumentation.akkahttp;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.instrumentation.akkahttp.AkkaHttpServerDecorator.DECORATE;

import akka.http.scaladsl.model.HttpRequest;
import akka.http.scaladsl.model.HttpResponse;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.context.Context;
import datadog.context.ContextScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;

public class DatadogWrapperHelper {
public static AgentScope createSpan(final HttpRequest request) {
final AgentSpanContext.Extracted extractedContext = DECORATE.extract(request);
public static ContextScope createSpan(final HttpRequest request) {
final Context extractedContext = DECORATE.extractContext(request);
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
DECORATE.afterStart(span);
DECORATE.onRequest(span, request, request, extractedContext);

return activateSpan(span);
return extractedContext.with(span).attach();
}

public static void finishSpan(final AgentSpan span, final HttpResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import net.bytebuddy.asm.Advice;

public class HTTPPluginAdvice {
Expand All @@ -17,7 +18,8 @@ public static AgentScope onEnter(@Advice.Argument(value = 2) final Object server
final AgentSpan span = startSpan(DECORATE.spanName()).setMeasured(true);
DECORATE.afterStart(span);
// serverTransaction is like request + connection in one object:
DECORATE.onRequest(span, serverTransaction, serverTransaction, null);
DECORATE.onRequest(
span, serverTransaction, serverTransaction, (AgentSpanContext.Extracted) null);
final AgentScope scope = activateSpan(span);
return scope;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.declaresMethod;
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.isAnnotatedWith;
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.Java8BytecodeBridge.spanFromContext;
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;
import static datadog.trace.instrumentation.azurefunctions.AzureFunctionsDecorator.DECORATE;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -14,11 +14,11 @@
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import datadog.context.Context;
import datadog.context.ContextScope;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -64,24 +64,24 @@ public void methodAdvice(MethodTransformer transformer) {

public static class AzureFunctionsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope methodEnter(
public static ContextScope methodEnter(
@Advice.Argument(0) final HttpRequestMessage request,
@Advice.Argument(1) final ExecutionContext context) {
final AgentSpanContext.Extracted extractedContext = DECORATE.extract(request);
final Context extractedContext = DECORATE.extractContext(request);
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
DECORATE.afterStart(span, context.getFunctionName());
DECORATE.onRequest(span, request, request, extractedContext);
HTTP_RESOURCE_DECORATOR.withRoute(
span, request.getHttpMethod().name(), request.getUri().getPath());
return activateSpan(span);
return extractedContext.with(span).attach();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final AgentScope scope,
@Advice.Enter final ContextScope scope,
@Advice.Return final HttpResponseMessage response,
@Advice.Thrown final Throwable throwable) {
final AgentSpan span = scope.span();
final AgentSpan span = spanFromContext(scope.context());
DECORATE.onError(span, throwable);
DECORATE.onResponse(span, response);
DECORATE.beforeFinish(span);
Expand Down
Loading
Loading