diff --git a/packages/logs/README.md b/packages/logs/README.md index 5c5be52088..6770aded64 100644 --- a/packages/logs/README.md +++ b/packages/logs/README.md @@ -343,6 +343,7 @@ After the Datadog browser log library is initialized, it is possible to: - Set the entire context for all your loggers with the `setLoggerGlobalContext (context: Context)` API. - Add a context to all your loggers with `addLoggerGlobalContext (key: string, value: any)` API. +- Get the entire global context with `getLoggerGlobalContext ()` API. ##### NPM @@ -354,6 +355,8 @@ import { datadogLogs } from '@datadog/browser-logs' datadogLogs.setLoggerGlobalContext("{'env': 'staging'}") datadogLogs.addLoggerGlobalContext('referrer', document.referrer) + +const context = datadogLogs.getLoggerGlobalContext() // => {env: 'staging', referrer: ...} ``` #### CDN async @@ -368,6 +371,10 @@ DD_LOGS.onReady(function() { DD_LOGS.onReady(function() { window.DD_LOGS && DD_LOGS.addLoggerGlobalContext('referrer', document.referrer) }) + +DD_LOGS.onReady(function() { + var context = window.DD_LOGS && DD_LOGS.getLoggerGlobalContext() // => {env: 'staging', referrer: ...} +}) ``` **Note:** Early API calls must be wrapped in the `DD_LOGS.onReady()` callback. This ensures the code only gets executed once the SDK is properly loaded. @@ -380,6 +387,8 @@ For CDN sync, use: window.DD_LOGS && DD_LOGS.setLoggerGlobalContext({ env: 'staging' }) window.DD_LOGS && DD_LOGS.addLoggerGlobalContext('referrer', document.referrer) + +var context = window.DD_LOGS && DD_LOGS.getLoggerGlobalContext() // => {env: 'staging', referrer: ...} ``` **Note**: The `window.DD_LOGS` check is used to prevent issues if a loading failure occurs with the library. diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index df70a7bd4e..08965f79c2 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -3,7 +3,6 @@ import { checkIsNotLocalFile, combine, Context, - ContextValue, createContextManager, defineGlobal, getGlobalObject, @@ -70,6 +69,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) { isAlreadyInitialized = true }), + getLoggerGlobalContext: monitor(globalContextManager.get), setLoggerGlobalContext: monitor(globalContextManager.set), addLoggerGlobalContext: monitor(globalContextManager.add), diff --git a/packages/logs/src/domain/loggerSession.ts b/packages/logs/src/domain/loggerSession.ts index 8ff9459ad6..eab4925cc9 100644 --- a/packages/logs/src/domain/loggerSession.ts +++ b/packages/logs/src/domain/loggerSession.ts @@ -29,7 +29,7 @@ export function startLoggerSession(configuration: Configuration, areCookieAuthor } } -function computeTrackingType(configuration: Configuration): string { +function computeTrackingType(configuration: Configuration) { if (!performDraw(configuration.sampleRate)) { return LoggerTrackingType.NOT_TRACKED } diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index e541cf62ef..0e78897654 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -80,6 +80,7 @@ export function makeRumGlobal(startRumImpl: StartRum) { removeRumGlobalContext: monitor(globalContextManager.remove), + getRumGlobalContext: monitor(globalContextManager.get), setRumGlobalContext: monitor(globalContextManager.set), getInternalContext: monitor((startTime?: number) => {