Skip to content

Commit d361e24

Browse files
authored
Associate child runs with the parent span ID (#1352)
* Add safe rootTaskRunId index and a README to @trigger.dev/database * Associate child runs with the span ID of the span in the parent run that triggered the child run * Update deprecation notice doc links
1 parent bff38dc commit d361e24

File tree

10 files changed

+85
-10
lines changed

10 files changed

+85
-10
lines changed

.changeset/short-tomatoes-beam.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+
Add otel propagation headers "below" the API fetch span, to attribute the child runs with the proper parent span ID

apps/webapp/app/v3/eventRepository.server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@ export class EventRepository {
835835
options: TraceEventOptions & { incomplete?: boolean },
836836
callback: (
837837
e: EventBuilder,
838-
traceContext: Record<string, string | undefined>
838+
traceContext: Record<string, string | undefined>,
839+
traceparent?: { traceId: string; spanId: string }
839840
) => Promise<TResult>
840841
): Promise<TResult> {
841842
const propagatedContext = extractContextFromCarrier(options.context ?? {});
@@ -892,7 +893,7 @@ export class EventRepository {
892893
},
893894
};
894895

895-
const result = await callback(eventBuilder, traceContext);
896+
const result = await callback(eventBuilder, traceContext, propagatedContext?.traceparent);
896897

897898
const duration = process.hrtime.bigint() - start;
898899

apps/webapp/app/v3/services/triggerTask.server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class TriggerTaskService extends BaseService {
240240
incomplete: true,
241241
immediate: true,
242242
},
243-
async (event, traceContext) => {
243+
async (event, traceContext, traceparent) => {
244244
const run = await autoIncrementCounter.incrementInTransaction(
245245
`v3-run:${environment.id}:${taskId}`,
246246
async (num, tx) => {
@@ -307,6 +307,8 @@ export class TriggerTaskService extends BaseService {
307307
traceContext: traceContext,
308308
traceId: event.traceId,
309309
spanId: event.spanId,
310+
parentSpanId:
311+
options.parentAsLinkType === "replay" ? undefined : traceparent?.spanId,
310312
lockedToVersionId: lockedToBackgroundWorker?.id,
311313
concurrencyKey: body.options?.concurrencyKey,
312314
queue: queueName,

packages/cli-v3/src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function validateConfig(config: TriggerConfig, warn = true) {
228228
if (config.additionalFiles && config.additionalFiles.length > 0) {
229229
warn &&
230230
prettyWarning(
231-
`The "additionalFiles" option is deprecated and will be removed. Use the "additionalFiles" build extension instead. See https://trigger.dev/docs/guides/new-build-system-preview#additionalfiles for more information.`
231+
`The "additionalFiles" option is deprecated and will be removed. Use the "additionalFiles" build extension instead. See https://trigger.dev/docs/config/config-file#additionalfiles for more information.`
232232
);
233233

234234
config.build ??= {};
@@ -239,7 +239,7 @@ function validateConfig(config: TriggerConfig, warn = true) {
239239
if (config.additionalPackages && config.additionalPackages.length > 0) {
240240
warn &&
241241
prettyWarning(
242-
`The "additionalPackages" option is deprecated and will be removed. Use the "additionalPackages" build extension instead. See https://trigger.dev/docs/guides/new-build-system-preview#additionalpackages for more information.`
242+
`The "additionalPackages" option is deprecated and will be removed. Use the "additionalPackages" build extension instead. See https://trigger.dev/docs/config/config-file#additionalpackages for more information.`
243243
);
244244

245245
config.build ??= {};
@@ -275,7 +275,7 @@ function validateConfig(config: TriggerConfig, warn = true) {
275275
if ("resolveEnvVars" in config && typeof config.resolveEnvVars === "function") {
276276
warn &&
277277
prettyWarning(
278-
`The "resolveEnvVars" option is deprecated and will be removed. Use the "syncEnvVars" build extension instead. See https://trigger.dev/docs/guides/new-build-system-preview#resolveenvvars for more information.`
278+
`The "resolveEnvVars" option is deprecated and will be removed. Use the "syncEnvVars" build extension instead. See https://trigger.dev/docs/config/config-file#syncenvvars for more information.`
279279
);
280280

281281
const resolveEnvVarsFn = config.resolveEnvVars as ResolveEnvironmentVariablesFunction;

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { RetryOptions } from "../schemas/index.js";
44
import { calculateNextRetryDelay } from "../utils/retries.js";
55
import { ApiConnectionError, ApiError, ApiSchemaValidationError } from "./errors.js";
66

7-
import { Attributes, Span } from "@opentelemetry/api";
7+
import { Attributes, Span, context, propagation } from "@opentelemetry/api";
88
import { SemanticInternalAttributes } from "../semanticInternalAttributes.js";
99
import { TriggerTracer } from "../tracer.js";
1010
import { accessoryAttributes } from "../utils/styleAttributes.js";
@@ -184,9 +184,11 @@ async function _doZodFetch<TResponseBodySchema extends z.ZodTypeAny>(
184184
requestInit?: PromiseOrValue<RequestInit>,
185185
options?: ZodFetchOptions
186186
): Promise<ZodFetchResult<z.output<TResponseBodySchema>>> {
187-
const $requestInit = await requestInit;
187+
let $requestInit = await requestInit;
188188

189189
return traceZodFetch({ url, requestInit: $requestInit, options }, async (span) => {
190+
$requestInit = injectPropagationHeadersIfInWorker($requestInit);
191+
190192
const result = await _doZodFetchWithRetries(schema, url, $requestInit, options);
191193

192194
if (options?.onResponseBody && span) {
@@ -577,3 +579,23 @@ export function isEmptyObj(obj: Object | null | undefined): boolean {
577579
export function hasOwn(obj: Object, key: string): boolean {
578580
return Object.prototype.hasOwnProperty.call(obj, key);
579581
}
582+
583+
// If the requestInit has a header x-trigger-worker = true, then we will do
584+
// propagation.inject(context.active(), headers);
585+
// and return the new requestInit.
586+
function injectPropagationHeadersIfInWorker(requestInit?: RequestInit): RequestInit | undefined {
587+
const headers = new Headers(requestInit?.headers);
588+
589+
if (headers.get("x-trigger-worker") !== "true") {
590+
return requestInit;
591+
}
592+
593+
const headersObject = Object.fromEntries(headers.entries());
594+
595+
propagation.inject(context.active(), headersObject);
596+
597+
return {
598+
...requestInit,
599+
headers: new Headers(headersObject),
600+
};
601+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { context, propagation } from "@opentelemetry/api";
21
import { z } from "zod";
32
import {
43
AddTagsRequestBody,
@@ -509,7 +508,6 @@ export class ApiClient {
509508
// Only inject the context if we are inside a task
510509
if (taskContext.isInsideTask) {
511510
headers["x-trigger-worker"] = "true";
512-
propagation.inject(context.active(), headers);
513511

514512
if (spanParentAsLink) {
515513
headers["x-trigger-span-parent-as-link"] = "1";

packages/database/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## @trigger.dev/database
2+
3+
This is the internal database package for the Trigger.dev project. It exports a generated prisma client that can be instantiated with a connection string.
4+
5+
### How to add a new index on a large table
6+
7+
1. Modify the Prisma.schema with a single index change (no other changes, just one index at a time)
8+
2. Create a Prisma migration using `cd packages/database && pnpm run db:migrate:dev --create-only`
9+
3. Modify the SQL file: add IF NOT EXISTS to it and CONCURRENTLY:
10+
11+
```sql
12+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
13+
```
14+
15+
4. Don’t apply the Prisma migration locally yet. This is a good opportunity to test the flow.
16+
5. Manually apply the index to your database, by running the index command.
17+
6. Then locally run `pnpm run db:migrate:deploy`
18+
19+
#### Before deploying
20+
21+
Run the index creation before deploying
22+
23+
```sql
24+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
25+
```
26+
27+
These commands are useful:
28+
29+
```sql
30+
-- creates an index safely, this can take a long time (2 mins maybe)
31+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "JobRun_eventId_idx" ON "JobRun" ("eventId");
32+
-- checks the status of an index
33+
SELECT * FROM pg_stat_progress_create_index WHERE relid = '"JobRun"'::regclass;
34+
-- checks if the index is there
35+
SELECT * FROM pg_indexes WHERE tablename = 'JobRun' AND indexname = 'JobRun_eventId_idx';
36+
```
37+
38+
Now, when you deploy and prisma runs the migration, it will skip the index creation because it already exists. If you don't do this, there will be pain.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- CreateIndex
2+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "TaskRun_rootTaskRunId_idx" ON "TaskRun"("rootTaskRunId");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "TaskRun" ADD COLUMN "parentSpanId" TEXT;

packages/database/prisma/schema.prisma

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,9 +1748,14 @@ model TaskRun {
17481748
/// The depth of this task run in the task run hierarchy
17491749
depth Int @default(0)
17501750
1751+
/// The span ID of the "trigger" span in the parent task run
1752+
parentSpanId String?
1753+
17511754
@@unique([runtimeEnvironmentId, taskIdentifier, idempotencyKey])
17521755
// Finding child runs
17531756
@@index([parentTaskRunId])
1757+
// Finding ancestor runs
1758+
@@index([rootTaskRunId])
17541759
// Task activity graph
17551760
@@index([projectId, createdAt, taskIdentifier])
17561761
//Runs list

0 commit comments

Comments
 (0)