Comments (6)
The ability to remove global context key has been added to log in v1.15.0 and to rum in v1.20.0.
from browser-sdk.
The idea is similar to MDC (Mapped Diagnostic Context) in Java applications and it would be a nice addition!
from browser-sdk.
Hi @mboogerd!
About context
- Incrementally add contextual information to my logging, as my code progresses.
Would the logger.addContext (key: string, value: any)
suit this need?
- Remove contextual information when I leave the context for which that data is relevant.
We don't have a specific API to remove specific key/value of the context but you should achieve this behavior by setting a key to undefined.
logger.addContext("foo", undefined)
Let me know if this approach could work for you or if I missed something on your use case.
About console.logs
- Have console.log statements include the context
Yes, we don't automatically collect console.logs
for now, there is an open issue to track this feature: #400
from browser-sdk.
Hi @bcaudan, thanks for your response!
I had not considered that setting a key's value to undefined
would effectively clear it, but that makes absolute sense (facepalm moment) and does satisfy part of the need of clearing context. It narrows the difference between what I'm proposing and what is currently offered, but doesn't entirely nullify it. The difference is twofold, in part API convenience and in part preventing maintaining double state.
An example may help to clarify. Consider the following internal context-stack for an imaginary IRC clone:
[{app:my-chat-app, version: 1.0}, {module: chat, user:me}, {activeChannel: myTeamChannel}]
might be a stack of partial contexts, individually pushed as you navigate the chat app. The effective total context is the merge of all partial ones, and is the one that would be included with log-messages dispatched from the context. (Of course, internally, you would memoize those merges, but that would make the example bulky)
Convenience
- Pushing an additional layer of context
{module: chat, user: me}
has a minor convenience benefit over issuing several statements, but - Popping that layer when leaving the context is more convenient, because I no longer need to be aware of what keys were set, or have to be re-set.
Double state
With the method of clearing up key-value pairs by setting the value to undefined
, this is only an issue when multiple levels of context reuse a key. Imagine a context such as [{ nav: admin}, {nav: admin/users}, {nav: admin/users/[user]}]
. If I were to leave the context of the configuration of a single user, and return to the list of users, I would have to restore the previous nav: admin/users
value in the context. I cannot query the logging context, so I would either:
- have to maintain this as a second bit of state such that this can be restored dynamically, or
- ensure that when I leave the context, a bit of code is triggered that statically knows which value to restore.
Either way, it feels like stuff you want to concern yourself with as little as possible on an application level. Although it is less pressing for me, now that I know you can at least remove part of the context, I do think that managing those contexts incrementally would offer a nice API for users of this library.
from browser-sdk.
Thanks for the details @mboogerd!
I had not considered that setting a key's value to undefined would effectively clear it, but that makes absolute sense
I did not encounter this kind of case before but we should either document this behavior or offer a dedicated API for it.
About the convenience of a single API for adding/removing a set of context key, I totally agree.
We would probably need to put more though on the API but we could have something like:
// single key
logger.addContext("foo", "bar")
logger.removeContext("foo")
// multiple keys
logger.addContext({app:my-chat-app, version: 1.0})
logger.removeContext({app:my-chat-app, version: 1.0})
It will probably not be in our top priorities, but feel free to open a PR for it if you want.
from browser-sdk.
We would probably need to put more though on the API but we could have something like:
// single key logger.addContext("foo", "bar") logger.removeContext("foo") // multiple keys logger.addContext({app:my-chat-app, version: 1.0}) logger.removeContext({app:my-chat-app, version: 1.0})It will probably not be in our top priorities, but feel free to open a PR for it if you want.
This would be useful for projects that have used services like TrackJS in the past. TrackJS allows you to add metadata that can be removed when not needed anymore (https://docs.trackjs.com/browser-agent/sdk-reference/agent-methods/#removemetadata). This can be helpful when debugging errors that can be hard to reproduce or to have more context when a known error is triggered.
eg.
function errorHandler(statusBitmap, resourceId) {
trackJs.addMetadata('statusBitmap', statusBitmap);
trackJs.addMetadata('resourceId', resourceId);
console.error('Failed to launch resource');
trackJs.removeMetadata('statusBitmap');
trackJs.removeMetadata('resourceId');
}
from browser-sdk.
Related Issues (20)
- 💡 Support for "pending" for trackingConsent HOT 1
- 🙏 "Reached max sessionReplay events size queued for upload: 3MiB" meaning HOT 1
- 🙏 My support need HOT 3
- 💡 Prerendering support HOT 1
- 🐛 Track Click Action produces memory leak HOT 2
- 🙏 Inflated LCP + load time values HOT 2
- 🐛 Browser Logs SDK is constantly writing to _dd_s cookie HOT 4
- 🐛 API calls are not logged with withFetch() API in newer versions of Angular HOT 5
- Warnings on import: "SDK is loaded more than once. This is unsupported and might have unexpected behavior" HOT 4
- 🐛 Expiration of cookie __dd_s after setTrackingConsent('not-granted') call
- Invalid traceparent generated is causing dd-python library to throw errors HOT 3
- 🙏 Proxied RUM session replay fails to load in the dashboard HOT 2
- 🐛 Some requests being processed as both XHR and Fetch, produces duplicate headers HOT 3
- History API is wrapped wrong HOT 3
- 🐛 Warning message when setting Japanese tags HOT 3
- Support RUM work on Web-Worker HOT 1
- Response undefined in type=resource resource-type=XHR events HOT 2
- Client side page views seem to no longer be recognized (script tag version) HOT 4
- Reported error traces at Datadog RUM is truncated than original error traces of browser side. HOT 3
- 🐛 rum requests pending forever HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from browser-sdk.