Skip to content

Commit 9cb8f10

Browse files
authored
Bridge session id from 2.x SDK (#1930)
* Add test for Session.setId * Update submodule
1 parent 76a54b9 commit 9cb8f10

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
4242
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryUtil;
4343
import io.opentelemetry.api.common.AttributeKey;
44+
import io.opentelemetry.api.common.AttributeType;
4445
import io.opentelemetry.api.common.Attributes;
4546
import io.opentelemetry.api.trace.SpanContext;
4647
import io.opentelemetry.api.trace.SpanId;
@@ -95,6 +96,8 @@ public class Exporter implements SpanExporter {
9596
// for ThreadContext.getRequestTelemetryContext().getRequestTelemetry().setSource()
9697
private static final AttributeKey<String> AI_SPAN_SOURCE_KEY =
9798
AttributeKey.stringKey("applicationinsights.internal.source");
99+
private static final AttributeKey<String> AI_SESSION_ID_KEY =
100+
AttributeKey.stringKey("applicationinsights.internal.session_id");
98101

99102
private static final AttributeKey<String> AI_LOG_LEVEL_KEY =
100103
AttributeKey.stringKey("applicationinsights.internal.log_level");
@@ -795,6 +798,13 @@ private void exportRequest(SpanData span) {
795798

796799
data.setSource(getSource(attributes, span.getSpanContext()));
797800

801+
String sessionId = attributes.get(AI_SESSION_ID_KEY);
802+
if (sessionId != null) {
803+
// this is only used by the 2.x web interop bridge for
804+
// ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getSession().setId()
805+
telemetry.getTags().put(ContextTagKeys.AI_SESSION_ID.toString(), sessionId);
806+
}
807+
798808
// TODO(trask)? for batch consumer, enqueuedTime should be the average of this attribute
799809
// across all links
800810
Long enqueuedTime = attributes.get(AZURE_SDK_ENQUEUED_TIME);
@@ -844,7 +854,7 @@ public static String getHttpUrlFromServerSpan(Attributes attributes) {
844854
@Nullable
845855
private static String getSource(Attributes attributes, SpanContext spanContext) {
846856
// this is only used by the 2.x web interop bridge
847-
// for ThreadContext.getRequestTelemetryContext().getRequestTelemetry().setSource()
857+
// for ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().setSource()
848858
String source = attributes.get(AI_SPAN_SOURCE_KEY);
849859
if (source != null) {
850860
return source;
@@ -1035,11 +1045,12 @@ private static void setExtraAttributes(
10351045
return;
10361046
}
10371047
// special case mappings
1038-
if (key.equals(SemanticAttributes.ENDUSER_ID) && value instanceof String) {
1048+
if (stringKey.equals(SemanticAttributes.ENDUSER_ID.getKey()) && value instanceof String) {
10391049
telemetry.getTags().put(ContextTagKeys.AI_USER_ID.toString(), (String) value);
10401050
return;
10411051
}
1042-
if (key.equals(SemanticAttributes.HTTP_USER_AGENT) && value instanceof String) {
1052+
if (stringKey.equals(SemanticAttributes.HTTP_USER_AGENT.getKey())
1053+
&& value instanceof String) {
10431054
telemetry.getTags().put("ai.user.userAgent", (String) value);
10441055
return;
10451056
}
@@ -1064,16 +1075,16 @@ private static void setExtraAttributes(
10641075
if (STANDARD_ATTRIBUTE_PREFIXES.getOrDefault(stringKey, false)) {
10651076
return;
10661077
}
1067-
String val = getStringValue(key, value);
1078+
String val = convertToString(value, key.getType());
10681079
if (value != null) {
10691080
TelemetryUtil.getProperties(data).put(key.getKey(), val);
10701081
}
10711082
});
10721083
}
10731084

10741085
@Nullable
1075-
private static String getStringValue(AttributeKey<?> attributeKey, Object value) {
1076-
switch (attributeKey.getType()) {
1086+
private static String convertToString(Object value, AttributeType type) {
1087+
switch (type) {
10771088
case STRING:
10781089
case BOOLEAN:
10791090
case LONG:
@@ -1085,7 +1096,7 @@ private static String getStringValue(AttributeKey<?> attributeKey, Object value)
10851096
case DOUBLE_ARRAY:
10861097
return join((List<?>) value);
10871098
}
1088-
logger.warn("unexpected attribute type: {}", attributeKey.getType());
1099+
logger.warn("unexpected attribute type: {}", type);
10891100
return null;
10901101
}
10911102

test/smoke/testApps/LegacySdkWebInterop/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public String test() {
4141
requestTelemetry.getProperties().put("myattr1", "myvalue1");
4242
requestTelemetry.getProperties().put("myattr2", "myvalue2");
4343
requestTelemetry.getContext().getUser().setId("myuser");
44+
requestTelemetry.getContext().getSession().setId("mysessionid");
4445
requestTelemetry.setName("myspanname");
4546
requestTelemetry.setSource("mysource");
4647
requestTelemetry.setSuccess(false);

test/smoke/testApps/LegacySdkWebInterop/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/LegacySdkWebInteropTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void doMostBasicTest() throws Exception {
3737
assertEquals("myspanname", telemetry.rd.getName());
3838
assertEquals("mysource", telemetry.rd.getSource());
3939
assertEquals("myuser", telemetry.rdEnvelope.getTags().get("ai.user.id"));
40+
assertEquals("mysessionid", telemetry.rdEnvelope.getTags().get("ai.session.id"));
4041
assertEquals("myvalue1", telemetry.rd.getProperties().get("myattr1"));
4142
assertEquals("myvalue2", telemetry.rd.getProperties().get("myattr2"));
4243
assertEquals(2, telemetry.rd.getProperties().size());

0 commit comments

Comments
 (0)