@servicetitan/datadog-rum 36.3.0 → 36.4.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.
Files changed (94) hide show
  1. package/dist/before-send.d.ts +11 -1
  2. package/dist/before-send.d.ts.map +1 -1
  3. package/dist/before-send.js +2 -56
  4. package/dist/before-send.js.map +1 -1
  5. package/dist/build-allowed-tracing-urls.d.ts +9 -0
  6. package/dist/build-allowed-tracing-urls.d.ts.map +1 -0
  7. package/dist/build-allowed-tracing-urls.js +54 -0
  8. package/dist/build-allowed-tracing-urls.js.map +1 -0
  9. package/dist/build-before-send.d.ts +4 -0
  10. package/dist/build-before-send.d.ts.map +1 -0
  11. package/dist/build-before-send.js +64 -0
  12. package/dist/build-before-send.js.map +1 -0
  13. package/dist/bundle-state.d.ts +10 -0
  14. package/dist/bundle-state.d.ts.map +1 -0
  15. package/dist/bundle-state.js +10 -0
  16. package/dist/bundle-state.js.map +1 -0
  17. package/dist/create-impl.d.ts +8 -0
  18. package/dist/create-impl.d.ts.map +1 -0
  19. package/dist/create-impl.js +65 -0
  20. package/dist/create-impl.js.map +1 -0
  21. package/dist/datadog-provider.d.ts +2 -5
  22. package/dist/datadog-provider.d.ts.map +1 -1
  23. package/dist/datadog-provider.js +2 -2
  24. package/dist/datadog-provider.js.map +1 -1
  25. package/dist/enrich-event.d.ts +6 -0
  26. package/dist/enrich-event.d.ts.map +1 -0
  27. package/dist/enrich-event.js +33 -0
  28. package/dist/enrich-event.js.map +1 -0
  29. package/dist/get-shared.d.ts +10 -0
  30. package/dist/get-shared.d.ts.map +1 -0
  31. package/dist/get-shared.js +16 -0
  32. package/dist/get-shared.js.map +1 -0
  33. package/dist/handle-resource.d.ts +3 -0
  34. package/dist/handle-resource.d.ts.map +1 -0
  35. package/dist/handle-resource.js +27 -0
  36. package/dist/handle-resource.js.map +1 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/init.d.ts +17 -0
  42. package/dist/init.d.ts.map +1 -0
  43. package/dist/init.js +28 -0
  44. package/dist/init.js.map +1 -0
  45. package/dist/set-user.d.ts +13 -0
  46. package/dist/set-user.d.ts.map +1 -0
  47. package/dist/set-user.js +22 -0
  48. package/dist/set-user.js.map +1 -0
  49. package/dist/shared-impl.d.ts +11 -0
  50. package/dist/shared-impl.d.ts.map +1 -0
  51. package/dist/shared-impl.js +3 -0
  52. package/dist/shared-impl.js.map +1 -0
  53. package/dist/types.d.ts +50 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +3 -0
  56. package/dist/types.js.map +1 -0
  57. package/dist/use-rum-service.d.ts +12 -0
  58. package/dist/use-rum-service.d.ts.map +1 -0
  59. package/dist/use-rum-service.js +25 -0
  60. package/dist/use-rum-service.js.map +1 -0
  61. package/package.json +3 -3
  62. package/src/__mocks__/index.ts +6 -0
  63. package/src/__mocks__/init-config.ts +6 -0
  64. package/src/__mocks__/process-event.ts +20 -0
  65. package/src/__mocks__/reset-shared-impl.ts +8 -0
  66. package/src/__mocks__/tracing-urls.ts +30 -0
  67. package/src/__mocks__/types.ts +1 -0
  68. package/src/__tests__/before-send.test.ts +39 -87
  69. package/src/__tests__/build-allowed-tracing-urls.test.ts +120 -0
  70. package/src/__tests__/build-before-send.test.ts +193 -0
  71. package/src/__tests__/create-impl.test.ts +211 -0
  72. package/src/__tests__/datadog-provider.test.tsx +55 -25
  73. package/src/__tests__/enrich-event.test.ts +34 -0
  74. package/src/__tests__/get-shared.test.ts +56 -0
  75. package/src/__tests__/handle-resource.test.ts +76 -0
  76. package/src/__tests__/init.test.ts +69 -0
  77. package/src/__tests__/integration.test.ts +147 -0
  78. package/src/__tests__/set-user.test.ts +61 -0
  79. package/src/__tests__/use-rum-service.test.tsx +82 -0
  80. package/src/before-send.ts +15 -63
  81. package/src/build-allowed-tracing-urls.ts +53 -0
  82. package/src/build-before-send.ts +81 -0
  83. package/src/bundle-state.ts +9 -0
  84. package/src/create-impl.ts +71 -0
  85. package/src/datadog-provider.tsx +4 -6
  86. package/src/enrich-event.ts +38 -0
  87. package/src/get-shared.ts +24 -0
  88. package/src/handle-resource.ts +30 -0
  89. package/src/index.ts +3 -0
  90. package/src/init.ts +39 -0
  91. package/src/set-user.ts +25 -0
  92. package/src/shared-impl.ts +9 -0
  93. package/src/types.ts +61 -0
  94. package/src/use-rum-service.ts +28 -0
