@logbrew/next 0.1.1 → 0.1.2
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 +89 -8
- package/client.cjs +1 -1
- package/client.js +1 -1
- package/examples/client-route-spans.mjs +1 -1
- package/examples/readme-example.mjs +1 -1
- package/examples/real-user-smoke.mjs +1 -1
- package/index.cjs +9 -5
- package/index.d.cts +8 -1
- package/index.d.ts +8 -1
- package/index.js +9 -5
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -11,8 +11,68 @@ This package is intentionally thin. It creates server-side LogBrew clients, wrap
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
npm install @logbrew/sdk @logbrew/next next react react-dom
|
|
15
|
-
pnpm add @logbrew/sdk @logbrew/next next react react-dom
|
|
14
|
+
npm install @logbrew/sdk @logbrew/node @logbrew/next next react react-dom
|
|
15
|
+
pnpm add @logbrew/sdk @logbrew/node @logbrew/next next react react-dom
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Set `LOGBREW_SERVER_API_KEY` to a project-scoped server ingest key before starting
|
|
19
|
+
the app.
|
|
20
|
+
|
|
21
|
+
If you are automating setup, follow the same safe path as a person.
|
|
22
|
+
Do not ask a person to paste an ingest key.
|
|
23
|
+
Do not treat a missing environment variable as a reason to stop setup.
|
|
24
|
+
First run `logbrew status --json`. When it reports approved authentication,
|
|
25
|
+
create a temporary project and key with the commands below.
|
|
26
|
+
Continue without a browser handoff. Confirm authenticated readback, and then
|
|
27
|
+
archive the project.
|
|
28
|
+
Only stop for account authentication when the status command reports that no
|
|
29
|
+
approved authentication is available.
|
|
30
|
+
|
|
31
|
+
## Create a Project and Confirm Hosted Delivery
|
|
32
|
+
|
|
33
|
+
Use LogBrew CLI 0.1.32 or newer with approved account authentication to create
|
|
34
|
+
the project. This requires no dashboard sign-in. The destination key file must
|
|
35
|
+
not already exist. The CLI creates it with owner-only permissions and never
|
|
36
|
+
prints the one-time key or its file path.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
logbrew status --json
|
|
40
|
+
install -d -m 700 "$HOME/.logbrew"
|
|
41
|
+
|
|
42
|
+
project_result="$(
|
|
43
|
+
logbrew projects create next-service \
|
|
44
|
+
--runtime node \
|
|
45
|
+
--environment development \
|
|
46
|
+
--ingest-key-file "$HOME/.logbrew/next-service.ingest" \
|
|
47
|
+
--json
|
|
48
|
+
)"
|
|
49
|
+
export LOGBREW_PROJECT_ID="$(jq -er '.project.id' <<<"$project_result")"
|
|
50
|
+
unset project_result
|
|
51
|
+
export LOGBREW_SERVER_API_KEY="$(< "$HOME/.logbrew/next-service.ingest")"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Start the app after wrapping the Route Handler below, then request one safe
|
|
55
|
+
route that your app already owns. For example:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
curl --fail --request POST http://127.0.0.1:3000/api/logbrew
|
|
59
|
+
|
|
60
|
+
logbrew doctor --project "$LOGBREW_PROJECT_ID" --json
|
|
61
|
+
logbrew read logs --project "$LOGBREW_PROJECT_ID" \
|
|
62
|
+
--search "POST /api/logbrew" \
|
|
63
|
+
--since 1h \
|
|
64
|
+
--json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
A successful ingest response proves submission. The authenticated read proves
|
|
68
|
+
that the event is indexed for the same project. If you no longer need the
|
|
69
|
+
temporary project, archive it and remove its revoked one-time key file:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
unset LOGBREW_SERVER_API_KEY
|
|
73
|
+
logbrew projects archive "$LOGBREW_PROJECT_ID" --yes --json
|
|
74
|
+
rm -f "$HOME/.logbrew/next-service.ingest"
|
|
75
|
+
unset LOGBREW_PROJECT_ID
|
|
16
76
|
```
|
|
17
77
|
|
|
18
78
|
## Release Artifacts
|
|
@@ -47,7 +107,6 @@ The helper defaults minified URLs to `app:///_next/static/chunks/...`. Use `mini
|
|
|
47
107
|
|
|
48
108
|
```js
|
|
49
109
|
// app/api/logbrew/route.js
|
|
50
|
-
import { RecordingTransport } from "@logbrew/sdk";
|
|
51
110
|
import { getActiveLogBrewTrace, withLogBrewRouteHandler } from "@logbrew/next";
|
|
52
111
|
|
|
53
112
|
export const runtime = "nodejs";
|
|
@@ -68,15 +127,37 @@ export const POST = withLogBrewRouteHandler(
|
|
|
68
127
|
return Response.json(JSON.parse(client.previewJson()));
|
|
69
128
|
},
|
|
70
129
|
{
|
|
71
|
-
serverApiKey:
|
|
130
|
+
serverApiKey: process.env.LOGBREW_SERVER_API_KEY,
|
|
72
131
|
spanIdFactory: () => "b7ad6b7169203331",
|
|
73
|
-
|
|
132
|
+
onCaptureError(error) {
|
|
133
|
+
console.error("LogBrew route capture failed", error);
|
|
134
|
+
}
|
|
74
135
|
}
|
|
75
136
|
);
|
|
76
137
|
```
|
|
77
138
|
|
|
139
|
+
With no `transport` option, the wrapper sends to the LogBrew ingest API by default
|
|
140
|
+
through `@logbrew/node`. You can pass `endpoint`, `fetchImpl`, or `headers` to
|
|
141
|
+
configure that fetch transport, or pass an explicit LogBrew `Transport`.
|
|
142
|
+
Delivery failures call `onCaptureError`; they are never converted into a
|
|
143
|
+
simulated successful response.
|
|
144
|
+
|
|
78
145
|
Use `serverApiKey` directly for local server examples, or set `LOGBREW_SERVER_API_KEY` in your server environment and omit it. `apiKey` and `LOGBREW_API_KEY` are still accepted for compatibility with lower-level SDK examples, but Next.js Route Handlers should use server-side keys only. Use the Browser, React, Vue, Svelte, Angular, or React Native packages for frontend `clientKey` setup.
|
|
79
146
|
|
|
147
|
+
`RecordingTransport` remains available from `@logbrew/sdk` when you want to
|
|
148
|
+
inspect serialized server events locally. It does not send data to LogBrew;
|
|
149
|
+
use the default Node fetch transport for production telemetry:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
import { RecordingTransport } from "@logbrew/sdk";
|
|
153
|
+
|
|
154
|
+
const previewTransport = RecordingTransport.alwaysAccept();
|
|
155
|
+
export const POST = withLogBrewRouteHandler(handler, {
|
|
156
|
+
serverApiKey: "local-preview-key",
|
|
157
|
+
transport: previewTransport
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
80
161
|
By default, successful Route Handler responses are captured after your handler returns. When the incoming `Request` has a valid W3C `traceparent` header, the wrapper exposes a normalized trace object through `helpers.trace` and `getActiveLogBrewTrace()`, then records the request as a LogBrew `span` that continues the incoming trace. Use that same `traceId` and `spanId` in app-owned logs, product actions, or custom event callbacks when you want route-level debugging correlation. Requests without `traceparent`, or with a malformed header, fall back to a request `log` event so bad client headers do not break your route. Use `captureRequests: false` when a route should only flush manual events, use `spanIdFactory` when your runtime needs app-provided child span IDs, and use `onCaptureError` to observe telemetry delivery failures without letting observability own the route response.
|
|
81
162
|
|
|
82
163
|
The public trace helper only includes normalized W3C IDs plus sampled state: `traceId`, `spanId`, `parentSpanId`, and `sampled`. It does not expose the raw `traceparent` header, request headers, request bodies, cookies, or query strings.
|
|
@@ -113,7 +194,7 @@ import {
|
|
|
113
194
|
const client = createLogBrewNextBrowserClient({
|
|
114
195
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
115
196
|
sdkName: "my-next-app",
|
|
116
|
-
sdkVersion: "0.1.
|
|
197
|
+
sdkVersion: "0.1.2"
|
|
117
198
|
});
|
|
118
199
|
|
|
119
200
|
const routePatterns = [
|
|
@@ -150,10 +231,10 @@ import { createLogBrewNextClient } from "@logbrew/next";
|
|
|
150
231
|
const client = createLogBrewNextClient({
|
|
151
232
|
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
152
233
|
sdkName: "my-next-app",
|
|
153
|
-
sdkVersion: "0.1.
|
|
234
|
+
sdkVersion: "0.1.2"
|
|
154
235
|
});
|
|
155
236
|
```
|
|
156
237
|
|
|
157
238
|
## Example Source
|
|
158
239
|
|
|
159
|
-
The package includes example source for App Router Route Handlers, server-side client creation, and app-owned responses. Use the snippets above as the starting point for wiring LogBrew into your Next.js application.
|
|
240
|
+
The package includes example source for App Router Route Handlers, server-side client creation, and app-owned responses. The packaged server examples use an explicit `RecordingTransport` so they can run offline and print serialized event batches without sending them. Use the default network-delivery snippets above as the starting point for wiring LogBrew into your Next.js application.
|
package/client.cjs
CHANGED
package/client.js
CHANGED
|
@@ -13,7 +13,7 @@ const routeTemplate = createNextRouteTemplate({
|
|
|
13
13
|
const client = createLogBrewNextBrowserClient({
|
|
14
14
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
15
15
|
sdkName: "logbrew-next-client-route-example",
|
|
16
|
-
sdkVersion: "0.1.
|
|
16
|
+
sdkVersion: "0.1.2"
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
const event = captureNextNavigation(client, {
|
package/index.cjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
const { AsyncLocalStorage } = require("node:async_hooks");
|
|
2
2
|
const {
|
|
3
3
|
LogBrewClient,
|
|
4
|
-
RecordingTransport,
|
|
5
4
|
SdkError,
|
|
6
5
|
parseTraceparent
|
|
7
6
|
} = require("@logbrew/sdk");
|
|
7
|
+
const { createNodeFetchTransport } = require("@logbrew/node");
|
|
8
8
|
|
|
9
9
|
const DEFAULT_SDK_NAME = "logbrew-next";
|
|
10
|
-
const DEFAULT_SDK_VERSION = "0.1.
|
|
10
|
+
const DEFAULT_SDK_VERSION = "0.1.2";
|
|
11
11
|
const activeTraceContext = new AsyncLocalStorage();
|
|
12
12
|
|
|
13
13
|
function createLogBrewNextClient({
|
|
@@ -32,9 +32,13 @@ function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
32
32
|
throw new SdkError("configuration_error", "withLogBrewRouteHandler requires a handler function");
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
const defaultTransport = options.transport === undefined
|
|
36
|
+
? createNodeFetchTransport(options)
|
|
37
|
+
: undefined;
|
|
38
|
+
|
|
35
39
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
36
40
|
const client = resolveClient(options, request, context);
|
|
37
|
-
const transport = resolveTransport(options, request, context, client);
|
|
41
|
+
const transport = resolveTransport(options, request, context, client, defaultTransport);
|
|
38
42
|
const trace = createRouteTraceContext(request, undefined, options);
|
|
39
43
|
const helpers = createRouteHelpers(client, transport, trace);
|
|
40
44
|
const startedAt = nowMs(options);
|
|
@@ -168,11 +172,11 @@ function resolveClient(options, request, context) {
|
|
|
168
172
|
return createLogBrewNextClient(options);
|
|
169
173
|
}
|
|
170
174
|
|
|
171
|
-
function resolveTransport(options, request, context, client) {
|
|
175
|
+
function resolveTransport(options, request, context, client, defaultTransport) {
|
|
172
176
|
if (typeof options.transport === "function") {
|
|
173
177
|
return options.transport({ request, context, client });
|
|
174
178
|
}
|
|
175
|
-
return options.transport ??
|
|
179
|
+
return options.transport ?? defaultTransport;
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
function createRouteHelpers(client, transport, trace) {
|
package/index.d.cts
CHANGED
|
@@ -8,6 +8,12 @@ import type {
|
|
|
8
8
|
TransportResponse
|
|
9
9
|
} from "@logbrew/sdk";
|
|
10
10
|
|
|
11
|
+
type NodeFetchTransportConfig = {
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
fetchImpl?: typeof fetch;
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
};
|
|
16
|
+
|
|
11
17
|
export type CreateLogBrewNextClientConfig = {
|
|
12
18
|
apiKey?: string;
|
|
13
19
|
serverApiKey?: string;
|
|
@@ -90,8 +96,9 @@ export type LogBrewRouteMetricEvent = {
|
|
|
90
96
|
attributes: MetricAttributes;
|
|
91
97
|
};
|
|
92
98
|
|
|
93
|
-
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & {
|
|
99
|
+
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
94
100
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
101
|
+
/** Override the default Node fetch transport, optionally per request. */
|
|
95
102
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
96
103
|
captureRequests?: boolean;
|
|
97
104
|
captureRequestMetrics?: boolean;
|
package/index.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ import type {
|
|
|
8
8
|
TransportResponse
|
|
9
9
|
} from "@logbrew/sdk";
|
|
10
10
|
|
|
11
|
+
type NodeFetchTransportConfig = {
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
fetchImpl?: typeof fetch;
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
};
|
|
16
|
+
|
|
11
17
|
export type CreateLogBrewNextClientConfig = {
|
|
12
18
|
apiKey?: string;
|
|
13
19
|
serverApiKey?: string;
|
|
@@ -90,8 +96,9 @@ export type LogBrewRouteMetricEvent = {
|
|
|
90
96
|
attributes: MetricAttributes;
|
|
91
97
|
};
|
|
92
98
|
|
|
93
|
-
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & {
|
|
99
|
+
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
94
100
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
101
|
+
/** Override the default Node fetch transport, optionally per request. */
|
|
95
102
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
96
103
|
captureRequests?: boolean;
|
|
97
104
|
captureRequestMetrics?: boolean;
|
package/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import {
|
|
3
3
|
LogBrewClient,
|
|
4
|
-
RecordingTransport,
|
|
5
4
|
SdkError,
|
|
6
5
|
parseTraceparent
|
|
7
6
|
} from "@logbrew/sdk";
|
|
7
|
+
import { createNodeFetchTransport } from "@logbrew/node";
|
|
8
8
|
|
|
9
9
|
const DEFAULT_SDK_NAME = "logbrew-next";
|
|
10
|
-
const DEFAULT_SDK_VERSION = "0.1.
|
|
10
|
+
const DEFAULT_SDK_VERSION = "0.1.2";
|
|
11
11
|
const activeTraceContext = new AsyncLocalStorage();
|
|
12
12
|
|
|
13
13
|
export function createLogBrewNextClient({
|
|
@@ -32,9 +32,13 @@ export function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
32
32
|
throw new SdkError("configuration_error", "withLogBrewRouteHandler requires a handler function");
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
const defaultTransport = options.transport === undefined
|
|
36
|
+
? createNodeFetchTransport(options)
|
|
37
|
+
: undefined;
|
|
38
|
+
|
|
35
39
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
36
40
|
const client = resolveClient(options, request, context);
|
|
37
|
-
const transport = resolveTransport(options, request, context, client);
|
|
41
|
+
const transport = resolveTransport(options, request, context, client, defaultTransport);
|
|
38
42
|
const trace = createRouteTraceContext(request, undefined, options);
|
|
39
43
|
const helpers = createRouteHelpers(client, transport, trace);
|
|
40
44
|
const startedAt = nowMs(options);
|
|
@@ -168,11 +172,11 @@ function resolveClient(options, request, context) {
|
|
|
168
172
|
return createLogBrewNextClient(options);
|
|
169
173
|
}
|
|
170
174
|
|
|
171
|
-
function resolveTransport(options, request, context, client) {
|
|
175
|
+
function resolveTransport(options, request, context, client, defaultTransport) {
|
|
172
176
|
if (typeof options.transport === "function") {
|
|
173
177
|
return options.transport({ request, context, client });
|
|
174
178
|
}
|
|
175
|
-
return options.transport ??
|
|
179
|
+
return options.transport ?? defaultTransport;
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
function createRouteHelpers(client, transport, trace) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logbrew/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Next.js App Router helpers for the public LogBrew JavaScript SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|
|
@@ -75,7 +75,8 @@
|
|
|
75
75
|
"url": "git+https://github.com/LogBrewCo/sdk.git"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|
|
78
|
-
"@logbrew/
|
|
78
|
+
"@logbrew/node": "^0.1.2",
|
|
79
|
+
"@logbrew/sdk": "^0.1.3",
|
|
79
80
|
"next": ">=15",
|
|
80
81
|
"react": ">=18",
|
|
81
82
|
"react-dom": ">=18"
|