@@ -4,11 +4,14 @@ import { SemanticInternalAttributes } from "../semanticInternalAttributes.js";
44import { Context } from "@opentelemetry/api" ;
55import { flattenAttributes } from "../utils/flattenAttributes.js" ;
66import { taskContext } from "../task-context-api.js" ;
7+ import { Tracer } from "@opentelemetry/api" ;
78
89export class TaskContextSpanProcessor implements SpanProcessor {
910 private _innerProcessor : SpanProcessor ;
11+ private _tracer : Tracer ;
1012
11- constructor ( innerProcessor : SpanProcessor ) {
13+ constructor ( tracer : Tracer , innerProcessor : SpanProcessor ) {
14+ this . _tracer = tracer ;
1215 this . _innerProcessor = innerProcessor ;
1316 }
1417
@@ -26,7 +29,15 @@ export class TaskContextSpanProcessor implements SpanProcessor {
2629 ) ;
2730 }
2831
29- this . _innerProcessor . onStart ( span , parentContext ) ;
32+ if ( ! isPartialSpan ( span ) ) {
33+ const partialSpan = createPartialSpan ( this . _tracer , span , parentContext ) ;
34+
35+ this . _innerProcessor . onStart ( span , parentContext ) ;
36+
37+ partialSpan . end ( ) ;
38+ } else {
39+ this . _innerProcessor . onStart ( span , parentContext ) ;
40+ }
3041 }
3142
3243 // Delegate the rest of the methods to the wrapped processor
@@ -44,6 +55,44 @@ export class TaskContextSpanProcessor implements SpanProcessor {
4455 }
4556}
4657
58+ function isPartialSpan ( span : Span ) {
59+ return span . attributes [ SemanticInternalAttributes . SPAN_PARTIAL ] === true ;
60+ }
61+
62+ function createPartialSpan ( tracer : Tracer , span : Span , parentContext : Context ) {
63+ const partialSpan = tracer . startSpan (
64+ span . name ,
65+ {
66+ attributes : {
67+ [ SemanticInternalAttributes . SPAN_PARTIAL ] : true ,
68+ [ SemanticInternalAttributes . SPAN_ID ] : span . spanContext ( ) . spanId ,
69+ ...span . attributes ,
70+ } ,
71+ } ,
72+ parentContext
73+ ) ;
74+
75+ if ( taskContext . ctx ) {
76+ partialSpan . setAttributes (
77+ flattenAttributes (
78+ {
79+ [ SemanticInternalAttributes . ATTEMPT_ID ] : taskContext . ctx . attempt . id ,
80+ [ SemanticInternalAttributes . ATTEMPT_NUMBER ] : taskContext . ctx . attempt . number ,
81+ } ,
82+ SemanticInternalAttributes . METADATA
83+ )
84+ ) ;
85+ }
86+
87+ if ( span . events ) {
88+ for ( const event of span . events ) {
89+ partialSpan . addEvent ( event . name , event . attributes , event . time ) ;
90+ }
91+ }
92+
93+ return partialSpan ;
94+ }
95+
4796export class TaskContextLogProcessor implements LogRecordProcessor {
4897 private _innerProcessor : LogRecordProcessor ;
4998
0 commit comments