puzpuzpuz / cls-rtracer

Request Tracer - CLS-based request id generation for Express, Fastify, Koa and Hapi, batteries included
MIT License
311 stars 24 forks source link

I am unable to find the ctx arguments here where in I am trying to bind the context from my end #5

Closed kirankdeveloper closed 5 years ago

kirankdeveloper commented 5 years ago

https://github.com/puzpuzpuz/cls-rtracer/blob/19e7e8bc8aaf5e9feb2b18109f5ef2df500cdc51/index.js#L59

kirankdeveloper commented 5 years ago

I am unable to find the ctx arguments here where in I am trying to bind the context from my end: Code snippet I am using:

app.use(function* (next) { let koaMiddleWare = rTracer.koaMiddleware(); koaMiddleWare.bind(this.request.ctx, arguments); yield koaMiddleWare; yield next; });

puzpuzpuz commented 5 years ago

Could you describe what you're trying to achieve with this wrapper generator function? A sample script would also help.

aigoncharov commented 5 years ago

@kirankdeveloper could you also provide a stack trace?

kirankdeveloper commented 5 years ago

@keenondrums my library only accept the generator function so i have to use this way now somehow i am able to pass context to koaMiddleware function. This is my code now.

let koaMiddleWare = rTracer.koaMiddleware({ useHeader: true, headerName: 'test-header' }); yield koaMiddleWare(this.request.ctx, next);

However now my next issue is i want to access request id in another file call log.js which doesn't have access to request object. I am reading like this const requestId = rTracer.id(); but requestd is undefined here..

I think cls context losing somewhere because of that i am not able to get it. could you please guide me more about this.

puzpuzpuz commented 5 years ago

@kirankdeveloper if you're using Koa, then app.use() should accept the middleware from cls-rtracer as is (see this doc section):

app.use(rTracer.koaMiddleware({
  useHeader: true,
  headerName: 'test-header'
})

You can also check this sample for a more complete example.

kirankdeveloper commented 5 years ago

@puzpuzpuz I am using the older version of koa and for me if I try that way it is giving me an error that it expects generator function. rtracer.koaMiddleware returns a Promise so I am trying as following

app.use(function* (next) { let koaMiddleWare = rTracer.koaMiddleware({ useHeader: true, headerName: 'test-header' }); yield koaMiddleWare(this.request.ctx, next); });

This also seems to be working fine however I am unable to receive const rid = rTracer.id() on integration with my loggers

rishikapadia1 commented 5 years ago

@puzpuzpuz Looks like this library is not supported for an older version of Koa, Is there any hack which I can try to make this library work on older Koa (1.1.2)?

puzpuzpuz commented 5 years ago

@kirankdeveloper I see. Which version of Koa are you using?

@rishikapadia1 At the moment only Koa v2 is supported. But if there is a demand for Koa v1 support, I can implement it. Going to create a separate issue for this enhancement.

rishikapadia1 commented 5 years ago

@puzpuzpuz We (I and @kirankdeveloper) upgraded koa library and this is working great, you can close or resolve this issue. Thanks for your quick response.

puzpuzpuz commented 5 years ago

@kirankdeveloper @rishikapadia1 glad that you found a workaround. In any case, there is already the 1.2.0 release that include Koa v1 support.

kirankdeveloper commented 5 years ago

@puzpuzpuz Thank you for quickly responding on this. Nicely done!