sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

console.log from inside of breadcrumbCallback is bad times for everyone involved

Open lubelski opened this issue 9 years ago • 3 comments

Do you want to request a feature or report a bug?

Sort of a bug. Calling console.log inside of breadcrumbCallback (and I assume anything else that causes a side effect that would append a breadcrumb) results in bad times. Synchronously calling something that appends a breadcrumb will blow the stack. Asynchronously calling something will induce a kind of live-lock which is even less cool. However, something seems to conk out eventually and it doesn't just log into forever.

What is the current behavior?

repro:

to blow the stack:

breadcrumbCallback: function (crumb){
   console.log("crumb: ", crumb);
   return crumb;
}

or to spice things up:

var i = 1; 
...
breadcrumbCallback: function (crumb){
   setTimeout(function(){
      console.log("crumb " + i++, crumb);
   }, 0);
   return crumb;
}

What is the expected behavior?

My bad if this is a 'user error' but while I'm attempting to add this callback to my project, being able to log would be pretty helpful. My current workaround is to add console: false to autoBreadcrumbs, but that was not super obvious at first.

At first I thought maybe it should unwrap console.log during this check, but really this could apply to anything that appends a breadcrumb, so maybe it should suspend collection of breadcrumbs while processing the callback and warn the user if something is called? at least in dev mode I guess? minified version might not run such checks.

Though likely really does affect only console. No clue what you're doing if you're initiating xhr, dom, and location events within this callback.

Raven JS version: https://cdn.ravenjs.com/3.9.1/raven.min.js

lubelski avatar Jan 04 '17 18:01 lubelski

Oh geeze - thanks for the report. I can definitely see this happening; I've even gotten myself into similar loops while working on breadcrumb support for raven-node.

At the very least we should warn about this in the docs and mention that you can do console: false to disable console breadcrumbs to get around it. I agree with you, though, that it would be nicer to still be able to log from within the breadcrumbCallback during dev.

I'm not sure if it's reasonable to unwrap/rewrap console methods before/after each breadcrumbCallback invocation, but I think a potentially viable solution would be to:

  • flip a raven-instance-level flag before we invoke the breadcrumbCallback
  • flip it back when the breadcrumbCallback finishes
  • only run the breadcrumbCallback check if that flag is off

This way, we'll still capture any breadcrumbs generated (synchronously) by the breadcrumbCallback, but we won't filter them according to the breadcrumbCallback. I think this could be a reasonable compromise that enables the development workflow you're after without preventing anything someone would actually want to do in prod (assuming in prod nobody really wants breadcrumbCallback to generate more breadcrumbs). The caveat is that it doesn't help the asynchronous case at all, but I'm not inclined to worry about it since breadcrumbCallback's behavior is centered around its synchronous return value.

I'll update the docs with a heads up for now and wait for Ben's thoughts on code changes.

LewisJEllis avatar Jan 04 '17 19:01 LewisJEllis

yeah, that sounds pretty decent. I would just recommend that in the condition that you've yodawg'd yourself in the breadcrumb callback, it should throw a console.warn saying as much. Raven would have to block that from getting into a separate loop because that call would get breadcrumbed, but if that warning could only happen once globally, then you'd get one warning in your console, one extra breadcrumb somewhere in your logs, and I think everything else would work as expected.

lubelski avatar Jan 05 '17 16:01 lubelski

@LewisJEllis – I agree with the approach you outlined.

benvinegar avatar Mar 23 '17 05:03 benvinegar