autotel-adapters 2.0.0 → 2.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autotel-adapters",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Framework adapters and composable DX helpers for autotel",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -81,12 +81,11 @@
81
81
  },
82
82
  "files": [
83
83
  "dist",
84
- "README.md",
85
- "skills"
84
+ "README.md"
86
85
  ],
87
86
  "dependencies": {
88
- "autotel": "5.0.0",
89
- "autotel-edge": "4.0.0"
87
+ "autotel": "6.0.0",
88
+ "autotel-edge": "4.0.1"
90
89
  },
91
90
  "peerDependencies": {
92
91
  "hono": ">=4.12.31",
@@ -1,217 +0,0 @@
1
- ---
2
- name: autotel-adapters
3
- description: >
4
- Framework adapters for autotel that add request-scoped logging, tracing, and utility helpers for Next.js, Nitro, Cloudflare Workers, Hono, and TanStack Start.
5
- ---
6
-
7
- # autotel-adapters
8
-
9
- Framework-specific wrappers around autotel's core tracing primitives. Each adapter provides:
10
-
11
- - A handler wrapper that opens a span and binds a request-scoped `RequestLogger`
12
- - A `useLogger()` function that retrieves the logger from within the handler
13
- - Direct re-exports of the utilities `parseError`, `createStructuredError`, and `createDrainPipeline`
14
-
15
- Options are passed per call site (e.g. `withAutotel(handler, options)`). There are no
16
- `create*Adapter` factories or `*Toolkit` bundles.
17
-
18
- All adapters use `AsyncLocalStorage` (or a `WeakMap` for Cloudflare) internally so logger access is implicit. You never pass a logger through call chains manually.
19
-
20
- ## Setup
21
-
22
- Install the package and the relevant peer dependency for your framework:
23
-
24
- ```bash
25
- pnpm add autotel-adapters autotel
26
- # peer deps (install only the ones you use)
27
- pnpm add next # Next.js
28
- pnpm add nitropack h3 # Nitro
29
- pnpm add hono # Hono
30
- ```
31
-
32
- ## Configuration / Core Patterns
33
-
34
- ### Next.js
35
-
36
- Import from the subpath `autotel-adapters/next` (or from the barrel `autotel-adapters`).
37
-
38
- **Option A, per-handler wrap:**
39
-
40
- ```typescript
41
- import { withAutotel, useLogger } from 'autotel-adapters/next';
42
-
43
- export const GET = withAutotel(async (request) => {
44
- const log = useLogger(request);
45
- log.info('handling GET');
46
- return Response.json({ ok: true });
47
- });
48
- ```
49
-
50
- **Option B, share defaults by passing options at each wrap:**
51
-
52
- ```typescript
53
- // lib/autotel.ts
54
- import { withAutotel } from 'autotel-adapters/next';
55
-
56
- const autotelOptions = {
57
- spanName: (req) => `api ${new URL(req?.url ?? '/').pathname}`,
58
- enrichRequest: (req) => ({ 'tenant.id': req?.headers?.get('x-tenant-id') }),
59
- };
60
-
61
- export const withTracing = (handler) => withAutotel(handler, autotelOptions);
62
- ```
63
-
64
- `withAutotel` accepts any function whose first argument is `NextRequestLike`. The `spanName` option can be a static string or a function receiving the request. The auto-enrichment sets `http.request.method`, `url.full`, `http.route`, and `http.request.header.x-request-id`.
65
-
66
- ### Nitro
67
-
68
- ```typescript
69
- import { withAutotelEventHandler, useLogger } from 'autotel-adapters/nitro';
70
- import { defineEventHandler } from 'h3';
71
-
72
- export default defineEventHandler(
73
- withAutotelEventHandler(async (event) => {
74
- const log = useLogger(event);
75
- log.info('handling event');
76
- return { ok: true };
77
- }),
78
- );
79
- ```
80
-
81
- `withAutotelEventHandler` reads `event.method`, `event.path`, and `event.context.requestId` automatically. Pass options as the second argument to share config across handlers.
82
-
83
- ### Cloudflare Workers
84
-
85
- ```typescript
86
- import { withAutotelFetch, useLogger } from 'autotel-adapters/cloudflare';
87
-
88
- export default {
89
- fetch: withAutotelFetch(async (request, env, ctx) => {
90
- const log = useLogger(request);
91
- log.info('handling fetch');
92
- return new Response('ok');
93
- }),
94
- };
95
- ```
96
-
97
- Cloudflare stores the logger in a `WeakMap` keyed on the request object (no `AsyncLocalStorage` available in Workers). Auto-enrichment also reads `cf.country`, `cf.colo`, and `cf.city`. The `enrich` callback receives `(request, env, ctx)` for access to environment bindings.
98
-
99
- ### Hono
100
-
101
- ```typescript
102
- import { Hono } from 'hono';
103
- import { autotelMiddleware, useLogger } from 'autotel-adapters/hono';
104
-
105
- const app = new Hono();
106
- app.use('*', autotelMiddleware());
107
-
108
- app.get('/', (c) => {
109
- const log = useLogger();
110
- log.info('hello hono');
111
- return c.json({ ok: true });
112
- });
113
- ```
114
-
115
- Register `autotelMiddleware()` before your routes; it opens a span per request and
116
- binds the logger to `AsyncLocalStorage`, so `useLogger()` (no argument) resolves
117
- inside any downstream handler.
118
-
119
- ### TanStack Start
120
-
121
- ```typescript
122
- import { useLogger } from 'autotel-adapters/tanstack';
123
-
124
- // Inside a server function or API route already running in an autotel trace:
125
- const log = useLogger({ pathname: '/api/data', method: 'GET' });
126
- log.info('handling request');
127
- ```
128
-
129
- ### Utilities
130
-
131
- Each subpath re-exports `parseError`, `createStructuredError`, and
132
- `createDrainPipeline` directly (they also live in `autotel`):
133
-
134
- | Export | Description |
135
- | ------------------------------ | --------------------------------------------------------- |
136
- | `useLogger(ctx?, opts?)` | Get the request-scoped `RequestLogger` |
137
- | `parseError(error)` | Normalise any thrown value into `ParsedError` |
138
- | `createStructuredError(input)` | Build a `StructuredError` for consistent API error shapes |
139
- | `createDrainPipeline(opts?)` | Create a batching drain pipeline |
140
-
141
- ### Custom adapter (createUseLogger)
142
-
143
- ```typescript
144
- import { createUseLogger } from 'autotel-adapters/core';
145
-
146
- const useLogger = createUseLogger<MyContext>({
147
- adapterName: 'my-framework',
148
- enrich: (ctx) => ({ 'tenant.id': ctx.tenantId }),
149
- });
150
- ```
151
-
152
- ## Common Mistakes
153
-
154
- ### HIGH: Calling useLogger outside a traced handler
155
-
156
- ```typescript
157
- // WRONG: no active trace context
158
- export async function myFunction() {
159
- const log = useLogger(); // throws: "No active trace context"
160
- }
161
- ```
162
-
163
- ```typescript
164
- // CORRECT: always call useLogger inside a handler wrapped with withAutotel / withAutotelEventHandler
165
- export const GET = withAutotel(async (request) => {
166
- const log = useLogger(request); // ok — trace context is active
167
- await myFunction(log); // pass the logger down if needed
168
- });
169
- ```
170
-
171
- `useLogger` looks up an `AsyncLocalStorage` store populated by the handler wrapper. Calling it outside that wrapper throws.
172
-
173
- ### HIGH: Importing from wrong subpath
174
-
175
- ```typescript
176
- // WRONG
177
- import { useLogger } from 'autotel-adapters'; // this is Hono's useLogger from the barrel, not Next's
178
- ```
179
-
180
- ```typescript
181
- // CORRECT: use framework-specific subpaths
182
- import { autotelMiddleware, useLogger } from 'autotel-adapters/hono';
183
- import { useLogger } from 'autotel-adapters/next';
184
- import { withAutotelEventHandler } from 'autotel-adapters/nitro';
185
- ```
186
-
187
- ### MEDIUM: Skipping the request argument in Next.js useLogger
188
-
189
- ```typescript
190
- // WRONG: loses auto-enrichment (method, url, route, requestId)
191
- const log = useLogger();
192
- ```
193
-
194
- ```typescript
195
- // CORRECT: pass the request so auto-enrichment runs
196
- const log = useLogger(request);
197
- ```
198
-
199
- The request argument is optional only when you are certain the `AsyncLocalStorage` store is already populated (i.e., called from code deeply nested inside a `withAutotel`-wrapped handler).
200
-
201
- ### MEDIUM: Not passing request to useLogger on Cloudflare
202
-
203
- In Cloudflare Workers, the logger is stored per-request in a `WeakMap`. If you call `useLogger()` without the request object, you always get a new logger with no stored enrichment.
204
-
205
- ```typescript
206
- // WRONG
207
- const log = useLogger(); // new logger, not the one from withAutotelFetch
208
- ```
209
-
210
- ```typescript
211
- // CORRECT
212
- const log = useLogger(request); // retrieves from WeakMap
213
- ```
214
-
215
- ## Version
216
-
217
- Targets autotel-adapters v0.2.4. Peer frameworks: Next.js >=16.2.1, Hono >=4.12.9, Nitro/h3 ^2.0.0. See also: `autotel` (core), `autotel-backends` (vendor configs).