Skip to content

Commit 24d3a29

Browse files
committed
getting everything to work
1 parent e01872c commit 24d3a29

File tree

10 files changed

+159
-309
lines changed

10 files changed

+159
-309
lines changed

packages/cli-v3/src/cli/common.ts

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { flattenAttributes } from "@trigger.dev/core/v3";
2-
import { recordSpanException } from "@trigger.dev/core/v3/workers";
1+
import { outro } from "@clack/prompts";
32
import { Command } from "commander";
43
import { z } from "zod";
5-
import { getTracer, provider } from "../telemetry/tracing.js";
64
import { fromZodError } from "zod-validation-error";
7-
import { logger } from "../utilities/logger.js";
8-
import { outro } from "@clack/prompts";
9-
import { chalkError } from "../utilities/cliOutput.js";
5+
import { BundleError } from "../build/bundle.js";
106
import { CLOUD_API_URL } from "../consts.js";
7+
import { chalkError } from "../utilities/cliOutput.js";
118
import { readAuthConfigCurrentProfileName } from "../utilities/configFiles.js";
12-
import { BundleError } from "../build/bundle.js";
9+
import { logger } from "../utilities/logger.js";
10+
import { trace } from "@opentelemetry/api";
1311

1412
export const CommonCommandOptions = z.object({
1513
apiUrl: z.string().optional(),
@@ -39,69 +37,52 @@ export class OutroCommandError extends SkipCommandError {}
3937
export async function handleTelemetry(action: () => Promise<void>) {
4038
try {
4139
await action();
42-
43-
await provider?.forceFlush();
4440
} catch (e) {
45-
await provider?.forceFlush();
46-
4741
process.exitCode = 1;
4842
}
4943
}
5044

51-
export const tracer = getTracer();
52-
5345
export async function wrapCommandAction<T extends z.AnyZodObject, TResult>(
5446
name: string,
5547
schema: T,
5648
options: unknown,
5749
action: (opts: z.output<T>) => Promise<TResult>
5850
): Promise<TResult | undefined> {
59-
return await tracer.startActiveSpan(name, async (span) => {
60-
try {
61-
const parsedOptions = schema.safeParse(options);
62-
63-
if (!parsedOptions.success) {
64-
throw new Error(fromZodError(parsedOptions.error).toString());
65-
}
66-
67-
span.setAttributes({
68-
...flattenAttributes(parsedOptions.data, "cli.options"),
69-
});
70-
71-
logger.loggerLevel = parsedOptions.data.logLevel;
72-
73-
logger.debug(`Running "${name}" with the following options`, {
74-
options: options,
75-
spanContext: span?.spanContext(),
76-
});
77-
78-
const result = await action(parsedOptions.data);
51+
try {
52+
const parsedOptions = schema.safeParse(options);
7953

80-
span.end();
54+
if (!parsedOptions.success) {
55+
throw new Error(fromZodError(parsedOptions.error).toString());
56+
}
8157

82-
return result;
83-
} catch (e) {
84-
if (e instanceof SkipLoggingError) {
85-
recordSpanException(span, e);
86-
} else if (e instanceof OutroCommandError) {
87-
outro("Operation cancelled");
88-
} else if (e instanceof SkipCommandError) {
89-
// do nothing
90-
} else if (e instanceof BundleError) {
91-
process.exit(1);
92-
} else {
93-
recordSpanException(span, e);
58+
logger.loggerLevel = parsedOptions.data.logLevel;
9459

95-
logger.log(`${chalkError("X Error:")} ${e instanceof Error ? e.message : String(e)}`);
96-
}
60+
logger.debug(`Running "${name}" with the following options`, {
61+
options: options,
62+
});
9763

98-
span.end();
64+
const result = await action(parsedOptions.data);
9965

100-
throw e;
66+
return result;
67+
} catch (e) {
68+
if (e instanceof SkipLoggingError) {
69+
// do nothing
70+
} else if (e instanceof OutroCommandError) {
71+
outro("Operation cancelled");
72+
} else if (e instanceof SkipCommandError) {
73+
// do nothing
74+
} else if (e instanceof BundleError) {
75+
process.exit(1);
76+
} else {
77+
logger.log(`${chalkError("X Error:")} ${e instanceof Error ? e.message : String(e)}`);
10178
}
102-
});
79+
80+
throw e;
81+
}
10382
}
10483

84+
export const tracer = trace.getTracer("trigger.dev/cli");
85+
10586
export function installExitHandler() {
10687
process.on("SIGINT", () => {
10788
process.exit(0);

packages/cli-v3/src/entryPoints/dev-run-worker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
runtime,
2222
runTimelineMetrics,
2323
taskContext,
24+
TaskRunContext,
2425
TaskRunErrorCodes,
2526
TaskRunExecution,
2627
timeout,
@@ -175,11 +176,11 @@ async function doBootstrap() {
175176
const { config, handleError } = await importConfig(workerManifest.configPath);
176177

177178
const tracingSDK = new TracingSDK({
178-
url: env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
179+
url: env.TRIGGER_OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
179180
instrumentations: config.telemetry?.instrumentations ?? config.instrumentations ?? [],
180181
exporters: config.telemetry?.exporters ?? [],
181182
logExporters: config.telemetry?.logExporters ?? [],
182-
diagLogLevel: (env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
183+
diagLogLevel: (env.TRIGGER_OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
183184
forceFlushTimeoutMillis: 30_000,
184185
});
185186

@@ -361,6 +362,14 @@ const zodIpc = new ZodIpcConnection({
361362
return;
362363
}
363364

365+
const ctx = TaskRunContext.parse(execution);
366+
367+
taskContext.setGlobalTaskContext({
368+
ctx,
369+
worker: metadata,
370+
isWarmStart: isWarmStart ?? false,
371+
});
372+
364373
try {
365374
const { tracer, tracingSDK, consoleInterceptor, config, workerManifest } =
366375
await bootstrap();
@@ -516,7 +525,7 @@ const zodIpc = new ZodIpcConnection({
516525

517526
const signal = AbortSignal.any([_cancelController.signal, timeoutController.signal]);
518527

519-
const { result } = await executor.execute(execution, metadata, traceContext, signal);
528+
const { result } = await executor.execute(execution, ctx, traceContext, signal);
520529

521530
if (_isRunning && !_isCancelled) {
522531
const usageSample = usage.stop(_executionMeasurement);

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
runtime,
2121
runTimelineMetrics,
2222
taskContext,
23+
TaskRunContext,
2324
TaskRunErrorCodes,
2425
TaskRunExecution,
2526
timeout,
@@ -166,9 +167,9 @@ async function doBootstrap() {
166167
);
167168

168169
const tracingSDK = new TracingSDK({
169-
url: env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
170+
url: env.TRIGGER_OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
170171
instrumentations: config.instrumentations ?? [],
171-
diagLogLevel: (env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
172+
diagLogLevel: (env.TRIGGER_OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
172173
forceFlushTimeoutMillis: 30_000,
173174
exporters: config.telemetry?.exporters ?? [],
174175
logExporters: config.telemetry?.logExporters ?? [],
@@ -365,6 +366,14 @@ const zodIpc = new ZodIpcConnection({
365366
return;
366367
}
367368

369+
const ctx = TaskRunContext.parse(execution);
370+
371+
taskContext.setGlobalTaskContext({
372+
ctx,
373+
worker: metadata,
374+
isWarmStart: isWarmStart ?? false,
375+
});
376+
368377
try {
369378
const { tracer, tracingSDK, consoleInterceptor, config, workerManifest } =
370379
await bootstrap();
@@ -514,7 +523,7 @@ const zodIpc = new ZodIpcConnection({
514523

515524
const signal = AbortSignal.any([_cancelController.signal, timeoutController.signal]);
516525

517-
const { result } = await executor.execute(execution, metadata, traceContext, signal);
526+
const { result } = await executor.execute(execution, ctx, traceContext, signal);
518527

519528
if (_isRunning && !_isCancelled) {
520529
const usageSample = usage.stop(_executionMeasurement);

packages/cli-v3/src/telemetry/tracing.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

packages/cli-v3/src/utilities/session.ts

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { recordSpanException } from "@trigger.dev/core/v3/workers";
22
import { CliApiClient } from "../apiClient.js";
33
import { readAuthConfigProfile } from "./configFiles.js";
4-
import { getTracer } from "../telemetry/tracing.js";
54
import { logger } from "./logger.js";
65
import { GitMeta } from "@trigger.dev/core/v3";
76

8-
const tracer = getTracer();
9-
107
export type LoginResultOk = {
118
ok: true;
129
profile: string;
@@ -31,63 +28,44 @@ export type LoginResult =
3128
};
3229

3330
export async function isLoggedIn(profile: string = "default"): Promise<LoginResult> {
34-
return await tracer.startActiveSpan("isLoggedIn", async (span) => {
35-
try {
36-
const config = readAuthConfigProfile(profile);
37-
38-
if (!config?.accessToken || !config?.apiUrl) {
39-
span.recordException(new Error("You must login first"));
40-
span.end();
41-
return { ok: false as const, error: "You must login first" };
42-
}
43-
44-
const apiClient = new CliApiClient(config.apiUrl, config.accessToken);
45-
const userData = await apiClient.whoAmI();
46-
47-
if (!userData.success) {
48-
recordSpanException(span, userData.error);
49-
span.end();
50-
51-
return {
52-
ok: false as const,
53-
error: userData.error,
54-
auth: {
55-
apiUrl: config.apiUrl,
56-
accessToken: config.accessToken,
57-
},
58-
};
59-
}
60-
61-
span.setAttributes({
62-
"login.userId": userData.data.userId,
63-
"login.email": userData.data.email,
64-
"login.dashboardUrl": userData.data.dashboardUrl,
65-
"login.profile": profile,
66-
});
67-
68-
span.end();
31+
try {
32+
const config = readAuthConfigProfile(profile);
33+
34+
if (!config?.accessToken || !config?.apiUrl) {
35+
return { ok: false as const, error: "You must login first" };
36+
}
37+
38+
const apiClient = new CliApiClient(config.apiUrl, config.accessToken);
39+
const userData = await apiClient.whoAmI();
6940

41+
if (!userData.success) {
7042
return {
71-
ok: true as const,
72-
profile,
73-
userId: userData.data.userId,
74-
email: userData.data.email,
75-
dashboardUrl: userData.data.dashboardUrl,
43+
ok: false as const,
44+
error: userData.error,
7645
auth: {
7746
apiUrl: config.apiUrl,
7847
accessToken: config.accessToken,
7948
},
8049
};
81-
} catch (e) {
82-
recordSpanException(span, e);
83-
span.end();
84-
85-
return {
86-
ok: false as const,
87-
error: e instanceof Error ? e.message : "Unknown error",
88-
};
8950
}
90-
});
51+
52+
return {
53+
ok: true as const,
54+
profile,
55+
userId: userData.data.userId,
56+
email: userData.data.email,
57+
dashboardUrl: userData.data.dashboardUrl,
58+
auth: {
59+
apiUrl: config.apiUrl,
60+
accessToken: config.accessToken,
61+
},
62+
};
63+
} catch (e) {
64+
return {
65+
ok: false as const,
66+
error: e instanceof Error ? e.message : "Unknown error",
67+
};
68+
}
9169
}
9270

9371
export type GetEnvOptions = {

0 commit comments

Comments
 (0)