Skip to content

Commit c398731

Browse files
committed
Add support for configuring instrumentation
1 parent 4cb89cb commit c398731

File tree

13 files changed

+79
-28
lines changed

13 files changed

+79
-28
lines changed

packages/cli-v3/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
"@opentelemetry/sdk-trace-base": "^1.21.0",
9494
"@opentelemetry/sdk-trace-node": "^1.21.0",
9595
"@opentelemetry/semantic-conventions": "^1.21.0",
96-
"@traceloop/instrumentation-openai": "^0.3.9",
9796
"@trigger.dev/core": "workspace:^2.3.18",
9897
"@types/degit": "^2.8.3",
9998
"chalk": "^5.2.0",

packages/cli-v3/src/commands/deploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
import { logger } from "../utilities/logger.js";
4343
import { createTaskFileImports, gatherTaskFiles } from "../utilities/taskFiles";
4444
import { login } from "./login";
45-
import { bundleDependenciesPlugin } from "../utilities/build";
45+
import { bundleDependenciesPlugin, workerSetupImportConfigPlugin } from "../utilities/build";
4646
import { Glob } from "glob";
4747

4848
const DeployCommandOptions = CommonCommandOptions.extend({
@@ -816,7 +816,7 @@ async function compileProject(
816816
TRIGGER_API_URL: `"${config.triggerUrl}"`,
817817
__PROJECT_CONFIG__: JSON.stringify(config),
818818
},
819-
plugins: [bundleDependenciesPlugin(config)],
819+
plugins: [bundleDependenciesPlugin(config), workerSetupImportConfigPlugin(configPath)],
820820
});
821821

