Skip to content

Commit 8235238

Browse files
committed
Add usage and cost to TaskEvent
1 parent d791acd commit 8235238

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,18 @@ function convertSpansToCreateableEvents(resourceSpan: ResourceSpans): Array<Crea
307307
"."
308308
)
309309
) ?? resourceProperties.attemptNumber,
310-
usageDurationMs: extractDoubleAttribute(
310+
usageDurationMs:
311+
extractDoubleAttribute(
312+
span.attributes ?? [],
313+
SemanticInternalAttributes.USAGE_DURATION_MS
314+
) ??
315+
extractNumberAttribute(
316+
span.attributes ?? [],
317+
SemanticInternalAttributes.USAGE_DURATION_MS
318+
),
319+
usageCostInCents: extractDoubleAttribute(
311320
span.attributes ?? [],
312-
SemanticInternalAttributes.USAGE_DURATION_MS
321+
SemanticInternalAttributes.USAGE_COST_IN_CENTS
313322
),
314323
};
315324
})

packages/core/src/v3/semanticInternalAttributes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ export const SemanticInternalAttributes = {
4747
LINK_TITLE: "$link.title",
4848
IDEMPOTENCY_KEY: "ctx.run.idempotencyKey",
4949
USAGE_DURATION_MS: "$usage.durationMs",
50+
USAGE_COST_IN_CENTS: "$usage.costInCents",
5051
};

packages/core/src/v3/tracer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Logger, logs } from "@opentelemetry/api-logs";
1212
import { SemanticInternalAttributes } from "./semanticInternalAttributes";
1313
import { clock } from "./clock-api";
1414
import { usage } from "./usage-api";
15+
import { taskContext } from "./task-context-api";
1516

1617
export type TriggerTracerConfig =
1718
| {
@@ -100,9 +101,13 @@ export class TriggerTracer {
100101
throw e;
101102
} finally {
102103
const usageSample = usage.stop(usageMeasurement);
104+
const machine = taskContext.ctx?.machine;
103105

104106
span.setAttributes({
105107
[SemanticInternalAttributes.USAGE_DURATION_MS]: usageSample.cpuTime,
108+
[SemanticInternalAttributes.USAGE_COST_IN_CENTS]: machine?.centsPerMs
109+
? usageSample.cpuTime * machine.centsPerMs
110+
: 0,
106111
});
107112

108113
span.end(clock.preciseNow());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "TaskEvent" ADD COLUMN "usageCostInCents" DOUBLE PRECISION NOT NULL DEFAULT 0;

packages/database/prisma/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,8 @@ model TaskEvent {
19121912
createdAt DateTime @default(now())
19131913
19141914
// This represents the amount of "usage time" the event took, e.g. the CPU time
1915-
usageDurationMs Int @default(0)
1915+
usageDurationMs Int @default(0)
1916+
usageCostInCents Float @default(0)
19161917
19171918
machinePreset String?
19181919
machinePresetCpu Float?

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const usagePlayground = task({
1212

1313
logger.info("Cost and duration", { cost: ctx.run.costInCents, duration: ctx.run.durationMs });
1414

15-
await new Promise((resolve) => setTimeout(resolve, payload.duration));
15+
await logger.trace("Doing some work...", async () => {
16+
await new Promise((resolve) => setTimeout(resolve, payload.duration));
17+
});
1618

1719
let currentUsage = usage.getCurrent();
1820

0 commit comments

Comments
 (0)