@@ -20,6 +20,7 @@ import {
20
20
import {
21
21
BatchSpanProcessor ,
22
22
NodeTracerProvider ,
23
+ ReadableSpan ,
23
24
SimpleSpanProcessor ,
24
25
SpanExporter ,
25
26
} from "@opentelemetry/sdk-trace-node" ;
@@ -112,6 +113,8 @@ export class TracingSDK {
112
113
. merge (
113
114
new Resource ( {
114
115
[ SemanticResourceAttributes . CLOUD_PROVIDER ] : "trigger.dev" ,
116
+ [ SemanticResourceAttributes . SERVICE_NAME ] :
117
+ getEnvVar ( "OTEL_SERVICE_NAME" ) ?? "trigger.dev" ,
115
118
[ SemanticInternalAttributes . TRIGGER ] : true ,
116
119
[ SemanticInternalAttributes . CLI_VERSION ] : VERSION ,
117
120
} )
@@ -256,3 +259,49 @@ function setLogLevel(level: TracingDiagnosticLogLevel) {
256
259
257
260
diag . setLogger ( new DiagConsoleLogger ( ) , diagLogLevel ) ;
258
261
}
262
+
263
+ class ExternalSpanExporterWrapper {
264
+ constructor (
265
+ private underlyingExporter : SpanExporter ,
266
+ private externalTraceId : string
267
+ ) { }
268
+
269
+ private transformSpan ( span : ReadableSpan ) : ReadableSpan | undefined {
270
+ if ( span . attributes [ SemanticInternalAttributes . SPAN_PARTIAL ] ) {
271
+ // Skip partial spans
272
+ return ;
273
+ }
274
+
275
+ const spanContext = span . spanContext ( ) ;
276
+
277
+ return {
278
+ ...span ,
279
+ spanContext : ( ) => ( { ...spanContext , traceId : this . externalTraceId } ) ,
280
+ parentSpanId : span . attributes [ SemanticInternalAttributes . SPAN_ATTEMPT ]
281
+ ? undefined
282
+ : span . parentSpanId ,
283
+ } ;
284
+ }
285
+
286
+ export ( spans : any [ ] , resultCallback : ( result : any ) => void ) : void {
287
+ try {
288
+ const modifiedSpans = spans . map ( this . transformSpan . bind ( this ) ) ;
289
+ this . underlyingExporter . export (
290
+ modifiedSpans . filter ( Boolean ) as ReadableSpan [ ] ,
291
+ resultCallback
292
+ ) ;
293
+ } catch ( e ) {
294
+ console . error ( e ) ;
295
+ }
296
+ }
297
+
298
+ shutdown ( ) : Promise < void > {
299
+ return this . underlyingExporter . shutdown ( ) ;
300
+ }
301
+
302
+ forceFlush ?( ) : Promise < void > {
303
+ return this . underlyingExporter . forceFlush
304
+ ? this . underlyingExporter . forceFlush ( )
305
+ : Promise . resolve ( ) ;
306
+ }
307
+ }
0 commit comments