@@ -44,7 +44,7 @@ public class TelemetryCorrelationUtils {
4444 public static final String REQUEST_CONTEXT_HEADER_NAME = "Request-Context" ;
4545 public static final String REQUEST_CONTEXT_HEADER_APPID_KEY = "appId" ;
4646 public static final String REQUEST_CONTEXT_HEADER_ROLENAME_KEY = "roleName" ;
47- public static final int REQUESTID_MAXLENGTH = 1024 ;
47+ public static final int REQUESTID_MAXLENGTH = 1024 ;
4848
4949 private TelemetryCorrelationUtils () {}
5050
@@ -65,7 +65,7 @@ public static void resolveCorrelation(HttpServletRequest request, HttpServletRes
6565 InternalLogger .INSTANCE .error ("Failed to resolve correlation. response is null." );
6666 return ;
6767 }
68-
68+
6969 if (requestTelemetry == null ) {
7070 InternalLogger .INSTANCE .error ("Failed to resolve correlation. requestTelemetry is null." );
7171 return ;
@@ -104,12 +104,12 @@ public static void resolveCorrelation(HttpServletRequest request, HttpServletRes
104104 }
105105
106106 /**
107- * Generates a child Id for dependencies. Dependencies are children of requests and, therefore, their ID's
107+ * Generates a child Id for dependencies. Dependencies are children of requests and, therefore, their ID's
108108 * reflect this. The generated ID is based on the current request scope (stored in TLS).
109109 * @return The child Id.
110110 */
111111 public static String generateChildDependencyId () {
112-
112+
113113 try {
114114
115115 RequestTelemetryContext context = ThreadContext .getRequestTelemetryContext ();
@@ -162,8 +162,8 @@ public static String retrieveApplicationCorrelationId() {
162162
163163 String instrumentationKey = TelemetryConfiguration .getActive ().getInstrumentationKey ();
164164 String appId = InstrumentationKeyResolver .INSTANCE .resolveInstrumentationKey (instrumentationKey );
165-
166- //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just
165+
166+ //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just
167167 //return and let the next request resolve the ikey.
168168 if (appId == null ) {
169169 InternalLogger .INSTANCE .trace ("Application correlation Id could not be retrieved (e.g. task may be pending or failed)" );
@@ -174,7 +174,7 @@ public static String retrieveApplicationCorrelationId() {
174174 }
175175
176176 /**
177- * Given a request context, it generates a new dependency target, possibly including the appId found in
177+ * Given a request context, it generates a new dependency target, possibly including the appId found in
178178 * the given Request-Context.
179179 * @param requestContext - the Request-Context header value
180180 * @return the dependency target
@@ -183,7 +183,7 @@ public static String generateChildDependencyTarget(String requestContext) {
183183
184184 if (requestContext == null || requestContext .isEmpty ()) {
185185 InternalLogger .INSTANCE .trace ("generateChildDependencyTarget: won't continue as requestContext is null or empty." );
186- return "" ;
186+ return "" ;
187187 }
188188
189189 String instrumentationKey = TelemetryConfiguration .getActive ().getInstrumentationKey ();
@@ -215,7 +215,7 @@ public static void resolveRequestSource(HttpServletRequest request, RequestTelem
215215 InternalLogger .INSTANCE .error ("Failed to resolve correlation. request is null." );
216216 return ;
217217 }
218-
218+
219219 if (requestTelemetry == null ) {
220220 InternalLogger .INSTANCE .error ("Failed to resolve correlation. requestTelemetry is null." );
221221 return ;
@@ -236,7 +236,7 @@ public static void resolveRequestSource(HttpServletRequest request, RequestTelem
236236 InternalLogger .INSTANCE .error ("Failed to resolve correlation. InstrumentationKey is null or empty." );
237237 return ;
238238 }
239-
239+
240240 String source = generateSourceTargetCorrelation (instrumentationKey , requestContext );
241241 requestTelemetry .setSource (source );
242242 }
@@ -261,7 +261,7 @@ public static boolean isHierarchicalId(String id) {
261261 * @param requestTelemetry The request telemetry item.
262262 */
263263 private static void resolveCorrelationContext (HttpServletRequest request , RequestTelemetry requestTelemetry ) {
264-
264+
265265 // resolve baggages (Correlation-Context)
266266 @ SuppressWarnings ("unchecked" )
267267 Enumeration <String > baggages = request .getHeaders (CORRELATION_CONTEXT_HEADER_NAME );
@@ -278,7 +278,7 @@ private static void resolveCorrelationContext(HttpServletRequest request, Reques
278278 return ;
279279 }
280280
281- CorrelationContext currentCorrelationContext =
281+ CorrelationContext currentCorrelationContext =
282282 ThreadContext .getRequestTelemetryContext ().getCorrelationContext ();
283283
284284 while (baggages .hasMoreElements ()) {
@@ -295,7 +295,7 @@ private static void resolveCorrelationContext(HttpServletRequest request, Reques
295295 * @param response The servlet's response.
296296 */
297297 private static void addTargetAppIdForResponseHeader (HttpServletResponse response ) {
298-
298+
299299 if (response .containsHeader (REQUEST_CONTEXT_HEADER_NAME )) {
300300 return ;
301301 }
@@ -315,14 +315,14 @@ private static void addTargetAppIdForResponseHeader(HttpServletResponse response
315315 private static String generateSourceTargetCorrelation (String instrumentationKey , String requestContext ) {
316316 String appId = getKeyValueHeaderValue (requestContext , REQUEST_CONTEXT_HEADER_APPID_KEY );
317317 String roleName = getKeyValueHeaderValue (requestContext , REQUEST_CONTEXT_HEADER_ROLENAME_KEY );
318-
318+
319319 if (appId == null && roleName == null ) {
320320 return null ;
321321 }
322-
322+
323323 String myAppId = InstrumentationKeyResolver .INSTANCE .resolveInstrumentationKey (instrumentationKey );
324-
325- //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just
324+
325+ //it's possible the appId returned is null (e.g. async task is still pending or has failed). In this case, just
326326 //return and let the next request resolve the ikey.
327327 if (myAppId == null ) {
328328 InternalLogger .INSTANCE .trace ("Could not generate source/target correlation as the appId could not be resolved (e.g. task may be pending or failed)" );
@@ -346,8 +346,8 @@ private static String generateSourceTargetCorrelation(String instrumentationKey,
346346 }
347347
348348 /**
349- * Extracts the value of a "Key-Value" type of header. For example, for a header with value: "foo=bar, name=joe",
350- * we can extract "joe" with a call to this method passing the key "name".
349+ * Extracts the value of a "Key-Value" type of header. For example, for a header with value: "foo=bar, name=joe",
350+ * we can extract "joe" with a call to this method passing the key "name".
351351 * @param headerFullValue The entire header value.
352352 * @param key They key for which to extract the value
353353 * @return The extracted value
@@ -357,7 +357,7 @@ private static String getKeyValueHeaderValue(String headerFullValue, String key)
357357 }
358358
359359 private static Map <String , String > getPropertyBag (String baggage ) {
360-
360+
361361 Map <String , String > result = new HashMap <String , String >();
362362
363363 String [] pairs = baggage .split ("," );
@@ -366,18 +366,18 @@ private static Map<String, String> getPropertyBag(String baggage) {
366366 if (keyValuePair .length == 2 ) {
367367 String key = keyValuePair [0 ].trim ();
368368 String value = keyValuePair [1 ].trim ();
369- result .put (key , value );
369+ result .put (key , value );
370370 }
371371 }
372-
372+
373373 return result ;
374374 }
375375
376376
377377 static String extractRootId (String parentId ) {
378378 // ported from .NET's System.Diagnostics.Activity.cs implementation:
379379 // https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
380-
380+
381381 int rootEnd = parentId .indexOf ('.' );
382382 if (rootEnd < 0 ) {
383383 rootEnd = parentId .length ();
@@ -389,11 +389,18 @@ static String extractRootId(String parentId) {
389389 }
390390
391391 private static String generateRootId () {
392- UUID guid = UUID .randomUUID ();
393- long least = guid .getLeastSignificantBits ();
394- long most = guid .getMostSignificantBits ();
392+ return uuidToStringNoDashes (UUID .randomUUID ());
393+ }
395394
396- return Long .toHexString (most ) + Long .toHexString (least );
395+ static String uuidToStringNoDashes (UUID guid ) {
396+ long most = guid .getMostSignificantBits ();
397+ long least = guid .getLeastSignificantBits ();
398+ final long msb = 1L << 32 ;
399+ final long mask = msb - 1 ;
400+ return Long .toHexString (msb | ((most >> 32 ) & mask )).substring (1 )
401+ + Long .toHexString (msb | (most & mask )).substring (1 )
402+ + Long .toHexString (msb | ((least >> 32 ) & mask )).substring (1 )
403+ + Long .toHexString (msb | (least & mask )).substring (1 );
397404 }
398405
399406 private static String generateId (String parentId ) {
@@ -409,7 +416,7 @@ private static String generateId(String parentId) {
409416 }
410417
411418 private static String shortenId (String parentId , String suffix ) {
412-
419+
413420 // ported from .NET's System.Diagnostics.Activity.cs implementation:
414421 // https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs
415422 int trimPosition = REQUESTID_MAXLENGTH - 9 ; // make room for suffix + delimiter
@@ -419,17 +426,17 @@ private static String shortenId(String parentId, String suffix) {
419426 break ;
420427 trimPosition --;
421428 }
422-
429+
423430 // parentId is not a valid requestId, so generate one.
424431 if (trimPosition == 1 ) {
425432 return "|" + generateRootId () + "." ;
426433 }
427-
434+
428435 return parentId .substring (0 , trimPosition ) + suffix + '#' ;
429436 }
430437
431438 private static String sanitizeParentId (String parentId ) {
432-
439+
433440 String result = parentId ;
434441 if (!isHierarchicalId (parentId )) {
435442 result = "|" + result ;
@@ -439,12 +446,12 @@ private static String sanitizeParentId(String parentId) {
439446 if (lastChar != '.' && lastChar != '_' ) {
440447 result = result + '.' ;
441448 }
442-
449+
443450 return result ;
444451 }
445452
446453 private static String generateSuffix () {
447- // using ThreadLocalRandom instead of Random to avoid multi-threaded contention which would
454+ // using ThreadLocalRandom instead of Random to avoid multi-threaded contention which would
448455 // result in poor performance.
449456 int randomNumber = ThreadLocalRandom .current ().nextInt ();
450457 return String .format ("%08x" , randomNumber );
0 commit comments