/** * Extracts the appId/roleName out of Tracestate and compares it with the current appId. It then * generates the appropriate source or target. */ private static String generateSourceTargetCorrelation(String instrumentationKey, String appId) { assert instrumentationKey != null; assert appId != null; String myAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); return resolve(appId, myAppId); }
/** * Extracts the appId/roleName out of Tracestate and compares it with the current appId. It then * generates the appropriate source or target. */ private static String generateSourceTargetCorrelation(String instrumentationKey, String appId) { assert instrumentationKey != null; assert appId != null; String myAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); return resolve(appId, myAppId); }
/** * Retrieves the appId for the current active config's instrumentation key. */ public static String getAppId() { String instrumentationKey = TelemetryConfiguration.getActive().getInstrumentationKey(); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just //return and let the next request resolve the ikey. if (appId == null) { InternalLogger.INSTANCE.trace("Application correlation Id could not be retrieved (e.g. task may be pending or failed)"); return ""; } return appId; }
/** * Retrieves the appId (in correlation format) for the current active config's instrumentation key. */ public static String retrieveApplicationCorrelationId() { String instrumentationKey = TelemetryConfiguration.getActive().getInstrumentationKey(); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just //return and let the next request resolve the ikey. if (appId == null) { InternalLogger.INSTANCE.trace("Application correlation Id could not be retrieved (e.g. task may be pending or failed)"); return ""; } return REQUEST_CONTEXT_HEADER_APPID_KEY + "=" + appId; }
/** * Retrieves the appId (in correlation format) for the current active config's instrumentation key. */ public static String retrieveApplicationCorrelationId() { String instrumentationKey = TelemetryConfiguration.getActive().getInstrumentationKey(); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just //return and let the next request resolve the ikey. if (appId == null) { InternalLogger.INSTANCE.trace("Application correlation Id could not be retrieved (e.g. task may be pending or failed)"); return ""; } return REQUEST_CONTEXT_HEADER_APPID_KEY + "=" + appId; }
/** * Retrieves the appId for the current active config's instrumentation key. */ public static String getAppId() { String instrumentationKey = TelemetryConfiguration.getActive().getInstrumentationKey(); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey); //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just //return and let the next request resolve the ikey. if (appId == null) { InternalLogger.INSTANCE.trace("Application correlation Id could not be retrieved (e.g. task may be pending or failed)"); return ""; } return appId; }
String myAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey);
String myAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(instrumentationKey);
String currAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(TelemetryConfiguration.getActive() .getInstrumentationKey());
String currAppId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(TelemetryConfiguration.getActive() .getInstrumentationKey());
@Test public void testIkeyResolvedFromCache() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run Assert.assertEquals(0, mockFetcher.callCount()); InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals(1, mockFetcher.callCount()); //resolving the same ikey should not generate new call to fetcher InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals(1, mockFetcher.callCount()); //resolving another ikey increases call count InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey2"); Assert.assertEquals(2, mockFetcher.callCount()); } }
@Test public void testResolveInstrumentationKey() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("id1"); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertEquals("cid-v1:id1", appId); Assert.assertEquals(1, mockFetcher.callCount()); }
@Test public void testInstrumentationKeyIsResolvedDuringModuleInit() { Assert.assertEquals(0, mockProfileFetcher.callCount()); String ikey = TelemetryConfiguration.getActive().getInstrumentationKey(); //calling resolver now will actually retrieve the appId from the completed task mockProfileFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); mockProfileFetcher.setAppIdToReturn("someAppId"); String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(ikey); Assert.assertEquals(1, mockProfileFetcher.callCount()); Assert.assertEquals("cid-v1:someAppId", appId); //calling it again should retrieve appId from cache (i.e. fetcher call count remains 2) appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey(ikey); Assert.assertEquals(1, mockProfileFetcher.callCount()); Assert.assertEquals("cid-v1:someAppId", appId); }
@Test public void testResolveInstrumentationKeyWithPendingStatus() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); //fetcher will be called again since the task was pending (i.e. not yet in the cache) Assert.assertEquals(2, mockFetcher.callCount()); }
@Test public void testResolveInstrumentationKeyWithFailedStatus() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.FAILED); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); //fetcher will be called again since the previous attempt failed Assert.assertEquals(2, mockFetcher.callCount()); }
@Test public void testResolveInstrumentationKeyWhenExceptionThrown() { //setup MockProfileFetcher mockFetcher = new MockProfileFetcher(); mockFetcher.setAppIdToReturn("appId"); mockFetcher.setExceptionOn(true); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockFetcher); //run String appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //validate no exception is thrown back to the caller and appId is null Assert.assertNull(appId); Assert.assertEquals(1, mockFetcher.callCount()); //mimic calling resolver again after some time mockFetcher.setExceptionOn(false); mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); //result is still null since fetcher returns a task in pending state Assert.assertNull(appId); //fetcher will be called again since the previous attempt failed Assert.assertEquals(2, mockFetcher.callCount()); //mimic final call which returns the completed task mockFetcher.setResultStatus(ProfileFetcherResultTaskStatus.COMPLETE); appId = InstrumentationKeyResolver.INSTANCE.resolveInstrumentationKey("ikey"); Assert.assertEquals("cid-v1:appId", appId); Assert.assertEquals(3, mockFetcher.callCount()); }