ClientSideMonitoringRequestHandler extends RequestHandler2 { @Override public void afterAttempt(HandlerAfterAttemptContext context) { ApiCallAttemptMonitoringEvent event = generateApiCallAttemptMonitoringEvent(context); context.getRequest().addHandlerContext(LAST_CALL_ATTEMPT, event); handToMonitoringListeners(event); } ClientSideMonitoringRequestHandler(String clientId, Collection<MonitoringListener> monitoringListeners); @Override void afterAttempt(HandlerAfterAttemptContext context); @Override void afterResponse(Request<?> request, Response<?> response); @Override void afterError(Request<?> request, Response<?> response, Exception e);  }
@Test public void afterAttempt_Succeed_shouldSendApiCallAttemptEvent() { HandlerAfterAttemptContext context = getContext(getMetrics(1)); requestHandler.afterAttempt(context); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifySuccessfulAttemptEvent(event); }
@Test public void afterAttempt_AwsException_shouldSendApiCallAttemptEvent() { AmazonServiceException exception = new AmazonServiceException("test"); exception.setStatusCode(400); exception.setErrorCode("BadRequest"); Map<String, String> header = new HashMap<String, String>(); header.put("x-amzn-RequestId", "test"); exception.setHttpHeaders(header); AWSRequestMetrics metrics = getMetrics(1); requestHandler.afterAttempt(getContext(metrics, exception)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifyConditionalEntries(event, exception); }
@Test public void afterAttempt_AwsExceptionWithOverSizeMessage_ShouldTrim() { AmazonServiceException exception = new AmazonServiceException(RandomStringUtils.randomAlphanumeric(1000)); exception.setStatusCode(400); AWSRequestMetrics metrics = getMetrics(1); requestHandler.afterAttempt(getContext(metrics, exception)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass (ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyExceptionMessageSize(event.getAwsExceptionMessage()); }
@Test public void afterAttempt_ConnectionError_shouldSendApiCallAttemptEvent() { IOException ioException = new IOException("oops"); AWSRequestMetrics metrics = getMetrics(1); requestHandler.afterAttempt(getContext(metrics, ioException)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifyConditionalEntries(event, ioException); }
@Test public void afterAttempt_sdkExceptionWithOverSizeMessage_ShouldTrim() { SdkClientException ioException = new SdkClientException(new IOException(RandomStringUtils.randomAlphanumeric(1200))); AWSRequestMetrics metrics = getMetrics(1); requestHandler.afterAttempt(getContext(metrics, ioException)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyExceptionSize(event.getSdkException()); verifyExceptionMessageSize(event.getSdkExceptionMessage()); }
@Test public void afterAttempt_OtherException_shouldSendApiCallAttemptEvent() { RuntimeException runtimeException = new RuntimeException("unexpected"); AWSRequestMetrics metrics = getMetrics(1); requestHandler.afterAttempt(getContext(metrics, runtimeException)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifyConditionalEntries(event, runtimeException); }
@Test public void afterAttempt_SessionTokenLowerCase_ShouldStillWork() { Request<?> request = getRequest(); request.setAWSRequestMetrics(getMetrics(1)); request.getHeaders().clear(); Map<String, String> headers = new HashMap<String, String>(); headers.put("x-amz-security-token", SECURITY_TOKENS); request.setHeaders(headers); requestHandler.afterAttempt(getContext(request)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifySuccessfulAttemptEvent(event); }
@Test public void afterAttempt_SessionTokenCapitalized_ShouldStillWork() { Request<?> request = getRequest(); request.setAWSRequestMetrics(getMetrics(1)); request.getHeaders().clear(); Map<String, String> headers = new HashMap<String, String>(); headers.put(SignerConstants.X_AMZ_SECURITY_TOKEN, SECURITY_TOKENS); request.setHeaders(headers); requestHandler.afterAttempt(getContext(request)); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); verify(customMonitoringListener, times(1)).handleEvent(any(ApiCallAttemptMonitoringEvent.class)); ApiCallAttemptMonitoringEvent event = monitoringEventArgumentCaptor.getValue(); verifyRequiredEntries(event); verifySuccessfulAttemptEvent(event); }
@Test public void afterAttempt_agentMonitoringListenerThrowException_shouldNotAffectCustomListener() { Request<?> request = getRequest(); Response<?> response = getResponse(request); doThrow(new RuntimeException("Oops")).when(agentMonitoringListener).handleEvent(any(ApiMonitoringEvent.class)); requestHandler.afterAttempt(getContext(response)); verify(agentMonitoringListener, times(1)).handleEvent(any(ApiMonitoringEvent.class)); verify(customMonitoringListener, times(1)).handleEvent(any(ApiMonitoringEvent.class)); }
@Test public void afterAttempt_overSizeClientId_shouldTrim() { Request<?> request = getRequest(); request.setAWSRequestMetrics(getMetrics(1)); requestHandler.afterAttempt(getContext(getResponse(request))); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); ApiCallAttemptMonitoringEvent monitoringEvent = monitoringEventArgumentCaptor.getValue(); assertThat(monitoringEvent.getClientId().length(), is(lessThanOrEqualTo(255))); }
@Test public void afterAttempt_overSizeUserAgent_shouldTrim() { Request<?> request = getRequest(); request.getOriginalRequest().getRequestClientOptions().appendUserAgent(RandomStringUtils.randomAlphanumeric(345)); request.setAWSRequestMetrics(getMetrics(1)); requestHandler.afterAttempt(getContext(getResponse(request))); ArgumentCaptor<ApiCallAttemptMonitoringEvent> monitoringEventArgumentCaptor = ArgumentCaptor.forClass(ApiCallAttemptMonitoringEvent.class); verify(agentMonitoringListener, times(1)).handleEvent(monitoringEventArgumentCaptor.capture()); ApiCallAttemptMonitoringEvent monitoringEvent = monitoringEventArgumentCaptor.getValue(); assertThat(monitoringEvent.getUserAgent().length(), is(lessThanOrEqualTo(256))); }