Giter VIP home page Giter VIP logo

Comments (9)

aigoncharov avatar aigoncharov commented on September 6, 2024

I guess it should be something along these lines

import pino from "pino";
import { clsProxify, clsProxifyNamespace } from 'cls-proxify'

const logger = pino({ name: 'viewLinc', level: process.env.STAGE == 'prod' ? 'info' : 'debug' });
// wrap your logge with a proxy to get the request ID from the CLS
const loggerCls = clsProxify('requestId', logger)

export default loggerCls;
export default handler => middy(handler).use([
    () => {
      // Set  `requestId` in CLS to `requestId/co-relationId` so proxified pino picks it up
      setClsProxyValue('requestId', somehowGetValueOfRequestId-co-relationId())
    },
    loggerMiddleware({ logger: logger }), 
    //.....other middleware 
    httpErrorHandler(), ]);

from cls-proxify.

s1mrankaur avatar s1mrankaur commented on September 6, 2024

@aigoncharov 1.

  1. It throws " Middleware must be an object" at
 () => {
      // Set  `requestId` in CLS to `requestId/co-relationId` so proxified pino picks it up
      setClsProxyValue('requestId', somehowGetValueOfRequestId-co-relationId())
    },

I also tried

const baseHandler = (event, context) => {
  /* your business logic */
  // Set  `requestId` in CLS to `requestId/co-relationId` so proxified pino picks it up
  setClsProxyValue('requestId', 'testId')
}

// Export anonymous function
export default handler => middy(baseHandler).use([

])

and in this case, it throws :

**No context available. ns.run() or ns.bind() must be called first.**

  1. If it was working, wherever I say

  import logger.js
  logger.info("bla bla bla:)


will have the requestId although
setClsProxyValue('requestId', somehowGetValueOfRequestId-co-relationId())

is in the middleware and not where the logger object is created?

from cls-proxify.

aigoncharov avatar aigoncharov commented on September 6, 2024

@s1mrankaur I suggest you use https://github.com/aigoncharov/cls-proxify#any-other-framework-or-library as an example.
I have never used middy myself, but they seems to have a good section in their docs on writing middleware - https://github.com/middyjs/middy#writing-a-middleware. With that knowledge combined, it should be possible to write a CLS_enabled middleware.
Essential steps:

  • Create CLS context to run your request in via clsProxifyNamespace.run
  • Assign a request ID to a key in your CLS
  • Use that key to wrap your logger with a proxy

from cls-proxify.

s1mrankaur avatar s1mrankaur commented on September 6, 2024

@s1mrankaur I suggest you use https://github.com/aigoncharov/cls-proxify#any-other-framework-or-library as an example.
I have never used middy myself, but they seems to have a good section in their docs on writing middleware - https://github.com/middyjs/middy#writing-a-middleware. With that knowledge combined, it should be possible to write a CLS_enabled middleware.
Essential steps:

  • Create CLS context to run your request in via clsProxifyNamespace.run
  • Assign a request ID to a key in your CLS
  • Use that key to wrap your logger with a proxy

@aigoncharov If it was working, wherever I say


  import logger.js
  logger.info("bla bla bla:)

will have the requestId although
setClsProxyValue('requestId', somehowGetValueOfRequestId-co-relationId())
is in the middleware and not where the logger object is created?

Can you confirm this please?

from cls-proxify.

aigoncharov avatar aigoncharov commented on September 6, 2024

@s1mrankaur yes, this is correct.

from cls-proxify.

s1mrankaur avatar s1mrankaur commented on September 6, 2024

@aigoncharov Changed the middy handler as follows according to the steps in last message:


export default handler => middy(handler).
  before((async () => {
    const { context } = handler
    clsProxifyNamespace.run(() => {     //STEP 1
      const loggerProxy = {
        debug: (msg) => `${context.awsRequestId}: ${msg}`,
      }
      setClsProxyValue('requestId', loggerProxy);   //STEP 2
    });
   }))
   .use([
    loggerMiddleware({ logger: logger }),
]);

In pino-logger.ts:

import pino from "pino";
import { clsProxify, clsProxifyNamespace } from 'cls-proxify'

const logger = pino({ name: 'viewLinc', level: process.env.STAGE == 'prod' ? 'info' : 'debug' });
// wrap your logge with a proxy to get the request ID from the CLS
const loggerCls = clsProxify('requestId', logger)                           //STEP 3

export default loggerCls;

Added all 3 steps, Still nothing in traces though.

from cls-proxify.

aigoncharov avatar aigoncharov commented on September 6, 2024

This looks great! You seem to be on the right track.
Notice, how in the example in documentation we call 'next' inside of the 'namesapce.run'. It runs the rest of the request in the CLS context. You need to figure out something like that for middy

from cls-proxify.

s1mrankaur avatar s1mrankaur commented on September 6, 2024

@aigoncharov next() is deprecated in middy and the recommended way to do it is:

middy(handler).before((async () => {})

from cls-proxify.

s1mrankaur avatar s1mrankaur commented on September 6, 2024

Just to post the solution, https://www.npmjs.com/package/pino-lambda worked well for us. Thanks for your support.

from cls-proxify.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.