Skip to content

Commit d3821f1

Browse files
committed
use request attribute to flag if filterChain throws exception.
if an exception has been thrown, assume failed request with status=500
1 parent ef36be2 commit d3821f1

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

web/src/main/java/com/microsoft/applicationinsights/web/internal/WebRequestTrackingFilter.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,28 @@ public final class WebRequestTrackingFilter implements Filter {
7373
static {
7474
WebReflectionUtils.initialize();
7575
}
76+
7677
// region Members
7778
// Visible for testing
78-
final static String FILTER_NAME = "ApplicationInsightsWebFilter";
79-
private final static String WEB_INF_FOLDER = "WEB-INF/";
79+
static final String FILTER_NAME = "ApplicationInsightsWebFilter";
80+
private static final String WEB_INF_FOLDER = "WEB-INF/";
8081

8182
private WebModulesContainer webModulesContainer;
8283
private TelemetryClient telemetryClient;
8384
private final List<ThreadLocalCleaner> cleaners = new LinkedList<ThreadLocalCleaner>();
8485
private String appName;
85-
private static final String AGENT_LOCATOR_INTERFACE_NAME = "com.microsoft.applicationinsights."
86-
+ "agent.internal.coresync.AgentNotificationsHandler";
86+
private static final String AGENT_LOCATOR_INTERFACE_NAME = "com.microsoft.applicationinsights.agent.internal.coresync.AgentNotificationsHandler";
8787
private String filterName = FILTER_NAME;
8888

8989
/**
9090
* Constant for marking already processed request
9191
*/
92-
private final String ALREADY_FILTERED = "AI_FILTER_PROCESSED";
92+
private static final String ALREADY_FILTERED = "AI_FILTER_PROCESSED";
93+
/**
94+
* Request Attribute to flag if exception was thrown from servlet/downstream filter.
95+
* Value will be the caught (and rethrown) exception/throwable.
96+
*/
97+
public static final String APPLICATION_INSIGHTS_CAUGHT_EXCEPTION = "AI_CAUGHT_EXCEPTION";
9398

9499
/**
95100
* Utility handler used to instrument request start and end
@@ -137,13 +142,13 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
137142
httpRequest.setAttribute(ALREADY_FILTERED, Boolean.TRUE);
138143
chain.doFilter(httpRequest, httpResponse);
139144
} catch (ServletException | IOException | RuntimeException e) {
145+
httpRequest.setAttribute(APPLICATION_INSIGHTS_CAUGHT_EXCEPTION, e);
140146
handler.handleException(e);
141147
throw e;
142148
} finally {
143149
if (httpRequest.isAsyncStarted()) {
144150
AsyncContext context = httpRequest.getAsyncContext();
145-
AIHttpServletListener aiHttpServletListener =
146-
new AIHttpServletListener(handler, requestTelemetryContext, agentBinding);
151+
AIHttpServletListener aiHttpServletListener = new AIHttpServletListener(handler, requestTelemetryContext, agentBinding);
147152
context.addListener(aiHttpServletListener, httpRequest, httpResponse);
148153
} else {
149154
handler.handleEnd(httpRequest, httpResponse, requestTelemetryContext);

web/src/main/java/com/microsoft/applicationinsights/web/internal/httputils/AIHttpServletListener.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import javax.servlet.AsyncListener;
1010
import javax.servlet.ServletRequest;
1111
import javax.servlet.ServletResponse;
12+
13+
import com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter;
1214
import org.apache.commons.lang3.Validate;
1315

1416
/**
@@ -71,6 +73,7 @@ public void onError(AsyncEvent event) {
7173
} else {
7274
InternalLogger.INSTANCE.warn("Throwable is not instance of exception, cannot be captured: %s", throwable);
7375
}
76+
request.setAttribute(WebRequestTrackingFilter.APPLICATION_INSIGHTS_CAUGHT_EXCEPTION, throwable);
7477
handler.handleEnd(request, response, context);
7578
if (agentBinding != null) {
7679
agentBinding.unbindFromRunawayChildThreads();

web/src/main/java/com/microsoft/applicationinsights/web/internal/httputils/HttpServerHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.List;
1515
import javax.servlet.ServletRequest;
1616
import javax.servlet.ServletResponse;
17+
18+
import com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter;
1719
import org.apache.commons.lang3.Validate;
1820
import org.apache.commons.lang3.exception.ExceptionUtils;
1921

@@ -112,6 +114,11 @@ public void handleEnd(ServletRequest request, ServletResponse response,
112114
int resultCode = extractor.getStatusCode(response);
113115
requestTelemetry.setSuccess(resultCode < 400);
114116
requestTelemetry.setResponseCode(Integer.toString(resultCode));
117+
if (request.getAttribute(WebRequestTrackingFilter.APPLICATION_INSIGHTS_CAUGHT_EXCEPTION) != null) {
118+
requestTelemetry.setSuccess(false);
119+
requestTelemetry.setResponseCode("500");
120+
}
121+
115122
if (ThreadContext.getRequestTelemetryContext() == null) {
116123
// e.g. when called from AIHttpServletListener
117124
ThreadContext.setRequestTelemetryContext(context);

0 commit comments

Comments
 (0)