@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,147 @@
|
|
|
1
|
+
import type { RumEvent } from '@datadog/browser-rum';
|
|
2
|
+
import {
|
|
3
|
+
getInitAllowedTracingUrls,
|
|
4
|
+
isAllowedTracingUrl,
|
|
5
|
+
minimalApplication,
|
|
6
|
+
processEvent,
|
|
7
|
+
resetSharedImpl,
|
|
8
|
+
} from '../__mocks__';
|
|
9
|
+
import { init, RumServiceRegistration } from '../init';
|
|
10
|
+
|
|
11
|
+
jest.mock('@datadog/browser-rum');
|
|
12
|
+
|
|
13
|
+
describe('[datadog-rum] integration', () => {
|
|
14
|
+
const serviceContext = { logicalEnv: 'go5', orgId: '42' };
|
|
15
|
+
const applicationBeforeSendHandler = jest.fn();
|
|
16
|
+
let event: Partial<RumEvent>;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
jest.clearAllMocks();
|
|
20
|
+
resetSharedImpl();
|
|
21
|
+
|
|
22
|
+
applicationBeforeSendHandler.mockReturnValue(true);
|
|
23
|
+
|
|
24
|
+
init({
|
|
25
|
+
service: { name: 'desktop' },
|
|
26
|
+
application: { ...minimalApplication, beforeSend: applicationBeforeSendHandler },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
event = {
|
|
30
|
+
type: 'error',
|
|
31
|
+
error: {
|
|
32
|
+
message: 'boom',
|
|
33
|
+
stack: 'at https://unpkg.servicetitan.com/@servicetitan/task-hub@1.2.3/main.js',
|
|
34
|
+
} as any,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const subject = () => processEvent(event);
|
|
39
|
+
|
|
40
|
+
test('passes event through the pipeline', () => {
|
|
41
|
+
expect(subject()).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('sets event service and version', () => {
|
|
45
|
+
subject();
|
|
46
|
+
|
|
47
|
+
expect(event.service).toBe('task-hub');
|
|
48
|
+
expect(event.version).toBe('1.2.3');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('when application handler returns false', () => {
|
|
52
|
+
beforeEach(() => applicationBeforeSendHandler.mockReturnValue(false));
|
|
53
|
+
|
|
54
|
+
test('drops the event', () => {
|
|
55
|
+
expect(subject()).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('with service handler', () => {
|
|
60
|
+
let serviceBeforeSendHandler: jest.Mock;
|
|
61
|
+
let serviceRegistration: RumServiceRegistration;
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
serviceBeforeSendHandler = jest.fn(e => {
|
|
65
|
+
e.context = { ...e.context, kibanaUrl: 'https://kibana/example' };
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
serviceRegistration = init({
|
|
69
|
+
service: {
|
|
70
|
+
name: 'task-hub',
|
|
71
|
+
allowedTracingUrls: { tracecontext: [/\/api\/task-hub\//] },
|
|
72
|
+
beforeSend: serviceBeforeSendHandler,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
serviceRegistration.setServiceContext(serviceContext);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('traces registered URLs', () => {
|
|
79
|
+
expect(
|
|
80
|
+
isAllowedTracingUrl(
|
|
81
|
+
getInitAllowedTracingUrls(),
|
|
82
|
+
'https://go.example.com/api/task-hub/list'
|
|
83
|
+
)
|
|
84
|
+
).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('does not trace unregistered URLs', () => {
|
|
88
|
+
expect(
|
|
89
|
+
isAllowedTracingUrl(getInitAllowedTracingUrls(), 'https://go.example.com/api/other')
|
|
90
|
+
).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('runs service handlers', () => {
|
|
94
|
+
subject();
|
|
95
|
+
|
|
96
|
+
expect(serviceBeforeSendHandler).toHaveBeenCalled();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('enriches event.context', () => {
|
|
100
|
+
subject();
|
|
101
|
+
|
|
102
|
+
expect(event.context).toEqual({
|
|
103
|
+
kibanaUrl: 'https://kibana/example',
|
|
104
|
+
...serviceContext,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('when application handler returns false', () => {
|
|
109
|
+
beforeEach(() => applicationBeforeSendHandler.mockReturnValue(false));
|
|
110
|
+
|
|
111
|
+
test('does not run service handlers', () => {
|
|
112
|
+
subject();
|
|
113
|
+
|
|
114
|
+
expect(serviceBeforeSendHandler).not.toHaveBeenCalled();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('when service handler returns false', () => {
|
|
119
|
+
beforeEach(() => serviceBeforeSendHandler.mockReturnValue(false));
|
|
120
|
+
|
|
121
|
+
test('passes event through the pipeline', () => {
|
|
122
|
+
expect(subject()).toBe(true);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe('when service is unregistered', () => {
|
|
127
|
+
beforeEach(() => serviceRegistration.cleanup());
|
|
128
|
+
|
|
129
|
+
test('stops tracing URLs', () => {
|
|
130
|
+
subject();
|
|
131
|
+
|
|
132
|
+
expect(
|
|
133
|
+
isAllowedTracingUrl(
|
|
134
|
+
getInitAllowedTracingUrls(),
|
|
135
|
+
'https://go.example.com/api/task-hub/list'
|
|
136
|
+
)
|
|
137
|
+
).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('stops enriching event.context', () => {
|
|
141
|
+
subject();
|
|
142
|
+
|
|
143
|
+
expect(event.context).toBeUndefined();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { datadogRum } from '@datadog/browser-rum';
|
|
2
|
+
import { minimalApplication, resetSharedImpl } from '../__mocks__';
|
|
3
|
+
import { init } from '../init';
|
|
4
|
+
import { setUser, type RumUser } from '../set-user';
|
|
5
|
+
|
|
6
|
+
jest.mock('@datadog/browser-rum');
|
|
7
|
+
|
|
8
|
+
describe(`[datadog-rum] ${setUser.name}`, () => {
|
|
9
|
+
let user: RumUser;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
jest.clearAllMocks();
|
|
13
|
+
resetSharedImpl();
|
|
14
|
+
user = { id: '1' };
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const subject = () => setUser(user);
|
|
18
|
+
|
|
19
|
+
function itDoesNothing() {
|
|
20
|
+
test('does nothing', () => {
|
|
21
|
+
subject();
|
|
22
|
+
|
|
23
|
+
expect(datadogRum.setUser).not.toHaveBeenCalled();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function itCallsDatadogRumSetUser() {
|
|
28
|
+
test('calls datadogRum.setUser', () => {
|
|
29
|
+
subject();
|
|
30
|
+
|
|
31
|
+
expect(datadogRum.setUser).toHaveBeenCalledWith(user);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
itDoesNothing();
|
|
36
|
+
|
|
37
|
+
describe('when caller initialized the SDK', () => {
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
init({ service: { name: 'desktop' }, application: minimalApplication });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
itCallsDatadogRumSetUser();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('when datadogRum.init was called directly (legacy)', () => {
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
jest.mocked(datadogRum.getInitConfiguration).mockReturnValue({} as any);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
itCallsDatadogRumSetUser();
|
|
51
|
+
|
|
52
|
+
describe('when caller initialized the SDK', () => {
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
jest.spyOn(console, 'warn').mockImplementation(jest.fn()); // suppress console output
|
|
55
|
+
init({ service: { name: 'desktop' }, application: minimalApplication });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
itDoesNothing();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { renderHook, RenderHookResult } from '@testing-library/react';
|
|
2
|
+
import { getOrCreateSharedImpl } from '../get-shared';
|
|
3
|
+
import { init, RumService, type ServiceConfig } from '../init';
|
|
4
|
+
import { useRumService } from '../use-rum-service';
|
|
5
|
+
|
|
6
|
+
jest.mock('../init');
|
|
7
|
+
jest.mock('../get-shared');
|
|
8
|
+
|
|
9
|
+
describe(`[datadog-rum] ${useRumService.name}`, () => {
|
|
10
|
+
const cleanup = jest.fn();
|
|
11
|
+
const setServiceContext = jest.fn();
|
|
12
|
+
const initSetServiceContext = jest.fn();
|
|
13
|
+
let config: ServiceConfig;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
jest.clearAllMocks();
|
|
17
|
+
jest.mocked(init).mockReturnValue({ cleanup, setServiceContext: initSetServiceContext });
|
|
18
|
+
jest.mocked(getOrCreateSharedImpl).mockReturnValue({
|
|
19
|
+
setServiceContext,
|
|
20
|
+
} as unknown as ReturnType<typeof getOrCreateSharedImpl>);
|
|
21
|
+
|
|
22
|
+
config = { name: 'foo' };
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const subject = () =>
|
|
26
|
+
renderHook((cfg: ServiceConfig) => useRumService(cfg), { initialProps: config });
|
|
27
|
+
|
|
28
|
+
describe('on mount', () => {
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
subject();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('registers the service', () => {
|
|
34
|
+
expect(init).toHaveBeenCalledWith({ service: config });
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('on unmount', () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
const { unmount } = subject();
|
|
41
|
+
unmount();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('runs cleanup', () => {
|
|
45
|
+
expect(cleanup).toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('setServiceContext delegates to the shared implementation', () => {
|
|
50
|
+
const context = { foo: 'bar' };
|
|
51
|
+
|
|
52
|
+
subject().result.current.setServiceContext(context);
|
|
53
|
+
|
|
54
|
+
expect(setServiceContext).toHaveBeenCalledWith(config.name, context);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('when already rendered', () => {
|
|
58
|
+
let rerender: RenderHookResult<RumService, ServiceConfig>['rerender'];
|
|
59
|
+
let result: RenderHookResult<RumService, ServiceConfig>['result'];
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
const renderHookResult = subject();
|
|
63
|
+
rerender = renderHookResult.rerender;
|
|
64
|
+
result = renderHookResult.result;
|
|
65
|
+
jest.clearAllMocks();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('does not register the service again', () => {
|
|
69
|
+
rerender({ name: 'changed' });
|
|
70
|
+
|
|
71
|
+
expect(init).not.toHaveBeenCalled();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('returns same result', () => {
|
|
75
|
+
const initial = result.current;
|
|
76
|
+
|
|
77
|
+
rerender({ name: 'changed' });
|
|
78
|
+
|
|
79
|
+
expect(result.current).toBe(initial);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
package/src/before-send.ts
CHANGED
|
@@ -2,72 +2,25 @@ import type {
|
|
|
2
2
|
RumEvent,
|
|
3
3
|
RumEventDomainContext,
|
|
4
4
|
RumInitConfiguration,
|
|
5
|
-
RumResourceEvent,
|
|
6
5
|
RumResourceEventDomainContext,
|
|
7
6
|
} from '@datadog/browser-rum';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/unpkg\.servicetitan\.com\/@servicetitan\/(?<service>[\w-]+)(?:@(?<version>\d+\.\d+\.\d+(?:-[\w.]+)?))?/;
|
|
11
|
-
|
|
12
|
-
function isSameDomainRequest(url: string) {
|
|
13
|
-
return new URL(url).hostname === window.location.hostname;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function parseService(event: RumEvent, input: string | undefined) {
|
|
17
|
-
const match = input?.match(SERVICE_REGEX);
|
|
18
|
-
|
|
19
|
-
const service = match?.groups?.service;
|
|
20
|
-
const version = match?.groups?.version;
|
|
21
|
-
|
|
22
|
-
if (service) {
|
|
23
|
-
event.service = service;
|
|
24
|
-
event.version = version ?? 'unknown';
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
7
|
+
import { enrichEvent } from './enrich-event';
|
|
8
|
+
import { handleResource } from './handle-resource';
|
|
30
9
|
|
|
31
10
|
/**
|
|
32
|
-
*
|
|
11
|
+
* Used for `application.beforeSend`. Returning `false` drops the event app-wide;
|
|
12
|
+
* `void` / `true` passes it through.
|
|
33
13
|
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const message = event.type === 'error' ? event.error?.message : undefined;
|
|
14
|
+
export type ApplicationBeforeSendHandler = (
|
|
15
|
+
event: RumEvent,
|
|
16
|
+
context: RumEventDomainContext
|
|
17
|
+
) => boolean | void;
|
|
40
18
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function handleResource(event: RumResourceEvent, context: RumResourceEventDomainContext): void {
|
|
51
|
-
if (!isSameDomainRequest(event.resource.url)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
let cfRay: string | undefined | null = undefined;
|
|
56
|
-
|
|
57
|
-
switch (event.resource.type) {
|
|
58
|
-
case 'fetch':
|
|
59
|
-
cfRay = context.response?.headers.get('cf-ray');
|
|
60
|
-
break;
|
|
61
|
-
|
|
62
|
-
case 'xhr':
|
|
63
|
-
cfRay = context.xhr?.getResponseHeader('cf-ray');
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (cfRay) {
|
|
68
|
-
event.context = { ...event.context, cfRay };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
19
|
+
/**
|
|
20
|
+
* Used for per-service `beforeSend`. Transform-only: the return value is
|
|
21
|
+
* ignored. Only `application.beforeSend` may drop events.
|
|
22
|
+
*/
|
|
23
|
+
export type ServiceBeforeSendHandler = (event: RumEvent, context: RumEventDomainContext) => void;
|
|
71
24
|
|
|
72
25
|
export function beforeSend(
|
|
73
26
|
handler?: RumInitConfiguration['beforeSend']
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { MatchOption, PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';
|
|
2
|
+
import type { State } from './types';
|
|
3
|
+
|
|
4
|
+
const PROPAGATOR_TYPES: readonly PropagatorType[] = ['datadog', 'tracecontext', 'b3', 'b3multi'];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Mirrors `@datadog/browser-rum-core`'s tracing URL match semantics:
|
|
8
|
+
* - strings are prefix-matched (SDK passes `useStartsWith=true` for tracing URLs)
|
|
9
|
+
*
|
|
10
|
+
* Per-call try/catch mirrors the SDK's `matchList`: a throwing user function or
|
|
11
|
+
* malformed RegExp only loses its own match decision rather than breaking the
|
|
12
|
+
* wrapper for every other matcher.
|
|
13
|
+
*/
|
|
14
|
+
function matchOption(opt: MatchOption, url: string): boolean {
|
|
15
|
+
try {
|
|
16
|
+
if (typeof opt === 'function') {
|
|
17
|
+
return opt(url);
|
|
18
|
+
}
|
|
19
|
+
if (typeof opt === 'string') {
|
|
20
|
+
return url.startsWith(opt);
|
|
21
|
+
}
|
|
22
|
+
return opt.test(url);
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Emits one dynamic wrapper per `PropagatorType`. Each wrapper's `match` walks
|
|
30
|
+
* registered services and returns true if any matcher registered under that
|
|
31
|
+
* propagator matches. Late-loading MFEs work fully dynamically.
|
|
32
|
+
*/
|
|
33
|
+
export function buildAllowedTracingUrls(
|
|
34
|
+
state: State
|
|
35
|
+
): NonNullable<RumInitConfiguration['allowedTracingUrls']> {
|
|
36
|
+
return PROPAGATOR_TYPES.map(pt => ({
|
|
37
|
+
match: (url: string) => {
|
|
38
|
+
for (const entry of state.services.values()) {
|
|
39
|
+
const matchers = entry.allowedTracingUrls?.[pt];
|
|
40
|
+
if (!matchers) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
for (const m of matchers) {
|
|
44
|
+
if (matchOption(m, url)) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
},
|
|
51
|
+
propagatorTypes: [pt],
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
RumEvent,
|
|
3
|
+
RumEventDomainContext,
|
|
4
|
+
RumInitConfiguration,
|
|
5
|
+
RumResourceEventDomainContext,
|
|
6
|
+
} from '@datadog/browser-rum';
|
|
7
|
+
import type { ApplicationBeforeSendHandler, ServiceBeforeSendHandler } from './before-send';
|
|
8
|
+
import { enrichEvent } from './enrich-event';
|
|
9
|
+
import { handleResource } from './handle-resource';
|
|
10
|
+
import type { State } from './types';
|
|
11
|
+
|
|
12
|
+
function applyBuiltIn(event: RumEvent, context: RumEventDomainContext): void {
|
|
13
|
+
if (event.type === 'resource') {
|
|
14
|
+
handleResource(event, context as RumResourceEventDomainContext);
|
|
15
|
+
}
|
|
16
|
+
enrichEvent(event, context);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function callApplicationHandler(
|
|
20
|
+
handler: ApplicationBeforeSendHandler,
|
|
21
|
+
event: RumEvent,
|
|
22
|
+
context: RumEventDomainContext
|
|
23
|
+
): boolean {
|
|
24
|
+
try {
|
|
25
|
+
return handler(event, context) !== false;
|
|
26
|
+
} catch {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function callServiceHandler(
|
|
32
|
+
handler: ServiceBeforeSendHandler,
|
|
33
|
+
event: RumEvent,
|
|
34
|
+
context: RumEventDomainContext
|
|
35
|
+
): void {
|
|
36
|
+
try {
|
|
37
|
+
handler(event, context);
|
|
38
|
+
} catch {
|
|
39
|
+
// Fail-open: a service handler may not drop or block events.
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function applyApplication(state: State, event: RumEvent, context: RumEventDomainContext): boolean {
|
|
44
|
+
if (state.applicationBeforeSend) {
|
|
45
|
+
return callApplicationHandler(state.applicationBeforeSend, event, context);
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function applyServiceSpecific(state: State, event: RumEvent, context: RumEventDomainContext): void {
|
|
51
|
+
const serviceKey = event.service;
|
|
52
|
+
if (!serviceKey) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const entry = state.services.get(serviceKey);
|
|
56
|
+
if (!entry) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (entry.beforeSend) {
|
|
60
|
+
callServiceHandler(entry.beforeSend, event, context);
|
|
61
|
+
}
|
|
62
|
+
if (entry.context) {
|
|
63
|
+
event.context = { ...event.context, ...entry.context };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function buildBeforeSend(state: State): NonNullable<RumInitConfiguration['beforeSend']> {
|
|
68
|
+
return function (event: RumEvent, context: RumEventDomainContext) {
|
|
69
|
+
try {
|
|
70
|
+
applyBuiltIn(event, context);
|
|
71
|
+
if (!applyApplication(state, event, context)) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
applyServiceSpecific(state, event, context);
|
|
75
|
+
return true;
|
|
76
|
+
} catch {
|
|
77
|
+
// Fail-open: prefer noisy data over no data.
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bundle-local state. Each bundle that loads `@servicetitan/datadog-rum` gets
|
|
3
|
+
* its own copy of this module; nothing here lives on `globalThis`. The bundle
|
|
4
|
+
* whose `init` call wins the SDK init race sets `isSdkInitializer` here, and
|
|
5
|
+
* only its own bundle's `setUser` reads `true` from it.
|
|
6
|
+
*/
|
|
7
|
+
export const bundleState = {
|
|
8
|
+
isSdkInitializer: false,
|
|
9
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { datadogRum } from '@datadog/browser-rum';
|
|
2
|
+
import { buildAllowedTracingUrls } from './build-allowed-tracing-urls';
|
|
3
|
+
import { buildBeforeSend } from './build-before-send';
|
|
4
|
+
import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from './runtime-override';
|
|
5
|
+
import type { SharedImpl } from './shared-impl';
|
|
6
|
+
import type { ApplicationConfig, ServiceConfig, State } from './types';
|
|
7
|
+
|
|
8
|
+
const DEFAULTS = {
|
|
9
|
+
sessionSampleRate: 100,
|
|
10
|
+
sessionReplaySampleRate: 0,
|
|
11
|
+
defaultPrivacyLevel: 'mask-user-input',
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Interop surface: stored on globalThis, shared across independently
|
|
16
|
+
* deployed bundles. First bundle to load wins. Append-only — never
|
|
17
|
+
* remove or change method signatures.
|
|
18
|
+
*/
|
|
19
|
+
export function createImpl(): SharedImpl {
|
|
20
|
+
let initialized = false;
|
|
21
|
+
const state: State = {
|
|
22
|
+
services: new Map(),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function initializeSdk(
|
|
26
|
+
service: ServiceConfig,
|
|
27
|
+
application: ApplicationConfig,
|
|
28
|
+
enabled: boolean
|
|
29
|
+
): boolean {
|
|
30
|
+
if (initialized) {
|
|
31
|
+
// eslint-disable-next-line no-console
|
|
32
|
+
console.info(
|
|
33
|
+
'[datadog-rum] init: application config ignored; SDK already initialized.'
|
|
34
|
+
);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (datadogRum.getInitConfiguration()) {
|
|
38
|
+
// eslint-disable-next-line no-console
|
|
39
|
+
console.warn(
|
|
40
|
+
'[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.'
|
|
41
|
+
);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;
|
|
46
|
+
if (appBeforeSend) {
|
|
47
|
+
state.applicationBeforeSend = appBeforeSend;
|
|
48
|
+
}
|
|
49
|
+
datadogRum.init({
|
|
50
|
+
...DEFAULTS,
|
|
51
|
+
...sdkConfigPart,
|
|
52
|
+
service: service.name,
|
|
53
|
+
beforeSend: buildBeforeSend(state),
|
|
54
|
+
allowedTracingUrls: buildAllowedTracingUrls(state),
|
|
55
|
+
});
|
|
56
|
+
if (!enabled) {
|
|
57
|
+
datadogRum.setGlobalContextProperty(DATADOG_RUM_FOR_DEV, true);
|
|
58
|
+
}
|
|
59
|
+
initialized = true;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function registerService({ name, ...reg }: ServiceConfig): void {
|
|
64
|
+
const existing = state.services.get(name);
|
|
65
|
+
state.services.set(name, { ...reg, context: existing?.context });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
init({ service, application, enabled = true }) {
|
|
70
|
+
let initializedSdk = false;
|
|
71
|
+
|
|
72
|
+
if (application) {
|
|
73
|
+
if (!enabled) {
|
|
74
|
+
installDevConsole();
|
|
75
|
+
}
|
|
76
|
+
if (enabled || isForceEnabled()) {
|
|
77
|
+
initializedSdk = initializeSdk(service, application, enabled);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
registerService(service);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
cleanup: () => state.services.delete(service.name),
|
|
85
|
+
initializedSdk,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
setServiceContext(key, context) {
|
|
90
|
+
const existing = state.services.get(key);
|
|
91
|
+
state.services.set(key, { ...existing, context });
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
package/src/datadog-provider.tsx
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import { datadogRum } from '@datadog/browser-rum';
|
|
2
1
|
import { FC, Fragment, PropsWithChildren, useEffect } from 'react';
|
|
3
2
|
import { HashRouting } from './hash-routing';
|
|
4
|
-
|
|
5
|
-
type User = Omit<import('@datadog/browser-core').User, 'id'> & { id: string };
|
|
3
|
+
import { setUser, type RumUser } from './set-user';
|
|
6
4
|
|
|
7
5
|
export interface Options {
|
|
8
6
|
hashRouting?: boolean;
|
|
9
7
|
}
|
|
10
8
|
|
|
11
|
-
const SetUser: FC<{ user?:
|
|
9
|
+
const SetUser: FC<{ user?: RumUser }> = ({ user }) => {
|
|
12
10
|
useEffect(() => {
|
|
13
11
|
if (user) {
|
|
14
|
-
|
|
12
|
+
setUser(user);
|
|
15
13
|
}
|
|
16
14
|
}, [user]);
|
|
17
15
|
|
|
@@ -21,7 +19,7 @@ const SetUser: FC<{ user?: User }> = ({ user }) => {
|
|
|
21
19
|
export const DatadogProvider: FC<
|
|
22
20
|
PropsWithChildren & {
|
|
23
21
|
isEnabled: boolean;
|
|
24
|
-
user?:
|
|
22
|
+
user?: RumUser;
|
|
25
23
|
options?: Options;
|
|
26
24
|
}
|
|
27
25
|
> = ({ children, isEnabled, user, options }) => {
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
|
|
2
|
+
|
|
3
|
+
const SERVICE_REGEX =
|
|
4
|
+
/unpkg\.servicetitan\.com\/@servicetitan\/(?<service>[\w-]+)(?:@(?<version>\d+\.\d+\.\d+(?:-[\w.]+)?))?/;
|
|
5
|
+
|
|
6
|
+
function parseService(event: RumEvent, input: string | undefined) {
|
|
7
|
+
const match = input?.match(SERVICE_REGEX);
|
|
8
|
+
|
|
9
|
+
const service = match?.groups?.service;
|
|
10
|
+
const version = match?.groups?.version;
|
|
11
|
+
|
|
12
|
+
if (service) {
|
|
13
|
+
event.service = service;
|
|
14
|
+
event.version = version ?? 'unknown';
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* See https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/?tab=npm#micro-frontend
|
|
23
|
+
*/
|
|
24
|
+
export function enrichEvent(event: RumEvent, context: RumEventDomainContext): void {
|
|
25
|
+
const stack =
|
|
26
|
+
(context && 'handlingStack' in context ? context.handlingStack : undefined) ??
|
|
27
|
+
(event.type === 'error' ? event.error?.stack : undefined);
|
|
28
|
+
|
|
29
|
+
const message = event.type === 'error' ? event.error?.message : undefined;
|
|
30
|
+
|
|
31
|
+
const url = event.type === 'resource' ? event.resource?.url : undefined;
|
|
32
|
+
|
|
33
|
+
for (const source of [stack, message, url]) {
|
|
34
|
+
if (parseService(event, source)) {
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|