Skip to content

Commit ecceaf7

Browse files
committed
feat:add sourcemaps and telemetry
1 parent f089133 commit ecceaf7

31 files changed

Lines changed: 422 additions & 87 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
80.5 KB
Binary file not shown.

packages/plugins/rum/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"typecheck": "tsc --noEmit"
3232
},
3333
"dependencies": {
34-
"@datadog/js-instrumentation-wasm": "0.9.4",
34+
"@datadog/js-instrumentation-wasm": "1.0.0",
3535
"@dd/core": "workspace:*",
3636
"@rollup/pluginutils": "5.1.4",
3737
"chalk": "2.3.1"

packages/plugins/rum/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const getPlugins: GetPlugins = ({ options, context }) => {
5757

5858
if (validatedOptions.privacy) {
5959
// Add the privacy plugin.
60-
const privacyPlugin = getPrivacyPlugin(validatedOptions.privacy);
60+
const privacyPlugin = getPrivacyPlugin(validatedOptions.privacy, context);
6161
if (privacyPlugin) {
6262
plugins.push(privacyPlugin);
6363
}

packages/plugins/rum/src/privacy/index.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright 2019-Present Datadog, Inc.
44

55
import { instrument } from '@datadog/js-instrumentation-wasm';
6-
import type { PluginOptions } from '@dd/core/types';
6+
import type { GlobalContext, PluginOptions } from '@dd/core/types';
77
import { createFilter } from '@rollup/pluginutils';
88
import fs from 'node:fs';
99
import path from 'node:path';
@@ -12,20 +12,18 @@ import { PRIVACY_HELPERS_MODULE_ID, PLUGIN_NAME } from './constants';
1212
import { buildTransformOptions } from './transform';
1313
import type { PrivacyOptions } from './types';
1414

15-
export const getPrivacyPlugin = (pluginOptions: PrivacyOptions): PluginOptions | undefined => {
15+
export const getPrivacyPlugin = (
16+
pluginOptions: PrivacyOptions,
17+
context: GlobalContext,
18+
): PluginOptions | undefined => {
19+
const log = context.getLogger(PLUGIN_NAME);
20+
1621
if (pluginOptions.disabled) {
1722
return;
1823
}
1924

2025
const transformOptions = buildTransformOptions(pluginOptions);
2126
const transformFilter = createFilter(pluginOptions.include, pluginOptions.exclude);
22-
23-
// Read the privacy helpers code
24-
const privacyHelpersPath = path.join(
25-
__dirname,
26-
pluginOptions.module === 'cjs' ? './privacy-helpers.js' : './privacy-helpers.mjs',
27-
);
28-
2927
return {
3028
name: PLUGIN_NAME,
3129
// Enforce when the plugin will be executed.
@@ -35,15 +33,33 @@ export const getPrivacyPlugin = (pluginOptions: PrivacyOptions): PluginOptions |
3533
// webpack's id filter is outside of loader logic,
3634
// an additional hook is needed for better perf on webpack
3735
async resolveId(source) {
38-
if (source === PRIVACY_HELPERS_MODULE_ID) {
39-
return { id: PRIVACY_HELPERS_MODULE_ID };
36+
if (source.includes(PRIVACY_HELPERS_MODULE_ID)) {
37+
return { id: source };
4038
}
4139
return null;
4240
},
4341

4442
async load(id) {
45-
if (id === PRIVACY_HELPERS_MODULE_ID) {
46-
return { code: fs.readFileSync(privacyHelpersPath, 'utf8') };
43+
let privacyHelpersPath: string;
44+
if (id.includes(PRIVACY_HELPERS_MODULE_ID)) {
45+
if (id.endsWith('.cjs')) {
46+
privacyHelpersPath = path.join(__dirname, 'privacy-helpers.js');
47+
} else {
48+
privacyHelpersPath = path.join(__dirname, 'privacy-helpers.mjs');
49+
}
50+
const code = fs.readFileSync(privacyHelpersPath, 'utf8');
51+
if (context.bundler.name === 'rollup') {
52+
// prepend AAAA to sourcemap
53+
const sourcemap = {
54+
version: 3,
55+
sources: [privacyHelpersPath],
56+
sourcesContent: [code],
57+
names: [],
58+
mappings: Array(code.split('\n').length).fill('AAAA').join(';'),
59+
};
60+
return { code, map: sourcemap };
61+
}
62+
return { code, map: null };
4763
}
4864
return null;
4965
},
@@ -53,7 +69,25 @@ export const getPrivacyPlugin = (pluginOptions: PrivacyOptions): PluginOptions |
5369
return transformFilter(id);
5470
},
5571
async transform(code, id) {
56-
return instrument({ id, code }, transformOptions);
72+
try {
73+
if (
74+
context.bundler.name === 'esbuild' ||
75+
context.bundler.name === 'webpack' ||
76+
context.bundler.name === 'rspack'
77+
) {
78+
transformOptions.output = {
79+
...transformOptions.output,
80+
inlineSourceMap: false,
81+
embedCodeInSourceMap: true,
82+
};
83+
}
84+
return instrument({ id, code }, transformOptions);
85+
} catch (e) {
86+
log.error(`Instrumentation Error: ${e}`);
87+
return {
88+
code,
89+
};
90+
}
5791
},
5892
};
5993
};

packages/plugins/rum/src/privacy/transform.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ export function buildTransformOptions(pluginOptions: PrivacyOptions): Instrument
2222
privacy: {
2323
addToDictionaryHelper: {
2424
import: {
25-
module: PRIVACY_HELPERS_MODULE_ID,
26-
func: '$',
25+
cjsModule: pluginOptions.helpersModule ?? `${PRIVACY_HELPERS_MODULE_ID}.cjs`,
26+
esmModule: pluginOptions.helpersModule ?? `${PRIVACY_HELPERS_MODULE_ID}.mjs`,
27+
func: pluginOptions.globalFunc ?? '$',
2728
},
2829
},
2930
},

packages/plugins/rum/src/privacy/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import type { Assign } from '@dd/core/types';
77
export interface PrivacyOptions {
88
exclude?: RegExp[] | string[];
99
include?: RegExp[] | string[];
10+
helpersModule?: string;
11+
globalFunc?: string;
1012
module?: 'cjs' | 'esm';
1113
jsx?: boolean;
1214
transformStrategy?: 'ast';
@@ -16,5 +18,5 @@ export interface PrivacyOptions {
1618

1719
export type PrivacyOptionsWithDefaults = Assign<
1820
PrivacyOptions,
19-
Pick<Required<PrivacyOptions>, 'exclude' | 'include' | 'module' | 'transformStrategy'>
21+
Pick<Required<PrivacyOptions>, 'exclude' | 'include' | 'transformStrategy'>
2022
>;

0 commit comments

Comments
 (0)