|
| 1 | +# Sentry and Bun 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 `bun add @sentry/bun@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 Bun applications. Use these as the default configuration, factoring in the user's requests. |
| 10 | + |
| 11 | +### Sentry Setup (applies to all configurations) |
| 12 | + |
| 13 | +Sentry should be initialized as early as possible in your application's lifecycle. Create a file named `instrument.js` that imports and initializes Sentry: |
| 14 | + |
| 15 | +```javascript |
| 16 | +import * as Sentry from "@sentry/bun"; |
| 17 | + |
| 18 | +// Initialize Sentry before importing any other modules |
| 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 | +``` |
| 30 | + |
| 31 | +Then import this file at the very beginning of your main application file: |
| 32 | + |
| 33 | +```javascript |
| 34 | +// Import this first! |
| 35 | +import "./instrument"; |
| 36 | + |
| 37 | +// Now import other modules |
| 38 | +import http from "http"; |
| 39 | + |
| 40 | +// Your application code goes here |
| 41 | +``` |
| 42 | + |
| 43 | +### Error Tracking and Exception Catching |
| 44 | + |
| 45 | +Instrument errors throughout the application using the following approaches: |
| 46 | + |
| 47 | +```javascript |
| 48 | +// Explicitly capture an exception |
| 49 | +try { |
| 50 | + throw new Error("Example error"); |
| 51 | +} catch (e) { |
| 52 | + Sentry.captureException(e); |
| 53 | +} |
| 54 | + |
| 55 | +// Capture a custom message |
| 56 | +Sentry.captureMessage("Something went wrong", "error"); |
| 57 | + |
| 58 | +// Add extra context to the error |
| 59 | +Sentry.configureScope((scope) => { |
| 60 | + scope.setTag("page_locale", "de-at"); |
| 61 | + scope.setUser({ id: '123', email: ' [email protected]' }); |
| 62 | + scope.setExtra("character_name", "Mighty Fighter"); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +### Tracing and Performance Monitoring |
| 67 | + |
| 68 | +Utilize the following examples for tracing scenarios: |
| 69 | + |
| 70 | +```javascript |
| 71 | +// Create a transaction |
| 72 | +const transaction = Sentry.startTransaction({ |
| 73 | + op: "test", |
| 74 | + name: "My First Test Transaction" |
| 75 | +}); |
| 76 | + |
| 77 | +// Set transaction as the current scope |
| 78 | +Sentry.getCurrentHub().configureScope(scope => { |
| 79 | + scope.setSpan(transaction); |
| 80 | +}); |
| 81 | + |
| 82 | +// Create a child span |
| 83 | +const span = transaction.startChild({ op: "functionX", description: "Function doing work" }); |
| 84 | + |
| 85 | +try { |
| 86 | + // Do something... |
| 87 | + span.setStatus("ok"); |
| 88 | +} catch (error) { |
| 89 | + span.setStatus("internal_error"); |
| 90 | + throw error; |
| 91 | +} finally { |
| 92 | + // Finish the span |
| 93 | + span.finish(); |
| 94 | + // Finish the transaction |
| 95 | + transaction.finish(); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Bun.serve Integration |
| 100 | + |
| 101 | +For Bun server applications: |
| 102 | + |
| 103 | +```javascript |
| 104 | +import * as Sentry from "@sentry/bun"; |
| 105 | + |
| 106 | +// Initialize Sentry before anything else |
| 107 | +Sentry.init({ |
| 108 | + dsn: "<sentry dsn>", |
| 109 | + integrations: [ |
| 110 | + // Add Bun server integration |
| 111 | + Sentry.bunServerIntegration(), |
| 112 | + ], |
| 113 | + tracesSampleRate: 1.0, |
| 114 | +}); |
| 115 | + |
| 116 | +// Example Bun server |
| 117 | +Bun.serve({ |
| 118 | + port: 3000, |
| 119 | + fetch(req) { |
| 120 | + const url = new URL(req.url); |
| 121 | + |
| 122 | + if (url.pathname === "/error") { |
| 123 | + // This will be automatically captured by Sentry |
| 124 | + throw new Error("Example server error"); |
| 125 | + } |
| 126 | + |
| 127 | + return new Response("Hello World!"); |
| 128 | + }, |
| 129 | +}); |
| 130 | +``` |
0 commit comments