Skip to content

APM tracing with async_hooks broken on Node 10. #20516

Description

@mcollina
  • 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}

Metadata

Metadata

Assignees

No one assigned

    Labels

    async_hooksIssues and PRs related to the async hooks subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions