Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/romainlanz/sentry
A wrapper around the Sentry SDK to make it easier to use in a AdonisJS application
https://github.com/romainlanz/sentry
adonisjs sentry sentry-sdk
Last synced: 2 months ago
JSON representation
A wrapper around the Sentry SDK to make it easier to use in a AdonisJS application
- Host: GitHub
- URL: https://github.com/romainlanz/sentry
- Owner: RomainLanz
- License: mit
- Created: 2024-06-01T22:01:44.000Z (7 months ago)
- Default Branch: 0.1
- Last Pushed: 2024-10-17T07:23:54.000Z (3 months ago)
- Last Synced: 2024-10-19T09:49:31.038Z (3 months ago)
- Topics: adonisjs, sentry, sentry-sdk
- Language: TypeScript
- Homepage:
- Size: 27.3 KB
- Stars: 27
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[![typescript-image]][typescript-url]
[![gh-workflow-image]][gh-workflow-url]
[![npm-image]][npm-url]
[![npm-download-image]][npm-download-url]
[![license-image]][license-url]
`@rlanz/sentry` is a simple wrapper around the Sentry SDK to make it easier to use in a AdonisJS application.
## Installation
```sh
node ace add @rlanz/sentry
```## Usage
The package will automatically register a middleware, configure the Sentry SDK and add an instance of the SDK to the `IoC Container` and the `HttpContext`.
```ts
import type { HttpContext } from '@adonisjs/core/http'export default class HelloController {
greet({ params, sentry, response}: HttpContext) {
sentry.captureMessage(`Hello, ${params.name}!`)return response.ok({ message: `Hello, ${params.name}!` })
}
}
```Since the SDK is also added to the `IoC Container`, you can also use it in your services. If you are inside a request context, the SDK injected will be scoped to it.
```ts
import { inject } from '@adonisjs/core'
import { Sentry } from '@rlanz/sentry'@inject()
export class GreetingService {
constructor(private sentry: Sentry) {}
greet(name: string) {
this.sentry.captureMessage(`Hello, ${name}!`)
return `Hello, ${name}!`
}
}
```### Capturing Errors
You can capture errors by calling the `captureException` method on the SDK instance inside your exception handler.
```ts
export default class HttpExceptionHandler extends ExceptionHandler {
// ...async report(error: unknown, ctx: HttpContext) {
if (this.shouldReport(error as any)) {
ctx.sentry.captureException(error)
}return super.report(error, ctx)
}
}
```### Assigning User Context
You can assign user context to the Sentry SDK by calling the `setUser` method on the SDK instance once you are logged in.
```ts
export default class SilentAuthMiddleware {
async handle(ctx: HttpContext, next: NextFn) {
// We are authenticating the user
await ctx.auth.check()// If the user is authenticated, we assign the user context to Sentry
if (ctx.auth.isAuthenticated) {
const user = ctx.auth.getUserOrFail()
ctx.sentry.setUser({
id: user.id,
email: user.email,
username: user.username,
});
}
return await next();
}
}
```### Adding Integrations
Sentry provides multiple integrations to enhance the data captured by the SDK. You can add integrations by changing the `integrations` array inside the configuration `config/sentry.ts`.
For example, if you want to add profiling to your application, you can add the `Profiler` integration.
```sh
npm install @sentry/profiling-node@7
``````ts
// config/sentry.tsimport { nodeProfilingIntegration } from '@sentry/profiling-node';
export default defineConfig({
// ...
integrations: [nodeProfilingIntegration()],
profilesSampleRate: 0.2,
})
```[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/rlanz/sentry/checks.yml?branch=main&style=for-the-badge
[gh-workflow-url]: https://github.com/rlanz/sentry/actions/workflows/checks.yml
[npm-image]: https://img.shields.io/npm/v/@rlanz/sentry.svg?style=for-the-badge&logo=npm
[npm-url]: https://www.npmjs.com/package/@rlanz/sentry
[npm-download-image]: https://img.shields.io/npm/dm/@rlanz/sentry?style=for-the-badge
[npm-download-url]: https://www.npmjs.com/package/@rlanz/sentry
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: https://www.typescriptlang.org
[license-image]: https://img.shields.io/npm/l/@rlanz/sentry?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md