Skip to content

Commit c5b5678

Browse files
feat: add requestId to each log line (#7)
1 parent 19fa8e8 commit c5b5678

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import { AsyncQueue, Binary, Chunk, MerkleTree } from 'cafe-utility'
33
import { existsSync, readFileSync, writeFileSync } from 'fs'
44
import { createServer, Server } from 'http'
55
import createKeccakHash from 'keccak'
6+
import { randomBytes } from 'crypto'
67
import { Stamper } from './stamper'
78
import { getConfig } from './config'
89
import { log, error } from './logger'
910

11+
function generateRequestId(): string {
12+
return randomBytes(8).toString('hex')
13+
}
14+
1015
process.on('uncaughtException', (err: unknown) => {
1116
error({ err }, 'Uncaught Exception');
1217
});
@@ -39,9 +44,11 @@ Chunk.hashFunction = (data: Uint8Array): Uint8Array => {
3944

4045
const server: Server = createServer((request, response) => {
4146
const requestStart = Date.now()
47+
const requestId = generateRequestId()
4248
const chunks: Buffer[] = []
4349

4450
log({
51+
requestId,
4552
method: request.method,
4653
url: request.url,
4754
}, 'Incoming request');
@@ -66,7 +73,7 @@ Chunk.hashFunction = (data: Uint8Array): Uint8Array => {
6673
await bee.uploadChunk(envelope, chunk.build());
6774
stampings++;
6875
} catch (err) {
69-
error({ err }, 'Failed to upload chunk');
76+
error({ requestId, err }, 'Failed to upload chunk');
7077
throw err; // Re-throw to make sure the queue knows this task failed
7178
}
7279
})
@@ -87,6 +94,7 @@ Chunk.hashFunction = (data: Uint8Array): Uint8Array => {
8794

8895
const processingTimeMs = Date.now() - requestStart
8996
log({
97+
requestId,
9098
processingTimeMs,
9199
stampings,
92100
reference: formattedReference,
@@ -96,6 +104,7 @@ Chunk.hashFunction = (data: Uint8Array): Uint8Array => {
96104
} catch (err: unknown) {
97105
const processingTimeMs = Date.now() - requestStart;
98106
error({
107+
requestId,
99108
processingTimeMs,
100109
err,
101110
stampings
@@ -107,6 +116,7 @@ Chunk.hashFunction = (data: Uint8Array): Uint8Array => {
107116

108117
request.on('error', (err: Error) => {
109118
error({
119+
requestId,
110120
err,
111121
processingTimeMs: Date.now() - requestStart
112122
}, 'Request error');

0 commit comments

Comments
 (0)