From fe120f08c3a73f8e21269782e414f2496944bc80 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 26 Oct 2021 15:19:20 -0700 Subject: [PATCH 1/2] Add test for Session.setId --- .../agent/internal/exporter/Exporter.java | 25 +++++++++++++------ .../smoketestapp/TestController.java | 1 + .../smoketest/LegacySdkWebInteropTest.java | 1 + 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java index 5a88cc31a8b..eb46b9b8ce1 100644 --- a/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java +++ b/agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java @@ -41,6 +41,7 @@ import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient; import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryUtil; import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.AttributeType; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.api.trace.SpanId; @@ -95,6 +96,8 @@ public class Exporter implements SpanExporter { // for ThreadContext.getRequestTelemetryContext().getRequestTelemetry().setSource() private static final AttributeKey AI_SPAN_SOURCE_KEY = AttributeKey.stringKey("applicationinsights.internal.source"); + private static final AttributeKey AI_SESSION_ID_KEY = + AttributeKey.stringKey("applicationinsights.internal.session_id"); private static final AttributeKey AI_LOG_LEVEL_KEY = AttributeKey.stringKey("applicationinsights.internal.log_level"); @@ -795,6 +798,13 @@ private void exportRequest(SpanData span) { data.setSource(getSource(attributes, span.getSpanContext())); + String sessionId = attributes.get(AI_SESSION_ID_KEY); + if (sessionId != null) { + // this is only used by the 2.x web interop bridge for + // ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getSession().setId() + telemetry.getTags().put(ContextTagKeys.AI_SESSION_ID.toString(), sessionId); + } + // TODO(trask)? for batch consumer, enqueuedTime should be the average of this attribute // across all links Long enqueuedTime = attributes.get(AZURE_SDK_ENQUEUED_TIME); @@ -844,7 +854,7 @@ public static String getHttpUrlFromServerSpan(Attributes attributes) { @Nullable private static String getSource(Attributes attributes, SpanContext spanContext) { // this is only used by the 2.x web interop bridge - // for ThreadContext.getRequestTelemetryContext().getRequestTelemetry().setSource() + // for ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().setSource() String source = attributes.get(AI_SPAN_SOURCE_KEY); if (source != null) { return source; @@ -1035,11 +1045,12 @@ private static void setExtraAttributes( return; } // special case mappings - if (key.equals(SemanticAttributes.ENDUSER_ID) && value instanceof String) { + if (stringKey.equals(SemanticAttributes.ENDUSER_ID.getKey()) && value instanceof String) { telemetry.getTags().put(ContextTagKeys.AI_USER_ID.toString(), (String) value); return; } - if (key.equals(SemanticAttributes.HTTP_USER_AGENT) && value instanceof String) { + if (stringKey.equals(SemanticAttributes.HTTP_USER_AGENT.getKey()) + && value instanceof String) { telemetry.getTags().put("ai.user.userAgent", (String) value); return; } @@ -1064,7 +1075,7 @@ private static void setExtraAttributes( if (STANDARD_ATTRIBUTE_PREFIXES.getOrDefault(stringKey, false)) { return; } - String val = getStringValue(key, value); + String val = convertToString(value, key.getType()); if (value != null) { TelemetryUtil.getProperties(data).put(key.getKey(), val); } @@ -1072,8 +1083,8 @@ private static void setExtraAttributes( } @Nullable - private static String getStringValue(AttributeKey attributeKey, Object value) { - switch (attributeKey.getType()) { + private static String convertToString(Object value, AttributeType type) { + switch (type) { case STRING: case BOOLEAN: case LONG: @@ -1085,7 +1096,7 @@ private static String getStringValue(AttributeKey attributeKey, Object value) case DOUBLE_ARRAY: return join((List) value); } - logger.warn("unexpected attribute type: {}", attributeKey.getType()); + logger.warn("unexpected attribute type: {}", type); return null; } diff --git a/test/smoke/testApps/LegacySdkWebInterop/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java b/test/smoke/testApps/LegacySdkWebInterop/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java index f254f8ccde5..f0e05e80f03 100644 --- a/test/smoke/testApps/LegacySdkWebInterop/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java +++ b/test/smoke/testApps/LegacySdkWebInterop/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java @@ -41,6 +41,7 @@ public String test() { requestTelemetry.getProperties().put("myattr1", "myvalue1"); requestTelemetry.getProperties().put("myattr2", "myvalue2"); requestTelemetry.getContext().getUser().setId("myuser"); + requestTelemetry.getContext().getSession().setId("mysessionid"); requestTelemetry.setName("myspanname"); requestTelemetry.setSource("mysource"); requestTelemetry.setSuccess(false); diff --git a/test/smoke/testApps/LegacySdkWebInterop/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LegacySdkWebInteropTest.java b/test/smoke/testApps/LegacySdkWebInterop/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LegacySdkWebInteropTest.java index 0d04fa8cd6b..73c05d39889 100644 --- a/test/smoke/testApps/LegacySdkWebInterop/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LegacySdkWebInteropTest.java +++ b/test/smoke/testApps/LegacySdkWebInterop/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LegacySdkWebInteropTest.java @@ -37,6 +37,7 @@ public void doMostBasicTest() throws Exception { assertEquals("myspanname", telemetry.rd.getName()); assertEquals("mysource", telemetry.rd.getSource()); assertEquals("myuser", telemetry.rdEnvelope.getTags().get("ai.user.id")); + assertEquals("mysessionid", telemetry.rdEnvelope.getTags().get("ai.session.id")); assertEquals("myvalue1", telemetry.rd.getProperties().get("myattr1")); assertEquals("myvalue2", telemetry.rd.getProperties().get("myattr2")); assertEquals(2, telemetry.rd.getProperties().size()); From 3c9f9a0a56e62093f3a4b67b16cc1c791d4c07c0 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 26 Oct 2021 16:16:41 -0700 Subject: [PATCH 2/2] Update submodule --- otel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otel b/otel index e025087d2b2..cb3c9a9c40b 160000 --- a/otel +++ b/otel @@ -1 +1 @@ -Subproject commit e025087d2b2d1b397a63080c20df14936c339d43 +Subproject commit cb3c9a9c40bedf700604c9f912a4825c71ce28e3