Skip to content

Commit 63a0f28

Browse files
author
Nicolas Dorseuil
committed
use requestId from OpenNext
1 parent f497cbe commit 63a0f28

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
"vars": {
126126
// This is a bit misleading, but it means that we can have 500 concurrent revalidations
127127
// This means that we'll have up to 100 durable objects instance running at the same time
128-
"MAX_REVALIDATE_CONCURRENCY": "100"
128+
"MAX_REVALIDATE_CONCURRENCY": "100",
129+
"OPEN_NEXT_REQUEST_ID_HEADER": true
129130
},
130131
"r2_buckets": [
131132
{

packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@
153153
// TODO: remove this once the issue is fixed
154154
"DEBUG_CLOUDFLARE": "true",
155155
"WORKER_VERSION_ID": "TO_REPLACE",
156-
"STAGE": "production"
156+
"STAGE": "production",
157+
"OPEN_NEXT_REQUEST_ID_HEADER": true
157158
},
158159
"routes": [
159160
{

packages/gitbook-v2/src/lib/logger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { createLogger } from '@/lib/logger';
22
import { cache } from 'react';
33

4+
const getRequestId = (fallbackRequestId?: string) => {
5+
// We are in OpenNext and we should have access to the request ID.
6+
if ((globalThis as any).__openNextAls) {
7+
return (globalThis as any).__openNextAls.getStore()?.requestId;
8+
}
9+
return fallbackRequestId;
10+
};
11+
412
// We don't want to use react cache in the edge runtime, so we use a different logger creation method.
513
// It is treeshaken by Next.js.
614
export const getLogger =
715
process.env.NEXT_RUNTIME === 'edge'
8-
? () => createLogger('GBO', {})
16+
? () =>
17+
createLogger('GBO', {
18+
requestId: getRequestId(),
19+
})
920
: cache(() => {
1021
// This is not cryptographically secure, but it's fine for logging purposes.
1122
// It allows us to identify logs from the same request.
12-
const requestId = Math.random().toString(36).substring(2, 15);
23+
const requestId = getRequestId(Math.random().toString(36).substring(2, 15));
1324
return createLogger('GBOV2', { requestId });
1425
});

0 commit comments

Comments
 (0)