RomainLanz / sentry

A wrapper around the Sentry SDK to make it easier to use in a AdonisJS application
MIT License
24 stars 0 forks source link

Integrating Sentry outside request context in AdonisJS #2

Closed joaosilva0345 closed 1 month ago

joaosilva0345 commented 1 month ago

Hi, everyone!

I am using Sentry integrated with the IoC Container and HttpContext in my AdonisJS application. The installation and configuration of Sentry are working perfectly to capture errors within the HTTP request context. However, I have no idea how to integrate Sentry outside of this context, specifically in asynchronous methods and jobs.

Below is an example of a job I am using:

import { Job } from '@rlanz/bull-queue'

export default class TestJob extends Job {
  static get $$filepath() {
    return import.meta.url
  }

  async handle() {
   // How do I log a message in Sentry?
   }

  async rescue() {}
}

Can anyone guide me on how to access the Sentry service outside the request context, in asynchronous functions and jobs?

Thank you!

joaosilva0345 commented 1 month ago

I arrived at the following solution, but I don't know if it is correct:

import { inject } from '@adonisjs/core'
import { Job } from '@rlanz/bull-queue'
import { Sentry } from '@rlanz/sentry'

@inject()
export default class TestJob extends Job {
  constructor(private sentry: Sentry) {
    super()
  }

  static get $$filepath() {
    return import.meta.url
  }

  async handle() {
    console.log(this.sentry.getUser()) // test sentry
    return
  }

  async rescue() {}
}

Is the only way to access the sentry out of context through injection?

RomainLanz commented 1 month ago

Hey @joaosilva0345! 👋🏻

Yes, it is the way!

joaosilva0345 commented 1 month ago

Thank you.