Skip to content

Commit 2c62a9c

Browse files
authored
πŸ› [RUM-11848] Block Logs initialization on Node environments (#3904)
fix: block logs in node fix: compatibility test fix: linter warnings fix: super weird linter fix fix: throw the error πŸ› pr-feedback: replace node check πŸ› replace check πŸ› replace isworker by due to multiple events πŸ› add check for messages πŸ› pr-feedback: add 100% telemetry sample rate
1 parent ec8a26a commit 2c62a9c

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

β€Žpackages/core/src/tools/globalObject.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ export function getGlobalObject<T = typeof globalThis>(): T {
4848
// eslint-disable-next-line local-rules/disallow-side-effects
4949
export const globalObject = getGlobalObject<GlobalObject>()
5050

51+
export const isBrowserEnvironment = 'document' in globalObject
5152
export const isWorkerEnvironment = 'WorkerGlobalScope' in globalObject
53+
export const isNodeEnvironment = 'process' in globalObject

β€Žpackages/logs/src/boot/preStartLogs.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TrackingConsentState } from '@datadog/browser-core'
22
import {
3+
isNodeEnvironment,
34
createBoundedBuffer,
45
canUseEventBridge,
56
display,
@@ -55,6 +56,10 @@ export function createPreStartStrategy(
5556

5657
return {
5758
init(initConfiguration, errorStack) {
59+
if (isNodeEnvironment) {
60+
return
61+
}
62+
5863
if (!initConfiguration) {
5964
display.error('Missing configuration')
6065
return

β€Žpackages/logs/src/boot/startLogs.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TrackingConsentState, BufferedObservable, BufferedData, PageMayExitEvent } from '@datadog/browser-core'
22
import {
3+
isBrowserEnvironment,
34
Observable,
45
sendToExtension,
56
createPageMayExitObservable,
@@ -11,7 +12,6 @@ import {
1112
TelemetryService,
1213
createIdentityEncoder,
1314
startUserContext,
14-
isWorkerEnvironment,
1515
} from '@datadog/browser-core'
1616
import { startLogsSessionManager, startLogsSessionManagerStub } from '../domain/logsSessionManager'
1717
import type { LogsConfiguration } from '../domain/configuration'
@@ -55,7 +55,7 @@ export function startLogs(
5555

5656
const reportError = startReportError(lifeCycle)
5757
// Page exit is not observable in worker environments (no window/document events)
58-
const pageMayExitObservable = isWorkerEnvironment
58+
const pageMayExitObservable = !isBrowserEnvironment
5959
? new Observable<PageMayExitEvent>()
6060
: createPageMayExitObservable(configuration)
6161

β€Žpackages/logs/src/domain/networkError/networkErrorCollection.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { FetchResolveContext, XhrCompleteContext } from '@datadog/browser-core'
22
import {
3-
isWorkerEnvironment,
3+
isBrowserEnvironment,
44
Observable,
55
ErrorSource,
66
initXhrObservable,
@@ -28,7 +28,7 @@ export function startNetworkErrorCollection(configuration: LogsConfiguration, li
2828

2929
// XHR is not available in web workers, so we use an empty observable that never emits
3030
const xhrSubscription = (
31-
isWorkerEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
31+
!isBrowserEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
3232
).subscribe((context) => {
3333
if (context.state === 'complete') {
3434
handleResponse(RequestType.XHR, context)

β€Žpackages/rum-core/src/boot/preStartRum.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { TrackingConsentState, DeflateWorker, Context, ContextManager, BoundedBuffer } from '@datadog/browser-core'
22
import {
3+
isNodeEnvironment,
34
createBoundedBuffer,
45
display,
56
canUseEventBridge,
@@ -100,6 +101,10 @@ export function createPreStartStrategy(
100101
}
101102

102103
function doInit(initConfiguration: RumInitConfiguration, errorStack?: string) {
104+
if (isNodeEnvironment) {
105+
return
106+
}
107+
103108
const eventBridgeAvailable = canUseEventBridge()
104109
if (eventBridgeAvailable) {
105110
initConfiguration = overrideInitConfigurationForBridge(initConfiguration)

β€Žtest/apps/vanilla/app.tsβ€Ž

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ if (typeof window !== 'undefined') {
1717
window.RUM_INIT()
1818
}
1919
} else {
20+
// Document is always generated by the SSR environment
21+
// @ts-ignore If document is mocked, the SDK executes further enough to throw an error
22+
globalThis.document = {}
23+
;(globalThis as any).__ddBrowserSdkExtensionCallback = () => {
24+
throw new Error('the SDK should not send events')
25+
}
26+
2027
// compat test
21-
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined })
22-
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined })
28+
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
29+
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
2330
datadogRum.setUser({ id: undefined })
31+
32+
if (datadogLogs.getInternalContext() || datadogRum.getInternalContext()) {
33+
throw new Error('SDK should not start on SSR environments')
34+
}
2435
}

0 commit comments

Comments
Β (0)