Skip to content
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

Update Grizzly instrumentation to check active span instead of scope #7982

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this was copy-paste from another part of the instrumentation where a scope is created and closed under different scenarios. Since the scope calls are balanced in the rest of this integration there's no need to close the scope here, it would just count as an extra close and be ignored.

}

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
Loading