Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bridge session id from 2.x SDK #1930

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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