Skip to content

Commit

Permalink
Update Grizzly instrumentation to check active span instead of scope
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Nov 27, 2024
1 parent 600378f commit 99a337f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import datadog.appsec.api.blocking.BlockingContentType;
import datadog.trace.api.gateway.Flow;
import datadog.trace.bootstrap.blocking.BlockingActionHelper;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import java.io.OutputStream;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -36,14 +35,14 @@ public class GrizzlyBlockingHelper {
private GrizzlyBlockingHelper() {}

public static boolean block(
Request request, Response response, Flow.Action.RequestBlockingAction rba, AgentScope scope) {
Request request, Response response, Flow.Action.RequestBlockingAction rba, AgentSpan span) {
return block(
request,
response,
rba.getStatusCode(),
rba.getBlockingContentType(),
rba.getExtraHeaders(),
scope);
span);
}

public static boolean block(
Expand All @@ -52,7 +51,7 @@ public static boolean block(
int statusCode,
BlockingContentType bct,
Map<String, String> extraHeaders,
AgentScope scope) {
AgentSpan span) {
if (GET_OUTPUT_STREAM == null) {
return false;
}
Expand All @@ -77,15 +76,13 @@ public static boolean block(
os.close();
response.finish();

scope.span().getRequestContext().getTraceSegment().effectivelyBlocked();
span.getRequestContext().getTraceSegment().effectivelyBlocked();
SpanClosingListener.LISTENER.onAfterService(request);
} catch (Throwable e) {
log.info("Error committing blocking response", e);
final AgentSpan span = scope.span();
DECORATE.onError(span, e);
DECORATE.beforeFinish(span);
span.finish();
scope.close();
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datadog.trace.api.gateway.BlockResponseFunction;
import datadog.trace.api.internal.TraceSegment;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
Expand Down Expand Up @@ -90,9 +90,9 @@ public boolean tryCommitBlockingResponse(
int statusCode,
BlockingContentType templateType,
Map<String, String> extraHeaders) {
AgentScope agentScope = AgentTracer.get().activeScope();
if (agentScope == null) {
log.warn("Can't block: no active scope");
AgentSpan agentSpan = AgentTracer.get().activeSpan();
if (agentSpan == null) {
log.warn("Can't block: no active span");
return false;
}

Expand All @@ -102,7 +102,7 @@ public boolean tryCommitBlockingResponse(
statusCode,
templateType,
extraHeaders,
agentScope);
agentSpan);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static class HandleAdvice {

Flow.Action.RequestBlockingAction rba = span.getRequestBlockingAction();
if (rba != null) {
boolean success = GrizzlyBlockingHelper.block(request, response, rba, scope);
boolean success = GrizzlyBlockingHelper.block(request, response, rba, span);
if (success) {
return true; /* skip body */
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.instrumentation.grizzlyhttp232;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_SPAN_ATTRIBUTE;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
Expand All @@ -15,7 +15,7 @@ public class FilterAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(
@Advice.This BaseFilter it, @Advice.Argument(0) final FilterChainContext ctx) {
if (ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE) == null || activeScope() != null) {
if (ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE) == null || activeSpan() != null) {
return null;
}
AgentScope scope =
Expand Down

0 comments on commit 99a337f

Please sign in to comment.