private boolean invokeSafeOnBeginRequest(ServletRequest req, ServletResponse res) { if (!isInitialized) { return false; } boolean success = true; try { RequestTelemetryContext context = new RequestTelemetryContext(new Date().getTime(), (HttpServletRequest) req); ThreadContext.setRequestTelemetryContext(context); webModulesContainer.invokeOnBeginRequest(req, res); } catch (Exception e) { InternalLogger.INSTANCE.error( "Failed to invoke OnBeginRequest on telemetry modules with the following exception: %s", ExceptionUtils.getStackTrace(e)); success = false; } return success; }
private boolean invokeSafeOnBeginRequest(ServletRequest req, ServletResponse res) { if (!isInitialized) { return false; } boolean success = true; try { RequestTelemetryContext context = new RequestTelemetryContext(new Date().getTime(), (HttpServletRequest) req); ThreadContext.setRequestTelemetryContext(context); webModulesContainer.invokeOnBeginRequest(req, res); } catch (Exception e) { InternalLogger.INSTANCE.error( "Failed to invoke OnBeginRequest on telemetry modules with the following exception: %s", ExceptionUtils.getStackTrace(e)); success = false; } return success; }
@Override public void run() { RequestTelemetryContext context = new RequestTelemetryContext(startTicks); ThreadContext.setRequestTelemetryContext(context); validate(); }
@Before public void testInitialize() throws Exception { RequestTelemetryContext requestTelemetryContext = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(requestTelemetryContext); RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); requestTelemetry.setHttpMethod(HTTP_METHOD); // Setting mock for action context // We can't stub classes of Method and Class, so We should use RequestNameInterceptorTests class for stub clazz = this.getClass(); method = clazz.getMethod("classInitialize"); invocationContext = mock(InvocationContext.class); when(invocationContext.getMethod()).thenReturn(method); when(invocationContext.proceed()).thenReturn(null); }
private Cookie callOnBeginRequestAndGetCookieResult(WebSessionTrackingTelemetryModule module) { ThreadContext.setRequestTelemetryContext(new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime())); module.initialize(TelemetryConfiguration.getActive()); HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); final Cookie[] cookies = new Cookie[1]; Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { cookies[0] = ((Cookie) invocation.getArguments()[0]); return null; } }).when(response).addCookie(any(Cookie.class)); module.onBeginRequest(request, response); return cookies[0]; }
@Test public void noRequestTest() { WebSyntheticRequestTelemetryInitializer tested = new WebSyntheticRequestTelemetryInitializer(); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); Telemetry mockTelemetry = Mockito.mock(Telemetry.class); tested.initialize(mockTelemetry); Mockito.verify(mockTelemetry, Mockito.never()).getContext(); }
@Before public void testInitialize() throws NoSuchMethodException { handlerMethod = new HandlerMethod(this, DEFAULT_ACTION_NAME); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); }
@Before public void testInitialize() { RequestTelemetryContext requestTelemetryContext = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(requestTelemetryContext); RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); requestTelemetry.setHttpMethod(HTTP_METHOD); // Setting mock for action context actionContext = mock(ActionContext.class); when(actionContext.getName()).thenReturn(ACTION_NAME); ActionContext.setContext(actionContext); }
@Test public void gsmSyntheticHeadersTest() { WebSyntheticRequestTelemetryInitializer tested = new WebSyntheticRequestTelemetryInitializer(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); Mockito.doReturn("").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_SOURCE); Mockito.doReturn("A1").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_RUN_ID); Mockito.doReturn("A2").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_LOCATION); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime(), mockRequest); ThreadContext.setRequestTelemetryContext(context); TraceTelemetry telemetry = new TraceTelemetry(); tested.initialize(telemetry); assertEquals(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_SOURCE_NAME, telemetry.getContext().getOperation().getSyntheticSource()); assertEquals("A1", telemetry.getContext().getSession().getId()); assertEquals("A2", telemetry.getContext().getUser().getId()); }
@BeforeClass public static void classInitialize() { RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); context.getHttpRequestTelemetry().getContext().getUser().setUserAgent(HttpHelper.TEST_USER_AGENT); ThreadContext.setRequestTelemetryContext(context); }
@Test public void testTelemetryInitializedWithOperationId() { RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); OperationContext operationContext = createAndInitializeTelemetry(); Assert.assertEquals("Operation ID hasn't been set.", context.getHttpRequestTelemetry().getId(), operationContext.getId()); }
@Test public void testChildRequestDependencyIdGenerationWithNonHierarchicalRequestId() { //setup Hashtable<String, String> headers = new Hashtable<String, String>(); String incomingId = "guid"; headers.put(TelemetryCorrelationUtils.CORRELATION_HEADER_NAME, incomingId); HttpServletRequest request = ServletUtils.createServletRequestWithHeaders(headers); HttpServletResponse response = (HttpServletResponse)ServletUtils.generateDummyServletResponse(); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry(); //run TelemetryCorrelationUtils.resolveCorrelation(request, response, requestTelemetry); String childId = TelemetryCorrelationUtils.generateChildDependencyId(); //Incoming ID is non-hierarchical, so we must not modidy outgoing (child) id Assert.assertNotNull(childId); Assert.assertEquals(incomingId, childId); }
private void testRequestNameCalculationWithGivenQueryString(String queryString, String pathVariable) { RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); ServletRequest servletRequest = createServletRequest(queryString, pathVariable); defaultModule.onBeginRequest(servletRequest, null); RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); Assert.assertEquals("Request name not valid.", DEFAULT_REQUEST_NAME, requestTelemetry.getName()); }
@Test public void testTelemetryInitializedWithOperationName() { RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); OperationContext operationContext = createAndInitializeTelemetry(); Assert.assertEquals("Operation name hasn't been set.", context.getHttpRequestTelemetry().getName(), operationContext.getName()); }
@Test public void testChildRequestDependencyIdGeneration() { //setup Hashtable<String, String> headers = new Hashtable<String, String>(); String incomingId = "|guid_1."; headers.put(TelemetryCorrelationUtils.CORRELATION_HEADER_NAME, incomingId); HttpServletRequest request = ServletUtils.createServletRequestWithHeaders(headers); HttpServletResponse response = (HttpServletResponse)ServletUtils.generateDummyServletResponse(); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry(); //run TelemetryCorrelationUtils.resolveCorrelation(request, response, requestTelemetry); String childId = TelemetryCorrelationUtils.generateChildDependencyId(); //validate we have generated proper ID's Assert.assertNotNull(childId); Assert.assertEquals(requestTelemetry.getId() + "1.", childId); }
@Test public void noSyntheticHeadersTest() { WebSyntheticRequestTelemetryInitializer tested = new WebSyntheticRequestTelemetryInitializer(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); Mockito.doReturn("").when(mockRequest).getHeader(anyString()); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime(), mockRequest); ThreadContext.setRequestTelemetryContext(context); TraceTelemetry telemetry = new TraceTelemetry(); tested.initialize(telemetry); assertTrue(CommonUtils.isNullOrEmpty(telemetry.getContext().getSession().getId())); assertTrue(CommonUtils.isNullOrEmpty(telemetry.getContext().getUser().getId())); assertTrue(CommonUtils.isNullOrEmpty(telemetry.getContext().getOperation().getId())); }
@Before public void classInitialize() { RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); // Set session ID for the http request. RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry(); requestTelemetry.getContext().getSession().setId(REQUEST_SESSION_ID); requestTelemetry.getContext().getSession().setIsFirst(true); }
@Test public void testChildRequestDependencyIdGenerationWithNoParentId() { //setup - make sure no RequestId is set (i.e. no parent) Hashtable<String, String> headers = new Hashtable<String, String>(); HttpServletRequest request = ServletUtils.createServletRequestWithHeaders(headers); HttpServletResponse response = (HttpServletResponse)ServletUtils.generateDummyServletResponse(); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry(); //run TelemetryCorrelationUtils.resolveCorrelation(request, response, requestTelemetry); String childId = TelemetryCorrelationUtils.generateChildDependencyId(); //validate we have generated proper ID's Assert.assertNotNull(childId); Assert.assertEquals(requestTelemetry.getId() + "1.", childId); }
@Before public void classInitialize() { acquisitionTime = DateTimeUtils.getDateTimeNow(); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); // Set session ID for the http request. RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry(); requestTelemetry.getContext().getUser().setId(REQUEST_USER_ID); requestTelemetry.getContext().getUser().setAcquisitionDate(acquisitionTime); }
@Test public void testOnEndAddsSourceFieldForRequestWithRoleNameOnly() { //setup: initialize a request telemetry context RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime()); ThreadContext.setRequestTelemetryContext(context); //mock a servlet request with cross-component correlation headers Map<String, String> headers = new HashMap<String, String>(); headers.put(TelemetryCorrelationUtils.REQUEST_CONTEXT_HEADER_NAME, TelemetryCorrelationUtilsTests.getRequestContextHeaderValue(null, "Front End")); HttpServletRequest request = ServletUtils.createServletRequestWithHeaders(headers); HttpServletResponse response = (HttpServletResponse)ServletUtils.generateDummyServletResponse(); //configure mock appId fetcher to return different appId from what's on the request header mockProfileFetcher.setAppIdToReturn("id2"); mockProfileFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); //run defaultModule.onBeginRequest(request, response); defaultModule.onEndRequest(request, null); //validate source RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); Assert.assertEquals(TelemetryCorrelationUtilsTests.getRequestSourceValue(null, "Front End"), requestTelemetry.getSource()); }