@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 void cleanup() { try { ThreadContext.remove(); setKeyOnTLS(null); for (ThreadLocalCleaner cleaner : cleaners) { cleaner.clean(); } } catch (ThreadDeath td) { throw td; } catch (Throwable t) { } }
@Override public void run() { context[0] = ThreadContext.getRequestTelemetryContext(); } });
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; }
/** * Initializes properties of the given telemetry. * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override public void initialize(Telemetry telemetry) { // Some threads may not have TLS initialized, such as performance counters mechanism threads. if (ThreadContext.getRequestTelemetryContext() == null) { return; } onInitializeTelemetry(telemetry); }
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; }
@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); }
/** * Initializes properties of the given telemetry. * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override public void initialize(Telemetry telemetry) { // Some threads may not have TLS initialized, such as performance counters mechanism threads. if (ThreadContext.getRequestTelemetryContext() == null) { return; } onInitializeTelemetry(telemetry); }
@Before public void testInitialize() { channel.reset(); ThreadContext.setRequestTelemetryContext(null); }
private void cleanup() { try { ThreadContext.remove(); setKeyOnTLS(null); for (ThreadLocalCleaner cleaner : cleaners) { cleaner.clean(); } } catch (ThreadDeath td) { throw td; } catch (Throwable t) { } }
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()); }
/** * Helper method to retrieve Tracestate from ThreadLocal * @return */ public static String retriveTracestate() { //check if context is null - no correlation will happen if (ThreadContext.getRequestTelemetryContext() == null || ThreadContext.getRequestTelemetryContext(). getTracestate() == null) { InternalLogger.INSTANCE.warn("No correlation wil happen, Thread context is null"); return null; } Tracestate tracestate = ThreadContext.getRequestTelemetryContext().getTracestate(); return tracestate.toString(); }
@Before public void testInitialize() { channel.reset(); ThreadContext.setRequestTelemetryContext(null); }
@Test public void testTracestateIsPassedAsIsWhenAppIdResolutionIsFailed() { //turn on W3C defaultModule.isW3CEnabled = true; //setup: initialize a request 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<>(); Traceparent tp = new Traceparent(); headers.put(TraceContextCorrelation.TRACEPARENT_HEADER_NAME, tp.toString()); headers.put(TraceContextCorrelation.TRACESTATE_HEADER_NAME, TraceContextCorrelationTests.getTracestateHeaderValue("id1")); 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.FAILED); //run defaultModule.onBeginRequest(request, response); Assert.assertNotNull(ThreadContext.getRequestTelemetryContext().getTracestate()); Assert.assertEquals(TraceContextCorrelationTests.getTracestateHeaderValue("id1"), ThreadContext.getRequestTelemetryContext().getTracestate().toString()); }
/** * Helper method to retrieve Tracestate from ThreadLocal * @return */ public static String retriveTracestate() { //check if context is null - no correlation will happen if (ThreadContext.getRequestTelemetryContext() == null || ThreadContext.getRequestTelemetryContext(). getTracestate() == null) { InternalLogger.INSTANCE.warn("No correlation wil happen, Thread context is null"); return null; } Tracestate tracestate = ThreadContext.getRequestTelemetryContext().getTracestate(); return tracestate.toString(); }
@Override public void run() { RequestTelemetryContext context = new RequestTelemetryContext(startTicks); ThreadContext.setRequestTelemetryContext(context); validate(); }
@Test public void testTracestateIsSetWhenHeaderIsEmpty() { //turn on W3C defaultModule.isW3CEnabled = true; //setup: initialize a request 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<>(); Traceparent tp = new Traceparent(); headers.put(TraceContextCorrelation.TRACEPARENT_HEADER_NAME, tp.toString()); headers.put(TraceContextCorrelation.TRACESTATE_HEADER_NAME, ""); 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); Assert.assertNotNull(ThreadContext.getRequestTelemetryContext().getTracestate()); Assert.assertEquals(TraceContextCorrelationTests.getTracestateHeaderValue("id2"), ThreadContext.getRequestTelemetryContext().getTracestate().toString()); }
/** * Retrieves the currently stored correlation context from the request context. * @return The correlation context as a string. */ public static String retrieveCorrelationContext() { //check if context is null - no correlation will happen if (ThreadContext.getRequestTelemetryContext() == null) { InternalLogger.INSTANCE.warn("No correlation wil happen, Thread context is null"); return ""; } CorrelationContext context = ThreadContext.getRequestTelemetryContext().getCorrelationContext(); return context.toString(); }
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 testNewTracestateIsCreatedWhenHeaderIsNotPresent() { //turn on W3C defaultModule.isW3CEnabled = true; //setup: initialize a request 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<>(); Traceparent tp = new Traceparent(); headers.put(TraceContextCorrelation.TRACEPARENT_HEADER_NAME, tp.toString()); 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); Assert.assertNotNull(ThreadContext.getRequestTelemetryContext().getTracestate()); Assert.assertEquals(TraceContextCorrelationTests.getTracestateHeaderValue("id2"), ThreadContext.getRequestTelemetryContext().getTracestate().toString()); }