|
| 1 | +# Sentry and Node.js LLM Rules |
| 2 | + |
| 3 | +You are an expert in application architecture and the configuration of observability tools like Sentry. Ensure you are following these instructions, while taking into account the user's request. |
| 4 | + |
| 5 | +Always ensure you are installing Sentry using `npm install @sentry/node@latest` or `yarn add @sentry/node@latest`. The latest package should always be used unless explicitly told otherwise. |
| 6 | + |
| 7 | +## Configuration examples |
| 8 | + |
| 9 | +These examples should be used to configure the different Sentry functionality in Node.js applications. Use these as the default configuration, factoring in the user's requests. |
| 10 | + |
| 11 | +### Sentry Setup (applies to all configurations) |
| 12 | + |
| 13 | +Configure Sentry using the following parameters unless explicitly told otherwise. |
| 14 | + |
| 15 | +```javascript |
| 16 | +import * as Sentry from "@sentry/node"; |
| 17 | + |
| 18 | +// Initialize Sentry as early as possible |
| 19 | +Sentry.init({ |
| 20 | + dsn: "<sentry dsn>", |
| 21 | + |
| 22 | + // Enable tracing to capture 100% of transactions |
| 23 | + // Recommend adjusting this value in production |
| 24 | + tracesSampleRate: 1.0, |
| 25 | + |
| 26 | + // Set tracePropagationTargets for which URLs trace propagation should be enabled |
| 27 | + tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/], |
| 28 | + |
| 29 | + // Enable HTTP capturing |
| 30 | + integrations: [ |
| 31 | + Sentry.httpIntegration(), |
| 32 | + ], |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +### Error Tracking and Exception Catching |
| 37 | + |
| 38 | +Instrument errors throughout the application using the following approaches: |
| 39 | + |
| 40 | +```javascript |
| 41 | +// Explicitly capture an exception |
| 42 | +try { |
| 43 | + throw new Error("Example error"); |
| 44 | +} catch (e) { |
| 45 | + Sentry.captureException(e); |
| 46 | +} |
| 47 | + |
| 48 | +// Capture a custom message |
| 49 | +Sentry.captureMessage("Something went wrong", "error"); |
| 50 | + |
| 51 | +// Add extra context to the error |
| 52 | +Sentry.configureScope((scope) => { |
| 53 | + scope.setTag("page_locale", "de-at"); |
| 54 | + scope.setUser({ id: '123', email: ' [email protected]' }); |
| 55 | + scope.setExtra("character_name", "Mighty Fighter"); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### Tracing and Performance Monitoring |
| 60 | + |
| 61 | +Utilize the following examples for tracing scenarios: |
| 62 | + |
| 63 | +```javascript |
| 64 | +// Create a transaction |
| 65 | +const transaction = Sentry.startTransaction({ |
| 66 | + op: "test", |
| 67 | + name: "My First Test Transaction" |
| 68 | +}); |
| 69 | + |
| 70 | +// Set transaction as the current scope |
| 71 | +Sentry.getCurrentHub().configureScope(scope => { |
| 72 | + scope.setSpan(transaction); |
| 73 | +}); |
| 74 | + |
| 75 | +// Create a child span |
| 76 | +const span = transaction.startChild({ op: "functionX", description: "Function doing work" }); |
| 77 | + |
| 78 | +try { |
| 79 | + // Do something... |
| 80 | + span.setStatus("ok"); |
| 81 | +} catch (error) { |
| 82 | + span.setStatus("internal_error"); |
| 83 | + throw error; |
| 84 | +} finally { |
| 85 | + // Finish the span |
| 86 | + span.finish(); |
| 87 | + // Finish the transaction |
| 88 | + transaction.finish(); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Express Framework Integration |
| 93 | + |
| 94 | +For Express.js applications: |
| 95 | + |
| 96 | +```javascript |
| 97 | +import express from "express"; |
| 98 | +import * as Sentry from "@sentry/node"; |
| 99 | + |
| 100 | +const app = express(); |
| 101 | + |
| 102 | +// Initialize Sentry - this must happen before other app middleware |
| 103 | +Sentry.init({ |
| 104 | + dsn: "<sentry dsn>", |
| 105 | + integrations: [ |
| 106 | + // Enable Express.js monitoring |
| 107 | + Sentry.expressIntegration(), |
| 108 | + Sentry.httpIntegration(), |
| 109 | + ], |
| 110 | + tracesSampleRate: 1.0, |
| 111 | +}); |
| 112 | + |
| 113 | +// The request handler must be the first middleware on the app |
| 114 | +app.use(Sentry.Handlers.requestHandler()); |
| 115 | + |
| 116 | +// All your controllers should go here |
| 117 | +app.get("/", function rootHandler(req, res) { |
| 118 | + res.end("Hello world!"); |
| 119 | +}); |
| 120 | + |
| 121 | +// The error handler must be registered before any other error middleware and after all controllers |
| 122 | +app.use(Sentry.Handlers.errorHandler()); |
| 123 | + |
| 124 | +app.listen(3000); |
| 125 | +``` |
0 commit comments