Skip to content

Commit 7a5d9d9

Browse files
committed
feat: add tracing spans to all prompt management SDK methods
1 parent e583e5b commit 7a5d9d9

File tree

5 files changed

+110
-21
lines changed

5 files changed

+110
-21
lines changed

apps/webapp/app/services/logger.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function flattenArgs(args: Array<Record<string, unknown> | undefined>) {
4848
}
4949
export const logger = new Logger(
5050
"webapp",
51-
(process.env.APP_LOG_LEVEL ?? "debug") as LogLevel,
51+
(process.env.APP_LOG_LEVEL ?? "info") as LogLevel,
5252
["examples", "output", "connectionString", "payload"],
5353
sensitiveDataReplacer,
5454
() => {
@@ -60,7 +60,7 @@ export const logger = new Logger(
6060

6161
export const workerLogger = new Logger(
6262
"worker",
63-
(process.env.APP_LOG_LEVEL ?? "debug") as LogLevel,
63+
(process.env.APP_LOG_LEVEL ?? "info") as LogLevel,
6464
["examples", "output", "connectionString"],
6565
sensitiveDataReplacer,
6666
() => {
@@ -71,7 +71,7 @@ export const workerLogger = new Logger(
7171

7272
export const socketLogger = new Logger(
7373
"socket",
74-
(process.env.APP_LOG_LEVEL ?? "debug") as LogLevel,
74+
(process.env.APP_LOG_LEVEL ?? "info") as LogLevel,
7575
[],
7676
sensitiveDataReplacer,
7777
() => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class DynamicFlushScheduler<T> {
5252
private readonly isDroppableEvent?: (item: T) => boolean;
5353
private isLoadShedding: boolean = false;
5454

55-
private readonly logger: Logger = new Logger("EventRepo.DynamicFlushScheduler", "debug");
55+
private readonly logger: Logger = new Logger("EventRepo.DynamicFlushScheduler", "info");
5656

5757
constructor(config: DynamicFlushSchedulerConfig<T>) {
5858
this.batchQueue = [];
@@ -329,7 +329,7 @@ export class DynamicFlushScheduler<T> {
329329
droppedByKind[kind] = count;
330330
});
331331

332-
this.logger.info("DynamicFlushScheduler metrics", {
332+
this.logger.debug("DynamicFlushScheduler metrics", {
333333
totalQueuedItems: this.totalQueuedItems,
334334
batchQueueLength: this.batchQueue.length,
335335
currentBatchLength: this.currentBatch.length,

internal-packages/run-engine/src/run-queue/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ export class RunQueue {
24922492
return;
24932493
}
24942494

2495-
this.logger.info("Scanning concurrency sets for completed runs");
2495+
this.logger.debug("Scanning concurrency sets for completed runs");
24962496

24972497
const stats = {
24982498
streamCallbacks: 0,

packages/redis-worker/src/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ class Worker<TCatalog extends WorkerCatalog> {
10241024
Promise.allSettled(enqueuePromises).then((results) => {
10251025
results.forEach((result) => {
10261026
if (result.status === "fulfilled") {
1027-
this.logger.info("Enqueued cron job", { result: result.value });
1027+
this.logger.debug("Enqueued cron job", { result: result.value });
10281028
} else {
10291029
this.logger.error("Failed to enqueue cron job", { reason: result.reason });
10301030
}
@@ -1050,7 +1050,7 @@ class Worker<TCatalog extends WorkerCatalog> {
10501050
availableAt,
10511051
});
10521052

1053-
this.logger.info("Enqueued cron job", {
1053+
this.logger.debug("Enqueued cron job", {
10541054
identifier,
10551055
cron,
10561056
job,

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

Lines changed: 102 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
import {
2+
accessoryAttributes,
23
apiClientManager,
4+
SemanticInternalAttributes,
35
type ApiRequestOptions,
46
type CreatePromptOverrideRequestBody,
57
type ListPromptsResponseBody,
68
type ListPromptVersionsResponseBody,
79
type PromptOkResponseBody,
810
type PromptOverrideCreatedResponseBody,
9-
type ResolvePromptResponseBody,
1011
type UpdatePromptOverrideRequestBody,
1112
} from "@trigger.dev/core/v3";
1213
import type { ResolvedPrompt } from "./prompt.js";
14+
import { tracer } from "./tracer.js";
15+
16+
function promptSpanOptions(name: string, slug: string) {
17+
return {
18+
tracer,
19+
name,
20+
icon: "tabler-file-text-ai",
21+
attributes: {
22+
[SemanticInternalAttributes.STYLE_ICON]: "tabler-file-text-ai",
23+
...accessoryAttributes({
24+
items: [{ text: slug, variant: "normal" as const }],
25+
style: "codepath",
26+
}),
27+
},
28+
};
29+
}
1330

1431
function makeToAISDKTelemetry(
1532
slug: string,
@@ -49,11 +66,36 @@ export async function resolvePrompt(
4966
): Promise<ResolvedPrompt> {
5067
const apiClient = apiClientManager.clientOrThrow();
5168
const vars = variables ?? {};
52-
const response = await apiClient.resolvePrompt(slug, {
53-
variables: vars,
54-
label: options?.label,
55-
version: options?.version,
56-
});
69+
const response = await apiClient.resolvePrompt(
70+
slug,
71+
{
72+
variables: vars,
73+
label: options?.label,
74+
version: options?.version,
75+
},
76+
{
77+
...promptSpanOptions("prompts.resolve()", slug),
78+
attributes: {
79+
[SemanticInternalAttributes.STYLE_ICON]: "tabler-file-text-ai",
80+
[SemanticInternalAttributes.ENTITY_TYPE]: "prompt",
81+
[SemanticInternalAttributes.ENTITY_ID]: slug,
82+
...accessoryAttributes({
83+
items: [{ text: slug, variant: "normal" as const }],
84+
style: "codepath",
85+
}),
86+
},
87+
onResponseBody: (body, span) => {
88+
span.setAttribute("prompt.version", body.data.version);
89+
span.setAttribute("prompt.slug", body.data.slug);
90+
span.setAttribute("prompt.labels", body.data.labels.join(", "));
91+
if (body.data.model) span.setAttribute("prompt.model", body.data.model);
92+
if (body.data.text) span.setAttribute("prompt.text", body.data.text);
93+
if (vars && Object.keys(vars).length > 0) {
94+
span.setAttribute("prompt.input", JSON.stringify(vars));
95+
}
96+
},
97+
}
98+
);
5799

58100
const data = response.data;
59101
const inputJson = Object.keys(vars).length > 0 ? JSON.stringify(vars) : undefined;
@@ -81,7 +123,12 @@ export function listPrompts(
81123
requestOptions?: ApiRequestOptions
82124
): Promise<ListPromptsResponseBody> {
83125
const apiClient = apiClientManager.clientOrThrow();
84-
return apiClient.listPrompts(requestOptions);
126+
return apiClient.listPrompts({
127+
...promptSpanOptions("prompts.list()", "all"),
128+
onResponseBody: (body, span) => {
129+
span.setAttribute("prompt.count", body.data.length);
130+
},
131+
});
85132
}
86133

87134
/** List all versions for a prompt. */
@@ -90,7 +137,13 @@ export function listPromptVersions(
90137
requestOptions?: ApiRequestOptions
91138
): Promise<ListPromptVersionsResponseBody> {
92139
const apiClient = apiClientManager.clientOrThrow();
93-
return apiClient.listPromptVersions(slug, requestOptions);
140+
return apiClient.listPromptVersions(slug, {
141+
...promptSpanOptions("prompts.versions()", slug),
142+
onResponseBody: (body, span) => {
143+
span.setAttribute("prompt.slug", slug);
144+
span.setAttribute("prompt.versions.count", body.data.length);
145+
},
146+
});
94147
}
95148

96149
/** Promote a code-deployed version to be the current version. */
@@ -100,7 +153,14 @@ export async function promotePromptVersion(
100153
requestOptions?: ApiRequestOptions
101154
): Promise<PromptOkResponseBody> {
102155
const apiClient = apiClientManager.clientOrThrow();
103-
return apiClient.promotePromptVersion(slug, { version }, requestOptions);
156+
return apiClient.promotePromptVersion(slug, { version }, {
157+
...promptSpanOptions("prompts.promote()", slug),
158+
attributes: {
159+
...promptSpanOptions("prompts.promote()", slug).attributes,
160+
"prompt.slug": slug,
161+
"prompt.version": version,
162+
},
163+
});
104164
}
105165

106166
/** Create an override — a dashboard/API edit that takes priority over the deployed version. */
@@ -110,7 +170,17 @@ export async function createPromptOverride(
110170
requestOptions?: ApiRequestOptions
111171
): Promise<PromptOverrideCreatedResponseBody> {
112172
const apiClient = apiClientManager.clientOrThrow();
113-
return apiClient.createPromptOverride(slug, body, requestOptions);
173+
return apiClient.createPromptOverride(slug, body, {
174+
...promptSpanOptions("prompts.createOverride()", slug),
175+
attributes: {
176+
...promptSpanOptions("prompts.createOverride()", slug).attributes,
177+
"prompt.slug": slug,
178+
...(body.model ? { "prompt.model": body.model } : {}),
179+
},
180+
onResponseBody: (body, span) => {
181+
span.setAttribute("prompt.override.version", body.version);
182+
},
183+
});
114184
}
115185

116186
/** Update the active override's content or model. */
@@ -120,7 +190,13 @@ export async function updatePromptOverride(
120190
requestOptions?: ApiRequestOptions
121191
): Promise<PromptOkResponseBody> {
122192
const apiClient = apiClientManager.clientOrThrow();
123-
return apiClient.updatePromptOverride(slug, body, requestOptions);
193+
return apiClient.updatePromptOverride(slug, body, {
194+
...promptSpanOptions("prompts.updateOverride()", slug),
195+
attributes: {
196+
...promptSpanOptions("prompts.updateOverride()", slug).attributes,
197+
"prompt.slug": slug,
198+
},
199+
});
124200
}
125201

126202
/** Remove the active override, reverting to the current deployed version. */
@@ -129,7 +205,13 @@ export async function removePromptOverride(
129205
requestOptions?: ApiRequestOptions
130206
): Promise<PromptOkResponseBody> {
131207
const apiClient = apiClientManager.clientOrThrow();
132-
return apiClient.removePromptOverride(slug, requestOptions);
208+
return apiClient.removePromptOverride(slug, {
209+
...promptSpanOptions("prompts.removeOverride()", slug),
210+
attributes: {
211+
...promptSpanOptions("prompts.removeOverride()", slug).attributes,
212+
"prompt.slug": slug,
213+
},
214+
});
133215
}
134216

135217
/** Reactivate a previously removed override version. */
@@ -139,5 +221,12 @@ export async function reactivatePromptOverride(
139221
requestOptions?: ApiRequestOptions
140222
): Promise<PromptOkResponseBody> {
141223
const apiClient = apiClientManager.clientOrThrow();
142-
return apiClient.reactivatePromptOverride(slug, { version }, requestOptions);
224+
return apiClient.reactivatePromptOverride(slug, { version }, {
225+
...promptSpanOptions("prompts.reactivateOverride()", slug),
226+
attributes: {
227+
...promptSpanOptions("prompts.reactivateOverride()", slug).attributes,
228+
"prompt.slug": slug,
229+
"prompt.version": version,
230+
},
231+
});
143232
}

0 commit comments

Comments
 (0)