Skip to content

Commit

Permalink
[WIP] Inject SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Dec 2, 2024
1 parent ddaca25 commit 73486c7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
62 changes: 59 additions & 3 deletions packages/plugins/rum/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import { doRequest } from '@dd/core/helpers';
import type { GlobalContext, GetPlugins, Logger } from '@dd/core/types';

import { CONFIG_KEY, PLUGIN_NAME } from './constants';
Expand All @@ -20,6 +21,14 @@ export type types = {
OptionsWithRum: OptionsWithRum;
};

type RumAppResponse = {
data: {
attributes: {
client_token: string;
};
};
};

export const getPlugins: GetPlugins<OptionsWithRum> = (
opts: OptionsWithRum,
context: GlobalContext,
Expand All @@ -28,9 +37,10 @@ export const getPlugins: GetPlugins<OptionsWithRum> = (
// Verify configuration.
const options = validateOptions(opts, log);

if (!options.sdk?.clientToken) {
// Fetch the client token from the API.
}
context.inject({
type: 'file',
value: 'https://www.datadoghq-browser-agent.com/us1/v5/datadog-rum.js',
});

return [
{
Expand All @@ -39,6 +49,52 @@ export const getPlugins: GetPlugins<OptionsWithRum> = (
// Not supported by Rollup and ESBuild.
// https://vitejs.dev/guide/api-plugin.html#plugin-ordering
enforce: 'pre',
async buildStart() {
if (!options.sdk) {
return;
}

if (!options.sdk.clientToken) {
if (!context.auth?.apiKey || !context.auth?.appKey) {
throw new Error(
'Missing auth.apiKey and/or auth.appKey to fetch clientToken.',
);
}

const sdkOpts = options.sdk;
let clientToken: string;

try {
// Fetch the client token from the API.
const appResponse = await doRequest<RumAppResponse>({
url: `https://api.datadoghq.com/api/v2/rum/applications/${options.sdk.applicationId}`,
type: 'json',
auth: context.auth,
});

clientToken = appResponse.data?.attributes?.client_token;
} catch (e: any) {
// Could not fetch the clientToken.
// Let's crash the build.
throw new Error(`Could not fetch the clientToken: ${e.message}`);
}

// Still no clientToken.
if (!clientToken) {
throw new Error('Missing clientToken in the API response.');
}

console.log('INJECTING');
// Inject the initialization code.
context.inject({
type: 'code',
value: `DD_RUM.init(${JSON.stringify({
clientToken,
...sdkOpts,
})});`,
});
}
},
},
];
};
30 changes: 30 additions & 0 deletions packages/tests/src/plugins/rum/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 { runBundlers } from '@dd/tests/_jest/helpers/runBundlers';
import type { CleanupFn } from '@dd/tests/_jest/helpers/types';

describe('RUM Plugin', () => {
const cleanups: CleanupFn[] = [];

afterAll(async () => {
await Promise.all(cleanups.map((cleanup) => cleanup()));
});

test('Should get the clientToken.', async () => {
cleanups.push(
await runBundlers({
auth: {
apiKey: process.env.DD_API_KEY,
appKey: process.env.DD_APP_KEY,
},
rum: {
sdk: {
applicationId: '54caaabe-a702-4c07-89d8-8b7a064089aa',
},
},
}),
);
});
});

0 comments on commit 73486c7

Please sign in to comment.