Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

having issues on aws..

walshe opened this issue · comments

commented

locally cls-hooked seems to be working fine. I have amiddleware:

``
app.use((req, res, next) => {

      //Create new CLS contect for each request
      //At this point CLS puts the new context in a map of contexts by current execution ID.
      session.run( () => {
        next();
      });
    })

I set some things further down in another middleware:

session.set("sub", req.auth.sub); if(req.headers['X-Amzn-Trace-Id']){ session.set('X-Amzn-Trace-Id', req.headers['X-Amzn-Trace-Id']); }

and I access it in a logging service:

const jsonFormatter = (logEntry) => {

  const session = getNamespace('session');
  
  const sub = session.get("sub");
  let json = (sub ) ? Object.assign({ sub: sub }, logEntry) : logEntry;
  
  const amazonTraceId = session.get('X-Amzn-Trace-Id')
  json = (amazonTraceId ) ? Object.assign({ 'X-Amzn-Trace-Id': amazonTraceId }, json) : json;

  logEntry[MESSAGE] = JSON.stringify(json); queotes etc
  return logEntry;

that works fine..

but on aws ecs/fargate I get an error on the session.get():

``
TypeError: Cannot read property 'get' of undefined

  | 2021-05-13T16:37:49.193+01:00 | at Format.jsonFormatter [as transform] (/usr/src/app/service/logging-service.js:25:23)
``