|
1 | | -import { flattenAttributes } from "@trigger.dev/core/v3"; |
2 | | -import { recordSpanException } from "@trigger.dev/core/v3/workers"; |
| 1 | +import { outro } from "@clack/prompts"; |
3 | 2 | import { Command } from "commander"; |
4 | 3 | import { z } from "zod"; |
5 | | -import { getTracer, provider } from "../telemetry/tracing.js"; |
6 | 4 | 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"; |
10 | 6 | import { CLOUD_API_URL } from "../consts.js"; |
| 7 | +import { chalkError } from "../utilities/cliOutput.js"; |
11 | 8 | 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"; |
13 | 11 |
|
14 | 12 | export const CommonCommandOptions = z.object({ |
15 | 13 | apiUrl: z.string().optional(), |
@@ -39,69 +37,52 @@ export class OutroCommandError extends SkipCommandError {} |
39 | 37 | export async function handleTelemetry(action: () => Promise<void>) { |
40 | 38 | try { |
41 | 39 | await action(); |
42 | | - |
43 | | - await provider?.forceFlush(); |
44 | 40 | } catch (e) { |
45 | | - await provider?.forceFlush(); |
46 | | - |
47 | 41 | process.exitCode = 1; |
48 | 42 | } |
49 | 43 | } |
50 | 44 |
|
51 | | -export const tracer = getTracer(); |
52 | | - |
53 | 45 | export async function wrapCommandAction<T extends z.AnyZodObject, TResult>( |
54 | 46 | name: string, |
55 | 47 | schema: T, |
56 | 48 | options: unknown, |
57 | 49 | action: (opts: z.output<T>) => Promise<TResult> |
58 | 50 | ): 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); |
79 | 53 |
|
80 | | - span.end(); |
| 54 | + if (!parsedOptions.success) { |
| 55 | + throw new Error(fromZodError(parsedOptions.error).toString()); |
| 56 | + } |
81 | 57 |
|
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; |
94 | 59 |
|
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 | + }); |
97 | 63 |
|
98 | | - span.end(); |
| 64 | + const result = await action(parsedOptions.data); |
99 | 65 |
|
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)}`); |
101 | 78 | } |
102 | | - }); |
| 79 | + |
| 80 | + throw e; |
| 81 | + } |
103 | 82 | } |
104 | 83 |
|
| 84 | +export const tracer = trace.getTracer("trigger.dev/cli"); |
| 85 | + |
105 | 86 | export function installExitHandler() { |
106 | 87 | process.on("SIGINT", () => { |
107 | 88 | process.exit(0); |
|
0 commit comments