Jeff-Lewis / cls-hooked

cls-hooked : CLS using AsynWrap or async_hooks instead of async-listener for node 4.7+
BSD 2-Clause "Simplified" License
759 stars 89 forks source link

Migration from CLS to CLS-hooked time outs requests #41

Open zatziky opened 5 years ago

zatziky commented 5 years ago

I don't know how to describe this issue and hope that you'll give me some hints of what is going on. Any, really any hints are highly appreciated as I can't debug the app to give me any useful info whatsoever. (It seems like a limit in my skills. :)

Background

Since we are using async/await, in our logging lib we have migrated from CLS to CLS-hooked. It fixed the context and we could reach any values needed again. However, when running tests, they started to time-out and the overall app started to react strangely.

All the migration meant only replacement of const continuationStorage = require('continuation-local-storage') // v3.2.1 for const continuationStorage = require('cls-hooked') // v4.2.2.

Node Versions tested

We have tested against these versions:

How do we use CLS?

  1. This file takes care of context creation. An excerpt:
    
    const continuationStorage = require('cls-hooked')

const createNamespace = continuationStorage.createNamespace const getNamespace = continuationStorage.getNamespace

const NSP_REQUEST = 'logzio-node-debug-request' const KEY_MDC = 'mdc' const requestNamespace = createNamespace(NSP_REQUEST)

function createContext(next) { const mdc = getAll() const mdcCopy = Object.assign({}, mdc) requestNamespace.run(() => { requestNamespace.set(KEY_MDC, mdcCopy) next() }) }

1. We create a new context for every incoming request. We're using socket.io

// MDC i const MDC = require('logzio-node-debug').MDC

socket.use((packet, next) => { MDC.createContext(() => { MDC.put('key', 'value') next() }) })


2. We also put other values during code execution:

const MDC = require('logzio-node-debug').MDC MDC.put('key2', 'value2')



#### Question
Is this a bug? Are we doing something wrong? 
zatziky commented 5 years ago

I managed to identify that the problems happen only with database tests. We are using mysqljs/mysql. Is it possible that it handles queueing improperly?