@@ -0,0 +1,76 @@
1
+ import type { RumResourceEvent } from '@datadog/browser-rum';
2
+ import { type Mutable } from '../__mocks__';
3
+ import { handleResource } from '../handle-resource';
4
+
5
+ describe(`[datadog-rum] ${handleResource.name}`, () => {
6
+ const cfRay = 'ray-1';
7
+ const headers = new Map([['cf-ray', cfRay]]);
8
+
9
+ let event: Mutable<RumResourceEvent>;
10
+ let context: any;
11
+
12
+ function makeResourceEvent(type: 'fetch' | 'xhr', url: string) {
13
+ return {
14
+ type: 'resource',
15
+ resource: { type, url },
16
+ } as unknown as Mutable<RumResourceEvent>;
17
+ }
18
+
19
+ const subject = () => handleResource(event, context);
20
+
21
+ function itAddsCfRayToContext() {
22
+ test('adds the cf-ray value to event context', () => {
23
+ subject();
24
+
25
+ expect(event.context?.cfRay).toBe(cfRay);
26
+ });
27
+ }
28
+
29
+ function itDoesNotAddCfRay() {
30
+ test('does not add a cf-ray value', () => {
31
+ subject();
32
+
33
+ expect(event.context?.cfRay).toBeUndefined();
34
+ });
35
+ }
36
+
37
+ describe('with a "fetch" resource', () => {
38
+ beforeEach(() => {
39
+ event = makeResourceEvent('fetch', window.location.toString());
40
+ context = { response: { headers } };
41
+ });
42
+
43
+ itAddsCfRayToContext();
44
+
45
+ describe('when the request targets a different domain', () => {
46
+ beforeEach(() => (event = makeResourceEvent('fetch', 'https://example.com/')));
47
+
48
+ itDoesNotAddCfRay();
49
+ });
50
+
51
+ describe('without a cf-ray response header', () => {
52
+ beforeEach(() => (context = { response: { headers: new Map() } }));
53
+
54
+ itDoesNotAddCfRay();
55
+ });
56
+
57
+ describe('with pre-existing event context', () => {
58
+ beforeEach(() => (event.context = { existing: 'keep' }));
59
+
60
+ test('merges the cf-ray value without dropping existing keys', () => {
61
+ subject();
62
+
63
+ expect(event.context).toEqual({ existing: 'keep', cfRay });
64
+ });
65
+ });
66
+ });
67
+
68
+ describe('with an "xhr" resource', () => {
69
+ beforeEach(() => {
70
+ event = makeResourceEvent('xhr', window.location.toString());
71
+ context = { xhr: { getResponseHeader: (key: string) => headers.get(key) } };
72
+ });
73
+
74
+ itAddsCfRayToContext();
75
+ });
76
+ });
@@ -0,0 +1,69 @@
1
+ import { minimalApplication } from '../__mocks__/init-config';
2
+ import { bundleState } from '../bundle-state';
3
+ import { getOrCreateSharedImpl } from '../get-shared';
4
+ import { init } from '../init';
5
+ import type { InitRumOptions } from '../types';
6
+
7
+ jest.mock('../get-shared');
8
+
9
+ describe(`[datadog-rum] ${init.name}`, () => {
10
+ const cleanup = jest.fn();
11
+ const setServiceContext = jest.fn();
12
+ let initializedSdk: boolean;
13
+ let options: InitRumOptions;
14
+
15
+ beforeEach(() => {
16
+ jest.clearAllMocks();
17
+ bundleState.isSdkInitializer = false;
18
+ initializedSdk = false;
19
+ jest.mocked(getOrCreateSharedImpl).mockReturnValue({
20
+ init: jest.fn(() => ({ cleanup, initializedSdk })),
21
+ setServiceContext,
22
+ });
23
+ options = { service: { name: 'desktop' }, application: minimalApplication };
24
+ });
25
+
26
+ const subject = () => init(options);
27
+
28
+ test('forwards to the shared implementation', () => {
29
+ subject();
30
+
31
+ expect(getOrCreateSharedImpl().init).toHaveBeenCalledWith(options);
32
+ });
33
+
34
+ test('returns the cleanup from the implementation', () => {
35
+ expect(subject().cleanup).toBe(cleanup);
36
+ });
37
+
38
+ test('returns a setServiceContext bound to the service name', () => {
39
+ const context = { orgId: '42' };
40
+
41
+ subject().setServiceContext(context);
42
+
43
+ expect(setServiceContext).toHaveBeenCalledWith(options.service.name, context);
44
+ });
45
+
46
+ describe('when the implementation initialized the SDK', () => {
47
+ beforeEach(() => {
48
+ initializedSdk = true;
49
+ });
50
+
51
+ test('marks this bundle as the SDK initializer', () => {
52
+ subject();
53
+
54
+ expect(bundleState.isSdkInitializer).toBe(true);
55
+ });
56
+ });
57
+
58
+ describe('when the implementation did not initialize the SDK', () => {
59
+ beforeEach(() => {
60
+ initializedSdk = false;
61
+ });
62
+
63
+ test('does not mark this bundle as the SDK initializer', () => {
64
+ subject();
65
+
66
+ expect(bundleState.isSdkInitializer).toBe(false);
67
+ });
68
+ });
69
+ });
@@ -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
+ });
@@ -1,74 +1,26 @@
1
1
  import type {
2
2
  RumEvent,
3
3
  RumEventDomainContext,
4
- RumFetchResourceEventDomainContext,
5
4
  RumInitConfiguration,
6
- RumResourceEvent,
7
- RumXhrResourceEventDomainContext,
5
+ RumResourceEventDomainContext,
8
6
  } from '@datadog/browser-rum';
