@@ -20,6 +20,7 @@ import {
2020import {
2121 BatchSpanProcessor,
2222 NodeTracerProvider,
23+ ReadableSpan,
2324 SimpleSpanProcessor,
2425 SpanExporter,
2526} from "@opentelemetry/sdk-trace-node";
@@ -112,6 +113,8 @@ export class TracingSDK {
112113 .merge(
113114 new Resource({
114115 [SemanticResourceAttributes.CLOUD_PROVIDER]: "trigger.dev",
116+ [SemanticResourceAttributes.SERVICE_NAME]:
117+ getEnvVar("OTEL_SERVICE_NAME") ?? "trigger.dev",
115118 [SemanticInternalAttributes.TRIGGER]: true,
116119 [SemanticInternalAttributes.CLI_VERSION]: VERSION,
117120 })
@@ -256,3 +259,49 @@ function setLogLevel(level: TracingDiagnosticLogLevel) {
256259
257260 diag.setLogger(new DiagConsoleLogger(), diagLogLevel);
258261}
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