Skip to content

Commit ec3c412

Browse files
Integrate adlrb/logs-node (#3914) into staging-43
Integrated commit sha: 26e2c93 Co-authored-by: mormubis <[email protected]>
2 parents 0e17f7b + 26e2c93 commit ec3c412

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

packages/core/src/tools/globalObject.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ 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 =
54+
// @ts-expect-error for Node.js-specific globals that are not present in browser environments
55+
typeof process !== 'undefined' && process.versions !== null && process.versions.node !== null

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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ 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+
24+
// Check if the SDK sent any events
25+
;(globalThis as any).__ddBrowserSdkExtensionCallback = () => {
26+
throw new Error('the SDK should not send events')
27+
}
28+
2029
// compat test
21-
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined })
22-
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined })
30+
31+
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
32+
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
2333
datadogRum.setUser({ id: undefined })
34+
35+
// Check the SDK is not started
36+
if (datadogLogs.getInternalContext() || datadogRum.getInternalContext()) {
37+
throw new Error('SDK should not start on SSR environments')
38+
}
2439
}

0 commit comments

Comments
 (0)