private void initializeSession(final SessionContext sessionContext) { sessionContext.setId(UUID.randomUUID().toString()); }
@BeforeClass public static void initialize() throws Exception { sessionId = LocalStringsUtils.generateRandomId(true); String formattedCookie = SessionCookie.formatCookie(new String[] { sessionId }); defaultCookie = new Cookie(SessionCookie.COOKIE_NAME, formattedCookie); sessionContext = new SessionContext(new ConcurrentHashMap<String, String>()); sessionContext.setId(sessionId); SessionCookie sessionCookie = new SessionCookie(defaultCookie); requestTelemetryContextMock = mock(RequestTelemetryContext.class); when(requestTelemetryContextMock.getSessionCookie()).thenReturn(sessionCookie); }
/** * Begin request processing. * * @param req The request to process * @param res The response to modify */ @Override public void onBeginRequest(ServletRequest req, ServletResponse res) { HttpServletRequest request = (HttpServletRequest)req; RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext(); SessionCookie sessionCookie = com.microsoft.applicationinsights.web.internal.cookies.Cookie.getCookie( SessionCookie.class, request, SessionCookie.COOKIE_NAME); if (sessionCookie == null) { return; } context.setSessionCookie(sessionCookie); String sessionId = sessionCookie.getSessionId(); getTelemetrySessionContext(context).setId(sessionId); }
/** * Begin request processing. * * @param req The request to process * @param res The response to modify */ @Override public void onBeginRequest(ServletRequest req, ServletResponse res) { HttpServletRequest request = (HttpServletRequest)req; RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext(); SessionCookie sessionCookie = com.microsoft.applicationinsights.web.internal.cookies.Cookie.getCookie( SessionCookie.class, request, SessionCookie.COOKIE_NAME); if (sessionCookie == null) { return; } context.setSessionCookie(sessionCookie); String sessionId = sessionCookie.getSessionId(); getTelemetrySessionContext(context).setId(sessionId); }
@Test public void testSetId() { ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>(); SessionContext context = new SessionContext(map); context.setId("mock"); assertEquals(context.getId(), "mock"); assertEquals(map.size(), 1); assertEquals(map.get(ContextTagKeys.getKeys().getSessionId()), "mock"); }
private void handlePossibleGSMSyntheticRequest(Telemetry telemetry, HttpServletRequest request) { String gsmSyntheticTestRunId = request.getHeader(SYNTHETIC_TEST_RUN_ID); if (CommonUtils.isNullOrEmpty(gsmSyntheticTestRunId)) { return; } String syntheticSource = telemetry.getContext().getOperation().getSyntheticSource(); if (CommonUtils.isNullOrEmpty(syntheticSource)) { telemetry.getContext().getOperation().setSyntheticSource(SYNTHETIC_SOURCE_NAME); } String sessionId = telemetry.getContext().getSession().getId(); if (CommonUtils.isNullOrEmpty(sessionId)) { telemetry.getContext().getSession().setId(gsmSyntheticTestRunId); } String userId = telemetry.getContext().getUser().getId(); if (CommonUtils.isNullOrEmpty(userId)) { String header = request.getHeader(SYNTHETIC_TEST_LOCATION); telemetry.getContext().getUser().setId(header); } }
private void handlePossibleGSMSyntheticRequest(Telemetry telemetry, HttpServletRequest request) { String gsmSyntheticTestRunId = request.getHeader(SYNTHETIC_TEST_RUN_ID); if (CommonUtils.isNullOrEmpty(gsmSyntheticTestRunId)) { return; } String syntheticSource = telemetry.getContext().getOperation().getSyntheticSource(); if (CommonUtils.isNullOrEmpty(syntheticSource)) { telemetry.getContext().getOperation().setSyntheticSource(SYNTHETIC_SOURCE_NAME); } String sessionId = telemetry.getContext().getSession().getId(); if (CommonUtils.isNullOrEmpty(sessionId)) { telemetry.getContext().getSession().setId(gsmSyntheticTestRunId); } String userId = telemetry.getContext().getUser().getId(); if (CommonUtils.isNullOrEmpty(userId)) { String header = request.getHeader(SYNTHETIC_TEST_LOCATION); telemetry.getContext().getUser().setId(header); } }
/** * Initializes the properties of the given telemetry. * * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override protected void onInitializeTelemetry(Telemetry telemetry) { SessionContext session = telemetry.getContext().getSession(); if (!CommonUtils.isNullOrEmpty(session.getId())) { return; } RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); SessionContext requestSessionContext = requestTelemetry.getContext().getSession(); if (requestSessionContext == null) { return; } session.setId(requestSessionContext.getId()); Boolean isFirst = requestSessionContext.getIsFirst(); session.setIsFirst(isFirst != null ? isFirst : false); } }
/** * Initializes the properties of the given telemetry. * * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override protected void onInitializeTelemetry(Telemetry telemetry) { SessionContext session = telemetry.getContext().getSession(); if (!CommonUtils.isNullOrEmpty(session.getId())) { return; } RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); SessionContext requestSessionContext = requestTelemetry.getContext().getSession(); if (requestSessionContext == null) { return; } session.setId(requestSessionContext.getId()); Boolean isFirst = requestSessionContext.getIsFirst(); session.setIsFirst(isFirst != null ? isFirst : false); } }
private void handleCommonSyntheticRequest(String syntheticSourceHeader, Telemetry telemetry, HttpServletRequest request) { String syntheticSource = telemetry.getContext().getOperation().getSyntheticSource(); if (CommonUtils.isNullOrEmpty(syntheticSource)) { telemetry.getContext().getOperation().setSyntheticSource(syntheticSourceHeader); } String userId = telemetry.getContext().getUser().getId(); if (CommonUtils.isNullOrEmpty(userId)) { String header = request.getHeader(SYNTHETIC_TEST_USER_ID); telemetry.getContext().getUser().setId(header); } String sessionId = telemetry.getContext().getSession().getId(); if (CommonUtils.isNullOrEmpty(sessionId)) { String header = request.getHeader(SYNTHETIC_TEST_SESSION_ID); telemetry.getContext().getSession().setId(header); } String operationId = telemetry.getContext().getOperation().getId(); if (CommonUtils.isNullOrEmpty(operationId)) { String header = request.getHeader(SYNTHETIC_TEST_OPERATION_ID); telemetry.getContext().getOperation().setId(header); } putInProperties(telemetry, request, SYNTHETIC_TEST_TEST_NAME, SYNTHETIC_TEST_RUN_ID, SYNTHETIC_TEST_LOCATION); }
@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); }
private void handleCommonSyntheticRequest(String syntheticSourceHeader, Telemetry telemetry, HttpServletRequest request) { String syntheticSource = telemetry.getContext().getOperation().getSyntheticSource(); if (CommonUtils.isNullOrEmpty(syntheticSource)) { telemetry.getContext().getOperation().setSyntheticSource(syntheticSourceHeader); } String userId = telemetry.getContext().getUser().getId(); if (CommonUtils.isNullOrEmpty(userId)) { String header = request.getHeader(SYNTHETIC_TEST_USER_ID); telemetry.getContext().getUser().setId(header); } String sessionId = telemetry.getContext().getSession().getId(); if (CommonUtils.isNullOrEmpty(sessionId)) { String header = request.getHeader(SYNTHETIC_TEST_SESSION_ID); telemetry.getContext().getSession().setId(header); } String operationId = telemetry.getContext().getOperation().getId(); if (CommonUtils.isNullOrEmpty(operationId)) { String header = request.getHeader(SYNTHETIC_TEST_OPERATION_ID); telemetry.getContext().getOperation().setId(header); } putInProperties(telemetry, request, SYNTHETIC_TEST_TEST_NAME, SYNTHETIC_TEST_RUN_ID, SYNTHETIC_TEST_LOCATION); }
@Test public void testSessionIdNotOverriddenIfAlreadySet() { String expectedSessionId = "SOME_ID"; TraceTelemetry telemetry = new TraceTelemetry(); telemetry.getContext().getSession().setId(expectedSessionId); sessionTelemetryInitializer.onInitializeTelemetry(telemetry); Assert.assertEquals(expectedSessionId, telemetry.getContext().getSession().getId()); }
private void testNoSessionCookie(Boolean isFirstValue) { RequestTelemetryContext requestTelemetryContext = ThreadContext.getRequestTelemetryContext(); requestTelemetryContext.getHttpRequestTelemetry().getContext().getSession().setIsFirst(isFirstValue); requestTelemetryContext.getHttpRequestTelemetry().getContext().getSession().setId(null); TraceTelemetry telemetry = new TraceTelemetry(); sessionTelemetryInitializer.onInitializeTelemetry(telemetry); Assert.assertNull(telemetry.getContext().getSession().getId()); Assert.assertFalse(telemetry.getContext().getSession().getIsFirst()); } }