Skip to content

Commit cb50288

Browse files
committed
Update id format when in W3C mode
1 parent 5c60fb0 commit cb50288

File tree

5 files changed

+26
-43
lines changed

5 files changed

+26
-43
lines changed

web/src/main/java/com/microsoft/applicationinsights/web/internal/SdkBridgeImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.microsoft.applicationinsights.telemetry.RequestTelemetry;
2727
import com.microsoft.applicationinsights.web.internal.correlation.TelemetryCorrelationUtils;
2828
import com.microsoft.applicationinsights.web.internal.correlation.TraceContextCorrelation;
29+
import com.microsoft.applicationinsights.web.internal.correlation.tracecontext.Traceparent;
2930

3031
class SdkBridgeImpl extends AbstractSdkBridge<RequestTelemetryContext> {
3132

@@ -63,21 +64,20 @@ public String generateChildDependencyTarget(String requestContext, boolean w3c)
6364
@Override
6465
public <C> String propagate(Setter<C> setter, C carrier, boolean w3c, boolean w3cBackCompat) {
6566
if (w3c) {
66-
String traceparent = TraceContextCorrelation.generateChildDependencyTraceparent();
67+
Traceparent traceparent = TraceContextCorrelation.generateChildDependencyTraceparentObj();
6768
if (traceparent == null) {
6869
// this means an error occurred (and was logged) in above method, so just return a valid outgoingSpanId
69-
return TelemetryCorrelationUtils.generateChildDependencyId();
70+
return new Traceparent().getSpanId();
7071
}
71-
String outgoingSpanId = TraceContextCorrelation.createChildIdFromTraceparentString(traceparent);
7272
String tracestate = TraceContextCorrelation.retriveTracestate();
73-
setter.put(carrier, "traceparent", traceparent);
73+
setter.put(carrier, "traceparent", traceparent.toString());
7474
if (w3cBackCompat) {
75-
setter.put(carrier, "Request-Id", outgoingSpanId);
75+
setter.put(carrier, "Request-Id", "|" + traceparent.getTraceId() + "." + traceparent.getSpanId() + ".");
7676
}
7777
if (tracestate != null) {
7878
setter.put(carrier, "tracestate", tracestate);
7979
}
80-
return outgoingSpanId;
80+
return traceparent.getSpanId();
8181
} else {
8282
String outgoingSpanId = TelemetryCorrelationUtils.generateChildDependencyId();
8383
String correlationContext = TelemetryCorrelationUtils.retrieveCorrelationContext();

web/src/main/java/com/microsoft/applicationinsights/web/internal/correlation/TraceContextCorrelation.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,14 @@ public static void resolveCorrelation(HttpServletRequest request, HttpServletRes
7373
Traceparent processedTraceParent = processIncomingTraceparent(incomingTraceparent, request);
7474

7575
// represents the id of the current request.
76-
requestTelemetry.setId("|" + processedTraceParent.getTraceId() + "." + processedTraceParent.getSpanId()
77-
+ ".");
76+
requestTelemetry.setId(processedTraceParent.getSpanId());
7877

7978
// represents the trace-id of this distributed trace
8079
requestTelemetry.getContext().getOperation().setId(processedTraceParent.getTraceId());
8180

8281
// assign parent id
8382
if (incomingTraceparent != null) {
84-
requestTelemetry.getContext().getOperation().setParentId("|" + processedTraceParent.getTraceId() + "." +
85-
incomingTraceparent.getSpanId() + ".");
83+
requestTelemetry.getContext().getOperation().setParentId(incomingTraceparent.getSpanId());
8684
} else {
8785
// set parentId only if not already set (legacy processing can set it)
8886
if (requestTelemetry.getContext().getOperation().getParentId() == null) {
@@ -453,6 +451,11 @@ public static String retriveTracestate() {
453451
* @return Outbound Traceparent
454452
*/
455453
public static String generateChildDependencyTraceparent() {
454+
Traceparent tp = generateChildDependencyTraceparentObj();
455+
return tp == null ? null : tp.toString();
456+
}
457+
458+
public static Traceparent generateChildDependencyTraceparentObj() {
456459
try {
457460

458461
RequestTelemetryContext context = ThreadContext.getRequestTelemetryContext();
@@ -461,7 +464,7 @@ public static String generateChildDependencyTraceparent() {
461464
// This is likely worker role scenario, where a worker is trying
462465
// to create a new outbound call, so generate a new traceparent.
463466
if (context == null) {
464-
return new Traceparent().toString();
467+
return new Traceparent();
465468
}
466469

467470
RequestTelemetry requestTelemetry = context.getHttpRequestTelemetry();
@@ -470,7 +473,7 @@ public static String generateChildDependencyTraceparent() {
470473
Traceparent tp = new Traceparent(0, traceId, null, context.getTraceflag());
471474

472475
// We need to propagate full blown traceparent header.
473-
return tp.toString();
476+
return tp;
474477
}
475478
catch (Exception ex) {
476479
InternalLogger.INSTANCE.error("Failed to generate child ID. Exception information: %s", ex.toString());
@@ -485,6 +488,8 @@ public static String generateChildDependencyTraceparent() {
485488
* @param traceparent
486489
* @return legacy format traceparent
487490
*/
491+
// no longer used, but not removing since public
492+
@Deprecated
488493
public static String createChildIdFromTraceparentString(String traceparent) {
489494
if (traceparent == null) {
490495
throw new NullPointerException("traceparent cannot be null");

web/src/test/java/com/microsoft/applicationinsights/web/extensibility/initializers/WebOperationIdTelemetryInitializerTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
package com.microsoft.applicationinsights.web.extensibility.initializers;
2323

2424
import org.junit.*;
25+
2526
import java.util.List;
27+
2628
import com.microsoft.applicationinsights.TelemetryConfiguration;
2729
import com.microsoft.applicationinsights.extensibility.context.OperationContext;
2830
import com.microsoft.applicationinsights.internal.util.DateTimeUtils;
@@ -80,11 +82,7 @@ public void testRequestTelemetryInitializedWithOperationId() throws Exception {
8082
assertEquals(1, items.size());
8183
RequestTelemetry requestTelemetry = items.get(0);
8284

83-
// the WebRequestTrackingModule automatically creates a hierarchical ID for request telemetry of the
84-
// following form: "|guid.spanid", where guid is the OperationId
85-
String requestTelemetryId = requestTelemetry.getId();
86-
requestTelemetryId = requestTelemetryId.substring(0, requestTelemetryId.indexOf('.') + 1);
87-
Assert.assertEquals("Operation id not match", requestTelemetryId, "|" + requestTelemetry.getContext().getOperation().getId() + ".");
85+
Assert.assertNotNull("Operation id not set", requestTelemetry.getContext().getOperation().getId());
8886
}
8987

9088
@Test
@@ -94,7 +92,8 @@ public void testTelemetryInitializedWithOperationId() {
9492

9593
OperationContext operationContext = createAndInitializeTelemetry();
9694

97-
Assert.assertEquals("Operation ID hasn't been set.", context.getHttpRequestTelemetry().getId(), operationContext.getId());
95+
Assert.assertEquals("Operation ID hasn't been set.", context.getHttpRequestTelemetry().getId(),
96+
operationContext.getId());
9897
}
9998

10099
@Test

web/src/test/java/com/microsoft/applicationinsights/web/extensibility/modules/WebRequestTrackingTelemetryModuleTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,13 @@ public void testCrossComponentCorrelationHeadersAreCapturedWhenW3CTurnedOn() thr
260260
RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
261261
assertNotNull(requestTelemetry.getId());
262262
// spanIds are different
263-
String[] id = requestTelemetry.getId().split("[.]");
264-
Assert.assertNotEquals(tp.getSpanId(), id[1]);
265-
// traceIds are same
266-
Assert.assertTrue(requestTelemetry.getId().startsWith(formatedID(tp.getTraceId())));
263+
String spanId = requestTelemetry.getId();
264+
Assert.assertNotEquals(tp.getSpanId(), spanId);
267265

268266
//validate operation context ID's
269267
OperationContext operation = requestTelemetry.getContext().getOperation();
270268
assertEquals(tp.getTraceId(), operation.getId());
271-
assertEquals(formatedID(tp.getTraceId() + "." + tp.getSpanId()), operation.getParentId());
269+
assertEquals(tp.getSpanId(), operation.getParentId());
272270

273271
//run onEnd
274272
defaultModule.onEndRequest(request, null);
@@ -310,8 +308,6 @@ public void testLegacyHeadersAreCapturedWhenW3CIsTurnedOn() throws Exception {
310308
RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry();
311309
assertNotNull(requestTelemetry.getId());
312310

313-
Assert.assertTrue(requestTelemetry.getId().startsWith("|"+tp.getTraceId()));
314-
315311
//validate operation context ID's
316312
OperationContext operation = requestTelemetry.getContext().getOperation();
317313
assertEquals(tp.getTraceId(), operation.getId());
@@ -524,11 +520,6 @@ public void testLegacyHeadersAreNotCapturedWhenW3CIsTurnedOnAndBackPortSwitchIsO
524520
Assert.assertFalse(requestTelemetry.getContext().getProperties().containsKey("ai_legacyRootID"));
525521
}
526522

527-
528-
private String formatedID(String id) {
529-
return "|" + id + ".";
530-
}
531-
532523
@Test
533524
public void testTelemetryCreatedWithinRequestScopeIsRequestChild() throws Exception {
534525

web/src/test/java/com/microsoft/applicationinsights/web/internal/correlation/TraceContextCorrelationTests.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ public void testTraceparentAreResolved() {
4747
//validate we have generated proper ID's
4848
assertNotNull(requestTelemetry.getId());
4949

50-
assertTrue(requestTelemetry.getId().startsWith(formatedID(t.getTraceId())));
51-
5250
//validate operation context ID's
5351
OperationContext operation = requestTelemetry.getContext().getOperation();
5452
assertEquals(t.getTraceId(), operation.getId());
55-
assertEquals(formatedID(t.getTraceId() + "." + t.getSpanId()), operation.getParentId());
53+
assertEquals(t.getSpanId(), operation.getParentId());
5654
}
5755

5856
@Test
@@ -73,9 +71,6 @@ public void testCorrelationIdsAreResolvedIfNoTraceparentHeader() {
7371
OperationContext operation = requestTelemetry.getContext().getOperation();
7472

7573
assertNotNull(requestTelemetry.getId());
76-
77-
// First trace will have it's own spanId also.
78-
assertTrue(requestTelemetry.getId().startsWith(formatedID(operation.getId())));
7974
assertNull(operation.getParentId());
8075
}
8176

@@ -99,8 +94,6 @@ public void testCorrelationIdsAreResolvedIfTraceparentEmpty() {
9994
OperationContext operation = requestTelemetry.getContext().getOperation();
10095

10196
assertNotNull(requestTelemetry.getId());
102-
// First trace will have it's own spanId also.
103-
assertTrue(requestTelemetry.getId().startsWith("|" + operation.getId()));
10497
assertNull(operation.getParentId());
10598
}
10699

@@ -228,9 +221,4 @@ public static String getRequestSourceValue(String appId) {
228221

229222
return String.format("cid-v1:%s", appId);
230223
}
231-
232-
private String formatedID(String id) {
233-
return "|" + id + ".";
234-
}
235-
236224
}

0 commit comments

Comments
 (0)