Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Auto-breadcrumbs causes getContext warning #250

Closed
cbrunnkvist opened this issue Dec 22, 2016 · 4 comments
Closed

Auto-breadcrumbs causes getContext warning #250

cbrunnkvist opened this issue Dec 22, 2016 · 4 comments

Comments

@cbrunnkvist
Copy link

My primary use case for the raven client lib is in an Express(3) app, and it works fine even with {autoBreadcrumbs:true} but this causes warnings on the console like below:

raven@1.1.1 alert: Enabled automatic breadcrumbs for console
raven@1.1.1 alert: getContext called without context; this may indicate incorrect setup - refer to docs on contexts

I concluded that it was because at startup, before and after I have instantiated the Express app, I need to write some messages to the console. I recreated the error, using the Express example with the addition of one console.log() call:

var app = require('express')();
var Raven = require('raven');

var __DSN__ = 'https://....@sentry.io/....';
Raven.config(__DSN__, {autoBreadcrumbs:{console:true}}).install();

console.log('Starting server ... etc.'); // Is this a crime? :'-(

app.use(Raven.requestHandler());

app.get('/', function mainHandler(req, res) {
    throw new Error('Broke!');
});

app.use(Raven.errorHandler());

app.use(function onError(err, req, res, next) {
    res.statusCode = 500;
    res.end(res.sentry + '\n');
});

app.listen(3000);

Is there any way to use console.log outside of the Express app context like this, without this warning? Reporting and everything seems to work fine, (even console calls!). Please advice.

@LewisJEllis
Copy link
Contributor

LewisJEllis commented Dec 22, 2016

Yea, definitely makes sense that in this scenario you'd see that warning given the current implementation. It's a pretty reasonable use on your part, though. A few options here:

  • Wrap everything after .install() in a sort of top-level global context:
var app = require('express')();
var Raven = require('raven');

var __DSN__ = 'https://....@sentry.io/....';
Raven.config(__DSN__, {autoBreadcrumbs:{console:true}}).install();
Raven.context(function () {
  console.log('Starting server ... etc.'); // Is this a crime? :'-(

  app.use(Raven.requestHandler());

  app.get('/', function mainHandler(req, res) {
      throw new Error('Broke!');
  });

  app.use(Raven.errorHandler());

  app.use(function onError(err, req, res, next) {
      res.statusCode = 500;
      res.end(res.sentry + '\n');
  });

  app.listen(3000);
});
  • Just do .config(...), at first, then do console stuff, then do Raven.install() right before the first app.use
  • Help me decide on a better policy for the circumstances under which to fire this alert
    • Goal: make this console alert less aggressive in non-issue cases like yours, while not being so relaxed as to allow users to unwittingly botch the setup and then wonder why they aren't receiving any breadcrumbs
    • I'll think on this some more and come up with some ideas

Either way, I should probably add a bit to the docs on "if you're getting console alert xyz, here are things you might be doing to cause it"; I added this alert to make sure people don't try to getContext directly when it doesn't make sense to, but of course some other functionality uses getContext under the hood, so it can lead to confusing outcomes like this one.

@migueloller
Copy link

@LewisJEllis, is there be a way to keep the functionality as is but changing the internals so that logging to the console doesn't trigger the alert?

It would seem that internal calls could have a flag that suppresses the alert.

@LewisJEllis
Copy link
Contributor

I made getContext stop throwing this warning in this commit, as part of the changes toward #265.

I'm going to keep this open for now until I improve the docs to help avoid users accidentally botching the setup and getting confused when they have global breadcrumbs doing weird things.

@kamilogorek
Copy link
Contributor

This issue shouldn't be relevant in v2 anymore (OP used v1.1.1 for the report).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants