Skip to content

[feat] Add synthetics plugin #153

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

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ packages/plugins/analytics @yoannmoin

# Custom Hooks
packages/plugins/custom-hooks @yoannmoinet

# Synthetics
packages/plugins/synthetics @yoannmoinet @DataDog/synthetics-ct
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ To interact with Datadog directly from your builds.
- [`customPlugins`](#customplugins)
- [Features](#features)
- [Error Tracking](#error-tracking-----)
- [Synthetics](#synthetics-----)
- [Telemetry](#telemetry-----)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -103,6 +104,9 @@ Follow the specific documentation for each bundler:
service: string;
};
};
synthetics?: {
disabled?: boolean;
};
telemetry?: {
disabled?: boolean;
enableTracing?: boolean;
Expand Down Expand Up @@ -246,6 +250,26 @@ datadogWebpackPlugin({

</details>

### Synthetics <img src="packages/assets/src/esbuild.svg" alt="ESBuild" width="17" /> <img src="packages/assets/src/rollup.svg" alt="Rollup" width="17" /> <img src="packages/assets/src/rspack.svg" alt="Rspack" width="17" /> <img src="packages/assets/src/vite.svg" alt="Vite" width="17" /> <img src="packages/assets/src/webpack.svg" alt="Webpack" width="17" />

> Interact with Synthetics at build time.

#### [📝 Full documentation ➡️](/packages/plugins/synthetics#readme)

<details>

<summary>Configuration</summary>

```typescript
datadogWebpackPlugin({
synthetics?: {
disabled?: boolean,
}
});
```

</details>

### Telemetry <img src="packages/assets/src/esbuild.svg" alt="ESBuild" width="17" /> <img src="packages/assets/src/rollup.svg" alt="Rollup" width="17" /> <img src="packages/assets/src/rspack.svg" alt="Rspack" width="17" /> <img src="packages/assets/src/vite.svg" alt="Vite" width="17" /> <img src="packages/assets/src/webpack.svg" alt="Webpack" width="17" />

> Display and send telemetry data as metrics to Datadog.
Expand Down
10 changes: 10 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ declare global {
* For instance, we only submit logs to Datadog when the environment is `production`.
*/
BUILD_PLUGINS_ENV?: Env;
/**
* The port of the dev server of our synthetics plugin.
*
* This is only used by datadog-ci, in its build-and-test command,
* using the customer's build command, if it includes our plugin,
* will launch a dev server to serve the outdir of the build so datadog-ci
* can trigger a CI batch over the branch's code using a tunnel.
*
*/
BUILD_PLUGINS_S8S_PORT?: string;
/**
* Defined in github actions when running in CI.
*/
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/helpers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type File = {
content: string;
};
type RouteVerb = 'get' | 'post' | 'put' | 'patch' | 'delete';
type Routes = Record<
export type Routes = Record<
string,
Partial<
Record<
Expand All @@ -33,13 +33,13 @@ type Routes = Record<
>
>
>;
type Response = {
export type Response = {
statusCode: number;
headers: Record<string, string>;
body: string;
error?: Error;
};
type RunServerOptions = {
export type RunServerOptions = {
port: number;
root: string;
routes?: Routes;
Expand Down Expand Up @@ -76,8 +76,8 @@ export const prepareFile = async (root: string, requestUrl: string): Promise<Fil
return { found, ext, content: fileContent };
};

export const runServer = ({ port, root, middleware, routes }: RunServerOptions) => {
const server = http.createServer(async (req, res) => {
export const getServer = ({ root, middleware, routes }: Omit<RunServerOptions, 'port'>) => {
return http.createServer(async (req, res) => {
const response: Response = {
statusCode: 200,
headers: {},
Expand Down Expand Up @@ -125,7 +125,10 @@ export const runServer = ({ port, root, middleware, routes }: RunServerOptions)
res.writeHead(response.statusCode, response.headers);
res.end(response.body);
});
};

export const runServer = ({ port, root, middleware, routes }: RunServerOptions) => {
const server = getServer({ root, middleware, routes });
server.listen(port);
return server;
};
8 changes: 7 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type { ErrorTrackingOptions } from '@dd/error-tracking-plugin/types';
import type * as errorTracking from '@dd/error-tracking-plugin';
import type { RumOptions } from '@dd/rum-plugin/types';
import type * as rum from '@dd/rum-plugin';
import type { SyntheticsOptions } from '@dd/synthetics-plugin/types';
import type * as synthetics from '@dd/synthetics-plugin';
import type { TelemetryOptions } from '@dd/telemetry-plugin/types';
import type * as telemetry from '@dd/telemetry-plugin';
// #imports-injection-marker
Expand All @@ -22,8 +24,11 @@ import type { UnpluginOptions } from 'unplugin';

import type { ALL_ENVS, FULL_NAME_BUNDLERS, SUPPORTED_BUNDLERS } from './constants';

// Re-assign B into A.
export type Assign<A, B> = Omit<A, keyof B> & B;
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
// Type object with specified keys required.
export type Ensure<T, K extends keyof T> = Assign<T, Required<Pick<T, K>>>;
// Target one item from an iterable.
export type IterableElement<IterableType extends Iterable<unknown>> =
IterableType extends Iterable<infer ElementType> ? ElementType : never;

Expand Down Expand Up @@ -197,6 +202,7 @@ export interface Options extends BaseOptions {
// #types-injection-marker
[errorTracking.CONFIG_KEY]?: ErrorTrackingOptions;
[rum.CONFIG_KEY]?: RumOptions;
[synthetics.CONFIG_KEY]?: SyntheticsOptions;
[telemetry.CONFIG_KEY]?: TelemetryOptions;
// #types-injection-marker
customPlugins?: GetCustomPlugins;
Expand Down
1 change: 1 addition & 0 deletions packages/factory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@dd/internal-git-plugin": "workspace:*",
"@dd/internal-injection-plugin": "workspace:*",
"@dd/rum-plugin": "workspace:*",
"@dd/synthetics-plugin": "workspace:*",
"@dd/telemetry-plugin": "workspace:*",
"chalk": "2.3.1",
"unplugin": "1.16.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/factory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { getContext, validateOptions } from './helpers';
// #imports-injection-marker
import * as errorTracking from '@dd/error-tracking-plugin';
import * as rum from '@dd/rum-plugin';
import * as synthetics from '@dd/synthetics-plugin';
import * as telemetry from '@dd/telemetry-plugin';
import { getAnalyticsPlugins } from '@dd/internal-analytics-plugin';
import { getBuildReportPlugins } from '@dd/internal-build-report-plugin';
Expand All @@ -39,6 +40,7 @@ import { getInjectionPlugins } from '@dd/internal-injection-plugin';
// #types-export-injection-marker
export type { types as ErrorTrackingTypes } from '@dd/error-tracking-plugin';
export type { types as RumTypes } from '@dd/rum-plugin';
export type { types as SyntheticsTypes } from '@dd/synthetics-plugin';
export type { types as TelemetryTypes } from '@dd/telemetry-plugin';
// #types-export-injection-marker

Expand Down Expand Up @@ -102,6 +104,7 @@ export const buildPluginFactory = ({
// #configs-injection-marker
errorTracking,
rum,
synthetics,
telemetry,
// #configs-injection-marker
];
Expand Down
27 changes: 27 additions & 0 deletions packages/plugins/synthetics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Synthetics Plugin <!-- #omit in toc -->

Interact with Synthetics at build time.

<!-- 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)
- [Build and test](#build-and-test)
<!-- #toc -->

## Configuration

```ts
synthetics?: {
disabled?: boolean;
}
```

## Build and test

Using [`datadog-ci`'s `synthetics build-and-test` command](https://github.com/DataDog/datadog-ci/tree/master/src/commands/synthetics#run-tests-command),
you can have the build spin a dev server to serve the outdir of the build in order [to trigger a CI batch](https://docs.datadoghq.com/continuous_testing/) over the branch's code.
26 changes: 26 additions & 0 deletions packages/plugins/synthetics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@dd/synthetics-plugin",
"packageManager": "yarn@4.0.2",
"license": "MIT",
"private": true,
"author": "Datadog",
"description": "Interact with Synthetics at build time.",
"homepage": "https://github.com/DataDog/build-plugins/tree/main/packages/plugins/synthetics#readme",
"repository": {
"type": "git",
"url": "https://github.com/DataDog/build-plugins",
"directory": "packages/plugins/synthetics"
},
"exports": {
".": "./src/index.ts",
"./*": "./src/*.ts"
},
"scripts": {
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@dd/core": "workspace:*",
"@dd/error-tracking-plugin": "workspace:*",
"chalk": "2.3.1"
}
}
10 changes: 10 additions & 0 deletions packages/plugins/synthetics/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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 = 'synthetics' as const;
export const PLUGIN_NAME: PluginName = 'datadog-synthetics-plugin' as const;

export const API_PREFIX = '_datadog-ci_' as const;
Loading
Loading