@deeptracer/nextjs 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +269 -0
- package/package.json +3 -3
package/README.md
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# @deeptracer/nextjs
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@deeptracer/nextjs)
|
|
4
|
+
|
|
5
|
+
DeepTracer Next.js integration — automatic server-side error capture with one-file setup. The easiest way to add observability to a Next.js application.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @deeptracer/nextjs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Peer dependencies:** `next >=14`, `react >=18`, `react-dom >=18`
|
|
14
|
+
|
|
15
|
+
## Quick Start (1 file, 5 lines)
|
|
16
|
+
|
|
17
|
+
Create `instrumentation.ts` in your project root:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { init } from "@deeptracer/nextjs"
|
|
21
|
+
|
|
22
|
+
export const { register, onRequestError } = init({
|
|
23
|
+
product: "my-app",
|
|
24
|
+
service: "web",
|
|
25
|
+
environment: "production",
|
|
26
|
+
endpoint: "https://deeptracer.example.com",
|
|
27
|
+
apiKey: process.env.DEEPTRACER_API_KEY!,
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
That's it. All server-side errors are now captured automatically.
|
|
32
|
+
|
|
33
|
+
## What Gets Captured Automatically
|
|
34
|
+
|
|
35
|
+
With just `instrumentation.ts`, the following are captured without any additional code:
|
|
36
|
+
|
|
37
|
+
- **Server Component errors** — rendering failures in React Server Components
|
|
38
|
+
- **Route Handler errors** — uncaught exceptions in `app/api/**/route.ts`
|
|
39
|
+
- **Middleware errors** — failures in `middleware.ts` / `proxy.ts`
|
|
40
|
+
- **Uncaught exceptions** — `process.on("uncaughtException")` in Node.js runtime
|
|
41
|
+
- **Unhandled rejections** — `process.on("unhandledRejection")` in Node.js runtime
|
|
42
|
+
|
|
43
|
+
## API Reference (Server)
|
|
44
|
+
|
|
45
|
+
Import from `@deeptracer/nextjs`:
|
|
46
|
+
|
|
47
|
+
### `init(config)`
|
|
48
|
+
|
|
49
|
+
Initialize DeepTracer for Next.js. Returns `register` and `onRequestError` for direct re-export from `instrumentation.ts`.
|
|
50
|
+
|
|
51
|
+
**Parameters:**
|
|
52
|
+
- `config: NextjsConfig` — extends `LoggerConfig` with:
|
|
53
|
+
|
|
54
|
+
| Field | Type | Default | Description |
|
|
55
|
+
|-------|------|---------|-------------|
|
|
56
|
+
| `captureGlobalErrors` | `boolean` | `true` | Capture uncaught exceptions and unhandled rejections via `process.on` (Node.js runtime only). |
|
|
57
|
+
| `captureConsole` | `boolean` | `false` | Intercept `console.*` calls and forward to DeepTracer. |
|
|
58
|
+
|
|
59
|
+
**Returns:** `{ register, onRequestError, logger }`
|
|
60
|
+
|
|
61
|
+
| Property | Type | Description |
|
|
62
|
+
|----------|------|-------------|
|
|
63
|
+
| `register` | `() => void` | Called by Next.js on server start. Sets up error handlers. |
|
|
64
|
+
| `onRequestError` | `(err, request, context) => Promise<void>` | Called by Next.js on every server-side error. |
|
|
65
|
+
| `logger` | `Logger` | The Logger instance for manual logging, server actions, and route handlers. |
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// instrumentation.ts
|
|
69
|
+
import { init } from "@deeptracer/nextjs"
|
|
70
|
+
|
|
71
|
+
const deeptracer = init({
|
|
72
|
+
product: "my-app",
|
|
73
|
+
service: "web",
|
|
74
|
+
environment: "production",
|
|
75
|
+
endpoint: "https://deeptracer.example.com",
|
|
76
|
+
apiKey: process.env.DEEPTRACER_API_KEY!,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
export const { register, onRequestError } = deeptracer
|
|
80
|
+
export const logger = deeptracer.logger
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
### `withServerAction(logger, name, fn)`
|
|
86
|
+
|
|
87
|
+
Wrap a Next.js Server Action with automatic tracing and error capture. Creates a span and catches errors (re-throws after reporting).
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
// app/actions.ts
|
|
91
|
+
"use server"
|
|
92
|
+
import { withServerAction } from "@deeptracer/nextjs"
|
|
93
|
+
import { logger } from "@/instrumentation"
|
|
94
|
+
|
|
95
|
+
export async function createUser(formData: FormData) {
|
|
96
|
+
return withServerAction(logger, "createUser", async () => {
|
|
97
|
+
const name = formData.get("name") as string
|
|
98
|
+
const user = await db.user.create({ data: { name } })
|
|
99
|
+
return user
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
### `withRouteHandler(logger, name, handler)`
|
|
107
|
+
|
|
108
|
+
Wrap a Next.js Route Handler with automatic tracing and error capture. Creates a request-scoped span and extracts trace context from headers.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
// app/api/users/route.ts
|
|
112
|
+
import { withRouteHandler } from "@deeptracer/nextjs"
|
|
113
|
+
import { logger } from "@/instrumentation"
|
|
114
|
+
|
|
115
|
+
export const GET = withRouteHandler(logger, "GET /api/users", async (request) => {
|
|
116
|
+
const users = await db.user.findMany()
|
|
117
|
+
return Response.json(users)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
export const POST = withRouteHandler(logger, "POST /api/users", async (request) => {
|
|
121
|
+
const body = await request.json()
|
|
122
|
+
const user = await db.user.create({ data: body })
|
|
123
|
+
return Response.json(user, { status: 201 })
|
|
124
|
+
})
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
### Re-exported from @deeptracer/node
|
|
130
|
+
|
|
131
|
+
All exports from `@deeptracer/node` and `@deeptracer/core` are available:
|
|
132
|
+
|
|
133
|
+
- `createLogger`, `Logger`, `captureGlobalErrors`, `captureConsole`, `honoMiddleware`, `expressMiddleware`
|
|
134
|
+
- Types: `LoggerConfig`, `User`, `Breadcrumb`, `BeforeSendEvent`, `Span`, `InactiveSpan`, etc.
|
|
135
|
+
- Constants: `SDK_VERSION`, `SDK_NAME`
|
|
136
|
+
|
|
137
|
+
## API Reference (Client)
|
|
138
|
+
|
|
139
|
+
Import from `@deeptracer/nextjs/client`:
|
|
140
|
+
|
|
141
|
+
### `DeepTracerProvider`
|
|
142
|
+
|
|
143
|
+
React context provider for client-side error capture. See [`@deeptracer/react`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/react) for full docs.
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
// app/layout.tsx
|
|
147
|
+
import { DeepTracerProvider } from "@deeptracer/nextjs/client"
|
|
148
|
+
|
|
149
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
150
|
+
return (
|
|
151
|
+
<html>
|
|
152
|
+
<body>
|
|
153
|
+
<DeepTracerProvider config={{
|
|
154
|
+
product: "my-app",
|
|
155
|
+
service: "web",
|
|
156
|
+
environment: "production",
|
|
157
|
+
endpoint: process.env.NEXT_PUBLIC_DEEPTRACER_ENDPOINT!,
|
|
158
|
+
apiKey: process.env.NEXT_PUBLIC_DEEPTRACER_API_KEY!,
|
|
159
|
+
}}>
|
|
160
|
+
{children}
|
|
161
|
+
</DeepTracerProvider>
|
|
162
|
+
</body>
|
|
163
|
+
</html>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### `DeepTracerErrorPage`
|
|
169
|
+
|
|
170
|
+
Drop-in error page for `error.tsx` / `global-error.tsx`.
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
// app/global-error.tsx
|
|
174
|
+
"use client"
|
|
175
|
+
export { DeepTracerErrorPage as default } from "@deeptracer/nextjs/client"
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Other client exports
|
|
179
|
+
|
|
180
|
+
- `DeepTracerErrorBoundary` — class-based error boundary for wrapping React trees
|
|
181
|
+
- `useLogger()` — hook to access Logger from context
|
|
182
|
+
- `useDeepTracerErrorReporter(error)` — hook for custom error pages
|
|
183
|
+
|
|
184
|
+
## Full Setup (Server + Client)
|
|
185
|
+
|
|
186
|
+
### 1. Server-side (required)
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
// instrumentation.ts
|
|
190
|
+
import { init } from "@deeptracer/nextjs"
|
|
191
|
+
|
|
192
|
+
const deeptracer = init({
|
|
193
|
+
product: "my-app",
|
|
194
|
+
service: "web",
|
|
195
|
+
environment: "production",
|
|
196
|
+
endpoint: "https://deeptracer.example.com",
|
|
197
|
+
apiKey: process.env.DEEPTRACER_API_KEY!,
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
export const { register, onRequestError } = deeptracer
|
|
201
|
+
export const logger = deeptracer.logger
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 2. Client-side provider (optional)
|
|
205
|
+
|
|
206
|
+
```tsx
|
|
207
|
+
// app/layout.tsx
|
|
208
|
+
import { DeepTracerProvider } from "@deeptracer/nextjs/client"
|
|
209
|
+
|
|
210
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
211
|
+
return (
|
|
212
|
+
<html>
|
|
213
|
+
<body>
|
|
214
|
+
<DeepTracerProvider config={{
|
|
215
|
+
product: "my-app",
|
|
216
|
+
service: "web",
|
|
217
|
+
environment: "production",
|
|
218
|
+
endpoint: process.env.NEXT_PUBLIC_DEEPTRACER_ENDPOINT!,
|
|
219
|
+
apiKey: process.env.NEXT_PUBLIC_DEEPTRACER_API_KEY!,
|
|
220
|
+
}}>
|
|
221
|
+
{children}
|
|
222
|
+
</DeepTracerProvider>
|
|
223
|
+
</body>
|
|
224
|
+
</html>
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### 3. Error boundary (optional)
|
|
230
|
+
|
|
231
|
+
```tsx
|
|
232
|
+
// app/global-error.tsx
|
|
233
|
+
"use client"
|
|
234
|
+
export { DeepTracerErrorPage as default } from "@deeptracer/nextjs/client"
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Environment Variables
|
|
238
|
+
|
|
239
|
+
| Variable | Side | Description |
|
|
240
|
+
|----------|------|-------------|
|
|
241
|
+
| `DEEPTRACER_API_KEY` | Server | API key for server-side SDK |
|
|
242
|
+
| `NEXT_PUBLIC_DEEPTRACER_ENDPOINT` | Client | Ingestion endpoint URL |
|
|
243
|
+
| `NEXT_PUBLIC_DEEPTRACER_API_KEY` | Client | API key for client-side SDK |
|
|
244
|
+
| `NEXT_PUBLIC_DEEPTRACER_PRODUCT` | Client | Product name (for zero-config provider) |
|
|
245
|
+
| `NEXT_PUBLIC_DEEPTRACER_SERVICE` | Client | Service name (default: `"web"`) |
|
|
246
|
+
| `NEXT_PUBLIC_DEEPTRACER_ENVIRONMENT` | Client | `"production"` or `"staging"` (default: `"production"`) |
|
|
247
|
+
|
|
248
|
+
## Monorepo
|
|
249
|
+
|
|
250
|
+
This package is part of the [DeepTracer JavaScript SDK](https://github.com/getdeeptracer/deeptracer-js) monorepo:
|
|
251
|
+
|
|
252
|
+
| Package | Description |
|
|
253
|
+
|---------|-------------|
|
|
254
|
+
| [`@deeptracer/core`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/core) | Zero-dependency shared core |
|
|
255
|
+
| [`@deeptracer/node`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/node) | Node.js/Bun SDK |
|
|
256
|
+
| [`@deeptracer/ai`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/ai) | AI SDK wrappers |
|
|
257
|
+
| [`@deeptracer/browser`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/browser) | Browser SDK |
|
|
258
|
+
| [`@deeptracer/react`](https://github.com/getdeeptracer/deeptracer-js/tree/main/packages/react) | React integration |
|
|
259
|
+
| **`@deeptracer/nextjs`** | Next.js integration (this package) |
|
|
260
|
+
|
|
261
|
+
## Links
|
|
262
|
+
|
|
263
|
+
- [deeptracer.dev](https://deeptracer.dev) — Homepage
|
|
264
|
+
- [Docs](https://deeptracer.dev/docs) — Documentation
|
|
265
|
+
- [GitHub](https://github.com/getdeeptracer/deeptracer-js) — Source code
|
|
266
|
+
|
|
267
|
+
## License
|
|
268
|
+
|
|
269
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deeptracer/nextjs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "DeepTracer Next.js integration — automatic server-side error capture with one-file setup",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
},
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@deeptracer/node": "0.3.
|
|
32
|
-
"@deeptracer/react": "0.3.
|
|
31
|
+
"@deeptracer/node": "0.3.1",
|
|
32
|
+
"@deeptracer/react": "0.3.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"next": ">=14",
|