question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CorrelationContext.getCurrentContext() returning undefined when deeply nested

See original GitHub issue

Our auto-collected dependencies are inconsistently being correlated to the request (operation).

It appears that CorrelationContext.getCurrentContext() is working properly if called directly within the middleware (express) but if called further down (within callbacks and/or promises) it is returning undefined.

I have hacked in a workaround using continuation-local-storage:

var cls = require('continuation-local-storage');
var ns = cls.createNamespace('my.request');


//hack hack hack
var correlationContextManager = require('applicationinsights/out/AutoCollection/CorrelationContextManager').CorrelationContextManager;
var origGetCurrentContext = correlationContextManager.getCurrentContext;
function getCurrentContext(){
  return ns.get('ctx') || origGetCurrentContext();
}
correlationContextManager.getCurrentContext = getCurrentContext;

module.exports = function (req, res, next) {

  ns.bindEmitter(req);
  ns.bindEmitter(res);
  ns.run(function(){
    ns.set('ctx', origGetCurrentContext());
    next();
  });

};

We have all auto-collection enabled:

appInsights.setup(config.appInsightsKey)
  .start();

We use bluebird promises heavily, which may affect how the underlying zones function.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
markwolffcommented, May 17, 2019

Closing this as Zone.js has been deprecated in favor of cls-hooked, so this is likely no longer an issue

1reaction
OsvaldoRosadocommented, Aug 15, 2017

This looks to me like you’re seeing the effects of multiple versions of zone.js being loaded. Notably the version of zone.js currently depended on in this SDK does not include the bluebird patch files or the __load_patch method it uses. This makes me suspect you’ve npm installed another version of zone.js that’s incompatible. We should perhaps switch zone.js to peerDependency so this isn’t possible

Read more comments on GitHub >

github_iconTop Results From Across the Web

Automated Malware Analysis Report for f_00002a
_loadApplicationContext = function (packageJsonPath) {.. var version = "unknown";.. var description = undefined;.. try {.. // note: this should return the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found