Skip to content

Commit

Permalink
✨ [RUMF-771] Add getLoggerGlobalContext and getRumGlobalContext (#614)
Browse files Browse the repository at this point in the history
* 🏷️ [RUM] Use more specific return type

* 🎨 [RUM] Remove unused import

* ✨ [RUM] Add getLoggerGlobalContext

* ✨ [RUM] Add getRumGlobalContext

* 📝 [RUM] Update README

* 🎨 [RUM] Fix TSLint errors

* 🏷️ [RUM] Remove unecessary type
  • Loading branch information
webNeat authored Nov 13, 2020
1 parent 538bff0 commit b4b57c4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/boot/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
checkIsNotLocalFile,
combine,
Context,
ContextValue,
createContextManager,
defineGlobal,
getGlobalObject,
Expand Down Expand Up @@ -70,6 +69,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) {
isAlreadyInitialized = true
}),

getLoggerGlobalContext: monitor(globalContextManager.get),
setLoggerGlobalContext: monitor(globalContextManager.set),

addLoggerGlobalContext: monitor(globalContextManager.add),
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/domain/loggerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions packages/rum/src/boot/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit b4b57c4

Please sign in to comment.