|
| 1 | +# Creating a new SDK |
| 2 | + |
| 3 | +While each SDK (e.g. `@sentry/react` or `@sentry/nextjs`) is somewhat unique, we try to follow some general themes when |
| 4 | +creating a new SDK. |
| 5 | + |
| 6 | +## Types of SDKs |
| 7 | + |
| 8 | +Broadly speaking, there are three types of SDKs: |
| 9 | + |
| 10 | +1. Browser SDKs (e.g. `@sentry/react` or `@sentry/angular`) |
| 11 | +2. Server SDKs (e.g. `@sentry/bun` or `@sentry/node`) |
| 12 | +3. Meta SDKs (e.g. `@sentry/nextjs` or `@sentry/sveltekit`) - which cover both Browser & Server |
| 13 | + |
| 14 | +Depending on what type of SDK you are creating, you'll have to include different things. |
| 15 | + |
| 16 | +## General Guidelines |
| 17 | + |
| 18 | +As a rule of thumb, we should follow these two ideas: |
| 19 | + |
| 20 | +a. Whenever possible, instrumentation should work without (or with as little as possible) user configuration. b. |
| 21 | +Instrumentation should follow common patterns for a specific platform. No config is always preferred, but if config is |
| 22 | +unavoidable, it should feel reasonable to users of the given framework. |
| 23 | + |
| 24 | +## 1. Browser SDKs |
| 25 | + |
| 26 | +A purely browser SDK generally should cover the following things: |
| 27 | + |
| 28 | +### 1a. Routing Instrumentation |
| 29 | + |
| 30 | +While we have a default `browserTracingIntegration`, this has no access to a router, and is thus only based on URLs. We |
| 31 | +should strive to provide a custom `browserTracingIntegration` for SDKs that can leverage the router & routing |
| 32 | +information. |
| 33 | + |
| 34 | +Ideally, this means that we can emit pageload & navigation spans with parametrized route names instead of full URLs. |
| 35 | + |
| 36 | +### 1b. Error Capturing |
| 37 | + |
| 38 | +We have global error handlers out of the box. However, in many frameworks there are ways for users to capture errors |
| 39 | +too, which may lead to them not bubble up to our global handlers. Generally, the goal is that all errors are captured by |
| 40 | +Sentry. |
| 41 | + |
| 42 | +Either we should use some hook (e.g. `app.on('error')`) to capture exceptions, or provide composables (e.g. an error |
| 43 | +boundary component in React) that users can use in their app to ensure all errors are captured by Sentry. |
| 44 | + |
| 45 | +### 1c. OPTIONAL: Component Tracking |
| 46 | + |
| 47 | +Optionally, we may also track component renders and similar things. These are stretch goals, though, and do not need to |
| 48 | +be part of an MVP. |
| 49 | + |
| 50 | +## 2. Server SDKs |
| 51 | + |
| 52 | +A purely server SDK generally should cover the following things: |
| 53 | + |
| 54 | +### 2a. `http.server` spans with route information |
| 55 | + |
| 56 | +Most SDKs that build on top of `@sentry/node` should automatically have basic `http.server` spans emitted by |
| 57 | +`httpIntegration`. However, these spans do not contain any routing information (e.g. a `http.route` attribute). A server |
| 58 | +SDK should make sure to add route information to these spans. |
| 59 | + |
| 60 | +If there are things that should be captured in spans that are not covered by `httpIntegration`, we may need to write our |
| 61 | +own instrumentation to capture `http.server` spans. |
| 62 | + |
| 63 | +### 2b. Error Capturing |
| 64 | + |
| 65 | +We have global error handlers out of the box. However, in many frameworks there are ways for users to capture errors |
| 66 | +too, which may lead to them not bubble up to our global handlers. Generally, the goal is that all errors are captured by |
| 67 | +Sentry. |
| 68 | + |
| 69 | +Either we should use some hook (e.g. `app.on('error')`) to capture exceptions, or provide composables (e.g. |
| 70 | +`setupFastifyErrorHandler(app)`) that user can call. |
| 71 | + |
| 72 | +### 2c. OPTIONAL: Middleware Tracking |
| 73 | + |
| 74 | +If possible, we may want to instrument middlewares, and create spans for them. |
| 75 | + |
| 76 | +### 2d. OPTIONAL: Additional features |
| 77 | + |
| 78 | +We may also want to instrument additional features, if applicable, including: |
| 79 | + |
| 80 | +- Crons |
| 81 | +- Cache module |
| 82 | + |
| 83 | +## 3. Meta SDKs |
| 84 | + |
| 85 | +Meta SDKs should contain both the things pointed out in 1. and 2, _PLUS_: |
| 86 | + |
| 87 | +### 3a. Connected Traces |
| 88 | + |
| 89 | +Traces from SSR (server side) should be continued in the client side (browser). Usually this means that we have to |
| 90 | +inject the trace data as `<meta>` tags into the rendered HTML pages. If possible, we should do that automatically. If |
| 91 | +there is no way to do that automatically, we should provide a utility for users to do it themselves. |
| 92 | + |
| 93 | +### 3b. Instrumented Server Components / API Routes / etc. |
| 94 | + |
| 95 | +Depending on the framework, we should instrument all the pieces that exist in this framework. This includes capturing |
| 96 | +errors & spans for things like: |
| 97 | + |
| 98 | +- Server Components |
| 99 | +- API Routes |
| 100 | +- Layouts |
| 101 | +- etc. |
| 102 | + |
| 103 | +When possible, we should auto-capture this. If not possible, we should provide utilities for users to do this |
| 104 | +themselves. |
0 commit comments