Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscelaneous updates #132

Merged
merged 6 commits into from
Feb 12, 2025
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions LICENSES-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Component,Origin,Licence,Copyright
@rollup/plugin-commonjs,virtual,MIT,Rich Harris (https://github.com/rollup/plugins/tree/master/packages/commonjs/#readme)
@rollup/plugin-json,virtual,MIT,rollup (https://github.com/rollup/plugins/tree/master/packages/json#readme)
@rollup/plugin-node-resolve,virtual,MIT,Rich Harris (https://github.com/rollup/plugins/tree/master/packages/node-resolve/#readme)
@rollup/plugin-terser,virtual,MIT,Peter Placzek (https://github.com/rollup/plugins/tree/master/packages/terser#readme)
@rollup/pluginutils,virtual,MIT,Rich Harris (https://github.com/rollup/plugins/tree/master/packages/pluginutils#readme)
@rollup/rollup-darwin-arm64,npm,MIT,Lukas Taegert-Atkinson (https://rollupjs.org/)
@rspack/binding,npm,MIT,(https://rspack.dev)
Expand Down Expand Up @@ -822,6 +823,7 @@ simple-git,npm,MIT,Steve King (https://www.npmjs.com/package/simple-git)
sisteransi,npm,MIT,Terkel Gjervig (https://terkel.com)
slash,npm,MIT,Sindre Sorhus (sindresorhus.com)
slice-ansi,npm,MIT,(https://www.npmjs.com/package/slice-ansi)
smob,npm,MIT,Peter Placzek (https://github.com/Tada5hi/smob#readme)
snapdragon,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon)
snapdragon-node,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon-node)
snapdragon-util,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon-util)
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ To interact with Datadog directly from your builds.
- [Usage](#usage)
- [Configuration](#configuration)
- [`auth.apiKey`](#authapikey)
- [`auth.appKey`](#authappkey)
- [`logLevel`](#loglevel)
- [`customPlugins`](#customplugins)
- [Features](#features)
Expand Down Expand Up @@ -128,6 +129,12 @@ Follow the specific documentation for each bundler:

In order to interact with Datadog, you have to use [your own API Key](https://app.datadoghq.com/organization-settings/api-keys).

### `auth.appKey`

> default `null`

In order to interact with Datadog, you have to use [your own Application Key](https://app.datadoghq.com/organization-settings/application-keys).

### `logLevel`

> default: `'warn'`
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const ERROR_CODES_NO_RETRY = [400, 403, 413];
export const NB_RETRIES = 5;
// Do a retriable fetch.
export const doRequest = <T>(opts: RequestOpts): Promise<T> => {
const { url, method = 'GET', getData, onRetry, type = 'text' } = opts;
const { auth, url, method = 'GET', getData, onRetry, type = 'text' } = opts;
return retry(
async (bail: (e: Error) => void, attempt: number) => {
let response: Response;
Expand All @@ -110,14 +110,24 @@ export const doRequest = <T>(opts: RequestOpts): Promise<T> => {
// https://github.com/nodejs/node/issues/46221
duplex: 'half',
};
let requestHeaders: RequestInit['headers'] = {};

// Do auth if present.
if (auth?.apiKey) {
requestHeaders['DD-API-KEY'] = auth.apiKey;
}

if (auth?.appKey) {
requestHeaders['DD-APPLICATION-KEY'] = auth.appKey;
}

if (typeof getData === 'function') {
const { data, headers } = await getData();
requestInit.body = data;
requestInit.headers = headers;
requestHeaders = { ...requestHeaders, ...headers };
}

response = await fetch(url, requestInit);
response = await fetch(url, { ...requestInit, headers: requestHeaders });
} catch (error: any) {
// We don't want to retry if there is a non-fetch related error.
bail(error);
Expand Down
13 changes: 11 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,16 @@ export type SerializedInput = Assign<Input, { dependencies: string[]; dependents
export type SerializedOutput = Assign<Output, { inputs: string[] }>;

export type BuildReport = {
bundler: Omit<BundlerReport, 'outDir' | 'rawConfig'>;
errors: string[];
warnings: string[];
logs: { pluginName: string; type: LogLevel; message: string; time: number }[];
logs: {
bundler: BundlerFullName;
pluginName: string;
type: LogLevel;
message: string;
time: number;
}[];
entries?: Entry[];
inputs?: Input[];
outputs?: Output[];
Expand Down Expand Up @@ -127,6 +134,7 @@ export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'none';

export type AuthOptions = {
apiKey?: string;
appKey?: string;
};

export interface BaseOptions {
Expand All @@ -149,9 +157,10 @@ export type OptionsWithDefaults = Assign<Options, GetPluginsOptions>;

export type PluginName = `datadog-${Lowercase<string>}-plugin`;

type Data = { data: BodyInit; headers?: Record<string, string> };
type Data = { data?: BodyInit; headers?: Record<string, string> };
export type RequestOpts = {
url: string;
auth?: AuthOptions;
method?: string;
getData?: () => Promise<Data> | Data;
type?: 'json' | 'text';
Expand Down
24 changes: 17 additions & 7 deletions packages/factory/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const logPriority: Record<LogLevel, number> = {
export const getLoggerFactory =
(build: BuildReport, logLevel: LogLevel = 'warn'): GetLogger =>
(name) => {
const cleanName = name.replace(/(^datadog-|-plugin$)/g, '');
const log = (text: any, type: LogLevel = 'debug') => {
// By default (debug) we print dimmed.
let color = c.dim;
Expand All @@ -45,11 +46,17 @@ export const getLoggerFactory =
logFn = console.log;
}

const prefix = `[${type}|${name}]`;
const prefix = `[${type}|${build.bundler.fullName}|${cleanName}]`;

// Keep a trace of the log in the build report.
const content = typeof text === 'string' ? text : JSON.stringify(text, null, 2);
build.logs.push({ pluginName: name, type, message: content, time: Date.now() });
build.logs.push({
bundler: build.bundler.fullName,
pluginName: cleanName,
type,
message: content,
time: Date.now(),
});
if (type === 'error') {
build.errors.push(content);
}
Expand All @@ -66,7 +73,7 @@ export const getLoggerFactory =
return {
getLogger: (subName: string) => {
const logger = getLoggerFactory(build, logLevel);
return logger(`${name}:${subName}`);
return logger(`${cleanName}:${subName}`);
},
error: (text: any) => log(text, 'error'),
warn: (text: any) => log(text, 'warn'),
Expand Down Expand Up @@ -94,17 +101,20 @@ export const getContext = ({
errors: [],
warnings: [],
logs: [],
bundler: {
name: bundlerName,
fullName: `${bundlerName}${variant}` as BundlerFullName,
variant,
version: bundlerVersion,
},
};
const context: GlobalContext = {
auth: options.auth,
pluginNames: [],
bundler: {
name: bundlerName,
fullName: `${bundlerName}${variant}` as BundlerFullName,
variant,
...build.bundler,
// This will be updated in the bundler-report plugin once we have the configuration.
outDir: cwd,
version: bundlerVersion,
},
build,
// This will be updated in the bundler-report plugin once we have the configuration.
Expand Down
2 changes: 2 additions & 0 deletions packages/plugins/build-report/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const serializeBuildReport = (report: BuildReport): SerializedBuildReport
// To make it JSON serializable, we need to remove the self references
// and replace them with strings, we'll use "filepath" to still have them uniquely identifiable.
const jsonReport: SerializedBuildReport = {
bundler: report.bundler,
errors: report.errors,
warnings: report.warnings,
logs: report.logs,
Expand Down Expand Up @@ -103,6 +104,7 @@ export const serializeBuildReport = (report: BuildReport): SerializedBuildReport
// Mostly useful for debugging and testing.
export const unserializeBuildReport = (report: SerializedBuildReport): BuildReport => {
const buildReport: BuildReport = {
bundler: report.bundler,
errors: report.errors,
warnings: report.warnings,
logs: report.logs,
Expand Down
5 changes: 4 additions & 1 deletion packages/plugins/injection/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export const getContentToInject = (contentToInject: Map<string, string>) => {
return '';
}

const stringToInject = Array.from(contentToInject.values()).join('\n\n');
const stringToInject = Array.from(contentToInject.values())
// Wrapping it in order to avoid variable name collisions.
.map((content) => `(() => {${content}})();`)
.join('\n\n');
return `${BEFORE_INJECTION}\n${stringToInject}\n${AFTER_INJECTION}`;
};

Expand Down
1 change: 1 addition & 0 deletions packages/published/esbuild-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.4",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"esbuild": "0.24.0",
Expand Down
19 changes: 12 additions & 7 deletions packages/published/esbuild-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
// will be updated using the 'yarn cli integrity' command.

import type { Options } from '@dd/core/types';
import type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
import * as factory from '@dd/factory';
import esbuild from 'esbuild';

import pkg from '../package.json';

export const datadogEsbuildPlugin = factory.buildPluginFactory({
bundler: esbuild,
version: pkg.version,
}).esbuild;

export type EsbuildPluginOptions = Options;

export type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
};

export const datadogEsbuildPlugin = factory.buildPluginFactory({
bundler: esbuild,
version: pkg.version,
}).esbuild;

export const version = pkg.version;
export const helpers = factory.helpers;
1 change: 1 addition & 0 deletions packages/published/rollup-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.4",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"esbuild": "0.24.0",
Expand Down
19 changes: 12 additions & 7 deletions packages/published/rollup-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
// will be updated using the 'yarn cli integrity' command.

import type { Options } from '@dd/core/types';
import type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
import * as factory from '@dd/factory';
import rollup from 'rollup';

import pkg from '../package.json';

export const datadogRollupPlugin = factory.buildPluginFactory({
bundler: rollup,
version: pkg.version,
}).rollup;

export type RollupPluginOptions = Options;

export type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
};

export const datadogRollupPlugin = factory.buildPluginFactory({
bundler: rollup,
version: pkg.version,
}).rollup;

export const version = pkg.version;
export const helpers = factory.helpers;
1 change: 1 addition & 0 deletions packages/published/rspack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.4",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"esbuild": "0.24.0",
Expand Down
19 changes: 12 additions & 7 deletions packages/published/rspack-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
// will be updated using the 'yarn cli integrity' command.

import type { Options } from '@dd/core/types';
import type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
import * as factory from '@dd/factory';
import rspack from '@rspack/core';

import pkg from '../package.json';

export const datadogRspackPlugin = factory.buildPluginFactory({
bundler: rspack,
version: pkg.version,
}).rspack;

export type RspackPluginOptions = Options;

export type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
};

export const datadogRspackPlugin = factory.buildPluginFactory({
bundler: rspack,
version: pkg.version,
}).rspack;

export const version = pkg.version;
export const helpers = factory.helpers;
1 change: 1 addition & 0 deletions packages/published/vite-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.4",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"esbuild": "0.24.0",
Expand Down
19 changes: 12 additions & 7 deletions packages/published/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
// will be updated using the 'yarn cli integrity' command.

import type { Options } from '@dd/core/types';
import type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
import * as factory from '@dd/factory';
import vite from 'vite';

import pkg from '../package.json';

export const datadogVitePlugin = factory.buildPluginFactory({
bundler: vite,
version: pkg.version,
}).vite;

export type VitePluginOptions = Options;

export type {
// #types-export-injection-marker
ErrorTrackingTypes,
TelemetryTypes,
// #types-export-injection-marker
} from '@dd/factory';
};

export const datadogVitePlugin = factory.buildPluginFactory({
bundler: vite,
version: pkg.version,
}).vite;

export const version = pkg.version;
export const helpers = factory.helpers;
1 change: 1 addition & 0 deletions packages/published/webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-terser": "0.4.4",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"esbuild": "0.24.0",
Expand Down
Loading