in agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java [699:809]
private void exportRequest(SpanData span) {
TelemetryItem telemetry = new TelemetryItem();
RequestData data = new RequestData();
telemetryClient.initRequestTelemetry(telemetry, data);
Attributes attributes = span.getAttributes();
long startEpochNanos = span.getStartEpochNanos();
float samplingPercentage = getSamplingPercentage(span.getSpanContext().getTraceState());
// set standard properties
data.setId(span.getSpanId());
setTime(telemetry, startEpochNanos);
setSampleRate(telemetry, samplingPercentage);
setExtraAttributes(telemetry, data, attributes);
addLinks(data, span.getLinks());
String operationName = getOperationName(span);
telemetry.getTags().put(ContextTagKeys.AI_OPERATION_NAME.toString(), operationName);
telemetry.getTags().put(ContextTagKeys.AI_OPERATION_ID.toString(), span.getTraceId());
// see behavior specified at https://github.com/microsoft/ApplicationInsights-Java/issues/1174
String aiLegacyParentId = span.getAttributes().get(AI_LEGACY_PARENT_ID_KEY);
if (aiLegacyParentId != null) {
// this was the real (legacy) parent id, but it didn't fit span id format
telemetry.getTags().put(ContextTagKeys.AI_OPERATION_PARENT_ID.toString(), aiLegacyParentId);
} else if (span.getParentSpanContext().isValid()) {
telemetry
.getTags()
.put(
ContextTagKeys.AI_OPERATION_PARENT_ID.toString(),
span.getParentSpanContext().getSpanId());
}
String aiLegacyRootId = span.getAttributes().get(AI_LEGACY_ROOT_ID_KEY);
if (aiLegacyRootId != null) {
telemetry.getTags().put("ai_legacyRootID", aiLegacyRootId);
}
// set request-specific properties
data.setName(operationName);
data.setDuration(FormattedDuration.fromNanos(span.getEndEpochNanos() - startEpochNanos));
data.setSuccess(getSuccess(span));
String httpUrl = getHttpUrlFromServerSpan(attributes);
if (httpUrl != null) {
data.setUrl(httpUrl);
}
Long httpStatusCode = attributes.get(SemanticAttributes.HTTP_STATUS_CODE);
if (httpStatusCode == null) {
httpStatusCode = attributes.get(SemanticAttributes.RPC_GRPC_STATUS_CODE);
}
if (httpStatusCode != null) {
data.setResponseCode(Long.toString(httpStatusCode));
} else {
data.setResponseCode("0");
}
String locationIp = attributes.get(SemanticAttributes.HTTP_CLIENT_IP);
if (locationIp == null) {
// only use net.peer.ip if http.client_ip is not available
locationIp = attributes.get(SemanticAttributes.NET_PEER_IP);
}
if (locationIp != null) {
telemetry.getTags().put(ContextTagKeys.AI_LOCATION_IP.toString(), locationIp);
}
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);
}
String deviceOs = attributes.get(AI_DEVICE_OS_KEY);
if (deviceOs != null) {
// this is only used by the 2.x web interop bridge for
// ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getDevice().setOperatingSystem()
telemetry.getTags().put(ContextTagKeys.AI_DEVICE_OS.toString(), deviceOs);
}
String deviceOsVersion = attributes.get(AI_DEVICE_OS_VERSION_KEY);
if (deviceOsVersion != null) {
// this is only used by the 2.x web interop bridge for
// ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry().getContext().getDevice().setOperatingSystemVersion()
telemetry.getTags().put(ContextTagKeys.AI_DEVICE_OS_VERSION.toString(), deviceOsVersion);
}
// TODO(trask)? for batch consumer, enqueuedTime should be the average of this attribute
// across all links
Long enqueuedTime = attributes.get(AZURE_SDK_ENQUEUED_TIME);
if (enqueuedTime != null) {
long timeSinceEnqueuedMillis =
Math.max(
0L, NANOSECONDS.toMillis(span.getStartEpochNanos()) - SECONDS.toMillis(enqueuedTime));
if (data.getMeasurements() == null) {
data.setMeasurements(new HashMap<>());
}
data.getMeasurements().put("timeSinceEnqueued", (double) timeSinceEnqueuedMillis);
}
Long timeSinceEnqueuedMillis = attributes.get(KAFKA_RECORD_QUEUE_TIME_MS);
if (timeSinceEnqueuedMillis != null) {
if (data.getMeasurements() == null) {
data.setMeasurements(new HashMap<>());
}
data.getMeasurements().put("timeSinceEnqueued", (double) timeSinceEnqueuedMillis);
}
// export
telemetryClient.trackAsync(telemetry);
exportEvents(span, operationName, samplingPercentage);
}