@logbrew/next 0.1.0 → 0.1.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 +103 -21
- package/client.cjs +340 -0
- package/client.d.cts +95 -0
- package/client.d.ts +95 -0
- package/client.js +339 -0
- package/examples/client-route-spans.mjs +38 -0
- package/examples/index.mjs +4 -0
- package/examples/package.json +1 -0
- package/index.cjs +209 -59
- package/index.d.cts +45 -2
- package/index.d.ts +45 -2
- package/index.js +209 -59
- package/package.json +32 -2
- package/release-artifacts.cjs +199 -0
- package/release-artifacts.d.cts +35 -0
- package/release-artifacts.d.ts +35 -0
- package/release-artifacts.js +6 -0
package/client.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React from "react";
|
|
4
|
+
import {
|
|
5
|
+
LogBrewClient,
|
|
6
|
+
SdkError,
|
|
7
|
+
parseTraceparent
|
|
8
|
+
} from "@logbrew/sdk";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_SDK_NAME = "logbrew-next-client";
|
|
11
|
+
const DEFAULT_SDK_VERSION = "0.1.0";
|
|
12
|
+
|
|
13
|
+
export function createLogBrewNextBrowserClient({
|
|
14
|
+
apiKey,
|
|
15
|
+
clientKey,
|
|
16
|
+
eventFilter,
|
|
17
|
+
sdkName = DEFAULT_SDK_NAME,
|
|
18
|
+
sdkVersion = DEFAULT_SDK_VERSION,
|
|
19
|
+
maxQueueSize,
|
|
20
|
+
maxRetries = 2,
|
|
21
|
+
onEventDropped
|
|
22
|
+
} = {}) {
|
|
23
|
+
const authKey = clientKey ?? apiKey;
|
|
24
|
+
if (!authKey) {
|
|
25
|
+
throw new SdkError("configuration_error", "createLogBrewNextBrowserClient requires clientKey or apiKey");
|
|
26
|
+
}
|
|
27
|
+
return LogBrewClient.create({
|
|
28
|
+
apiKey: authKey,
|
|
29
|
+
eventFilter,
|
|
30
|
+
maxQueueSize,
|
|
31
|
+
maxRetries,
|
|
32
|
+
onEventDropped,
|
|
33
|
+
sdkName,
|
|
34
|
+
sdkVersion
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createNextRouteTemplate({
|
|
39
|
+
pathname,
|
|
40
|
+
routePatterns = [],
|
|
41
|
+
routeTemplate
|
|
42
|
+
} = {}) {
|
|
43
|
+
const safeRouteTemplate = normalizeRouteTemplate(routeTemplate);
|
|
44
|
+
if (safeRouteTemplate) {
|
|
45
|
+
return safeRouteTemplate;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const safePathname = pathnameOnly(pathname);
|
|
49
|
+
if (!safePathname || !Array.isArray(routePatterns)) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const matches = routePatterns
|
|
54
|
+
.map((pattern) => normalizeRouteTemplate(pattern))
|
|
55
|
+
.filter((pattern) => pattern && routePatternMatchesPathname(pattern, safePathname));
|
|
56
|
+
|
|
57
|
+
const matched = matches.sort(compareRouteSpecificity)[0];
|
|
58
|
+
return matched;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function createNextNavigationSpanEvent({
|
|
62
|
+
durationMs,
|
|
63
|
+
id,
|
|
64
|
+
idFactory = defaultNextNavigationSpanId,
|
|
65
|
+
metadata = {},
|
|
66
|
+
navigationIndex,
|
|
67
|
+
navigationType,
|
|
68
|
+
now = () => new Date().toISOString(),
|
|
69
|
+
parentSpanId,
|
|
70
|
+
pathname,
|
|
71
|
+
routePatterns,
|
|
72
|
+
routeTemplate,
|
|
73
|
+
spanId,
|
|
74
|
+
spanIdFactory = defaultSpanIdFactory,
|
|
75
|
+
status = "ok",
|
|
76
|
+
timestamp,
|
|
77
|
+
traceId,
|
|
78
|
+
traceparent
|
|
79
|
+
} = {}) {
|
|
80
|
+
const safeRouteTemplate = createNextRouteTemplate({ pathname, routePatterns, routeTemplate });
|
|
81
|
+
if (!safeRouteTemplate) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const trace = traceContextFromInput({ parentSpanId, spanId, spanIdFactory, traceId, traceparent });
|
|
85
|
+
return {
|
|
86
|
+
id: id ?? idFactory({ navigationIndex, navigationType, routeTemplate: safeRouteTemplate }),
|
|
87
|
+
timestamp: timestamp ?? now(),
|
|
88
|
+
type: "span",
|
|
89
|
+
attributes: {
|
|
90
|
+
durationMs,
|
|
91
|
+
name: `next.route ${safeRouteTemplate}`,
|
|
92
|
+
parentSpanId: trace.parentSpanId,
|
|
93
|
+
spanId: trace.spanId,
|
|
94
|
+
status,
|
|
95
|
+
traceId: trace.traceId,
|
|
96
|
+
metadata: compactMetadata({
|
|
97
|
+
framework: "nextjs",
|
|
98
|
+
navigationIndex,
|
|
99
|
+
navigationType,
|
|
100
|
+
routeTemplate: safeRouteTemplate,
|
|
101
|
+
source: "next.client.route",
|
|
102
|
+
...metadata
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function captureNextNavigation(client, input = {}) {
|
|
109
|
+
if (!client) {
|
|
110
|
+
throw new SdkError("configuration_error", "captureNextNavigation requires a client");
|
|
111
|
+
}
|
|
112
|
+
const event = createNextNavigationSpanEvent(input);
|
|
113
|
+
if (!event) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
client.span(event.id, event.timestamp, event.attributes);
|
|
117
|
+
return event;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function useLogBrewNextNavigation(options = {}) {
|
|
121
|
+
const lastNavigationKey = React.useRef(null);
|
|
122
|
+
const navigationIndex = React.useRef(0);
|
|
123
|
+
|
|
124
|
+
React.useEffect(() => {
|
|
125
|
+
try {
|
|
126
|
+
if (!options.client) {
|
|
127
|
+
throw new SdkError("configuration_error", "useLogBrewNextNavigation requires client");
|
|
128
|
+
}
|
|
129
|
+
const routeTemplate = createNextRouteTemplate(options);
|
|
130
|
+
if (!routeTemplate) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const navigationKey = nextNavigationKey(options.pathname, routeTemplate);
|
|
134
|
+
if (navigationKey === lastNavigationKey.current) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
lastNavigationKey.current = navigationKey;
|
|
138
|
+
navigationIndex.current += 1;
|
|
139
|
+
const event = captureNextNavigation(options.client, {
|
|
140
|
+
...options,
|
|
141
|
+
navigationIndex: navigationIndex.current,
|
|
142
|
+
routeTemplate
|
|
143
|
+
});
|
|
144
|
+
if (event && typeof options.onNavigation === "function") {
|
|
145
|
+
options.onNavigation(event);
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (typeof options.onCaptureError === "function") {
|
|
149
|
+
options.onCaptureError(error);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function compactMetadata(metadata) {
|
|
158
|
+
const compacted = {};
|
|
159
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
160
|
+
if (value === undefined) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
|
|
164
|
+
compacted[key] = value;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return compacted;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function traceContextFromInput({ parentSpanId, spanId, spanIdFactory, traceId, traceparent }) {
|
|
171
|
+
if (typeof traceparent === "string") {
|
|
172
|
+
const parsed = parseTraceparent(traceparent);
|
|
173
|
+
return {
|
|
174
|
+
parentSpanId: parentSpanId ?? parsed.parentSpanId,
|
|
175
|
+
spanId: spanId ?? spanIdFactory(),
|
|
176
|
+
traceId: traceId ?? parsed.traceId
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (!traceId || !spanId) {
|
|
180
|
+
throw new SdkError("configuration_error", "Next.js navigation requires traceparent or traceId and spanId");
|
|
181
|
+
}
|
|
182
|
+
return { parentSpanId, spanId, traceId };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function nextNavigationKey(pathname, routeTemplate) {
|
|
186
|
+
return `${routeTemplate}|${pathnameOnly(pathname) ?? ""}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function normalizeRouteTemplate(value) {
|
|
190
|
+
const pathname = pathnameOnly(value);
|
|
191
|
+
if (!pathname) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
const segments = pathname
|
|
195
|
+
.split("/")
|
|
196
|
+
.filter(Boolean)
|
|
197
|
+
.filter((segment) => !isRouteGroupSegment(segment) && !segment.startsWith("@"));
|
|
198
|
+
if (segments.length === 0) {
|
|
199
|
+
return "/";
|
|
200
|
+
}
|
|
201
|
+
return `/${segments.join("/")}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isRouteGroupSegment(segment) {
|
|
205
|
+
return segment.startsWith("(") && segment.endsWith(")");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function pathnameOnly(value) {
|
|
209
|
+
if (value === undefined || value === null) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
const trimmed = String(value).trim();
|
|
213
|
+
if (trimmed === "") {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
const withoutQueryOrHash = trimmed.split(/[?#]/u, 1)[0];
|
|
217
|
+
let pathname = withoutQueryOrHash;
|
|
218
|
+
if (hasUrlScheme(withoutQueryOrHash)) {
|
|
219
|
+
try {
|
|
220
|
+
pathname = new URL(withoutQueryOrHash).pathname;
|
|
221
|
+
} catch {
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (!pathname.startsWith("/")) {
|
|
226
|
+
pathname = `/${pathname}`;
|
|
227
|
+
}
|
|
228
|
+
return pathname.length > 1 && pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function hasUrlScheme(url) {
|
|
232
|
+
return /^[a-z][a-z0-9+.-]*:/iu.test(url);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function routePatternMatchesPathname(routePattern, pathname) {
|
|
236
|
+
const patternSegments = routePattern.split("/").filter(Boolean);
|
|
237
|
+
const pathSegments = pathname.split("/").filter(Boolean);
|
|
238
|
+
if (patternSegments.length === 0) {
|
|
239
|
+
return pathSegments.length === 0;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
let pathIndex = 0;
|
|
243
|
+
for (let patternIndex = 0; patternIndex < patternSegments.length; patternIndex += 1) {
|
|
244
|
+
const segment = patternSegments[patternIndex];
|
|
245
|
+
if (isOptionalCatchAllSegment(segment)) {
|
|
246
|
+
return patternIndex === patternSegments.length - 1;
|
|
247
|
+
}
|
|
248
|
+
if (isCatchAllSegment(segment)) {
|
|
249
|
+
return patternIndex === patternSegments.length - 1 && pathIndex < pathSegments.length;
|
|
250
|
+
}
|
|
251
|
+
if (pathIndex >= pathSegments.length) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
if (isDynamicSegment(segment)) {
|
|
255
|
+
pathIndex += 1;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (segment !== pathSegments[pathIndex]) {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
pathIndex += 1;
|
|
262
|
+
}
|
|
263
|
+
return pathIndex === pathSegments.length;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function compareRouteSpecificity(left, right) {
|
|
267
|
+
const scoreDelta = routeSpecificityScore(left) - routeSpecificityScore(right);
|
|
268
|
+
if (scoreDelta !== 0) {
|
|
269
|
+
return scoreDelta;
|
|
270
|
+
}
|
|
271
|
+
return right.split("/").length - left.split("/").length;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function routeSpecificityScore(routePattern) {
|
|
275
|
+
return routePattern
|
|
276
|
+
.split("/")
|
|
277
|
+
.filter(Boolean)
|
|
278
|
+
.reduce((score, segment) => {
|
|
279
|
+
if (isOptionalCatchAllSegment(segment)) {
|
|
280
|
+
return score + 1000;
|
|
281
|
+
}
|
|
282
|
+
if (isCatchAllSegment(segment)) {
|
|
283
|
+
return score + 100;
|
|
284
|
+
}
|
|
285
|
+
if (isDynamicSegment(segment)) {
|
|
286
|
+
return score + 10;
|
|
287
|
+
}
|
|
288
|
+
return score;
|
|
289
|
+
}, 0);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function isDynamicSegment(segment) {
|
|
293
|
+
return /^\[[^[\]]+\]$/u.test(segment);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function isCatchAllSegment(segment) {
|
|
297
|
+
return /^\[\.\.\.[^[\]]+\]$/u.test(segment);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function isOptionalCatchAllSegment(segment) {
|
|
301
|
+
return /^\[\[\.\.\.[^[\]]+\]\]$/u.test(segment);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function defaultNextNavigationSpanId({ routeTemplate }) {
|
|
305
|
+
return `evt_next_route_${slugify(routeTemplate ?? "route")}`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function slugify(value) {
|
|
309
|
+
return String(value)
|
|
310
|
+
.toLowerCase()
|
|
311
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
312
|
+
.replace(/^_+|_+$/g, "") || "event";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function defaultSpanIdFactory() {
|
|
316
|
+
return randomHex(8);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function randomHex(bytes) {
|
|
320
|
+
const randomValues = new Uint8Array(bytes);
|
|
321
|
+
if (globalThis.crypto && typeof globalThis.crypto.getRandomValues === "function") {
|
|
322
|
+
globalThis.crypto.getRandomValues(randomValues);
|
|
323
|
+
} else {
|
|
324
|
+
for (let index = 0; index < bytes; index += 1) {
|
|
325
|
+
randomValues[index] = Math.floor(Math.random() * 256);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return Array.from(randomValues, (value) => value.toString(16).padStart(2, "0")).join("");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const defaultExport = {
|
|
332
|
+
captureNextNavigation,
|
|
333
|
+
createLogBrewNextBrowserClient,
|
|
334
|
+
createNextNavigationSpanEvent,
|
|
335
|
+
createNextRouteTemplate,
|
|
336
|
+
useLogBrewNextNavigation
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export default defaultExport;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
captureNextNavigation,
|
|
3
|
+
createLogBrewNextBrowserClient,
|
|
4
|
+
createNextRouteTemplate
|
|
5
|
+
} from "@logbrew/next/client";
|
|
6
|
+
|
|
7
|
+
const routePatterns = ["/projects/[projectId]/settings", "/docs/[[...slug]]"];
|
|
8
|
+
const routeTemplate = createNextRouteTemplate({
|
|
9
|
+
pathname: "/projects/public-example-123/settings?debug=true#panel",
|
|
10
|
+
routePatterns
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const client = createLogBrewNextBrowserClient({
|
|
14
|
+
clientKey: "LOGBREW_CLIENT_KEY",
|
|
15
|
+
sdkName: "logbrew-next-client-route-example",
|
|
16
|
+
sdkVersion: "0.1.0"
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const event = captureNextNavigation(client, {
|
|
20
|
+
pathname: "/projects/public-example-123/settings?debug=true#panel",
|
|
21
|
+
routePatterns,
|
|
22
|
+
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
|
23
|
+
spanIdFactory: () => "b7ad6b7169203331",
|
|
24
|
+
timestamp: "2026-06-02T10:00:09Z",
|
|
25
|
+
durationMs: 19
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (!event || event.attributes.metadata.routeTemplate !== "/projects/[projectId]/settings") {
|
|
29
|
+
throw new Error(`unexpected Next client route span: ${JSON.stringify(event)}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
console.log(JSON.stringify({
|
|
33
|
+
ok: true,
|
|
34
|
+
pendingEvents: client.pendingEvents(),
|
|
35
|
+
routeTemplate,
|
|
36
|
+
spanName: event.attributes.name
|
|
37
|
+
}));
|
|
38
|
+
console.error(JSON.stringify({ ok: true, routeTemplate }));
|
package/examples/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const commands = new Map([
|
|
4
|
+
["client-route-spans", new URL("./client-route-spans.mjs", import.meta.url)],
|
|
4
5
|
["readme-example", new URL("./readme-example.mjs", import.meta.url)],
|
|
5
6
|
["real-user-smoke", new URL("./real-user-smoke.mjs", import.meta.url)]
|
|
6
7
|
]);
|
|
@@ -22,15 +23,18 @@ if (command === "--help" || command === "-h") {
|
|
|
22
23
|
function printHelp() {
|
|
23
24
|
console.log("LogBrew Next.js examples");
|
|
24
25
|
console.log("node node_modules/@logbrew/next/examples/index.mjs --list");
|
|
26
|
+
console.log("node node_modules/@logbrew/next/examples/index.mjs client-route-spans");
|
|
25
27
|
console.log("node node_modules/@logbrew/next/examples/index.mjs readme-example");
|
|
26
28
|
console.log("node node_modules/@logbrew/next/examples/index.mjs real-user-smoke");
|
|
27
29
|
console.log("node node_modules/@logbrew/next/examples/index.mjs");
|
|
28
30
|
console.log("npm --prefix node_modules/@logbrew/next/examples run list");
|
|
31
|
+
console.log("npm --prefix node_modules/@logbrew/next/examples run client-route-spans");
|
|
29
32
|
console.log("npm --prefix node_modules/@logbrew/next/examples run readme-example");
|
|
30
33
|
console.log("npm --prefix node_modules/@logbrew/next/examples run real-user-smoke");
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
function printList() {
|
|
37
|
+
console.log("client-route-spans -> node node_modules/@logbrew/next/examples/index.mjs client-route-spans");
|
|
34
38
|
console.log("readme-example -> node node_modules/@logbrew/next/examples/index.mjs readme-example");
|
|
35
39
|
console.log("real-user-smoke -> node node_modules/@logbrew/next/examples/index.mjs real-user-smoke");
|
|
36
40
|
}
|
package/examples/package.json
CHANGED