Skip to content

Commit

Permalink
Add intake override and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Jun 24, 2024
1 parent 263640f commit 19e3862
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
4 changes: 4 additions & 0 deletions packages/plugins/rum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ rum?: {
sourcemaps?: {
basePath: string;
dryRun?: boolean;
intakeUrl?: string;
maxConcurrency?: number;
minifiedPathPrefix?: string;
releaseVersion: string;
service: string;
};
}
```

> [!NOTE]
> You can also override the intake URL by setting the `DATADOG_SOURCEMAP_INTAKE_URL` environment variable.
8 changes: 1 addition & 7 deletions packages/plugins/rum/src/sourcemaps/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ import { getPayload } from './payload';
const errorCodesNoRetry = [400, 403, 413];
const nbRetries = 5;

export const getApiUrl = (context: GlobalContext) => {
const siteUrl = context.auth?.endPoint || 'datadoghq.com';
return `https://sourcemap-intake.${siteUrl}/api/v2/srcmap`;
};

export const doRequest = async (
url: string,
data: Gzip,
Expand Down Expand Up @@ -91,7 +86,6 @@ export const upload = async (
return;
}

const url = getApiUrl(context);
const queue = new PQueue({ concurrency: options.maxConcurrency });
const gz = createGzip();
const defaultHeaders = {
Expand Down Expand Up @@ -126,7 +120,7 @@ export const upload = async (

// eslint-disable-next-line no-await-in-loop
queue.add(async () => {
await doRequest(url, data, headers, (error: Error, attempt: number) => {
await doRequest(options.intakeUrl, data, headers, (error: Error, attempt: number) => {
log(
`Failed to upload sourcemaps: ${error.message}\nRetrying ${attempt}/${nbRetries}`,
'warn',
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/rum/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type RumSourcemapsOptions = {
// TODO: Compute this basePath directly from the bundler's configuration, using the CrossHelper Plugin.
basePath: string;
dryRun?: boolean;
intakeUrl?: string;
maxConcurrency?: number;
minifiedPathPrefix: MinifiedPathPrefix;
releaseVersion: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/plugins/rum/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ export const validateSourcemapsOptions = (
}
}

const defaultIntakeUrl = `https://sourcemap-intake.logs.${config.auth?.endPoint || 'datadoghq.com'}/v1/input`;

// Add the defaults.
const sourcemapsWithDefaults: RumSourcemapsOptionsWithDefaults = {
dryRun: false,
maxConcurrency: 20,
intakeUrl:
process.env.DATADOG_SOURCEMAP_INTAKE_URL ||
validatedOptions.sourcemaps.intakeUrl ||
defaultIntakeUrl,
...validatedOptions.sourcemaps,
};

Expand Down
24 changes: 1 addition & 23 deletions packages/tests/src/plugins/rum/sourcemaps/sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { GlobalContext } from '@dd/core/types';
import { doRequest, getApiUrl, sendSourcemaps } from '@dd/rum-plugins/sourcemaps/sender';
import { doRequest, sendSourcemaps } from '@dd/rum-plugins/sourcemaps/sender';
import { getContextMock, getFetchMock } from '@dd/tests/helpers';
import { vol } from 'memfs';
import { createGzip } from 'zlib';
Expand Down Expand Up @@ -84,27 +83,6 @@ describe('RUM Plugin Sourcemaps', () => {
});
});

describe('getApiUrl', () => {
test.each([
{ domain: undefined, expected: 'https://sourcemap-intake.datadoghq.com/api/v2/srcmap' },
{
domain: 'other-domain.com',
expected: 'https://sourcemap-intake.other-domain.com/api/v2/srcmap',
},
])('It should return $expected for domain "$domain"', ({ domain, expected }) => {
const context: Partial<GlobalContext> = {};
if (domain) {
context.auth = {
apiKey: '123456',
endPoint: domain,
};
}

const apiUrl = getApiUrl(getContextMock(context));
expect(apiUrl).toMatch(expected);
});
});

describe('doRequest', () => {
const gz = createGzip();

Expand Down
13 changes: 13 additions & 0 deletions packages/tests/src/plugins/rum/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ import type {
} from '@dd/rum-plugins/types';
import { defaultDestination } from '@dd/tests/helpers';

export const getMinimalSourcemapsConfiguration = (
options: Partial<RumSourcemapsOptions> = {},
): RumSourcemapsOptions => {
return {
basePath: defaultDestination,
minifiedPathPrefix: '/prefix',
releaseVersion: '1.0.0',
service: 'rum-build-plugin-sourcemaps',
...options,
};
};

export const getSourcemapsConfiguration = (
options: Partial<RumSourcemapsOptions> = {},
): RumSourcemapsOptionsWithDefaults => {
return {
basePath: defaultDestination,
dryRun: false,
maxConcurrency: 10,
intakeUrl: 'https://example.com',
minifiedPathPrefix: '/prefix',
releaseVersion: '1.0.0',
service: 'rum-build-plugin-sourcemaps',
Expand Down
60 changes: 47 additions & 13 deletions packages/tests/src/plugins/rum/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// 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 { RumSourcemapsOptions } from '@dd/rum-plugins/types';
import { validateOptions, validateSourcemapsOptions } from '@dd/rum-plugins/validate';
import stripAnsi from 'strip-ansi';

import { getSourcemapsConfiguration } from './testHelpers';
import { getMinimalSourcemapsConfiguration } from './testHelpers';

describe('RUM Plugins validate', () => {
describe('validateOptions', () => {
Expand Down Expand Up @@ -58,32 +57,32 @@ describe('RUM Plugins validate', () => {
});

test('It should return the validated configuration with defaults', () => {
const configObject: RumSourcemapsOptions = {
basePath: 'src',
minifiedPathPrefix: '/path/to/minified',
releaseVersion: '1.0.0',
service: 'service',
};

const { config, errors } = validateSourcemapsOptions({
rum: {
sourcemaps: {
basePath: 'src',
releaseVersion: '1.0.0',
service: 'service',
minifiedPathPrefix: '/path/to/minified',
},
sourcemaps: getMinimalSourcemapsConfiguration(configObject),
},
});

expect(errors.length).toBe(0);
expect(config).toEqual({
basePath: 'src',
dryRun: false,
maxConcurrency: 20,
minifiedPathPrefix: '/path/to/minified',
releaseVersion: '1.0.0',
service: 'service',
intakeUrl: 'https://sourcemap-intake.logs.datadoghq.com/v1/input',
...configObject,
});
});

test('It should return an error with a bad minifiedPathPrefix', () => {
const { errors } = validateSourcemapsOptions({
rum: {
sourcemaps: getSourcemapsConfiguration({
sourcemaps: getMinimalSourcemapsConfiguration({
minifiedPathPrefix:
'bad-prefix' as RumSourcemapsOptions['minifiedPathPrefix'],
}),
Expand All @@ -95,5 +94,40 @@ describe('RUM Plugins validate', () => {
"sourcemaps.minifiedPathPrefix must be a valid URL or start with '/'.",
);
});

test('It should default to the expected intake url', () => {
const { config } = validateSourcemapsOptions({
rum: {
sourcemaps: getMinimalSourcemapsConfiguration(),
},
});

expect(config?.intakeUrl).toBe('https://sourcemap-intake.logs.datadoghq.com/v1/input');
});

test('It should use the provided configuration as the intake url', () => {
const { config } = validateSourcemapsOptions({
rum: {
sourcemaps: getMinimalSourcemapsConfiguration({
intakeUrl: 'https://example.com',
}),
},
});

expect(config?.intakeUrl).toBe('https://example.com');
});

test('It should use the env var if provided as the intake url', () => {
const initialEnvValue = process.env.DATADOG_SOURCEMAP_INTAKE_URL;
process.env.DATADOG_SOURCEMAP_INTAKE_URL = 'https://example.com';
const { config } = validateSourcemapsOptions({
rum: {
sourcemaps: getMinimalSourcemapsConfiguration(),
},
});

expect(config?.intakeUrl).toBe('https://example.com');
process.env.DATADOG_SOURCEMAP_INTAKE_URL = initialEnvValue;
});
});
});

0 comments on commit 19e3862

Please sign in to comment.