Skip to content

Commit

Permalink
Bridge session id from 2.x SDK (#1930)
Browse files Browse the repository at this point in the history
* Add test for Session.setId

* Update submodule
  • Loading branch information
trask authored Oct 27, 2021
1 parent 76a54b9 commit 9cb8f10
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,8 @@ public class Exporter implements SpanExporter {
// for ThreadContext.getRequestTelemetryContext().getRequestTelemetry().setSource()
private static final AttributeKey<String> AI_SPAN_SOURCE_KEY =
AttributeKey.stringKey("applicationinsights.internal.source");
private static final AttributeKey<String> AI_SESSION_ID_KEY =
AttributeKey.stringKey("applicationinsights.internal.session_id");

private static final AttributeKey<String> AI_LOG_LEVEL_KEY =
AttributeKey.stringKey("applicationinsights.internal.log_level");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -1064,16 +1075,16 @@ 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);
}
});
}

@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:
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 9cb8f10

Please sign in to comment.