Skip to content

Commit 00bf380

Browse files
committed
more structured logs
1 parent 971ddc3 commit 00bf380

File tree

10 files changed

+126
-111
lines changed

10 files changed

+126
-111
lines changed

apps/supervisor/src/clients/kubernetes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { Informer } from "@kubernetes/client-node";
33
import { ListPromise } from "@kubernetes/client-node";
44
import { KubernetesObject } from "@kubernetes/client-node";
55
import { assertExhaustive } from "@trigger.dev/core/utils";
6+
import { SimpleStructuredLogger } from "@trigger.dev/core/v3/utils/structuredLogger";
67

78
export const RUNTIME_ENV = process.env.KUBERNETES_PORT ? "kubernetes" : "local";
89

10+
const logger = new SimpleStructuredLogger("kubernetes-client");
11+
912
export function createK8sApi() {
1013
const kubeConfig = getKubeConfig();
1114

@@ -31,7 +34,7 @@ export function createK8sApi() {
3134
export type K8sApi = ReturnType<typeof createK8sApi>;
3235

3336
function getKubeConfig() {
34-
console.log("getKubeConfig()", { RUNTIME_ENV });
37+
logger.debug("getKubeConfig()", { RUNTIME_ENV });
3538

3639
const kubeConfig = new k8s.KubeConfig();
3740

apps/supervisor/src/envUtil.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { z } from "zod";
2+
import { SimpleStructuredLogger } from "@trigger.dev/core/v3/utils/structuredLogger";
3+
4+
const logger = new SimpleStructuredLogger("env-util");
25

36
export const BoolEnv = z.preprocess((val) => {
47
if (typeof val !== "string") {
@@ -33,7 +36,7 @@ export const AdditionalEnvVars = z.preprocess((val) => {
3336
// Return undefined if no valid key-value pairs were found
3437
return Object.keys(result).length === 0 ? undefined : result;
3538
} catch (error) {
36-
console.warn("Failed to parse additional env vars", { error, val });
39+
logger.warn("Failed to parse additional env vars", { error, val });
3740
return undefined;
3841
}
3942
}, z.record(z.string(), z.string()).optional());

apps/supervisor/src/index.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ManagedSupervisor {
3333
private readonly metricsServer?: HttpServer;
3434
private readonly workloadServer: WorkloadServer;
3535
private readonly workloadManager: WorkloadManager;
36-
private readonly logger = new SimpleStructuredLogger("managed-worker");
36+
private readonly logger = new SimpleStructuredLogger("managed-supervisor");
3737
private readonly resourceMonitor: ResourceMonitor;
3838
private readonly checkpointClient?: CheckpointClient;
3939

@@ -47,11 +47,11 @@ class ManagedSupervisor {
4747
const { TRIGGER_WORKER_TOKEN, MANAGED_WORKER_SECRET, ...envWithoutSecrets } = env;
4848

4949
if (env.DEBUG) {
50-
console.debug("[ManagedSupervisor] Starting up", { envWithoutSecrets });
50+
this.logger.debug("Starting up", { envWithoutSecrets });
5151
}
5252

5353
if (this.warmStartUrl) {
54-
this.logger.log("[ManagedWorker] 🔥 Warm starts enabled", {
54+
this.logger.log("🔥 Warm starts enabled", {
5555
warmStartUrl: this.warmStartUrl,
5656
});
5757
}
@@ -71,7 +71,7 @@ class ManagedSupervisor {
7171

7272
if (this.isKubernetes) {
7373
if (env.POD_CLEANER_ENABLED) {
74-
this.logger.log("[ManagedWorker] 🧹 Pod cleaner enabled", {
74+
this.logger.log("🧹 Pod cleaner enabled", {
7575
namespace: env.KUBERNETES_NAMESPACE,
7676
batchSize: env.POD_CLEANER_BATCH_SIZE,
7777
intervalMs: env.POD_CLEANER_INTERVAL_MS,
@@ -83,11 +83,11 @@ class ManagedSupervisor {
8383
intervalMs: env.POD_CLEANER_INTERVAL_MS,
8484
});
8585
} else {
86-
this.logger.warn("[ManagedWorker] Pod cleaner disabled");
86+
this.logger.warn("Pod cleaner disabled");
8787
}
8888

8989
if (env.FAILED_POD_HANDLER_ENABLED) {
90-
this.logger.log("[ManagedWorker] 🔁 Failed pod handler enabled", {
90+
this.logger.log("🔁 Failed pod handler enabled", {
9191
namespace: env.KUBERNETES_NAMESPACE,
9292
reconnectIntervalMs: env.FAILED_POD_HANDLER_RECONNECT_INTERVAL_MS,
9393
});
@@ -97,7 +97,7 @@ class ManagedSupervisor {
9797
reconnectIntervalMs: env.FAILED_POD_HANDLER_RECONNECT_INTERVAL_MS,
9898
});
9999
} else {
100-
this.logger.warn("[ManagedWorker] Failed pod handler disabled");
100+
this.logger.warn("Failed pod handler disabled");
101101
}
102102

103103
this.resourceMonitor = new KubernetesResourceMonitor(
@@ -144,7 +144,7 @@ class ManagedSupervisor {
144144
});
145145

146146
if (env.TRIGGER_CHECKPOINT_URL) {
147-
this.logger.log("[ManagedWorker] 🥶 Checkpoints enabled", {
147+
this.logger.log("🥶 Checkpoints enabled", {
148148
checkpointUrl: env.TRIGGER_CHECKPOINT_URL,
149149
});
150150

@@ -157,41 +157,38 @@ class ManagedSupervisor {
157157

158158
// setInterval(async () => {
159159
// const resources = await this.resourceMonitor.getNodeResources(true);
160-
// this.logger.debug("[ManagedWorker] Current resources", { resources });
160+
// this.logger.debug("Current resources", { resources });
161161
// }, 1000);
162162

163163
this.workerSession.on("runNotification", async ({ time, run }) => {
164-
this.logger.log("[ManagedWorker] runNotification", { time, run });
164+
this.logger.log("runNotification", { time, run });
165165

166166
this.workloadServer.notifyRun({ run });
167167
});
168168

169169
this.workerSession.on("runQueueMessage", async ({ time, message }) => {
170-
this.logger.log(
171-
`[ManagedWorker] Received message with timestamp ${time.toLocaleString()}`,
172-
message
173-
);
170+
this.logger.log(`Received message with timestamp ${time.toLocaleString()}`, message);
174171

175172
if (message.completedWaitpoints.length > 0) {
176-
this.logger.debug("[ManagedWorker] Run has completed waitpoints", {
173+
this.logger.debug("Run has completed waitpoints", {
177174
runId: message.run.id,
178175
completedWaitpoints: message.completedWaitpoints.length,
179176
});
180177
// TODO: Do something with them or if we don't need the data here, maybe we shouldn't even send it
181178
}
182179

183180
if (!message.image) {
184-
this.logger.error("[ManagedWorker] Run has no image", { runId: message.run.id });
181+
this.logger.error("Run has no image", { runId: message.run.id });
185182
return;
186183
}
187184

188185
const { checkpoint, ...rest } = message;
189186

190187
if (checkpoint) {
191-
this.logger.log("[ManagedWorker] Restoring run", { runId: message.run.id });
188+
this.logger.log("Restoring run", { runId: message.run.id });
192189

193190
if (!this.checkpointClient) {
194-
this.logger.error("[ManagedWorker] No checkpoint client", { runId: message.run.id });
191+
this.logger.error("No checkpoint client", { runId: message.run.id });
195192
return;
196193
}
197194

@@ -206,23 +203,23 @@ class ManagedSupervisor {
206203
});
207204

208205
if (didRestore) {
209-
this.logger.log("[ManagedWorker] Restore successful", { runId: message.run.id });
206+
this.logger.log("Restore successful", { runId: message.run.id });
210207
} else {
211-
this.logger.error("[ManagedWorker] Restore failed", { runId: message.run.id });
208+
this.logger.error("Restore failed", { runId: message.run.id });
212209
}
213210
} catch (error) {
214-
this.logger.error("[ManagedWorker] Failed to restore run", { error });
211+
this.logger.error("Failed to restore run", { error });
215212
}
216213

217214
return;
218215
}
219216

220-
this.logger.log("[ManagedWorker] Scheduling run", { runId: message.run.id });
217+
this.logger.log("Scheduling run", { runId: message.run.id });
221218

222219
const didWarmStart = await this.tryWarmStart(message);
223220

224221
if (didWarmStart) {
225-
this.logger.log("[ManagedWorker] Warm start successful", { runId: message.run.id });
222+
this.logger.log("Warm start successful", { runId: message.run.id });
226223
return;
227224
}
228225

@@ -249,7 +246,7 @@ class ManagedSupervisor {
249246
// memory: message.run.machine.memory,
250247
// });
251248
} catch (error) {
252-
this.logger.error("[ManagedWorker] Failed to create workload", { error });
249+
this.logger.error("Failed to create workload", { error });
253250
}
254251
});
255252

@@ -277,12 +274,12 @@ class ManagedSupervisor {
277274
}
278275

279276
async onRunConnected({ run }: { run: { friendlyId: string } }) {
280-
this.logger.debug("[ManagedWorker] Run connected", { run });
277+
this.logger.debug("Run connected", { run });
281278
this.workerSession.subscribeToRunNotifications([run.friendlyId]);
282279
}
283280

284281
async onRunDisconnected({ run }: { run: { friendlyId: string } }) {
285-
this.logger.debug("[ManagedWorker] Run disconnected", { run });
282+
this.logger.debug("Run disconnected", { run });
286283
this.workerSession.unsubscribeFromRunNotifications([run.friendlyId]);
287284
}
288285

@@ -303,7 +300,7 @@ class ManagedSupervisor {
303300
});
304301

305302
if (!res.ok) {
306-
this.logger.error("[ManagedWorker] Warm start failed", {
303+
this.logger.error("Warm start failed", {
307304
runId: dequeuedMessage.run.id,
308305
});
309306
return false;
@@ -313,7 +310,7 @@ class ManagedSupervisor {
313310
const parsedData = z.object({ didWarmStart: z.boolean() }).safeParse(data);
314311

315312
if (!parsedData.success) {
316-
this.logger.error("[ManagedWorker] Warm start response invalid", {
313+
this.logger.error("Warm start response invalid", {
317314
runId: dequeuedMessage.run.id,
318315
data,
319316
});
@@ -322,7 +319,7 @@ class ManagedSupervisor {
322319

323320
return parsedData.data.didWarmStart;
324321
} catch (error) {
325-
this.logger.error("[ManagedWorker] Warm start error", {
322+
this.logger.error("Warm start error", {
326323
runId: dequeuedMessage.run.id,
327324
error,
328325
});
@@ -331,29 +328,29 @@ class ManagedSupervisor {
331328
}
332329

333330
async start() {
334-
this.logger.log("[ManagedWorker] Starting up");
331+
this.logger.log("Starting up");
335332

336333
// Optional services
337334
await this.podCleaner?.start();
338335
await this.failedPodHandler?.start();
339336
await this.metricsServer?.start();
340337

341338
if (env.TRIGGER_WORKLOAD_API_ENABLED) {
342-
this.logger.log("[ManagedWorker] Workload API enabled", {
339+
this.logger.log("Workload API enabled", {
343340
protocol: env.TRIGGER_WORKLOAD_API_PROTOCOL,
344341
domain: env.TRIGGER_WORKLOAD_API_DOMAIN,
345342
port: env.TRIGGER_WORKLOAD_API_PORT_INTERNAL,
346343
});
347344
await this.workloadServer.start();
348345
} else {
349-
this.logger.warn("[ManagedWorker] Workload API disabled");
346+
this.logger.warn("Workload API disabled");
350347
}
351348

352349
await this.workerSession.start();
353350
}
354351

355352
async stop() {
356-
this.logger.log("[ManagedWorker] Shutting down");
353+
this.logger.log("Shutting down");
357354
await this.workerSession.stop();
358355

359356
// Optional services

0 commit comments

Comments
 (0)