- Version: 10.0.0 and master
- Platform: all
- Subsystem: async_hooks
The following represent a pattern very commonly used by APMs:
'use strict'consthttp=require('http')constsleep=require('util').promisify(setTimeout)constasyncHooks=require('async_hooks')constasyncHook=asyncHooks.createHook({init, destroy})consttransactions=newMap()asyncHook.enable()functiongetCLS(){constasyncId=asyncHooks.executionAsyncId()returntransactions.has(asyncId) ? transactions.get(asyncId) : null}functionsetCLS(value){constasyncId=asyncHooks.executionAsyncId()transactions.set(asyncId,value)}functioninit(asyncId,type,triggerAsyncId,resource){transactions.set(asyncId,getCLS())}functiondestroy(asyncId){transactions.delete(asyncId)}http.createServer(function(req,res){handler().then(function(data){res.end(JSON.stringify(data))}).catch(function(err){res.statusCode=500res.end(err.stack)})}).listen(3000)varcounter=0asyncfunctionhandler(){setCLS(counter++)awaitsleep(10)return{cls: getCLS()}}Correct behavior (node 8.11.1):
$ curl localhost:3000
{"cls":0}
master:
$ curl localhost:3000
{"cls":null}
The following represent a pattern very commonly used by APMs:
Correct behavior (node 8.11.1):
master: