Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Losing currentContext after installing Newrelic agent #1984

Closed
olivierl opened this issue Jan 23, 2016 · 13 comments
Closed

Losing currentContext after installing Newrelic agent #1984

olivierl opened this issue Jan 23, 2016 · 13 comments
Assignees
Labels

Comments

@olivierl
Copy link

When I add Newrelic agent on top of the server.js, the currentContext "lose" the accessToken in my operation hook...

Check this sandbox

server/server.js

require('newrelic');

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

app.use(loopback.context());
app.use(loopback.token({
  model: app.models.accessToken,
  currentUserLiteral: 'me'
}));

common/models/customer.js

Customer.observe('access', function limitToOwner(ctx, next) {
  var accessToken = loopback.getCurrentContext().get('accessToken');
  console.log(accessToken); // display 'undefined' in the console
  ctx.query = _.merge({where: {ownerId: accessToken.userId}}, ctx.query);
  next();
});

package.json

"loopback": "^2.26.2",
"loopback-boot": "^2.16.0",
"loopback-connector-postgresql": "^2.4.0",
"loopback-datasource-juggler": "^2.44.0",
"newrelic": "^1.25.0",
  • OS: Mac OS X El Capitain (10.11.3)
  • Node.js: 4.2.6 (with NVM 0.29.0)

REM: Everything is working fine on Node.js 0.12.x (tested on 0.12.7 and 0.12.9), so I only have the issue on Node.js 4.2.x (tested on 4.2.4 and 4.2.6)

@jannyHou jannyHou self-assigned this Jan 29, 2016
@esopian
Copy link

esopian commented Jan 29, 2016

+1

@jannyHou
Copy link
Contributor

hey @olivierl do you get the error from postgresql only or from the default db as well?

@olivierl
Copy link
Author

hi @jannyHou, I've made the test with the default db (memory) and it's worst, loopback.getCurrentContext() returns undefined

@jannyHou jannyHou added the bug label Feb 2, 2016
@jannyHou
Copy link
Contributor

jannyHou commented Feb 2, 2016

Thanks @olivierl I reproduced your error.
I created a sandbox to simplify your example and test it with default db:

When I hit the endpoint GET/ customers I got

"error": {
    "name": "TypeError",
    "status": 500,
    "message": "Cannot read property 'get' of null",
    "stack": "TypeError: Cannot read property 'get' of null\n    at limitToOwner (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\common\\models\\customer.js:7:51)\n    at notifySingleObserver (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:98:22)\n    at iterate (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:181:13)\n    at Object.async.eachSeries (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\node_modules\\async\\lib\\async.js:197:9)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:95:11)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at doNotify (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:93:49)\n    at Function.ObserverMixin._notifyBaseObservers (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:116:5)\n    at Function.ObserverMixin.notifyObserversOf (C:\\Users\\IBM_ADMIN\\Desktop\\triage3\\loopback-newrelic-issue\\node_modules\\loopback-datasource-juggler\\lib\\observer.js:91:8)"
  }

We will look into the problem!

One more question, I am not so familiar with newrelic agent, have you tried to get currentContext without it?

@andressrg
Copy link

+1

@jannyHou
Copy link
Contributor

jannyHou commented Feb 2, 2016

Thank you @esopian @andressrg for reporting this, we will take a look. One more question for better debug: are you also getting this error with newrelic or even without using newrelic?

@barocsi
Copy link

barocsi commented Feb 4, 2016

could be same origin as #1807

@jfurie
Copy link

jfurie commented Mar 8, 2016

+1

1 similar comment
@Valko54
Copy link

Valko54 commented Apr 27, 2016

+1

@a-c-m
Copy link

a-c-m commented Oct 5, 2016

+1

@viczam
Copy link

viczam commented Oct 5, 2016

-1

@odlevakp
Copy link

odlevakp commented Oct 5, 2016

+1

@bajtos
Copy link
Member

bajtos commented Jan 9, 2017

Hi, loopback-context is know to loose the context in many cases. We have deprecated it in favour of explicit context passing via the "options" argument, see #1495.

There is also an alternative approach being explored in https://github.com/strongloop/loopback-context, feel free to give it a try. If you do, then please report any issues found in loopback-context's issue tracker.

I am closing this issue.

@bajtos bajtos closed this as completed Jan 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests