/** * 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); }
/** * @param instrumentationKey The instrumentation key. * @return The applicationId associated with the instrumentation key or null if it cannot be retrieved. */ public String resolveInstrumentationKey(String instrumentationKey) { if (instrumentationKey == null || instrumentationKey.isEmpty()) { throw new IllegalArgumentException("instrumentationKey must be not null or empty"); } try { String appId = this.appIdCache.get(instrumentationKey); if (appId != null) { return appId; } ProfileFetcherResult result = this.profileFetcher.fetchAppProfile(instrumentationKey); appId = processResult(result, instrumentationKey); if (appId != null) { this.appIdCache.putIfAbsent(instrumentationKey, appId); } return appId; } catch (Exception e) { InternalLogger.INSTANCE.error("InstrumentationKeyResolver - failed to resolve instrumentation key: %s => Exception: %s", instrumentationKey, e); InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e)); } return null; }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }
@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()); }
@Before public void testInitialize() { InstrumentationKeyResolver.INSTANCE.clearCache(); }
@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()); } }
/** * 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); }
@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()); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); }
/** * @param instrumentationKey The instrumentation key. * @return The applicationId associated with the instrumentation key or null if it cannot be retrieved. */ public String resolveInstrumentationKey(String instrumentationKey) { if (instrumentationKey == null || instrumentationKey.isEmpty()) { throw new IllegalArgumentException("instrumentationKey must be not null or empty"); } try { String appId = this.appIdCache.get(instrumentationKey); if (appId != null) { return appId; } ProfileFetcherResult result = this.profileFetcher.fetchAppProfile(instrumentationKey); appId = processResult(result, instrumentationKey); if (appId != null) { this.appIdCache.putIfAbsent(instrumentationKey, appId); } return appId; } catch (Exception e) { InternalLogger.INSTANCE.error("InstrumentationKeyResolver - failed to resolve instrumentation key: %s => Exception: %s", instrumentationKey, e); InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e)); } return null; }
/** * 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; }
@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()); }
@Before public void testInitialize() { // initialize mock profile fetcher (for resolving ikeys to appIds) mockProfileFetcher = new MockProfileFetcher(); mockProfileFetcher.setResultStatus(ProfileFetcherResultTaskStatus.PENDING); InstrumentationKeyResolver.INSTANCE.setProfileFetcher(mockProfileFetcher); InstrumentationKeyResolver.INSTANCE.clearCache(); defaultModule = new WebRequestTrackingTelemetryModule(); defaultModule.initialize(TelemetryConfiguration.getActive()); channel.reset(); }
/** * 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; }
@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()); }
/** * 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());