Skip to content

Commit 73486c7

Browse files
committed
[WIP] Inject SDK
1 parent ddaca25 commit 73486c7

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

packages/plugins/rum/src/index.ts

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

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

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

24+
type RumAppResponse = {
25+
data: {
26+
attributes: {
27+
client_token: string;
28+
};
29+
};
30+
};
31+
2332
export const getPlugins: GetPlugins<OptionsWithRum> = (
2433
opts: OptionsWithRum,
2534
context: GlobalContext,
@@ -28,9 +37,10 @@ export const getPlugins: GetPlugins<OptionsWithRum> = (
2837
// Verify configuration.
2938
const options = validateOptions(opts, log);
3039

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

3545
return [
3646
{
@@ -39,6 +49,52 @@ export const getPlugins: GetPlugins<OptionsWithRum> = (
3949
// Not supported by Rollup and ESBuild.
4050
// https://vitejs.dev/guide/api-plugin.html#plugin-ordering
4151
enforce: 'pre',
52+
async buildStart() {
53+
if (!options.sdk) {
54+
return;
55+
}
56+
57+
if (!options.sdk.clientToken) {
58+
if (!context.auth?.apiKey || !context.auth?.appKey) {
59+
throw new Error(
60+
'Missing auth.apiKey and/or auth.appKey to fetch clientToken.',
61+
);
62+
}
63+
64+
const sdkOpts = options.sdk;
65+
let clientToken: string;
66+
67+
try {
68+
// Fetch the client token from the API.
69+
const appResponse = await doRequest<RumAppResponse>({
70+
url: `https://api.datadoghq.com/api/v2/rum/applications/${options.sdk.applicationId}`,
71+
type: 'json',
72+
auth: context.auth,
73+
});
74+
75+
clientToken = appResponse.data?.attributes?.client_token;
76+
} catch (e: any) {
77+
// Could not fetch the clientToken.
78+
// Let's crash the build.
79+
throw new Error(`Could not fetch the clientToken: ${e.message}`);
80+
}
81+
82+
// Still no clientToken.
83+
if (!clientToken) {
84+
throw new Error('Missing clientToken in the API response.');
85+
}
86+
87+
console.log('INJECTING');
88+
// Inject the initialization code.
89+
context.inject({
90+
type: 'code',
91+
value: `DD_RUM.init(${JSON.stringify({
92+
clientToken,
93+
...sdkOpts,
94+
})});`,
95+
});
96+
}
97+
},
4298
},
4399
];
44100
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
2+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
3+
// Copyright 2019-Present Datadog, Inc.
4+
5+
import { runBundlers } from '@dd/tests/_jest/helpers/runBundlers';
6+
import type { CleanupFn } from '@dd/tests/_jest/helpers/types';
7+
8+
describe('RUM Plugin', () => {
9+
const cleanups: CleanupFn[] = [];
10+
11+
afterAll(async () => {
12+
await Promise.all(cleanups.map((cleanup) => cleanup()));
13+
});
14+
15+
test('Should get the clientToken.', async () => {
16+
cleanups.push(
17+
await runBundlers({
18+
auth: {
19+
apiKey: process.env.DD_API_KEY,
20+
appKey: process.env.DD_APP_KEY,
21+
},
22+
rum: {
23+
sdk: {
24+
applicationId: '54caaabe-a702-4c07-89d8-8b7a064089aa',
25+
},
26+
},
27+
}),
28+
);
29+
});
30+
});

0 commit comments

Comments
 (0)