immobiliare/fastify-sentry

Enable a fastify/pino logger integration to add request pino logs to sentry as breadcrumbs

GlenTiki opened this issue · 6 comments

Just an idea I had as I'm working through adding this to a project.

dnlup commented

Hi @GlenTiki , thank you for the suggestion. It seems interesting but I am not sure I understand correctly . Can you share a poc implementation or an event screenshot that shows what you are working on?

we have a solution for this BUT we didn't figure out a way how to have request specific logs in breadcrumbs only, so even though you have logs, its not super useful

i have whipped up a quick gist for inspiration - its a new version I am currently evaluating so its not battle tested yet https://gist.github.com/tomasstrejcek/dfd37ff6efaf88477affcadd8775b81c

I think @tomasstrejcek is onto something here, I'll need to return focus to this issue soon to try it as I've since moved onto some other stuff.

@dnlup The idea here is when you look at an issue in sentry, it auto-captures console.log messages as breadcrumbs to this section (Seen here)
Screenshot 2023-09-14 at 12 11 36

But, if you're not using console as your logger, and leaning into fastifys built-in pino logger, the log messages you put in your handlers don't end up on sentry issues.

So ideally, I would have a server in fastify with a handler like below:

app.get("/", async (req, res) => {
  req.log.info({foo: "bar"}, "some message") // or app.log.info()
  //....
})

And in sentry, if this handler throws an error, I would see a breadcrumb for this log message (Like I would if I used the default sentry module, used console as my logger, and captured exceptions with that)

ARGUABLY: this is an upstream sentry module issue - they could improve pino support themselves to benefit us/others - getsentry/sentry-javascript#4192

But since this is closed upstream, I've come downstream to the sentry lib I'm using 😄

dnlup commented

Thank you @GlenTiki and @tomasstrejcek for the detailed description, I see the use case for this but I am not sure if it should be implemented in this plugin though. We can leave this open and discuss more about it, we could find a solution.

Lilja commented

I've made some work on this some time ago: https://github.com/Lilja/pino-sentry-breadcrumbs. Not sure if it will help

A basic solution to get fastify/pino logs to show up in sentry is to make pino emit logs to the console log methods, which sentry does instrument. I've just shared a code snippet for this in the upstream sentry issue: getsentry/sentry-javascript#4192 (comment)