-
-
Notifications
You must be signed in to change notification settings - Fork 708
Add custom telemetry exporter support #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1a774b
WIP langsmith & AI SDK integration
ericallam 551a6af
Add changeset
ericallam 11be900
Add exporter support to deployed tasks
ericallam d40a438
Better support for external exporters and group exporters and instrum…
ericallam d3bd091
Missing changes
ericallam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"trigger.dev": patch | ||
"@trigger.dev/core": patch | ||
--- | ||
|
||
Add otel exporter support |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import { | |
import { | ||
BatchSpanProcessor, | ||
NodeTracerProvider, | ||
ReadableSpan, | ||
SimpleSpanProcessor, | ||
SpanExporter, | ||
} from "@opentelemetry/sdk-trace-node"; | ||
|
@@ -85,6 +86,7 @@ export type TracingSDKConfig = { | |
forceFlushTimeoutMillis?: number; | ||
resource?: IResource; | ||
instrumentations?: Instrumentation[]; | ||
exporters?: SpanExporter[]; | ||
diagLogLevel?: TracingDiagnosticLogLevel; | ||
}; | ||
|
||
|
@@ -111,6 +113,8 @@ export class TracingSDK { | |
.merge( | ||
new Resource({ | ||
[SemanticResourceAttributes.CLOUD_PROVIDER]: "trigger.dev", | ||
[SemanticResourceAttributes.SERVICE_NAME]: | ||
getEnvVar("OTEL_SERVICE_NAME") ?? "trigger.dev", | ||
[SemanticInternalAttributes.TRIGGER]: true, | ||
[SemanticInternalAttributes.CLI_VERSION]: VERSION, | ||
}) | ||
|
@@ -153,6 +157,25 @@ export class TracingSDK { | |
) | ||
); | ||
|
||
const externalTraceId = crypto.randomUUID(); | ||
|
||
for (const exporter of config.exporters ?? []) { | ||
traceProvider.addSpanProcessor( | ||
getEnvVar("OTEL_BATCH_PROCESSING_ENABLED") === "1" | ||
? new BatchSpanProcessor(new ExternalSpanExporterWrapper(exporter, externalTraceId), { | ||
maxExportBatchSize: parseInt(getEnvVar("OTEL_SPAN_MAX_EXPORT_BATCH_SIZE") ?? "64"), | ||
scheduledDelayMillis: parseInt( | ||
getEnvVar("OTEL_SPAN_SCHEDULED_DELAY_MILLIS") ?? "200" | ||
), | ||
exportTimeoutMillis: parseInt( | ||
getEnvVar("OTEL_SPAN_EXPORT_TIMEOUT_MILLIS") ?? "30000" | ||
), | ||
maxQueueSize: parseInt(getEnvVar("OTEL_SPAN_MAX_QUEUE_SIZE") ?? "512"), | ||
}) | ||
: new SimpleSpanProcessor(new ExternalSpanExporterWrapper(exporter, externalTraceId)) | ||
); | ||
} | ||
|
||
traceProvider.register(); | ||
|
||
registerInstrumentations({ | ||
|
@@ -236,3 +259,49 @@ function setLogLevel(level: TracingDiagnosticLogLevel) { | |
|
||
diag.setLogger(new DiagConsoleLogger(), diagLogLevel); | ||
} | ||
|
||
class ExternalSpanExporterWrapper { | ||
constructor( | ||
private underlyingExporter: SpanExporter, | ||
private externalTraceId: string | ||
) {} | ||
|
||
private transformSpan(span: ReadableSpan): ReadableSpan | undefined { | ||
if (span.attributes[SemanticInternalAttributes.SPAN_PARTIAL]) { | ||
// Skip partial spans | ||
return; | ||
} | ||
|
||
const spanContext = span.spanContext(); | ||
|
||
return { | ||
...span, | ||
spanContext: () => ({ ...spanContext, traceId: this.externalTraceId }), | ||
parentSpanId: span.attributes[SemanticInternalAttributes.SPAN_ATTEMPT] | ||
? undefined | ||
: span.parentSpanId, | ||
}; | ||
} | ||
|
||
export(spans: any[], resultCallback: (result: any) => void): void { | ||
try { | ||
const modifiedSpans = spans.map(this.transformSpan.bind(this)); | ||
this.underlyingExporter.export( | ||
modifiedSpans.filter(Boolean) as ReadableSpan[], | ||
resultCallback | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
|
||
shutdown(): Promise<void> { | ||
return this.underlyingExporter.shutdown(); | ||
} | ||
|
||
forceFlush?(): Promise<void> { | ||
return this.underlyingExporter.forceFlush | ||
? this.underlyingExporter.forceFlush() | ||
: Promise.resolve(); | ||
} | ||
} | ||
Comment on lines
+263
to
+307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve type safety and error handling in ExternalSpanExporterWrapper The wrapper implementation has several areas for improvement:
Consider these improvements: +/**
+ * Wraps a SpanExporter to transform spans before export:
+ * - Replaces trace IDs with a consistent external ID
+ * - Filters out partial spans
+ * - Handles parent span relationships
+ */
class ExternalSpanExporterWrapper {
constructor(
private underlyingExporter: SpanExporter,
private externalTraceId: string
) {}
private transformSpan(span: ReadableSpan): ReadableSpan | undefined {
if (span.attributes[SemanticInternalAttributes.SPAN_PARTIAL]) {
// Skip partial spans
return;
}
const spanContext = span.spanContext();
return {
...span,
spanContext: () => ({ ...spanContext, traceId: this.externalTraceId }),
parentSpanId: span.attributes[SemanticInternalAttributes.SPAN_ATTEMPT]
? undefined
: span.parentSpanId,
};
}
- export(spans: any[], resultCallback: (result: any) => void): void {
+ export(
+ spans: ReadableSpan[],
+ resultCallback: (result: ExportResult) => void
+ ): void {
try {
const modifiedSpans = spans.map(this.transformSpan.bind(this));
this.underlyingExporter.export(
modifiedSpans.filter(Boolean) as ReadableSpan[],
resultCallback
);
} catch (e) {
- console.error(e);
+ console.error("Error in ExternalSpanExporterWrapper.export:", e);
+ resultCallback({ code: ExportResultCode.FAILED, error: e as Error });
}
}
shutdown(): Promise<void> {
- return this.underlyingExporter.shutdown();
+ return this.underlyingExporter.shutdown().catch((e) => {
+ console.error("Error in ExternalSpanExporterWrapper.shutdown:", e);
+ throw e;
+ });
}
forceFlush?(): Promise<void> {
- return this.underlyingExporter.forceFlush
+ return this.underlyingExporter.forceFlush?.()
+ .catch((e) => {
+ console.error("Error in ExternalSpanExporterWrapper.forceFlush:", e);
+ throw e;
+ })
? this.underlyingExporter.forceFlush()
: Promise.resolve();
}
}
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add validation and error handling for exporters configuration
The exporter initialization has several potential issues:
Consider these improvements:
📝 Committable suggestion