Skip to content

Commit 61c8bc8

Browse files
committed
Remove metadata from context, move it to it’s own tab
1 parent e783be1 commit 61c8bc8

File tree

10 files changed

+105
-26
lines changed

10 files changed

+105
-26
lines changed

apps/webapp/app/presenters/v3/SpanPresenter.server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ export class SpanPresenter extends BasePresenter {
192192

193193
const span = await eventRepository.getSpan(spanId, run.traceId);
194194

195-
const metadata = await parsePacket({
196-
data: run.metadata ?? undefined,
197-
dataType: run.metadataType,
198-
});
195+
const metadata = run.metadata
196+
? await prettyPrintPacket(run.metadata, run.metadataType)
197+
: undefined;
199198

200199
const context = {
201200
task: {
@@ -215,7 +214,6 @@ export class SpanPresenter extends BasePresenter {
215214
baseCostInCents: run.baseCostInCents,
216215
maxAttempts: run.maxAttempts ?? undefined,
217216
version: run.lockedToVersion?.version,
218-
metadata,
219217
},
220218
queue: {
221219
name: run.queue,
@@ -285,6 +283,7 @@ export class SpanPresenter extends BasePresenter {
285283
error,
286284
links: span?.links,
287285
context: JSON.stringify(context, null, 2),
286+
metadata,
288287
};
289288
}
290289

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.v3.$projectParam.runs.$runParam.spans.$spanParam/route.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,17 @@ function RunBody({
421421
>
422422
Context
423423
</TabButton>
424+
425+
<TabButton
426+
isActive={tab === "metadata"}
427+
layoutId="span-run"
428+
onClick={() => {
429+
replace({ tab: "metadata" });
430+
}}
431+
shortcut={{ key: "m" }}
432+
>
433+
Metadata
434+
</TabButton>
424435
</TabContainer>
425436
</div>
426437
<div className="overflow-y-auto px-3 scrollbar-thin scrollbar-track-transparent scrollbar-thumb-charcoal-600">
@@ -607,6 +618,16 @@ function RunBody({
607618
<div className="flex flex-col gap-4 py-3">
608619
<CodeBlock code={run.context} showLineNumbers={false} />
609620
</div>
621+
) : tab === "metadata" ? (
622+
<div className="flex flex-col gap-4 py-3">
623+
{run.metadata ? (
624+
<CodeBlock code={run.metadata} showLineNumbers={false} />
625+
) : (
626+
<Callout to="https://trigger.dev/docs/runs/metadata" variant="docs">
627+
No metadata set for this run. View our metadata documentation to learn more.
628+
</Callout>
629+
)}
630+
</div>
610631
) : (
611632
<div className="flex flex-col gap-4 pt-3">
612633
<div className="border-b border-grid-bright pb-3">

apps/webapp/app/routes/resources.runs.$runParam.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { LoaderFunctionArgs } from "@remix-run/server-runtime";
2-
import {
3-
MachinePresetName,
4-
parsePacket,
5-
prettyPrintPacket,
6-
TaskRunError,
7-
} from "@trigger.dev/core/v3";
2+
import { MachinePresetName, prettyPrintPacket, TaskRunError } from "@trigger.dev/core/v3";
83
import { typedjson, UseDataFunctionReturn } from "remix-typedjson";
94
import { RUNNING_STATUSES } from "~/components/runs/v3/TaskRunStatus";
105
import { $replica } from "~/db.server";
@@ -158,11 +153,6 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
158153
}
159154
}
160155

161-
const metadata = await parsePacket({
162-
data: run.metadata ?? undefined,
163-
dataType: run.metadataType,
164-
});
165-
166156
const context = {
167157
task: {
168158
id: run.taskIdentifier,
@@ -181,7 +171,6 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
181171
baseCostInCents: run.baseCostInCents,
182172
maxAttempts: run.maxAttempts ?? undefined,
183173
version: run.lockedToVersion?.version,
184-
metadata,
185174
},
186175
queue: {
187176
name: run.queue,

docs/runs/metadata.mdx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: "Run metadata"
3+
description: "Attach a small amount of data to a run and update it as the run progresses."
4+
---
5+
6+
You can attach up to 8KB of metadata to a run, which you can then access from inside the run function, via the API, and in the dashboard. You can use metadata to store additional, structured information on an run. For example, you could store your user’s full name and corresponding unique identifier from your system on every task that is associated with that user:
7+
8+
```ts
9+
const handle = await myTask.trigger(
10+
{ message: "hello world" },
11+
{ metadata: { user: { name: "Eric", id: "user_1234" } } }
12+
);
13+
```
14+
15+
Then inside your run function, you can access the metadata like this:
16+
17+
```ts
18+
import { task, metadata } from "@trigger.dev/sdk/v3";
19+
20+
export const myTask = task({
21+
id: "my-task",
22+
run: async (payload: { message: string }, { ctx }) => {
23+
const user = metadata.get("user");
24+
console.log(user.name); // "Eric"
25+
console.log(user.id); // "user_1234"
26+
},
27+
});
28+
```
29+
30+
You can also update the metadata during the run:
31+
32+
```ts
33+
import { task, metadata } from "@trigger.dev/sdk/v3";
34+
35+
export const myTask = task({
36+
id: "my-task",
37+
run: async (payload: { message: string }, { ctx }) => {
38+
// Do some work
39+
await metadata.set("progress", 0.1);
40+
41+
// Do some more work
42+
await metadata.set("progress", 0.5);
43+
44+
// Do even more work
45+
await metadata.set("progress", 1.0);
46+
},
47+
});
48+
```

docs/v3-openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,10 @@ components:
18971897
items:
18981898
type: string
18991899
description: A tag must be between 1 and 64 characters, a run can have up to 5 tags attached to it.
1900+
metadata:
1901+
type: object
1902+
description: The metadata of the run. See [Metadata](/runs/metadata) for more information.
1903+
example: { "foo": "bar" }
19001904
costInCents:
19011905
type: number
19021906
example: 0.00292

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export class RunMetadataAPI {
2525
return this.store;
2626
}
2727

28+
public getKey(key: string): DeserializedJson | undefined {
29+
return this.store?.[key];
30+
}
31+
2832
public async setKey(
2933
key: string,
3034
value: DeserializedJson,

packages/core/src/v3/schemas/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export const TaskRunContext = z.object({
223223
backgroundWorkerId: true,
224224
backgroundWorkerTaskId: true,
225225
}),
226-
run: TaskRun.omit({ payload: true, payloadType: true }),
226+
run: TaskRun.omit({ payload: true, payloadType: true, metadata: true }),
227227
queue: TaskRunExecutionQueue,
228228
environment: TaskRunExecutionEnvironment,
229229
organization: TaskRunExecutionOrganization,

packages/core/src/v3/workers/taskExecutor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export class TaskExecutor {
7272
worker,
7373
});
7474

75-
if (ctx.run.metadata) {
76-
runMetadata.enterWithMetadata(ctx.run.metadata);
75+
if (execution.run.metadata) {
76+
runMetadata.enterWithMetadata(execution.run.metadata);
7777
}
7878

7979
this._tracingSDK.asyncResourceDetector.resolveWithAttributes({

packages/trigger-sdk/src/v3/metadata.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { DeserializedJson } from "@trigger.dev/core";
22
import {
33
accessoryAttributes,
44
ApiRequestOptions,
5+
flattenAttributes,
56
mergeRequestOptions,
67
runMetadata,
78
} from "@trigger.dev/core/v3";
89
import { tracer } from "./tracer.js";
910

1011
export const metadata = {
1112
current: currentMetadata,
13+
get: getMetadataKey,
1214
set: setMetadataKey,
1315
del: deleteMetadataKey,
1416
update: updateMetadata,
@@ -23,6 +25,13 @@ function currentMetadata(): RunMetadata | undefined {
2325
return runMetadata.current();
2426
}
2527

28+
/**
29+
* Get a key from the metadata of the current run if inside a task run.
30+
*/
31+
function getMetadataKey(key: string): DeserializedJson | undefined {
32+
return runMetadata.getKey(key);
33+
}
34+
2635
/**
2736
* Set a key in the metadata of the current run if inside a task run.
2837
*
@@ -48,7 +57,7 @@ async function setMetadataKey(
4857
],
4958
style: "codepath",
5059
}),
51-
key,
60+
...flattenAttributes(value, key),
5261
},
5362
},
5463
requestOptions
@@ -94,6 +103,9 @@ async function updateMetadata(
94103
tracer,
95104
name: "metadata.update()",
96105
icon: "code-plus",
106+
attributes: {
107+
...flattenAttributes(metadata),
108+
},
97109
},
98110
requestOptions
99111
);

references/v3-catalog/src/trigger/runMetadata.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export const runMetadataTask = task({
1919
export const runMetadataChildTask = task({
2020
id: "run-metadata-child-task",
2121
run: async (payload: any, { ctx }) => {
22-
logger.info("metadata", { metadata: ctx.run.metadata });
23-
2422
await metadata.set("child", "task");
2523

2624
logger.info("metadata", { metadata: metadata.current() });
@@ -41,9 +39,13 @@ export const runMetadataChildTask = task({
4139
},
4240
});
4341

44-
// Now try and update the metadata with something larger than 8KB
45-
await metadata.update({
46-
large: new Array(10000).fill("a").join(""),
42+
await runMetadataChildTask2.triggerAndWait(payload, {
43+
metadata: metadata.current(),
4744
});
4845
},
4946
});
47+
48+
export const runMetadataChildTask2 = task({
49+
id: "run-metadata-child-task-2",
50+
run: async (payload: any, { ctx }) => {},
51+
});

0 commit comments

Comments
 (0)