Marking full-link logging helps to better resolve bugs and analyze interface performance, and this article uses Node as an example

  • Code sample
  • This paper addresses
  • github

What logs are generated when a request comes in

  • This request message
  • The database operation involved in this request
  • The cache operation involved in this request
  • The service request involved in this request
  • Exception encountered for this request
  • The key function executed for this request
  • The body of the response to this request

How do I query all logs of the request to response link this time

Each request is uniquely identified with a requestId, sometimes referred to as a sessionId or transactionId.

  1. userequestIdThis section describes the type of logs to be marked for each request
  2. Through theX-Request-IdThe (x-session-ID) flag is passed throughout the link in the request header
async function context (ctx: KoaContext, next: any) {
  const requestId = ctx.header['x-request-id'] || uuid()
  ctx.res.setHeader('requestId', requestId)
  ctx.requestId = requestId
  await next()
}

app.use('/todos/:id'.(ctx) = > {
  User.findByPk(ctx.body.id, {
    logging () {
      // log ctx.requestId}})})Copy the code

How can I mark each request in a less intrusive way

As mentioned, manually marking requestId on every database query is too tedious. Logger functions can be uniformly designed for markup

The code can be seen in one of my scaffolding logger.ts

The popular log library Winston (13582 Star) is used

import winston, { format } from 'winston'

const requestId = format((info) = > {
  info.requestId = session.get('requestId')
  return info
})

const logger = winston.createLogger({
  format: format.combine(
    format.timestamp(),
    requestId(),
    format.json()
  )
})
Copy the code

How to bind requestId in Logger. ts

Or how does logger.ts get the requestId throughout the request response lifecycle

  • The lifecycle of asynchronous behavior can be traced using async_hooks
  • You can get the requestId for each asynchronous request from clS-Hooked

See session.ts for the code

import { createNamespace } from 'cls-hooked'

const session = createNamespace('hello, world')

export { session }
Copy the code

How can I benefit from full-link logs

  1. whensentryWhen an abnormal alarm is received in (alarm system), passrequestIdCan be found inelkAll key logs about the exception (SQL, REDis, input and output of key functions) are retrieved from (logging system)
  2. Obtained from the request header when a request from the client is too slowrequestIdCan be found inelkTo analyze all database query time, request response time, cache hit and other indicators of the request
  3. Query the NUMBER of SQL statements executed by the API and check whether there are redundant SQL statements

In addition, zipkin can be used to track the total link time.


Welcome to pay attention to my public number shanyuexixing, here records my technical growth, welcome to exchange