Skip to content

Commit f967c63

Browse files
committed
Better support for external exporters and group exporters and instrumentations under the telemetry config property
1 parent 80046bc commit f967c63

File tree

6 files changed

+44
-55
lines changed

6 files changed

+44
-55
lines changed

packages/cli-v3/src/entryPoints/deploy-run-worker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ async function bootstrap() {
148148

149149
const tracingSDK = new TracingSDK({
150150
url: env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
151-
instrumentations: config.instrumentations ?? [],
152-
exporters: config.exporters ?? [],
151+
instrumentations: config.telemetry?.instrumentations ?? config.instrumentations ?? [],
152+
exporters: config.telemetry?.exporters ?? [],
153153
diagLogLevel: (env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
154154
forceFlushTimeoutMillis: 30_000,
155155
});

packages/cli-v3/src/entryPoints/dev-run-worker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ async function bootstrap() {
128128

129129
const tracingSDK = new TracingSDK({
130130
url: env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
131-
instrumentations: config.instrumentations ?? [],
132-
exporters: config.exporters ?? [],
131+
instrumentations: config.telemetry?.instrumentations ?? config.instrumentations ?? [],
132+
exporters: config.telemetry?.exporters ?? [],
133133
diagLogLevel: (env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
134134
forceFlushTimeoutMillis: 30_000,
135135
});

packages/core/src/v3/config.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,26 @@ export type TriggerConfig = {
5353
* Instrumentations to use for OpenTelemetry. This is useful if you want to add custom instrumentations to your tasks.
5454
*
5555
* @see https://trigger.dev/docs/config/config-file#instrumentations
56+
*
57+
* @deprecated Use the `telemetry.instrumentations` option instead.
5658
*/
5759
instrumentations?: Array<Instrumentation>;
5860

59-
exporters?: Array<any>;
61+
telemetry?: {
62+
/**
63+
* Instrumentations to use for OpenTelemetry. This is useful if you want to add custom instrumentations to your tasks.
64+
*
65+
* @see https://trigger.dev/docs/config/config-file#instrumentations
66+
*/
67+
instrumentations?: Array<Instrumentation>;
68+
69+
/**
70+
* Exporters to use for OpenTelemetry. This is useful if you want to add custom exporters to your tasks.
71+
*
72+
* @see https://trigger.dev/docs/config/config-file#exporters
73+
*/
74+
exporters?: Array<SpanExporter>;
75+
};
6076

6177
/**
6278
* Specify a custom path to your tsconfig file. This is useful if you have a custom tsconfig file that you want to use.

packages/core/src/v3/otel/tracingSDK.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,23 @@ export class TracingSDK {
154154
)
155155
);
156156

157+
const externalTraceId = crypto.randomUUID();
158+
157159
for (const exporter of config.exporters ?? []) {
158-
traceProvider.addSpanProcessor(new SimpleSpanProcessor(exporter));
160+
traceProvider.addSpanProcessor(
161+
getEnvVar("OTEL_BATCH_PROCESSING_ENABLED") === "1"
162+
? new BatchSpanProcessor(new ExternalSpanExporterWrapper(exporter, externalTraceId), {
163+
maxExportBatchSize: parseInt(getEnvVar("OTEL_SPAN_MAX_EXPORT_BATCH_SIZE") ?? "64"),
164+
scheduledDelayMillis: parseInt(
165+
getEnvVar("OTEL_SPAN_SCHEDULED_DELAY_MILLIS") ?? "200"
166+
),
167+
exportTimeoutMillis: parseInt(
168+
getEnvVar("OTEL_SPAN_EXPORT_TIMEOUT_MILLIS") ?? "30000"
169+
),
170+
maxQueueSize: parseInt(getEnvVar("OTEL_SPAN_MAX_QUEUE_SIZE") ?? "512"),
171+
})
172+
: new SimpleSpanProcessor(new ExternalSpanExporterWrapper(exporter, externalTraceId))
173+
);
159174
}
160175

161176
traceProvider.register();

references/nextjs-realtime/src/trigger/ai.ts

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export const openaiStreaming = schemaTask({
110110
});
111111

112112
const stream = await metadata.stream("openai", result.fullStream);
113+
114+
for await (const chunk of stream) {
115+
logger.log("Received chunk", { chunk });
116+
}
113117
},
114118
});
115119

references/nextjs-realtime/trigger.config.ts

+3-49
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,13 @@
11
import { defineConfig } from "@trigger.dev/sdk/v3";
22
import { rscExtension } from "@trigger.dev/rsc";
33
import { AISDKExporter } from "langsmith/vercel";
4-
import { Client } from "langsmith";
5-
6-
class LangsmithSpanExporterWrapper {
7-
constructor(
8-
private underlyingExporter: any,
9-
private transformSpan: (span: any) => any | undefined = (span) => {
10-
if (span.attributes["$span.partial"]) {
11-
// Skip partial spans
12-
return;
13-
}
14-
15-
// Check if this is an attempt span
16-
if (span.name.startsWith("Attempt ")) {
17-
// Create a new span that wraps the original but modifies spanContext
18-
const spanContext = span.spanContext();
19-
20-
return {
21-
...span,
22-
spanContext: () => spanContext,
23-
parentSpanId: undefined,
24-
};
25-
}
26-
return span;
27-
}
28-
) {}
29-
30-
export(spans: any[], resultCallback: (result: any) => void): void {
31-
const modifiedSpans = spans.map(this.transformSpan);
32-
this.underlyingExporter.export(modifiedSpans.filter(Boolean), resultCallback);
33-
}
34-
35-
shutdown(): Promise<void> {
36-
return this.underlyingExporter.shutdown();
37-
}
38-
39-
forceFlush?(): Promise<void> {
40-
return this.underlyingExporter.forceFlush
41-
? this.underlyingExporter.forceFlush()
42-
: Promise.resolve();
43-
}
44-
}
45-
46-
const client = new Client();
47-
48-
const exporter = new AISDKExporter({
49-
debug: true,
50-
client,
51-
});
524

535
export default defineConfig({
546
project: "proj_bzhdaqhlymtuhlrcgbqy",
557
dirs: ["./src/trigger"],
56-
exporters: [new LangsmithSpanExporterWrapper(exporter)],
8+
telemetry: {
9+
exporters: [new AISDKExporter()],
10+
},
5711
build: {
5812
extensions: [rscExtension({ reactDomEnvironment: "worker" })],
5913
},

0 commit comments

Comments
 (0)