@@ -153,8 +153,22 @@ private void exportRequest(SpanData span) {
153153 telemetry .setSource (peerAddress + "/" + destination );
154154 }
155155
156- setContext (span , telemetry );
157156 telemetry .setId (span .getSpanId ().toLowerBase16 ());
157+ telemetry .getContext ().getOperation ().setId (span .getTraceId ().toLowerBase16 ());
158+ String aiLegacyParentId = span .getTraceState ().get ("ai-legacy-parent-id" );
159+ if (aiLegacyParentId != null ) {
160+ // see behavior specified at https://github.com/microsoft/ApplicationInsights-Java/issues/1174
161+ telemetry .getContext ().getOperation ().setParentId (aiLegacyParentId );
162+ String aiLegacyOperationId = span .getTraceState ().get ("ai-legacy-operation-id" );
163+ if (aiLegacyOperationId != null ) {
164+ telemetry .getContext ().getProperties ().putIfAbsent ("ai_legacyRootID" , aiLegacyOperationId );
165+ }
166+ } else {
167+ SpanId parentSpanId = span .getParentSpanId ();
168+ if (parentSpanId .isValid ()) {
169+ telemetry .getContext ().getOperation ().setParentId (parentSpanId .toLowerBase16 ());
170+ }
171+ }
158172
159173 telemetry .setTimestamp (new Date (NANOSECONDS .toMillis (span .getStartEpochNanos ())));
160174 telemetry .setDuration (new Duration (NANOSECONDS .toMillis (span .getEndEpochNanos () - span .getStartEpochNanos ())));
@@ -211,8 +225,12 @@ private void exportRemoteDependency(String component, SpanData span, boolean inP
211225 }
212226 }
213227
214- setContext (span , telemetry );
215228 telemetry .setId (span .getSpanId ().toLowerBase16 ());
229+ telemetry .getContext ().getOperation ().setId (span .getTraceId ().toLowerBase16 ());
230+ SpanId parentSpanId = span .getParentSpanId ();
231+ if (parentSpanId .isValid ()) {
232+ telemetry .getContext ().getOperation ().setParentId (parentSpanId .toLowerBase16 ());
233+ }
216234
217235 telemetry .setTimestamp (new Date (NANOSECONDS .toMillis (span .getStartEpochNanos ())));
218236 telemetry .setDuration (new Duration (NANOSECONDS .toMillis (span .getEndEpochNanos () - span .getStartEpochNanos ())));
@@ -439,15 +457,6 @@ private void track(Telemetry telemetry, Double samplingPercentage) {
439457 public void shutdown () {
440458 }
441459
442- private static void setContext (SpanData span , Telemetry telemetry ) {
443- String traceId = span .getTraceId ().toLowerBase16 ();
444- telemetry .getContext ().getOperation ().setId (traceId );
445- SpanId parentSpanId = span .getParentSpanId ();
446- if (parentSpanId .isValid ()) {
447- telemetry .getContext ().getOperation ().setParentId (parentSpanId .toLowerBase16 ());
448- }
449- }
450-
451460 private static boolean isNonNullLong (AttributeValue attributeValue ) {
452461 return attributeValue != null && attributeValue .getType () == AttributeValue .Type .LONG ;
453462 }
0 commit comments