Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
},
"volta": {
"node": "18.20.8",
"yarn": "1.22.19"
"yarn": "1.22.22"
}
}
7 changes: 6 additions & 1 deletion packages/bundler-plugin-core/src/build-plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export function createSentryBuildPluginManager(
* E.g. `webpack` or `nextjs` or `turbopack`
*/
buildTool: string;
/**
* E.g. `5` for webpack v5 or `4` for Rollup v4
*/
buildToolMajorVersion?: string;
/**
* E.g. `[sentry-webpack-plugin]` or `[@sentry/nextjs]`
*/
Expand Down Expand Up @@ -195,7 +199,8 @@ export function createSentryBuildPluginManager(
const { sentryScope, sentryClient } = createSentryInstance(
options,
shouldSendTelemetry,
bundlerPluginMetaContext.buildTool
bundlerPluginMetaContext.buildTool,
bundlerPluginMetaContext.buildToolMajorVersion
);

const { release, environment = DEFAULT_ENVIRONMENT } = sentryClient.getOptions();
Expand Down
3 changes: 3 additions & 0 deletions packages/bundler-plugin-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface SentryUnpluginFactoryOptions {
webpack_forceExitOnBuildComplete?: boolean
) => UnpluginOptions;
bundleSizeOptimizationsPlugin: (buildFlags: SentrySDKBuildFlags) => UnpluginOptions;
getBundlerMajorVersion?: () => string | undefined;
}

/**
Expand All @@ -57,13 +58,15 @@ export function sentryUnpluginFactory({
componentNameAnnotatePlugin,
debugIdUploadPlugin,
bundleSizeOptimizationsPlugin,
getBundlerMajorVersion,
}: SentryUnpluginFactoryOptions): UnpluginInstance<Options | undefined, true> {
return createUnplugin<Options | undefined, true>((userOptions = {}, unpluginMetaContext) => {
const sentryBuildPluginManager = createSentryBuildPluginManager(userOptions, {
loggerPrefix:
userOptions._metaOptions?.loggerPrefixOverride ??
`[sentry-${unpluginMetaContext.framework}-plugin]`,
buildTool: unpluginMetaContext.framework,
buildToolMajorVersion: getBundlerMajorVersion?.(),
});

const {
Expand Down
1 change: 0 additions & 1 deletion packages/bundler-plugin-core/src/options-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export type NormalizedOptions = {
_metaOptions: {
telemetry: {
metaFramework: string | undefined;
bundlerMajorVersion: string | undefined;
};
};
applicationKey: string | undefined;
Expand Down
12 changes: 8 additions & 4 deletions packages/bundler-plugin-core/src/sentry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const stackParser = createStackParser(nodeStackLineParser());
export function createSentryInstance(
options: NormalizedOptions,
shouldSendTelemetry: Promise<boolean>,
buildTool: string
buildTool: string,
buildToolMajorVersion: string | undefined
): { sentryScope: Scope; sentryClient: Client } {
const clientOptions: ServerRuntimeClientOptions = {
platform: "node",
Expand Down Expand Up @@ -56,15 +57,16 @@ export function createSentryInstance(
const scope = new Scope();
scope.setClient(client);

setTelemetryDataOnScope(options, scope, buildTool);
setTelemetryDataOnScope(options, scope, buildTool, buildToolMajorVersion);

return { sentryScope: scope, sentryClient: client };
}

export function setTelemetryDataOnScope(
options: NormalizedOptions,
scope: Scope,
buildTool: string
buildTool: string,
buildToolMajorVersion?: string
): void {
const { org, project, release, errorHandler, sourcemaps, reactComponentAnnotation } = options;

Expand Down Expand Up @@ -111,7 +113,9 @@ export function setTelemetryDataOnScope(
bundler: buildTool,
});

scope.setTag("bundler-major-version", options._metaOptions.telemetry.bundlerMajorVersion);
if (buildToolMajorVersion) {
scope.setTag("bundler-major-version", buildToolMajorVersion);
}

scope.setUser({ id: org });
}
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Can we also test Vite here? AFAICT, this only runs for rollup. Would be nice to have this covered but if it doesn't work easily, we can also leave it at rollup

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking like Jest can't run Vite directly because... well Jest. Various ESM issues, etc.

What I really want to do is re-write these telemetry tests so we can override the DSN and capture the telemetry directly rather than this in-process hack (it was my hack 🤣). But this will have to wait until we can use Vitest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for checking. Sounds reasonable to me!

Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ test("rollup bundle telemetry", async () => {
"react-annotate": false,
"meta-framework": "none",
"application-key-set": false,
"bundler-major-version": "2",
bundler: "rollup",
}),
sdk: expect.objectContaining({
Expand Down
1 change: 1 addition & 0 deletions packages/integration-tests/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
transformIgnorePatterns: ["!node_modules/"],
};
16 changes: 16 additions & 0 deletions packages/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createComponentNameAnnotateHooks,
Logger,
} from "@sentry/bundler-plugin-core";
import { createRequire } from "node:module";
import type { UnpluginOptions } from "unplugin";

function rollupComponentNameAnnotatePlugin(
Expand Down Expand Up @@ -48,11 +49,26 @@ function rollupBundleSizeOptimizationsPlugin(
};
}

function getRollupMajorVersion(): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Rollup already transpiles this for us
const req = createRequire(import.meta.url);
const rollup = req("rollup") as { VERSION?: string };
return rollup.VERSION?.split(".")[0];
} catch (err) {
// do nothing, we'll just not report a version
}

return undefined;
}

const sentryUnplugin = sentryUnpluginFactory({
injectionPlugin: rollupInjectionPlugin,
componentNameAnnotatePlugin: rollupComponentNameAnnotatePlugin,
debugIdUploadPlugin: rollupDebugIdUploadPlugin,
bundleSizeOptimizationsPlugin: rollupBundleSizeOptimizationsPlugin,
getBundlerMajorVersion: getRollupMajorVersion,
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
16 changes: 16 additions & 0 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createComponentNameAnnotateHooks,
Logger,
} from "@sentry/bundler-plugin-core";
import { createRequire } from "node:module";
import { UnpluginOptions, VitePlugin } from "unplugin";

function viteInjectionPlugin(injectionCode: CodeInjection, debugIds: boolean): UnpluginOptions {
Expand Down Expand Up @@ -52,11 +53,26 @@ function viteBundleSizeOptimizationsPlugin(
};
}

function getViteMajorVersion(): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Rollup already transpiles this for us
const req = createRequire(import.meta.url);
const vite = req("vite") as { version?: string };
return vite.version?.split(".")[0];
} catch (err) {
// do nothing, we'll just not report a version
}

return undefined;
}

const sentryUnplugin = sentryUnpluginFactory({
injectionPlugin: viteInjectionPlugin,
componentNameAnnotatePlugin: viteComponentNameAnnotatePlugin,
debugIdUploadPlugin: viteDebugIdUploadPlugin,
bundleSizeOptimizationsPlugin: viteBundleSizeOptimizationsPlugin,
getBundlerMajorVersion: getViteMajorVersion,
});

export const sentryVitePlugin = (options?: Options): VitePlugin[] => {
Expand Down
32 changes: 2 additions & 30 deletions packages/webpack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,14 @@ const BannerPlugin = webpack4or5?.BannerPlugin || webpack4or5?.default?.BannerPl

const DefinePlugin = webpack4or5?.DefinePlugin || webpack4or5?.default?.DefinePlugin;

// Detect webpack major version for telemetry (helps differentiate webpack 4 vs 5 usage)
function getWebpackMajorVersion(): string | undefined {
try {
const webpack = webpack4or5 as unknown as
| { version?: string; default?: { version?: string } }
| undefined;
const version = webpack?.version ?? webpack?.default?.version;
const webpackMajorVersion = version?.split(".")[0]; // "4" or "5"
return webpackMajorVersion;
} catch (error) {
return undefined;
}
}

const webpackMajorVersion = getWebpackMajorVersion();

const sentryUnplugin = sentryWebpackUnpluginFactory({
BannerPlugin,
DefinePlugin,
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any = (options) => {
const enhancedOptions: SentryWebpackPluginOptions = {
...options,
_metaOptions: {
...options?._metaOptions,
telemetry: {
...options?._metaOptions?.telemetry,
bundlerMajorVersion:
options?._metaOptions?.telemetry?.bundlerMajorVersion ?? webpackMajorVersion,
},
},
};
return sentryUnplugin.webpack(enhancedOptions);
};
export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any =
sentryUnplugin.webpack;

export { sentryCliBinaryExists } from "@sentry/bundler-plugin-core";

Expand Down
17 changes: 17 additions & 0 deletions packages/webpack-plugin/src/webpack4and5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CodeInjection,
getDebugIdSnippet,
} from "@sentry/bundler-plugin-core";
import { createRequire } from "node:module";
import * as path from "path";
import { UnpluginOptions } from "unplugin";
import { v4 as uuidv4 } from "uuid";
Expand Down Expand Up @@ -150,6 +151,21 @@ function webpackDebugIdUploadPlugin(
};
}

// Detect webpack major version for telemetry (helps differentiate webpack 4 vs 5 usage)
function getWebpackMajorVersion(): string | undefined {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Rollup already transpiles this for us
const req = createRequire(import.meta.url);
const webpack = req("webpack") as { version?: string; default?: { version?: string } };
const version = webpack?.version ?? webpack?.default?.version;
const webpackMajorVersion = version?.split(".")[0]; // "4" or "5"
return webpackMajorVersion;
} catch (error) {
return undefined;
}
}

/**
* The factory function accepts BannerPlugin and DefinePlugin classes in
* order to avoid direct dependencies on webpack.
Expand All @@ -170,6 +186,7 @@ export function sentryWebpackUnpluginFactory({
componentNameAnnotatePlugin: webpackComponentNameAnnotatePlugin(),
debugIdUploadPlugin: webpackDebugIdUploadPlugin,
bundleSizeOptimizationsPlugin: webpackBundleSizeOptimizationsPlugin(DefinePlugin),
getBundlerMajorVersion: getWebpackMajorVersion,
});
}

Expand Down
Loading