Skip to content

Commit

Permalink
Add RUM Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Jun 7, 2024
1 parent 5c8c419 commit 64d26cf
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
# Telemetry
packages/plugins/telemetry @DataDog/frontend-devx @yoannmoinet
packages/tests/src/plugins/telemetry @DataDog/frontend-devx @yoannmoinet

# Rum
packages/plugins/rum @DataDog/rum @yoannmoinet
packages/tests/src/plugins/rum @DataDog/rum @yoannmoinet
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A set of plugins to interact with Datadog directly from your builds.

<!-- #toc -->
- [Plugins](#plugins)
- [`rum` RUM Plugin](#rum-rum-plugin)
- [`telemetry` Telemetry Plugin](#telemetry-telemetry-plugin)
- [Configuration](#configuration)
- [`auth.apiKey`](#authapikey)
Expand All @@ -32,6 +33,12 @@ A set of plugins to interact with Datadog directly from your builds.
## Plugins

<!-- #list-of-packages -->
### `rum` RUM Plugin

> Interact with our Real User Monitoring product (RUM) in Datadog directly from your build system.
<kbd>[📝 Full documentation ➡️](./packages/plugins/rum#readme)</kbd>

### `telemetry` Telemetry Plugin

> Display and send telemetry data as metrics to Datadog.
Expand All @@ -52,6 +59,9 @@ A set of plugins to interact with Datadog directly from your builds.
endPoint?: string;
};
logLevel?: 'debug' | 'warn' | 'error' | 'none';
rum?: {
disabled?: boolean;
};
telemetry?: {
disabled?: boolean;
output?: boolean
Expand Down
21 changes: 21 additions & 0 deletions packages/plugins/rum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# RUM Plugin <!-- #omit in toc -->

Interact with our Real User Monitoring product (RUM) in Datadog directly from your build system.

<!-- The title and the following line will both be added to the root README.md -->

## Table of content <!-- #omit in toc -->

<!-- This is auto generated with yarn cli integrity -->

<!-- #toc -->
- [Configuration](#configuration)
<!-- #toc -->

## Configuration

```ts
rum?: {
disabled?: boolean;
}
```
26 changes: 26 additions & 0 deletions packages/plugins/rum/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@dd/rum-plugins",
"packageManager": "yarn@4.0.2",
"license": "MIT",
"private": true,
"author": "Datadog",
"description": "Interact with our Real User Monitoring product (RUM) in Datadog directly from your build system.",
"homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/rum#readme",
"repository": {
"type": "git",
"url": "https://github.com/DataDog/build-plugins",
"directory": "packages/plugins/rum"
},
"exports": {
".": "./src/index.ts",
"./*": "./src/*.ts"
},
"scripts": {
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@dd/core": "workspace:*",
"chalk": "2.3.1",
"unplugin": "1.10.1"
}
}
8 changes: 8 additions & 0 deletions packages/plugins/rum/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { PluginName } from '@dd/core/types';

export const CONFIG_KEY = 'rum' as const;
export const PLUGIN_NAME: PluginName = 'datadog-rum-plugin' as const;
69 changes: 69 additions & 0 deletions packages/plugins/rum/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { getLogFn } from '@dd/core/log';
import type { Context } from '@dd/core/plugins';
import type { GetPlugins } from '@dd/core/types';
import chalk from 'chalk';

import { PLUGIN_NAME, CONFIG_KEY } from './constants';
import type { OptionsWithRumEnabled, RumOptions, RumOptionsEnabled } from './types';

export { CONFIG_KEY, PLUGIN_NAME } from './constants';

export const helpers = {
// Add the helpers you'd like to expose here.
};

export type types = {
// Add the types you'd like to expose here.
RumOptions: RumOptions;
OptionsWithRumEnabled: OptionsWithRumEnabled;
};

// Deal with validation and defaults here.
export const validateOptions = (config: Partial<OptionsWithRumEnabled>): RumOptionsEnabled => {
const red = chalk.bold.red;
const errors = [];
const validatedOptions: RumOptionsEnabled = config[CONFIG_KEY] || {};

if (validatedOptions.sourcemaps) {
if (!validatedOptions.sourcemaps.basePath) {
errors.push(`${red('sourcemaps.basePath')} is required.`);
}
if (!validatedOptions.sourcemaps.releaseVersion) {
errors.push(`${red('sourcemaps.releaseVersion')} is required.`);
}
}

if (errors.length) {
throw new Error(`Invalid configuration for ${PLUGIN_NAME}:\n ${errors.join('\n ')}`);
}

return validatedOptions;
};

export const getPlugins: GetPlugins<OptionsWithRumEnabled> = (
opts: OptionsWithRumEnabled,
context: Context,
) => {
return [
{
name: PLUGIN_NAME,
async writeBundle() {
const rumOptions = opts[CONFIG_KEY];
if (rumOptions.disabled || !rumOptions.sourcemaps) {
return;
}
const log = getLogFn(opts.logLevel, PLUGIN_NAME);
log(`Uploading sourcemaps.`);

// Verify configuration.
// Gather the sourcemaps files using glob.
// Build payloads (with git info from context plugin).
// Upload the sourcemaps.
},
},
];
};
26 changes: 26 additions & 0 deletions packages/plugins/rum/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { GetPluginsOptionsWithCWD } from '@dd/core/types';

import type { CONFIG_KEY } from './constants';

export type RumOptions = {
disabled?: boolean;
sourcemaps?: {
basePath: string;
dryRun?: boolean;
maxConcurrency?: number;
minifiedPathPrefix?: string;
releaseVersion: string;
};
};

export interface RumOptionsEnabled extends RumOptions {
disabled?: false;
}

export interface OptionsWithRumEnabled extends GetPluginsOptionsWithCWD {
[CONFIG_KEY]: RumOptionsEnabled;
}
10 changes: 10 additions & 0 deletions packages/plugins/rum/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "./",
"outDir": "./dist"
},
"include": ["**/*"],
"exclude": ["dist", "node_modules"]
}
51 changes: 51 additions & 0 deletions packages/tests/src/plugins/rum/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import fs from 'fs';
import path from 'path';

import { runBundlers } from '../../helpers';

// Mock log function.
const logFn = jest.fn((originalLogFn, ...args: any[]) => {
const [text, ...rest] = args;
originalLogFn(`[MOCKED] ${text}`, ...rest);
});

jest.mock('@dd/core/log', () => {
return {
getLogFn: (...args: any[]) => {
const { getLogFn: original } = jest.requireActual('@dd/core/log');
return (...argsLogs: any[]) => {
logFn(original(...args), ...argsLogs);
};
},
};
});

// Some paths.
const entry = path.resolve(__dirname, './fixtures/index.js');
const destination = path.resolve(__dirname, './fixtures/dist');

describe('RUM Plugin', () => {
afterAll(() => {
// Clean files
fs.rmSync(destination, {
recursive: true,
force: true,
});
});

test('It should log at the end of the build', async () => {
// Build everything.
const results = await runBundlers({ entry, destination }, { rum: { sourcemaps: {} } });

// Assert logs.
expect(logFn).toHaveBeenNthCalledWith(
results.length,
expect.any(Function),
expect.any(String),
);
});
});

0 comments on commit 64d26cf

Please sign in to comment.