Comments (15)
Our use case for these IDs in general is to use them as trace IDs in logs. We pass them on to the backend for every call as well, and every log statement in both the node app and the backend logs it out. That way we can trace a call through the whole stack. The client also passes it (and is picked up by the middleware provided by this module), so we can associate client request with db queries all the way through.
But since we cannot set the the id for non-request bound async work, we cannot trace that "request" through the system
from cls-rtracer.
Perfect, thanks! I'll test it right away
from cls-rtracer.
I forgot to report back - this worked wonderfully! Thank you so much for being so responsive and helpful here, and the work you and others do on ALS!
from cls-rtracer.
Thanks! I'm having troubles reproducing this locally, it seems to only happen when deployed, for some reason... Will keep on trying to get a reproduction locally
from cls-rtracer.
12.18.0 in prod, 12.18.1 locally. It works consistently locally with both 12.17.0 and 12.18.1. Hoping it's not something weird with the load balancers in GCP or something...
from cls-rtracer.
Would it be possible for us to manually set an ID before kicking of the async calls?
@SimenB
This sound a bit orthogonal with the main use case of cls-rtracer
, as these async calls seem to happen outside of requests (and web framework, in general).
Do you have multiple of such polling tasks? If it's only a single one per node.js app, why do you need to have an ID assigned to it? I'd like to understand the use case better before suggesting anything.
from cls-rtracer.
So, something like the following API would work for you?
rTracer.runWithId(() => {
const id = rTracer.id() // a new id will be available here
})
And the function will be runWithId(fn[, id])
, where id
is an optional override for the id available in the function.
from cls-rtracer.
If accessing rTracer.id()
inside any async functions called by rTracer.runWithId()
then yeah, that'd work 🙂
from cls-rtracer.
@SimenB then #31 should add the feature you need. 😉
from cls-rtracer.
I've just published v2.1.0 which includes this feature. Let me know if you face any issues.
It also includes the Fastify plugin.
from cls-rtracer.
@SimenB I'm glad to hear that the feature works fine. Thanks for your feedback!
from cls-rtracer.
@puzpuzpuz I've found one issue with this. For some reason, the very first request a client makes doesn't get an ID. I haven't dug into it (tried reproducing in a bare bones app without success), but I just noticed that those requests have been getting a trace id recently, but that ID is the same one as the one provided during boot. We have essentially this code
import rTracer from 'cls-rtracer';
import start from '@private/express-starter';
import setupApp from './server';
async function main() {
await rTracer.runWithId(async () => {
const app = await setupApp();
await start(app);
});
}
This gives a trace ID for all logging during startup, which is very nice. However, this ID is kept even after the function completes, and used in later requests which do not have an ID.
Any idea about what's going on? While the issue would probably go away if I figured out why the first request is missing an ID, shouldn't the old one be "garbage collected" when the function provided to runWithId
completes?
from cls-rtracer.
@SimenB hmm, sounds weird. I'll take a look at this issue later today.
from cls-rtracer.
@SimenB do you use the same node.js version locally and in production? Which one is that?
from cls-rtracer.
Any idea about what's going on? While the issue would probably go away if I figured out why the first request is missing an ID, shouldn't the old one be "garbage collected" when the function provided to
runWithId
completes?
@SimenB the ID will be available only within the function passed into runWithId()
method. But if somehow the request is being handled within async call chain that corresponds to the function (which is weird), then the ID may be available there. But it sounds really weird to me and I'd appreciate if you had a reproducer.
from cls-rtracer.
Related Issues (20)
- Add support for Fastify v3 HOT 1
- How to choose id generation algorithm? HOT 3
- Integration with winston JSON HOT 4
- Feature request - add generic request context object HOT 5
- Fastify Pino logger integration sample HOT 2
- Context is lost when using passport.js with session: false HOT 1
- Cannot access Request ID in form-data request using runWithId HOT 2
- Can't make it work with HAPI HOT 6
- How to add an optional/custom parameter in the cls-tracer express middleware request HOT 2
- id generation HOT 2
- Why does the Hapi plugin implementation use enterWith() instead of run()? HOT 3
- cls-rtracer causes pino-http >= version 7.1 to log success message (responses) twice HOT 9
- Memory leak in 2.6.1 HOT 2
- Restify Plugin Support HOT 2
- Question about multiple request HOT 4
- cannot get request id with koa-morgan HOT 2
- How to use with WebSocket? HOT 1
- add Additional information along with requestId HOT 2
- Can you set a unique id? HOT 7
- Expected requestId object is not returned with runWithId method 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 cls-rtracer.