822822
if (result.errors.length > 0) {

packages/cli-v3/src/commands/dev.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { z } from "zod";
2626
import * as packageJson from "../../package.json";
2727
import { CliApiClient } from "../apiClient";
2828
import { CommonCommandOptions, commonOptions, wrapCommandAction } from "../cli/common.js";
29-
import { bundleDependenciesPlugin } from "../utilities/build";
29+
import { bundleDependenciesPlugin, workerSetupImportConfigPlugin } from "../utilities/build";
3030
import { chalkPurple } from "../utilities/colors";
3131
import { readConfig } from "../utilities/configFiles";
3232
import { readJSONFile } from "../utilities/fileSystem";
@@ -363,6 +363,7 @@ function useDev({
363363
},
364364
plugins: [
365365
bundleDependenciesPlugin(config),
366+
workerSetupImportConfigPlugin(configPath),
366367
{
367368
name: "trigger.dev v3",
368369
setup(build) {

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@ import type { Plugin } from "esbuild";
44
import { extname, isAbsolute } from "node:path";
55
import tsConfigPaths from "tsconfig-paths";
66
import { logger } from "./logger";
7+
import { readFileSync } from "node:fs";
8+
9+
export function workerSetupImportConfigPlugin(configPath?: string): Plugin {
10+
return {
11+
name: "trigger-worker-setup",
12+
setup(build) {
13+
if (!configPath) {
14+
return;
15+
}
16+
17+
build.onLoad({ filter: /worker-setup\.js$/ }, async (args) => {
18+
let workerSetupContents = readFileSync(args.path, "utf-8");
19+
20+
workerSetupContents = workerSetupContents.replace(
21+
"__SETUP_IMPORTED_PROJECT_CONFIG__",
22+
`import * as setupImportedConfigExports from "${configPath}"; const setupImportedConfig = setupImportedConfigExports.config;`
23+
);
24+
25+
logger.debug("Loading worker setup", {
26+
args,
27+
workerSetupContents,
28+
configPath,
29+
});
30+
31+
return {
32+
contents: workerSetupContents,
33+
loader: "js",
34+
};
35+
});
36+
},
37+
};
38+
}
739

840
export function bundleDependenciesPlugin(config: ResolvedConfig): Plugin {
941
const matchPath = config.tsconfigPath ? createMatchPath(config.tsconfigPath) : undefined;
@@ -16,26 +48,26 @@ export function bundleDependenciesPlugin(config: ResolvedConfig): Plugin {
1648
}
1749

1850
return {
19-
name: "bundle-dependencies",
51+
name: "trigger-bundle-dependencies",
2052
setup(build) {
2153
build.onResolve({ filter: /.*/ }, (args) => {
2254
const resolvedPath = resolvePath(args.path);
2355

24-
logger.debug(`Checking if ${args.path} should be bundled or external`, {
56+
logger.ignore(`Checking if ${args.path} should be bundled or external`, {
2557
...args,
2658
resolvedPath,
2759
});
2860

2961
if (!isBareModuleId(resolvedPath)) {
30-
logger.debug(`Bundling ${args.path} because its not a bareModuleId`, {
62+
logger.ignore(`Bundling ${args.path} because its not a bareModuleId`, {
3163
...args,
3264
});
3365

3466
return undefined; // let esbuild bundle it
3567
}
3668

3769
if (args.path.startsWith("@trigger.dev/")) {
38-
logger.debug(`Bundling ${args.path} because its a trigger.dev package`, {
70+
logger.ignore(`Bundling ${args.path} because its a trigger.dev package`, {
3971
...args,
4072
});
4173

@@ -63,7 +95,7 @@ export function bundleDependenciesPlugin(config: ResolvedConfig): Plugin {
6395
}
6496
}
6597

66-
logger.debug(`Externalizing ${args.path}`, {
98+
logger.ignore(`Externalizing ${args.path}`, {
6799
...args,
68100
});
69101

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export async function readConfig(
131131

132132
const tempDir = await createTempDir();
133133

134-
const builtConfigFilePath = join(tempDir, "config.mjs");
134+
const builtConfigFilePath = join(tempDir, "config.js");
135135
const builtConfigFileHref = pathToFileURL(builtConfigFilePath).href;
136136

137137
logger.debug("Building config file", {
@@ -147,7 +147,7 @@ export async function readConfig(
147147
metafile: true,
148148
minify: false,
149149
write: true,
150-
format: "esm",
150+
format: "cjs",
151151
platform: "node",
152152
target: ["es2018", "node18"],
153153
outfile: builtConfigFilePath,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class Logger {
5555
columns = process.stdout.columns;
5656

5757
debug = (...args: unknown[]) => this.doLog("debug", args);
58+
ignore = (...args: unknown[]) => {};
5859
debugWithSanitization = (label: string, ...args: unknown[]) => {
5960
this.doLog("debug", [label, ...args]);
6061
};

packages/cli-v3/src/workers/dev/worker-facade.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
type TracingSDK,
77
type HandleErrorFunction,
88
} from "@trigger.dev/core/v3";
9-
import "source-map-support/register.js";
109

1110
__WORKER_SETUP__;
1211
declare const __WORKER_SETUP__: unknown;

packages/cli-v3/src/workers/dev/worker-setup.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
import "source-map-support/register.js";
12
import { Resource } from "@opentelemetry/resources";
2-
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
33
import {
4+
ProjectConfig,
45
SemanticInternalAttributes,
56
TracingDiagnosticLogLevel,
67
TracingSDK,
78
ZodMessageSender,
89
childToWorkerMessages,
910
} from "@trigger.dev/core/v3";
1011

12+
__SETUP_IMPORTED_PROJECT_CONFIG__;
13+
declare const __SETUP_IMPORTED_PROJECT_CONFIG__: unknown;
14+
declare const setupImportedConfig: ProjectConfig | undefined;
15+
1116
export const tracingSDK = new TracingSDK({
1217
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
1318
resource: new Resource({
1419
[SemanticInternalAttributes.CLI_VERSION]: "3.0.0",
1520
}),
16-
instrumentations: [new OpenAIInstrumentation()],
21+
instrumentations: setupImportedConfig?.instrumentations ?? [],
1722
diagLogLevel: (process.env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
1823
});
1924

packages/cli-v3/src/workers/prod/worker-setup.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { Resource } from "@opentelemetry/resources";
2-
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
32
import {
3+
ProjectConfig,
44
SemanticInternalAttributes,
55
TracingDiagnosticLogLevel,
66
TracingSDK,
7-
ZodMessageSender,
8-
childToWorkerMessages,
97
} from "@trigger.dev/core/v3";
108

9+
__SETUP_IMPORTED_PROJECT_CONFIG__;
10+
declare const __SETUP_IMPORTED_PROJECT_CONFIG__: unknown;
11+
declare const setupImportedConfig: ProjectConfig | undefined;
12+
1113
export const tracingSDK = new TracingSDK({
1214
url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
1315
resource: new Resource({
1416
[SemanticInternalAttributes.CLI_VERSION]: "3.0.0",
1517
}),
16-
instrumentations: [new OpenAIInstrumentation()],
18+
instrumentations: setupImportedConfig?.instrumentations ?? [],
1719
diagLogLevel: (process.env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
1820
});
1921

packages/core/src/v3/types/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RetryOptions } from "../schemas";
2+
import type { InstrumentationOption } from "@opentelemetry/instrumentation";
23

34
export interface ProjectConfig {
45
project: string;
@@ -25,4 +26,9 @@ export interface ProjectConfig {
2526
* The path to your project's tsconfig.json file. Will use tsconfig.json in the project directory if not provided.
2627
*/
2728
tsconfigPath?: string;
29+
30+
/**
31+
* The OpenTelemetry instrumentations to enable
32+
*/
33+
instrumentations?: InstrumentationOption[];
2834
}

pnpm-lock.yaml

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/v3-catalog/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"start:stripe": "ts-node -r tsconfig-paths/register -r dotenv/config src/stripeUsage.ts"
1515
},
1616
"dependencies": {
17+
"@opentelemetry/api": "^1.7.0",
1718
"@sindresorhus/slugify": "^2.2.1",
19+
"@traceloop/instrumentation-openai": "^0.3.9",
1820
"@trigger.dev/sdk": "2.3.18",
1921
"msw": "^2.2.1",
2022
"openai": "^4.28.0",

references/v3-catalog/trigger.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
2+
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
23

34
export { handleError } from "./src/handleError";
45

@@ -17,4 +18,5 @@ export const config: TriggerConfig = {
1718
additionalPackages: ["[email protected]"],
1819
additionalFiles: ["./wrangler/wrangler.toml"],
1920
dependenciesToBundle: [/@sindresorhus/, "escape-string-regexp"],
21+
instrumentations: [new OpenAIInstrumentation()],
2022
};

0 commit comments

Comments
 (0)