@servicetitan/datadog-rum 36.3.1 → 37.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/before-send.d.ts +11 -1
- package/dist/before-send.d.ts.map +1 -1
- package/dist/before-send.js +2 -56
- package/dist/before-send.js.map +1 -1
- package/dist/build-allowed-tracing-urls.d.ts +9 -0
- package/dist/build-allowed-tracing-urls.d.ts.map +1 -0
- package/dist/build-allowed-tracing-urls.js +54 -0
- package/dist/build-allowed-tracing-urls.js.map +1 -0
- package/dist/build-before-send.d.ts +4 -0
- package/dist/build-before-send.d.ts.map +1 -0
- package/dist/build-before-send.js +64 -0
- package/dist/build-before-send.js.map +1 -0
- package/dist/bundle-state.d.ts +10 -0
- package/dist/bundle-state.d.ts.map +1 -0
- package/dist/bundle-state.js +10 -0
- package/dist/bundle-state.js.map +1 -0
- package/dist/create-impl.d.ts +8 -0
- package/dist/create-impl.d.ts.map +1 -0
- package/dist/create-impl.js +81 -0
- package/dist/create-impl.js.map +1 -0
- package/dist/datadog-provider.d.ts +2 -5
- package/dist/datadog-provider.d.ts.map +1 -1
- package/dist/datadog-provider.js +2 -2
- package/dist/datadog-provider.js.map +1 -1
- package/dist/enrich-event.d.ts +6 -0
- package/dist/enrich-event.d.ts.map +1 -0
- package/dist/enrich-event.js +33 -0
- package/dist/enrich-event.js.map +1 -0
- package/dist/get-shared.d.ts +10 -0
- package/dist/get-shared.d.ts.map +1 -0
- package/dist/get-shared.js +16 -0
- package/dist/get-shared.js.map +1 -0
- package/dist/handle-resource.d.ts +3 -0
- package/dist/handle-resource.d.ts.map +1 -0
- package/dist/handle-resource.js +27 -0
- package/dist/handle-resource.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +17 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +28 -0
- package/dist/init.js.map +1 -0
- package/dist/runtime-override/constants.d.ts +2 -0
- package/dist/runtime-override/constants.d.ts.map +1 -0
- package/dist/runtime-override/constants.js +3 -0
- package/dist/runtime-override/constants.js.map +1 -0
- package/dist/runtime-override/dev-console.d.ts +12 -0
- package/dist/runtime-override/dev-console.d.ts.map +1 -0
- package/dist/runtime-override/dev-console.js +40 -0
- package/dist/runtime-override/dev-console.js.map +1 -0
- package/dist/runtime-override/index.d.ts +4 -0
- package/dist/runtime-override/index.d.ts.map +1 -0
- package/dist/runtime-override/index.js +5 -0
- package/dist/runtime-override/index.js.map +1 -0
- package/dist/runtime-override/override.d.ts +4 -0
- package/dist/runtime-override/override.d.ts.map +1 -0
- package/dist/runtime-override/override.js +35 -0
- package/dist/runtime-override/override.js.map +1 -0
- package/dist/set-user.d.ts +13 -0
- package/dist/set-user.d.ts.map +1 -0
- package/dist/set-user.js +22 -0
- package/dist/set-user.js.map +1 -0
- package/dist/shared-impl.d.ts +11 -0
- package/dist/shared-impl.d.ts.map +1 -0
- package/dist/shared-impl.js +3 -0
- package/dist/shared-impl.js.map +1 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/use-rum-service.d.ts +12 -0
- package/dist/use-rum-service.d.ts.map +1 -0
- package/dist/use-rum-service.js +25 -0
- package/dist/use-rum-service.js.map +1 -0
- package/package.json +3 -3
- package/src/__mocks__/index.ts +6 -0
- package/src/__mocks__/init-config.ts +6 -0
- package/src/__mocks__/process-event.ts +20 -0
- package/src/__mocks__/reset-shared-impl.ts +8 -0
- package/src/__mocks__/tracing-urls.ts +30 -0
- package/src/__mocks__/types.ts +1 -0
- package/src/__tests__/before-send.test.ts +39 -87
- package/src/__tests__/build-allowed-tracing-urls.test.ts +120 -0
- package/src/__tests__/build-before-send.test.ts +193 -0
- package/src/__tests__/create-impl.test.ts +293 -0
- package/src/__tests__/datadog-provider.test.tsx +55 -25
- package/src/__tests__/enrich-event.test.ts +34 -0
- package/src/__tests__/get-shared.test.ts +56 -0
- package/src/__tests__/handle-resource.test.ts +76 -0
- package/src/__tests__/init.test.ts +69 -0
- package/src/__tests__/integration.test.ts +147 -0
- package/src/__tests__/set-user.test.ts +61 -0
- package/src/__tests__/use-rum-service.test.tsx +82 -0
- package/src/before-send.ts +13 -60
- package/src/build-allowed-tracing-urls.ts +53 -0
- package/src/build-before-send.ts +81 -0
- package/src/bundle-state.ts +9 -0
- package/src/create-impl.ts +94 -0
- package/src/datadog-provider.tsx +4 -6
- package/src/enrich-event.ts +38 -0
- package/src/get-shared.ts +24 -0
- package/src/handle-resource.ts +30 -0
- package/src/index.ts +3 -0
- package/src/init.ts +39 -0
- package/src/runtime-override/__tests__/dev-console.test.ts +158 -0
- package/src/runtime-override/__tests__/override.test.ts +109 -0
- package/src/runtime-override/constants.ts +1 -0
- package/src/runtime-override/dev-console.ts +56 -0
- package/src/runtime-override/index.ts +3 -0
- package/src/runtime-override/override.ts +41 -0
- package/src/set-user.ts +25 -0
- package/src/shared-impl.ts +9 -0
- package/src/types.ts +62 -0
- package/src/use-rum-service.ts +28 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createImpl } from './create-impl';
|
|
2
|
+
import type { SharedImpl } from './shared-impl';
|
|
3
|
+
|
|
4
|
+
export const STATE_KEY = Symbol.for('@servicetitan/datadog-rum');
|
|
5
|
+
|
|
6
|
+
export interface GlobalSlot {
|
|
7
|
+
[STATE_KEY]?: SharedImpl;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function slot(): GlobalSlot {
|
|
11
|
+
return globalThis as unknown as GlobalSlot;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Returns the shared impl, creating it if it doesn't exist yet. Only called by `init`. */
|
|
15
|
+
export function getOrCreateSharedImpl(): SharedImpl {
|
|
16
|
+
const s = slot();
|
|
17
|
+
s[STATE_KEY] ??= createImpl();
|
|
18
|
+
return s[STATE_KEY];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Returns the shared impl if it already exists, or `undefined`. */
|
|
22
|
+
export function getSharedImpl(): SharedImpl | undefined {
|
|
23
|
+
return slot()[STATE_KEY];
|
|
24
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RumResourceEvent, RumResourceEventDomainContext } from '@datadog/browser-rum';
|
|
2
|
+
|
|
3
|
+
function isSameDomainRequest(url: string) {
|
|
4
|
+
return new URL(url).hostname === window.location.hostname;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function handleResource(
|
|
8
|
+
event: RumResourceEvent,
|
|
9
|
+
context: RumResourceEventDomainContext
|
|
10
|
+
): void {
|
|
11
|
+
if (!isSameDomainRequest(event.resource.url)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let cfRay: string | undefined | null = undefined;
|
|
16
|
+
|
|
17
|
+
switch (event.resource.type) {
|
|
18
|
+
case 'fetch':
|
|
19
|
+
cfRay = context.response?.headers.get('cf-ray');
|
|
20
|
+
break;
|
|
21
|
+
|
|
22
|
+
case 'xhr':
|
|
23
|
+
cfRay = context.xhr?.getResponseHeader('cf-ray');
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (cfRay) {
|
|
28
|
+
event.context = { ...event.context, cfRay };
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/index.ts
CHANGED
package/src/init.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { bundleState } from './bundle-state';
|
|
2
|
+
import { getOrCreateSharedImpl } from './get-shared';
|
|
3
|
+
import type { InitRumOptions, RumServiceRegistration } from './types';
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
ApplicationConfig,
|
|
7
|
+
Cleanup,
|
|
8
|
+
InitRumOptions,
|
|
9
|
+
RumService,
|
|
10
|
+
RumServiceRegistration,
|
|
11
|
+
ServiceConfig,
|
|
12
|
+
ServiceTracingUrls,
|
|
13
|
+
} from './types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Registers a service and, optionally, initializes the Datadog RUM SDK.
|
|
17
|
+
*
|
|
18
|
+
* Any caller may pass `application`. The first call that includes it
|
|
19
|
+
* initializes the SDK; subsequent callers' `application` config is ignored
|
|
20
|
+
* with a warning. Callers that omit `application` register their service
|
|
21
|
+
* without attempting SDK init.
|
|
22
|
+
*
|
|
23
|
+
* Returns a `RumServiceRegistration` — `{ cleanup, setServiceContext }`.
|
|
24
|
+
* `cleanup` removes this service's `beforeSend`, `allowedTracingUrls`, and
|
|
25
|
+
* persistent context. `setServiceContext` attaches session-lifetime metadata
|
|
26
|
+
* to every event whose `event.service` matches this service's name.
|
|
27
|
+
*/
|
|
28
|
+
export function init(opts: InitRumOptions): RumServiceRegistration {
|
|
29
|
+
const impl = getOrCreateSharedImpl();
|
|
30
|
+
const result = impl.init(opts);
|
|
31
|
+
if (result.initializedSdk) {
|
|
32
|
+
bundleState.isSdkInitializer = true;
|
|
33
|
+
}
|
|
34
|
+
const { name } = opts.service;
|
|
35
|
+
return {
|
|
36
|
+
cleanup: result.cleanup,
|
|
37
|
+
setServiceContext: context => impl.setServiceContext(name, context),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { datadogRum } from '@datadog/browser-rum';
|
|
2
|
+
import { DATADOG_RUM_FOR_DEV } from '../constants';
|
|
3
|
+
import { installDevConsole } from '../dev-console';
|
|
4
|
+
import { clearOverride, enableOverride } from '../override';
|
|
5
|
+
|
|
6
|
+
jest.mock('@datadog/browser-rum');
|
|
7
|
+
jest.mock('../override');
|
|
8
|
+
|
|
9
|
+
describe('[datadog-rum] runtime-override/dev-console', () => {
|
|
10
|
+
const originalLocation = window.location;
|
|
11
|
+
const reload = jest.fn();
|
|
12
|
+
const handle = () =>
|
|
13
|
+
(globalThis as { datadogRumForDev?: { start: () => void; stop: () => void } })
|
|
14
|
+
.datadogRumForDev!;
|
|
15
|
+
const storageCalls = () =>
|
|
16
|
+
jest.mocked(globalThis.addEventListener).mock.calls.filter(([type]) => type === 'storage');
|
|
17
|
+
let infoSpy: jest.SpyInstance;
|
|
18
|
+
|
|
19
|
+
beforeAll(() => {
|
|
20
|
+
delete (window as { location?: Location }).location;
|
|
21
|
+
(window as unknown as { location: { reload: jest.Mock } }).location = { reload };
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
(window as unknown as { location: Location }).location = originalLocation;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
jest.restoreAllMocks();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest.clearAllMocks();
|
|
34
|
+
delete (globalThis as { datadogRumForDev?: unknown }).datadogRumForDev;
|
|
35
|
+
infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
|
|
36
|
+
jest.spyOn(globalThis, 'addEventListener').mockImplementation(() => {});
|
|
37
|
+
installDevConsole();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('installs the global handle', () => {
|
|
41
|
+
expect(handle()).toEqual({ start: expect.any(Function), stop: expect.any(Function) });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('registers a single storage listener', () => {
|
|
45
|
+
expect(storageCalls()).toHaveLength(1);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('when installed again', () => {
|
|
49
|
+
let previous: ReturnType<typeof handle>;
|
|
50
|
+
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
previous = handle();
|
|
53
|
+
installDevConsole();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('keeps the same handle', () => {
|
|
57
|
+
expect(handle()).toBe(previous);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('does not add another storage listener', () => {
|
|
61
|
+
expect(storageCalls()).toHaveLength(1);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('the storage listener', () => {
|
|
66
|
+
let event: { key: string };
|
|
67
|
+
|
|
68
|
+
const subject = () => (storageCalls()[0][1] as EventListener)(event as unknown as Event);
|
|
69
|
+
|
|
70
|
+
describe('when the override key changes', () => {
|
|
71
|
+
beforeEach(() => (event = { key: DATADOG_RUM_FOR_DEV }));
|
|
72
|
+
|
|
73
|
+
test('reloads sibling tabs', () => {
|
|
74
|
+
subject();
|
|
75
|
+
|
|
76
|
+
expect(reload).toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('when an unrelated key changes', () => {
|
|
81
|
+
beforeEach(() => (event = { key: 'other' }));
|
|
82
|
+
|
|
83
|
+
test('does not reload', () => {
|
|
84
|
+
subject();
|
|
85
|
+
|
|
86
|
+
expect(reload).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('start', () => {
|
|
92
|
+
const subject = () => handle().start();
|
|
93
|
+
|
|
94
|
+
test('enables the override', () => {
|
|
95
|
+
subject();
|
|
96
|
+
|
|
97
|
+
expect(enableOverride).toHaveBeenCalled();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('reloads the page', () => {
|
|
101
|
+
subject();
|
|
102
|
+
|
|
103
|
+
expect(reload).toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('logs to the console', () => {
|
|
107
|
+
subject();
|
|
108
|
+
|
|
109
|
+
expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining('start()'));
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('stop', () => {
|
|
114
|
+
const subject = () => handle().stop();
|
|
115
|
+
|
|
116
|
+
test('ends the current Datadog session', () => {
|
|
117
|
+
subject();
|
|
118
|
+
|
|
119
|
+
expect(datadogRum.stopSession).toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('clears the override', () => {
|
|
123
|
+
subject();
|
|
124
|
+
|
|
125
|
+
expect(clearOverride).toHaveBeenCalled();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('reloads the page', () => {
|
|
129
|
+
subject();
|
|
130
|
+
|
|
131
|
+
expect(reload).toHaveBeenCalled();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('logs to the console', () => {
|
|
135
|
+
subject();
|
|
136
|
+
|
|
137
|
+
expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining('stop()'));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('when ending the session fails', () => {
|
|
141
|
+
beforeEach(() =>
|
|
142
|
+
jest.mocked(datadogRum.stopSession).mockImplementation(() => {
|
|
143
|
+
throw new Error('not initialized');
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
test('does not throw', () => {
|
|
148
|
+
expect(subject).not.toThrow();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('still clears the override', () => {
|
|
152
|
+
subject();
|
|
153
|
+
|
|
154
|
+
expect(clearOverride).toHaveBeenCalled();
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { DATADOG_RUM_FOR_DEV } from '../constants';
|
|
2
|
+
import { clearOverride, enableOverride, isForceEnabled } from '../override';
|
|
3
|
+
|
|
4
|
+
const TTL_MS = 12 * 60 * 60 * 1000;
|
|
5
|
+
const NOW = 1_700_000_000_000;
|
|
6
|
+
|
|
7
|
+
describe('[datadog-rum] runtime-override/override', () => {
|
|
8
|
+
const record = (exp: number) =>
|
|
9
|
+
localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({ exp }));
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
jest.restoreAllMocks();
|
|
13
|
+
localStorage.clear();
|
|
14
|
+
jest.spyOn(Date, 'now').mockReturnValue(NOW);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('isForceEnabled', () => {
|
|
18
|
+
const subject = () => isForceEnabled();
|
|
19
|
+
|
|
20
|
+
const itReturns = (value: boolean) =>
|
|
21
|
+
test(`is ${value}`, () => {
|
|
22
|
+
expect(subject()).toBe(value);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('without an override', () => itReturns(false));
|
|
26
|
+
|
|
27
|
+
describe('with a non-expired override', () => {
|
|
28
|
+
beforeEach(() => record(NOW + 1000));
|
|
29
|
+
|
|
30
|
+
itReturns(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('with an expired override', () => {
|
|
34
|
+
beforeEach(() => record(NOW - 1));
|
|
35
|
+
|
|
36
|
+
itReturns(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('with a malformed override', () => {
|
|
40
|
+
beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, 'not json'));
|
|
41
|
+
|
|
42
|
+
itReturns(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('with a record missing an expiry', () => {
|
|
46
|
+
beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({})));
|
|
47
|
+
|
|
48
|
+
itReturns(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('when storage access throws', () => {
|
|
52
|
+
beforeEach(() =>
|
|
53
|
+
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
|
54
|
+
throw new Error('blocked');
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
itReturns(false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('enableOverride', () => {
|
|
63
|
+
const subject = () => enableOverride();
|
|
64
|
+
|
|
65
|
+
test('persists a 12h override window', () => {
|
|
66
|
+
subject();
|
|
67
|
+
|
|
68
|
+
expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBe(
|
|
69
|
+
JSON.stringify({ exp: NOW + TTL_MS })
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('when the storage write fails', () => {
|
|
74
|
+
beforeEach(() =>
|
|
75
|
+
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
|
76
|
+
throw new Error('blocked');
|
|
77
|
+
})
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
test('does not throw', () => {
|
|
81
|
+
expect(subject).not.toThrow();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('clearOverride', () => {
|
|
87
|
+
const subject = () => clearOverride();
|
|
88
|
+
|
|
89
|
+
beforeEach(() => record(NOW + 1000));
|
|
90
|
+
|
|
91
|
+
test('removes the override', () => {
|
|
92
|
+
subject();
|
|
93
|
+
|
|
94
|
+
expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBeNull();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('when the storage removal fails', () => {
|
|
98
|
+
beforeEach(() =>
|
|
99
|
+
jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => {
|
|
100
|
+
throw new Error('blocked');
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
test('does not throw', () => {
|
|
105
|
+
expect(subject).not.toThrow();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DATADOG_RUM_FOR_DEV = 'datadogRumForDev';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { datadogRum } from '@datadog/browser-rum';
|
|
2
|
+
import { DATADOG_RUM_FOR_DEV } from './constants';
|
|
3
|
+
import { clearOverride, enableOverride } from './override';
|
|
4
|
+
|
|
5
|
+
interface DevConsole {
|
|
6
|
+
start: () => void;
|
|
7
|
+
stop: () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface DevGlobalSlot {
|
|
11
|
+
[DATADOG_RUM_FOR_DEV]?: DevConsole;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare global {
|
|
15
|
+
interface Window {
|
|
16
|
+
datadogRumForDev?: DevConsole;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* istanbul ignore next: thin seam over location.reload, which jsdom does not implement */
|
|
21
|
+
function reloadPage(): void {
|
|
22
|
+
globalThis.location.reload();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function start(): void {
|
|
26
|
+
enableOverride();
|
|
27
|
+
// eslint-disable-next-line no-console
|
|
28
|
+
console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.start(): RUM enabled for 12h; reloading.`);
|
|
29
|
+
reloadPage();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function stop(): void {
|
|
33
|
+
try {
|
|
34
|
+
datadogRum.stopSession();
|
|
35
|
+
} catch {
|
|
36
|
+
// ignore: SDK may not be initialized
|
|
37
|
+
}
|
|
38
|
+
clearOverride();
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.stop(): override cleared; reloading.`);
|
|
41
|
+
reloadPage();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function installDevConsole(): void {
|
|
45
|
+
const slot = globalThis as unknown as DevGlobalSlot;
|
|
46
|
+
if (slot[DATADOG_RUM_FOR_DEV]) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
slot[DATADOG_RUM_FOR_DEV] = { start, stop };
|
|
50
|
+
|
|
51
|
+
globalThis.addEventListener('storage', event => {
|
|
52
|
+
if (event.key === DATADOG_RUM_FOR_DEV) {
|
|
53
|
+
reloadPage();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DATADOG_RUM_FOR_DEV } from './constants';
|
|
2
|
+
|
|
3
|
+
const TTL_MS = 12 * 60 * 60 * 1000;
|
|
4
|
+
|
|
5
|
+
interface OverrideRecord {
|
|
6
|
+
exp: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function readOverride(): OverrideRecord | null {
|
|
10
|
+
try {
|
|
11
|
+
const raw = localStorage.getItem(DATADOG_RUM_FOR_DEV);
|
|
12
|
+
return raw ? (JSON.parse(raw) as OverrideRecord) : null;
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function writeOverride(record: OverrideRecord | null): void {
|
|
19
|
+
try {
|
|
20
|
+
if (record === null) {
|
|
21
|
+
localStorage.removeItem(DATADOG_RUM_FOR_DEV);
|
|
22
|
+
} else {
|
|
23
|
+
localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify(record));
|
|
24
|
+
}
|
|
25
|
+
} catch {
|
|
26
|
+
// storage unavailable
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isForceEnabled(): boolean {
|
|
31
|
+
const record = readOverride();
|
|
32
|
+
return record !== null && typeof record.exp === 'number' && Date.now() < record.exp;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function enableOverride(): void {
|
|
36
|
+
writeOverride({ exp: Date.now() + TTL_MS });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function clearOverride(): void {
|
|
40
|
+
writeOverride(null);
|
|
41
|
+
}
|
package/src/set-user.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { datadogRum } from '@datadog/browser-rum';
|
|
2
|
+
import { bundleState } from './bundle-state';
|
|
3
|
+
import { getSharedImpl } from './get-shared';
|
|
4
|
+
import type { RumUser } from './shared-impl';
|
|
5
|
+
|
|
6
|
+
export type { RumUser } from './shared-impl';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Sets the session user on the Datadog RUM SDK.
|
|
10
|
+
*
|
|
11
|
+
* - **SDK initialized via `init`**: only the bundle whose `init` call
|
|
12
|
+
* initialized the SDK may set the user; calls from any other bundle are
|
|
13
|
+
* silent no-ops.
|
|
14
|
+
* - **SDK initialized directly via `datadogRum.init`**: no gating is applied.
|
|
15
|
+
* - **Neither initialized**: no-op.
|
|
16
|
+
*/
|
|
17
|
+
export function setUser(user: RumUser): void {
|
|
18
|
+
if (bundleState.isSdkInitializer) {
|
|
19
|
+
datadogRum.setUser(user);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!getSharedImpl() && datadogRum.getInitConfiguration()) {
|
|
23
|
+
datadogRum.setUser(user);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context, User } from '@datadog/browser-core';
|
|
2
|
+
import type { Cleanup, InitRumOptions } from './types';
|
|
3
|
+
|
|
4
|
+
export type RumUser = Omit<User, 'id'> & Required<Pick<User, 'id'>>;
|
|
5
|
+
|
|
6
|
+
export interface SharedImpl {
|
|
7
|
+
init(opts: InitRumOptions): { cleanup: Cleanup; initializedSdk: boolean };
|
|
8
|
+
setServiceContext(key: string, context: Context): void;
|
|
9
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Context } from '@datadog/browser-core';
|
|
2
|
+
import type { MatchOption, PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';
|
|
3
|
+
import type { ApplicationBeforeSendHandler, ServiceBeforeSendHandler } from './before-send';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* URL matchers grouped by the propagator type whose headers they should
|
|
7
|
+
* activate. A service registers its tracing URLs as e.g.
|
|
8
|
+
* `{ tracecontext: [/api\//, 'https://...'] }`.
|
|
9
|
+
*/
|
|
10
|
+
export type ServiceTracingUrls = Partial<Record<PropagatorType, MatchOption[]>>;
|
|
11
|
+
|
|
12
|
+
/** Tears down a service registration: handlers, tracing URLs, and persistent context. */
|
|
13
|
+
export type Cleanup = () => void;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Service handle returned by `useRumService`. `setServiceContext` is bound to the
|
|
17
|
+
* service registered by the hook, so callers don't pass the service name again.
|
|
18
|
+
*/
|
|
19
|
+
export interface RumService {
|
|
20
|
+
setServiceContext: (context: Context) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Imperative service registration returned by `init`. Adds the cleanup contract. */
|
|
24
|
+
export interface RumServiceRegistration extends RumService {
|
|
25
|
+
cleanup: Cleanup;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ApplicationConfig = Omit<
|
|
29
|
+
RumInitConfiguration,
|
|
30
|
+
'beforeSend' | 'allowedTracingUrls' | 'service'
|
|
31
|
+
> & {
|
|
32
|
+
beforeSend?: ApplicationBeforeSendHandler;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface ServiceShape {
|
|
36
|
+
beforeSend?: ServiceBeforeSendHandler;
|
|
37
|
+
allowedTracingUrls?: ServiceTracingUrls;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ServiceConfig extends ServiceShape {
|
|
41
|
+
name: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface InitRumOptions {
|
|
45
|
+
service: ServiceConfig;
|
|
46
|
+
application?: ApplicationConfig;
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Per-service entry — `beforeSend` and `allowedTracingUrls` come from
|
|
52
|
+
* `init`; `context` comes from `setServiceContext`. One service = one entry.
|
|
53
|
+
*/
|
|
54
|
+
export interface ServiceEntry extends ServiceShape {
|
|
55
|
+
context?: Context;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Registration state: application-level handler + per-service entries keyed by service name. */
|
|
59
|
+
export interface State {
|
|
60
|
+
applicationBeforeSend?: ApplicationBeforeSendHandler;
|
|
61
|
+
services: Map<string, ServiceEntry>;
|
|
62
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect, useMemo } from 'react';
|
|
2
|
+
import { getOrCreateSharedImpl } from './get-shared';
|
|
3
|
+
import { init, type RumService, type ServiceConfig } from './init';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Registers a service on mount and disposes it on unmount. On unmount the
|
|
7
|
+
* service's `beforeSend`, `allowedTracingUrls`, and any context previously set
|
|
8
|
+
* via `setServiceContext` are removed atomically. The config is captured on
|
|
9
|
+
* first render — changing it across renders does not re-register.
|
|
10
|
+
*
|
|
11
|
+
* Returns a `RumService` — `{ setServiceContext }`, bound to this service.
|
|
12
|
+
* The handle is identity-stable across renders.
|
|
13
|
+
*/
|
|
14
|
+
export function useRumService(config: ServiceConfig): RumService {
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
return init({ service: config }).cleanup;
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
return useMemo(
|
|
21
|
+
() => ({
|
|
22
|
+
setServiceContext: context =>
|
|
23
|
+
getOrCreateSharedImpl().setServiceContext(config.name, context),
|
|
24
|
+
}),
|
|
25
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
26
|
+
[]
|
|
27
|
+
);
|
|
28
|
+
}
|