Skip to content

Commit d34e6f0

Browse files
authored
Merge pull request #1011 from microsoft/verify_response_code
add asserts to verify response code.
2 parents d1cb3a7 + 3595f08 commit d34e6f0

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

test/smoke/testApps/SpringBootTest/src/smokeTest/java/com/springbootstartertest/smoketest/SpringbootSmokeTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.hamcrest.TypeSafeMatcher;
1717
import org.junit.Test;
1818

19-
import static org.hamcrest.Matchers.hasItem;
19+
import static org.hamcrest.Matchers.*;
2020
import static org.junit.Assert.*;
2121

2222
@UseAgent
@@ -78,8 +78,17 @@ public void testResultCodeWhenRestControllerThrows() {
7878
Envelope exceptionEnvelope = exceptionEnvelopeList.get(0);
7979
RequestData d = getTelemetryDataForType(0, "RequestData");
8080
String requestOperationId = d.getId();
81-
assertTrue(requestOperationId.contains(exceptionEnvelope.getTags().
82-
getOrDefault("ai.operation.id", null)));
81+
final String opId = exceptionEnvelope.getTags().get("ai.operation.id");
82+
assertNotNull(opId);
83+
assertThat(requestOperationId, containsString(opId));
84+
System.out.println("Response code after exception: "+d.getResponseCode());
85+
int code = -123;
86+
try {
87+
code = Integer.parseInt(d.getResponseCode());
88+
} catch (NumberFormatException e) {
89+
fail("Response code is not a number");
90+
}
91+
assertThat(code, greaterThanOrEqualTo(500));
8392
}
8493

8594
@Test

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: 11 additions & 3 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

@@ -109,9 +111,15 @@ public void handleEnd(ServletRequest request, ServletResponse response,
109111
RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry();
110112
long endTime = new Date().getTime();
111113
requestTelemetry.setDuration(new Duration(endTime - context.getRequestStartTimeTicks()));
112-
int resultCode = extractor.getStatusCode(response);
113-
requestTelemetry.setSuccess(resultCode < 400);
114-
requestTelemetry.setResponseCode(Integer.toString(resultCode));
114+
if (request.getAttribute(WebRequestTrackingFilter.APPLICATION_INSIGHTS_CAUGHT_EXCEPTION) != null) {
115+
requestTelemetry.setSuccess(false);
116+
requestTelemetry.setResponseCode("500");
117+
} else {
118+
int resultCode = extractor.getStatusCode(response);
119+
requestTelemetry.setSuccess(resultCode < 400);
120+
requestTelemetry.setResponseCode(Integer.toString(resultCode));
121+
}
122+
115123
if (ThreadContext.getRequestTelemetryContext() == null) {
116124
// e.g. when called from AIHttpServletListener
117125
ThreadContext.setRequestTelemetryContext(context);

0 commit comments

Comments
 (0)