Skip to content

Commit 0681283

Browse files
authored
2.x SDK explicit operation id / operation parent id should take precedence (#1708)
* Support explicit operation id/parentId from 2.x * Test
1 parent 9fdc73e commit 0681283

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/instrumentation/sdk/BytecodeUtilImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.microsoft.applicationinsights.agent.bootstrap.BytecodeUtil.BytecodeUtilDelegate;
3232
import com.microsoft.applicationinsights.agent.internal.Global;
3333
import com.microsoft.applicationinsights.agent.internal.sampling.SamplingScoreGeneratorV2;
34+
import com.microsoft.applicationinsights.extensibility.context.OperationContext;
3435
import com.microsoft.applicationinsights.telemetry.Duration;
3536
import com.microsoft.applicationinsights.telemetry.EventTelemetry;
3637
import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry;
@@ -238,8 +239,13 @@ private static void track(Telemetry telemetry) {
238239
// sampled out
239240
return;
240241
}
241-
telemetry.getContext().getOperation().setId(context.getTraceId());
242-
telemetry.getContext().getOperation().setParentId(context.getSpanId());
242+
OperationContext operation = telemetry.getContext().getOperation();
243+
if (operation.getId() == null) {
244+
operation.setId(context.getTraceId());
245+
}
246+
if (operation.getParentId() == null) {
247+
operation.setParentId(context.getSpanId());
248+
}
243249
samplingPercentage =
244250
Exporter.getSamplingPercentage(context.getTraceState(), Global.getSamplingPercentage(), false);
245251
} else {

core/src/main/java/com/microsoft/applicationinsights/extensibility/context/OperationContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public void setId(String id) {
5151
MapUtil.setStringValueOrRemove(tags, ContextTagKeys.getKeys().getOperationId(), id);
5252
}
5353

54+
/**
55+
* Get the Operation Parent id
56+
*/
57+
public String getParentId() {
58+
return MapUtil.getValueOrNull(tags, ContextTagKeys.getKeys().getOperationParentId());
59+
}
60+
5461
/**
5562
* Set the Operation Parent id
5663
* @param parentId

test/smoke/testApps/CoreAndFilter/src/main/java/com/microsoft/applicationinsights/smoketestapp/SimpleTrackPageViewServlet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
2828
pvt2.getContext().getCloud().setRole("role-goes-here");
2929
pvt2.getContext().getCloud().setRoleInstance("role-instance-goes-here");
3030
pvt2.getContext().getOperation().setName("operation-name-goes-here");
31+
pvt2.getContext().getOperation().setId("operation-id-goes-here");
32+
pvt2.getContext().getOperation().setParentId("operation-parent-id-goes-here");
3133
pvt2.getContext().getUser().setId("user-id-goes-here");
3234
pvt2.getContext().getUser().setAccountId("account-id-goes-here");
3335
pvt2.getContext().getUser().setUserAgent("user-agent-goes-here");
@@ -49,6 +51,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
4951
otherClient.getContext().getCloud().setRole("role-goes-here");
5052
otherClient.getContext().getCloud().setRoleInstance("role-instance-goes-here");
5153
otherClient.getContext().getOperation().setName("operation-name-goes-here");
54+
otherClient.getContext().getOperation().setId("operation-id-goes-here");
55+
otherClient.getContext().getOperation().setParentId("operation-parent-id-goes-here");
5256
otherClient.getContext().getUser().setId("user-id-goes-here");
5357
otherClient.getContext().getUser().setAccountId("account-id-goes-here");
5458
otherClient.getContext().getUser().setUserAgent("user-agent-goes-here");

test/smoke/testApps/CoreAndFilter/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/CoreAndFilterTests.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ public void testTrackPageView() throws Exception {
255255
List<Envelope> rdList = mockedIngestion.waitForItems("RequestData", 1);
256256

257257
Envelope rdEnvelope = rdList.get(0);
258-
String operationId = rdEnvelope.getTags().get("ai.operation.id");
259-
List<Envelope> pvdList = mockedIngestion.waitForItemsInOperation("PageViewData", 3, operationId);
258+
List<Envelope> pvdList = mockedIngestion.waitForItems("PageViewData", 3);
260259
assertEquals(0, mockedIngestion.getCountForType("EventData"));
261260

262261
RequestData rd = (RequestData) ((Data<?>) rdEnvelope.getData()).getBaseData();
@@ -330,8 +329,14 @@ public void testTrackPageView() throws Exception {
330329
assertTrue(pvdEnvelope3.getTags().get("ai.internal.sdkVersion").startsWith("java:3."));
331330

332331
assertParentChild(rd, rdEnvelope, pvdEnvelope1, "GET /CoreAndFilter/trackPageView");
333-
assertParentChild(rd, rdEnvelope, pvdEnvelope2, "GET /CoreAndFilter/trackPageView", "operation-name-goes-here");
334-
assertParentChild(rd, rdEnvelope, pvdEnvelope3, "GET /CoreAndFilter/trackPageView", "operation-name-goes-here");
332+
333+
assertEquals("operation-id-goes-here", pvdEnvelope2.getTags().get("ai.operation.id"));
334+
assertEquals("operation-parent-id-goes-here", pvdEnvelope2.getTags().get("ai.operation.parentId"));
335+
assertEquals("operation-name-goes-here", pvdEnvelope2.getTags().get("ai.operation.name"));
336+
337+
assertEquals("operation-id-goes-here", pvdEnvelope3.getTags().get("ai.operation.id"));
338+
assertEquals("operation-parent-id-goes-here", pvdEnvelope3.getTags().get("ai.operation.parentId"));
339+
assertEquals("operation-name-goes-here", pvdEnvelope3.getTags().get("ai.operation.name"));
335340
}
336341

337342
@Test

0 commit comments

Comments
 (0)