Skip to content

Commit 6a379e4

Browse files
committed
Fix 3rd party otel propagation from breaking our Task Events data from being properly correlated to the correct trace
1 parent c0b815c commit 6a379e4

File tree

8 files changed

+288
-133
lines changed

8 files changed

+288
-133
lines changed

.changeset/tidy-tomatoes-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/core": patch
3+
---
4+
5+
Fix 3rd party otel propagation from breaking our Task Events data from being properly correlated to the correct trace

apps/webapp/app/routes/api.v1.tasks.$taskId.trigger.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const ParamsSchema = z.object({
1111
});
1212

1313
export const HeadersSchema = z.object({
14-
"idempotency-key": z.string().optional().nullable(),
15-
"trigger-version": z.string().optional().nullable(),
16-
"x-trigger-span-parent-as-link": z.coerce.number().optional().nullable(),
14+
"idempotency-key": z.string().nullish(),
15+
"trigger-version": z.string().nullish(),
16+
"x-trigger-span-parent-as-link": z.coerce.number().nullish(),
17+
"x-trigger-worker": z.string().nullish(),
1718
traceparent: z.string().optional(),
1819
tracestate: z.string().optional(),
1920
});
@@ -45,6 +46,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
4546
"x-trigger-span-parent-as-link": spanParentAsLink,
4647
traceparent,
4748
tracestate,
49+
"x-trigger-worker": isFromWorker,
4850
} = headers.data;
4951

5052
const { taskId } = ParamsSchema.parse(params);
@@ -58,20 +60,31 @@ export async function action({ request, params }: ActionFunctionArgs) {
5860
return json({ error: "Invalid request body" }, { status: 400 });
5961
}
6062

61-
logger.debug("Triggering task", {
62-
taskId,
63-
idempotencyKey,
64-
triggerVersion,
65-
body: body.data,
66-
});
67-
6863
const service = new TriggerTaskService();
6964

7065
try {
66+
const traceContext = traceparent
67+
? !triggerVersion // If the trigger version is NOT set, we are in an older version of the SDK
68+
? { traceparent, tracestate }
69+
: isFromWorker // If the trigger version is set, and the request is from a worker, we should pass the trace context
70+
? { traceparent, tracestate }
71+
: undefined
72+
: undefined;
73+
74+
logger.debug("Triggering task", {
75+
taskId,
76+
idempotencyKey,
77+
triggerVersion,
78+
headers: Object.fromEntries(request.headers),
79+
body: body.data,
80+
isFromWorker,
81+
traceContext,
82+
});
83+
7184
const run = await service.call(taskId, authenticationResult.environment, body.data, {
7285
idempotencyKey: idempotencyKey ?? undefined,
7386
triggerVersion: triggerVersion ?? undefined,
74-
traceContext: traceparent ? { traceparent, tracestate } : undefined,
87+
traceContext,
7588
spanParentAsLink: spanParentAsLink === 1,
7689
});
7790

packages/core/src/v3/apiClient/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { taskContext } from "../task-context-api";
2222
import { getEnvVar } from "../utils/getEnv";
2323
import { SafeAsyncLocalStorage } from "../utils/safeAsyncLocalStorage";
2424
import { APIError } from "../apiErrors";
25+
import { version } from "../../../package.json";
2526

2627
export type TriggerOptions = {
2728
spanParentAsLink?: boolean;
@@ -239,10 +240,12 @@ export class ApiClient {
239240
const headers: Record<string, string> = {
240241
"Content-Type": "application/json",
241242
Authorization: `Bearer ${this.accessToken}`,
243+
"trigger-version": version,
242244
};
243245

244246
// Only inject the context if we are inside a task
245247
if (taskContext.isInsideTask) {
248+
headers["x-trigger-worker"] = "true";
246249
propagation.inject(context.active(), headers);
247250

248251
if (spanParentAsLink) {

0 commit comments

Comments
 (0)