9
-
10
- const SERVICE_REGEX =
11
- /unpkg\.servicetitan\.com\/@servicetitan\/(?<service>[\w-]+)(?:@(?<version>\d+\.\d+\.\d+(?:-[\w.]+)?))?/;
12
-
13
- function isSameDomainRequest(url: string) {
14
- return new URL(url).hostname === window.location.hostname;
15
- }
16
-
17
- function parseService(event: RumEvent, input: string | undefined) {
18
- const match = input?.match(SERVICE_REGEX);
19
-
20
- const service = match?.groups?.service;
21
- const version = match?.groups?.version;
22
-
23
- if (service) {
24
- event.service = service;
25
- event.version = version ?? 'unknown';
26
- return true;
27
- }
28
-
29
- return false;
30
- }
7
+ import { enrichEvent } from './enrich-event';
8
+ import { handleResource } from './handle-resource';
31
9
 
32
10
  /**
33
- * See https://docs.datadoghq.com/real_user_monitoring/browser/advanced_configuration/?tab=npm#micro-frontend
11
+ * Used for `application.beforeSend`. Returning `false` drops the event app-wide;
12
+ * `void` / `true` passes it through.
34
13
  */
35
- function enrichEvent(event: RumEvent, context: RumEventDomainContext): void {
36
- const stack =
37
- (context && 'handlingStack' in context ? context.handlingStack : undefined) ??
38
- (event.type === 'error' ? event.error?.stack : undefined);
39
-
40
- const message = event.type === 'error' ? event.error?.message : undefined;
14
+ export type ApplicationBeforeSendHandler = (
15
+ event: RumEvent,
16
+ context: RumEventDomainContext
17
+ ) => boolean | void;
41
18
 
42
- const url = event.type === 'resource' ? event.resource?.url : undefined;
43
-
44
- for (const source of [stack, message, url]) {
45
- if (parseService(event, source)) {
46
- break;
47
- }
48
- }
49
- }
50
-
51
- function handleResource(event: RumResourceEvent, context: RumEventDomainContext): void {
52
- if (!isSameDomainRequest(event.resource.url)) {
53
- return;
54
- }
55
-
56
- let cfRay: string | undefined | null = undefined;
57
-
58
- switch (event.resource.type) {
59
- case 'fetch':
60
- cfRay = (context as RumFetchResourceEventDomainContext).response?.headers.get('cf-ray');
61
- break;
62
-
63
- case 'xhr':
64
- cfRay = (context as RumXhrResourceEventDomainContext).xhr?.getResponseHeader('cf-ray');
65
- break;
66
- }
67
-
68
- if (cfRay) {
69
- event.context = { ...event.context, cfRay };
70
- }
71
- }
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;
72
24
 
73
25
  export function beforeSend(
74
26
  handler?: RumInitConfiguration['beforeSend']
@@ -76,7 +28,7 @@ export function beforeSend(
76
28
  return function (event: RumEvent, context: RumEventDomainContext) {
77
29
  try {
78
30
  if (event.type === 'resource') {
79
- handleResource(event, context);
31
+ handleResource(event, context as RumResourceEventDomainContext);
80
32
  }
81
33
 
82
34
  enrichEvent(event, context);
@@ -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
+ };