logixia 1.12.4 → 1.12.6
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 +80 -27
- package/dist/.tsbuildinfo +1 -1
- package/dist/async-context-CSHWoHMB.js +88 -0
- package/dist/async-context-CSHWoHMB.js.map +1 -0
- package/dist/async-context-CwBNMm2E.mjs +68 -0
- package/dist/async-context-CwBNMm2E.mjs.map +1 -0
- package/dist/azure-monitor.transport-CG7ClkYj.d.ts +62 -0
- package/dist/azure-monitor.transport-CG7ClkYj.d.ts.map +1 -0
- package/dist/azure-monitor.transport-CHnjVRF-.mjs +152 -0
- package/dist/azure-monitor.transport-CHnjVRF-.mjs.map +1 -0
- package/dist/azure-monitor.transport-Cm74kXZS.d.mts +62 -0
- package/dist/azure-monitor.transport-Cm74kXZS.d.mts.map +1 -0
- package/dist/azure-monitor.transport-CrvfHd9C.js +157 -0
- package/dist/azure-monitor.transport-CrvfHd9C.js.map +1 -0
- package/dist/azure.d.mts +3 -0
- package/dist/azure.d.ts +3 -0
- package/dist/azure.js +6 -0
- package/dist/azure.mjs +6 -0
- package/dist/batch-buffer-BY-mRWIK.js +170 -0
- package/dist/batch-buffer-BY-mRWIK.js.map +1 -0
- package/dist/batch-buffer-PGvSgbmd.mjs +132 -0
- package/dist/batch-buffer-PGvSgbmd.mjs.map +1 -0
- package/dist/browser.d.mts +427 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.d.ts +427 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +590 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +586 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/chunk-DLSkYcPk.mjs +7 -0
- package/dist/chunk-nOFOJqeH.js +30 -0
- package/dist/cloudwatch.d.mts +3 -0
- package/dist/cloudwatch.d.ts +3 -0
- package/dist/cloudwatch.js +6 -0
- package/dist/cloudwatch.mjs +6 -0
- package/dist/cloudwatch.transport-CGHY4buE.d.mts +81 -0
- package/dist/cloudwatch.transport-CGHY4buE.d.mts.map +1 -0
- package/dist/cloudwatch.transport-DK_8cKd3.d.ts +81 -0
- package/dist/cloudwatch.transport-DK_8cKd3.d.ts.map +1 -0
- package/dist/cloudwatch.transport-pNBNWJLt.js +370 -0
- package/dist/cloudwatch.transport-pNBNWJLt.js.map +1 -0
- package/dist/cloudwatch.transport-yBwm4OeO.mjs +360 -0
- package/dist/cloudwatch.transport-yBwm4OeO.mjs.map +1 -0
- package/dist/coerce.utils-BN980Y9V.js +44 -0
- package/dist/coerce.utils-BN980Y9V.js.map +1 -0
- package/dist/coerce.utils-Dxv4_ikC.mjs +38 -0
- package/dist/coerce.utils-Dxv4_ikC.mjs.map +1 -0
- package/dist/correlation.d.mts +238 -0
- package/dist/correlation.d.mts.map +1 -0
- package/dist/correlation.d.ts +238 -0
- package/dist/correlation.d.ts.map +1 -0
- package/dist/correlation.js +272 -0
- package/dist/correlation.js.map +1 -0
- package/dist/correlation.mjs +261 -0
- package/dist/correlation.mjs.map +1 -0
- package/dist/gcp.d.mts +3 -0
- package/dist/gcp.d.ts +3 -0
- package/dist/gcp.js +6 -0
- package/dist/gcp.mjs +6 -0
- package/dist/gcp.transport-BJuNRFe_.mjs +192 -0
- package/dist/gcp.transport-BJuNRFe_.mjs.map +1 -0
- package/dist/gcp.transport-BOhkVKlI.d.ts +71 -0
- package/dist/gcp.transport-BOhkVKlI.d.ts.map +1 -0
- package/dist/gcp.transport-Bi6CUqZR.d.mts +71 -0
- package/dist/gcp.transport-Bi6CUqZR.d.mts.map +1 -0
- package/dist/gcp.transport-SHdtc1n0.js +198 -0
- package/dist/gcp.transport-SHdtc1n0.js.map +1 -0
- package/dist/{index-CwFww7VC.d.ts → index-BPKwwERo.d.mts} +18 -174
- package/dist/index-BPKwwERo.d.mts.map +1 -0
- package/dist/{index-DnyeaoZU.d.mts → index-C1X-TE5I.d.ts} +18 -174
- package/dist/index-C1X-TE5I.d.ts.map +1 -0
- package/dist/index.d.mts +41 -11
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +40 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +87 -147
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -132
- package/dist/index.mjs.map +1 -1
- package/dist/internal-log-BUJYMZbE.js +70 -0
- package/dist/internal-log-BUJYMZbE.js.map +1 -0
- package/dist/internal-log-Cm8SOWSV.mjs +52 -0
- package/dist/internal-log-Cm8SOWSV.mjs.map +1 -0
- package/dist/{logitron-logger.module-BIGTm2V6.mjs → logitron-logger.module-Bs3XYZgF.mjs} +266 -166
- package/dist/logitron-logger.module-Bs3XYZgF.mjs.map +1 -0
- package/dist/{logitron-logger.module-BTxDpK6u.js → logitron-logger.module-BsDZR5BH.js} +297 -203
- package/dist/logitron-logger.module-BsDZR5BH.js.map +1 -0
- package/dist/{logitron-logger.module-BmT-ewin.d.ts → logitron-logger.module-CxT1F6Cm.d.ts} +23 -10
- package/dist/logitron-logger.module-CxT1F6Cm.d.ts.map +1 -0
- package/dist/{logitron-logger.module-BCq3sF27.d.mts → logitron-logger.module-zDKVd_o0.d.mts} +23 -10
- package/dist/logitron-logger.module-zDKVd_o0.d.mts.map +1 -0
- package/dist/middleware.d.mts +5 -2
- package/dist/middleware.d.mts.map +1 -1
- package/dist/middleware.d.ts +5 -2
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +13 -4
- package/dist/middleware.js.map +1 -1
- package/dist/middleware.mjs +14 -4
- package/dist/middleware.mjs.map +1 -1
- package/dist/nest.d.mts +3 -2
- package/dist/nest.d.mts.map +1 -1
- package/dist/nest.d.ts +3 -2
- package/dist/nest.d.ts.map +1 -1
- package/dist/nest.js +6 -2
- package/dist/nest.mjs +6 -2
- package/dist/safe-stringify-DJ66tW5I.mjs +118 -0
- package/dist/safe-stringify-DJ66tW5I.mjs.map +1 -0
- package/dist/safe-stringify-DWo3aZp_.js +142 -0
- package/dist/safe-stringify-DWo3aZp_.js.map +1 -0
- package/dist/search-CFQRISUv.mjs +1596 -0
- package/dist/search-CFQRISUv.mjs.map +1 -0
- package/dist/search-CQOo-Iq6.d.mts +816 -0
- package/dist/search-CQOo-Iq6.d.mts.map +1 -0
- package/dist/search-DujhR94O.d.ts +816 -0
- package/dist/search-DujhR94O.d.ts.map +1 -0
- package/dist/search-njTI2hdo.js +1631 -0
- package/dist/search-njTI2hdo.js.map +1 -0
- package/dist/search.d.mts +4 -1
- package/dist/search.d.ts +4 -1
- package/dist/search.js +9 -1
- package/dist/search.mjs +3 -2
- package/dist/testing.d.mts +2 -1
- package/dist/testing.d.mts.map +1 -1
- package/dist/testing.d.ts +2 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/{transport.manager-ciG4r5uz.js → transport.manager-BivyUTtU.js} +497 -437
- package/dist/transport.manager-BivyUTtU.js.map +1 -0
- package/dist/{transport.manager-DETpXuaY.mjs → transport.manager-D6KL3t9V.mjs} +473 -364
- package/dist/transport.manager-D6KL3t9V.mjs.map +1 -0
- package/dist/transport.types-B5vcyv2k.d.mts +187 -0
- package/dist/transport.types-B5vcyv2k.d.mts.map +1 -0
- package/dist/transport.types-BuY84TwO.d.ts +187 -0
- package/dist/transport.types-BuY84TwO.d.ts.map +1 -0
- package/dist/transports.d.mts +90 -29
- package/dist/transports.d.mts.map +1 -1
- package/dist/transports.d.ts +90 -29
- package/dist/transports.d.ts.map +1 -1
- package/dist/transports.js +14 -2
- package/dist/transports.mjs +10 -2
- package/dist/worker-entry.js +1241 -0
- package/dist/worker.d.mts +3 -0
- package/dist/worker.d.ts +3 -0
- package/dist/worker.js +4 -0
- package/dist/worker.mjs +4 -0
- package/dist/worker.transport-BP8Q28Z2.d.mts +76 -0
- package/dist/worker.transport-BP8Q28Z2.d.mts.map +1 -0
- package/dist/worker.transport-BhMyvqQ2.mjs +167 -0
- package/dist/worker.transport-BhMyvqQ2.mjs.map +1 -0
- package/dist/worker.transport-CfH6z37Z.js +168 -0
- package/dist/worker.transport-CfH6z37Z.js.map +1 -0
- package/dist/worker.transport-mKXgNK0L.d.ts +76 -0
- package/dist/worker.transport-mKXgNK0L.d.ts.map +1 -0
- package/package.json +23 -18
- package/dist/index-CwFww7VC.d.ts.map +0 -1
- package/dist/index-DnyeaoZU.d.mts.map +0 -1
- package/dist/logitron-logger.module-BCq3sF27.d.mts.map +0 -1
- package/dist/logitron-logger.module-BIGTm2V6.mjs.map +0 -1
- package/dist/logitron-logger.module-BTxDpK6u.js.map +0 -1
- package/dist/logitron-logger.module-BmT-ewin.d.ts.map +0 -1
- package/dist/search-Cg_OasF-.d.ts +0 -1
- package/dist/search-DN676Dnz.d.mts +0 -1
- package/dist/search-DOvSI-mb.mjs +0 -1
- package/dist/search-DoZF3RZj.js +0 -0
- package/dist/transport.manager-DETpXuaY.mjs.map +0 -1
- package/dist/transport.manager-ciG4r5uz.js.map +0 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
//#region src/correlation.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* logixia — Microservices Correlation ID Propagation
|
|
4
|
+
*
|
|
5
|
+
* Provides zero-boilerplate correlation ID generation and propagation for
|
|
6
|
+
* distributed systems. A single correlation ID travels through every service
|
|
7
|
+
* in a request fan-out, making it trivial to join logs across services.
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* - UUID v4 correlation ID generation (via `crypto.randomUUID()`)
|
|
11
|
+
* - Express / Fastify / NestJS incoming-request middleware
|
|
12
|
+
* - `correlationFetch()` — wraps global `fetch`, auto-injects `X-Correlation-ID`
|
|
13
|
+
* - `createCorrelationAxiosInterceptor()` — axios request interceptor
|
|
14
|
+
* - `childFromRequest(req, logger)` — child logger with all request identifiers
|
|
15
|
+
* - Kafka / SQS / RabbitMQ message context helpers
|
|
16
|
+
* - Integration with `LogixiaContext` (AsyncLocalStorage) so every `logger.info()`
|
|
17
|
+
* call automatically includes the correlationId without per-call passing.
|
|
18
|
+
*
|
|
19
|
+
* @example Express middleware
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { correlationMiddleware } from 'logixia/correlation';
|
|
22
|
+
* import { logger } from 'logixia';
|
|
23
|
+
*
|
|
24
|
+
* app.use(correlationMiddleware());
|
|
25
|
+
*
|
|
26
|
+
* app.get('/orders', (req, res) => {
|
|
27
|
+
* // correlationId is automatically in every log inside this request
|
|
28
|
+
* logger.info('Listing orders');
|
|
29
|
+
* res.json({ ok: true });
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example Outbound fetch with correlation
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { correlationFetch } from 'logixia/correlation';
|
|
36
|
+
*
|
|
37
|
+
* // Reads correlationId from current LogixiaContext and injects it as header
|
|
38
|
+
* const data = await correlationFetch('https://payments.internal/charge', {
|
|
39
|
+
* method: 'POST',
|
|
40
|
+
* body: JSON.stringify({ amount: 100 }),
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example Manual correlation context
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { withCorrelationId } from 'logixia/correlation';
|
|
47
|
+
*
|
|
48
|
+
* await withCorrelationId('trace-abc-123', async () => {
|
|
49
|
+
* // All logs and outbound fetch calls in here carry 'trace-abc-123'
|
|
50
|
+
* await processOrder();
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
/** Fields injected into the log context by the correlation middleware. */
|
|
55
|
+
interface CorrelationContext {
|
|
56
|
+
correlationId: string;
|
|
57
|
+
traceId?: string;
|
|
58
|
+
/** Service that originated the request (from `X-Origin-Service` header). */
|
|
59
|
+
originService?: string;
|
|
60
|
+
/** Index signature so CorrelationContext is assignable to LogContext. */
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
interface CorrelationMiddlewareOptions {
|
|
64
|
+
/**
|
|
65
|
+
* HTTP header to read/write the correlation ID from/to.
|
|
66
|
+
* @default 'x-correlation-id'
|
|
67
|
+
*/
|
|
68
|
+
header?: string;
|
|
69
|
+
/**
|
|
70
|
+
* HTTP header for the originating service name.
|
|
71
|
+
* @default 'x-origin-service'
|
|
72
|
+
*/
|
|
73
|
+
originServiceHeader?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Function to generate a new correlation ID when none is present in the request.
|
|
76
|
+
* @default () => crypto.randomUUID()
|
|
77
|
+
*/
|
|
78
|
+
generate?: () => string;
|
|
79
|
+
/**
|
|
80
|
+
* When `true`, the response will include the correlation ID header so
|
|
81
|
+
* clients can log it client-side.
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
setResponseHeader?: boolean;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Generate a new correlation ID using `crypto.randomUUID()` (available in
|
|
88
|
+
* Node.js 14.17+, all modern browsers, and Edge runtimes).
|
|
89
|
+
*
|
|
90
|
+
* When the Web Crypto global is unavailable (e.g. Node 18 without
|
|
91
|
+
* `--experimental-global-webcrypto`), fall back to `node:crypto`'s
|
|
92
|
+
* `randomUUID`, which returns a proper UUID v4 — matching the shape this
|
|
93
|
+
* function's JSDoc and README promise. `logixia/correlation` is Node-only
|
|
94
|
+
* (it builds on `AsyncLocalStorage`), so importing `node:crypto` is safe here.
|
|
95
|
+
*/
|
|
96
|
+
declare function generateCorrelationId(): string;
|
|
97
|
+
/**
|
|
98
|
+
* Return the correlation ID from the current `LogixiaContext`, or `undefined`
|
|
99
|
+
* when called outside any context scope.
|
|
100
|
+
*/
|
|
101
|
+
declare function getCurrentCorrelationId(): string | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Run `callback` inside a correlation context scope.
|
|
104
|
+
*
|
|
105
|
+
* If a correlation ID is not provided, a new UUID is generated.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* await withCorrelationId('cid-abc', async () => {
|
|
110
|
+
* logger.info('Processing message'); // → { correlationId: 'cid-abc', ... }
|
|
111
|
+
* await callDownstream(); // correlationFetch() picks up cid-abc
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function withCorrelationId<T>(correlationId: string | undefined, callback: () => T): T;
|
|
116
|
+
/**
|
|
117
|
+
* Express-compatible middleware that:
|
|
118
|
+
* 1. Reads `X-Correlation-ID` from the incoming request (or generates a new one)
|
|
119
|
+
* 2. Wraps the request in a `LogixiaContext` so all loggers in the handler
|
|
120
|
+
* automatically include `correlationId`
|
|
121
|
+
* 3. Writes the correlation ID back to the response header (opt-out via options)
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { correlationMiddleware } from 'logixia/correlation';
|
|
126
|
+
* app.use(correlationMiddleware());
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare function correlationMiddleware(options?: CorrelationMiddlewareOptions): (req: Record<string, unknown>, res: Record<string, unknown>, next: () => void) => void;
|
|
130
|
+
/**
|
|
131
|
+
* Fastify `onRequest` lifecycle hook — same functionality as `correlationMiddleware`
|
|
132
|
+
* but adapted to Fastify's hook API.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { correlationFastifyHook } from 'logixia/correlation';
|
|
137
|
+
* fastify.addHook('onRequest', correlationFastifyHook());
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare function correlationFastifyHook(options?: CorrelationMiddlewareOptions): (request: Record<string, unknown>, reply: Record<string, unknown>, done: () => void) => void;
|
|
141
|
+
/**
|
|
142
|
+
* A drop-in replacement for `fetch` that automatically injects the current
|
|
143
|
+
* `correlationId` from `LogixiaContext` as an `X-Correlation-ID` request header.
|
|
144
|
+
*
|
|
145
|
+
* Falls back to the global `fetch` when no correlation context is active.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { correlationFetch } from 'logixia/correlation';
|
|
150
|
+
*
|
|
151
|
+
* // Inside a request handler with correlationMiddleware active:
|
|
152
|
+
* const response = await correlationFetch('https://payments.internal/v1/charge', {
|
|
153
|
+
* method: 'POST',
|
|
154
|
+
* body: JSON.stringify({ amount: 100 }),
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
declare function correlationFetch(input: string | URL | Request, init?: RequestInit, options?: {
|
|
159
|
+
header?: string;
|
|
160
|
+
}): Promise<Response>;
|
|
161
|
+
/**
|
|
162
|
+
* Returns an axios request interceptor that automatically injects the current
|
|
163
|
+
* correlation ID into outgoing axios requests.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import axios from 'axios';
|
|
168
|
+
* import { createCorrelationAxiosInterceptor } from 'logixia/correlation';
|
|
169
|
+
*
|
|
170
|
+
* axios.interceptors.request.use(createCorrelationAxiosInterceptor());
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function createCorrelationAxiosInterceptor(options?: {
|
|
174
|
+
header?: string;
|
|
175
|
+
}): (config: Record<string, unknown>) => Record<string, unknown>;
|
|
176
|
+
/**
|
|
177
|
+
* Create a child logger enriched with all correlation-relevant identifiers
|
|
178
|
+
* extracted from the incoming HTTP request.
|
|
179
|
+
*
|
|
180
|
+
* The child logger's every call automatically includes:
|
|
181
|
+
* `correlationId`, `requestId`, `method`, `url`, `userAgent`, `ip`
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { childFromRequest } from 'logixia/correlation';
|
|
186
|
+
* import { logger } from 'logixia';
|
|
187
|
+
*
|
|
188
|
+
* app.use((req, res, next) => {
|
|
189
|
+
* req.logger = childFromRequest(req, logger);
|
|
190
|
+
* next();
|
|
191
|
+
* });
|
|
192
|
+
*
|
|
193
|
+
* app.get('/orders', (req, res) => {
|
|
194
|
+
* req.logger.info('Listing orders'); // → correlationId, requestId, method, url ...
|
|
195
|
+
* });
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function childFromRequest<TLogger extends {
|
|
199
|
+
child(ctx: Record<string, unknown>): TLogger;
|
|
200
|
+
}>(req: Record<string, unknown>, logger: TLogger, options?: {
|
|
201
|
+
correlationHeader?: string;
|
|
202
|
+
}): TLogger;
|
|
203
|
+
/**
|
|
204
|
+
* Extract a correlation context from a Kafka / RabbitMQ / SQS message object.
|
|
205
|
+
*
|
|
206
|
+
* Looks for `correlationId`, `correlation_id`, or `x-correlation-id` in
|
|
207
|
+
* `message.headers` (Kafka) or `message.MessageAttributes` (SQS).
|
|
208
|
+
*
|
|
209
|
+
* @example Kafka consumer
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { extractMessageCorrelationId, withCorrelationId } from 'logixia/correlation';
|
|
212
|
+
*
|
|
213
|
+
* consumer.run({
|
|
214
|
+
* eachMessage: async ({ message }) => {
|
|
215
|
+
* const correlationId = extractMessageCorrelationId(message);
|
|
216
|
+
* await withCorrelationId(correlationId, () => processMessage(message));
|
|
217
|
+
* },
|
|
218
|
+
* });
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare function extractMessageCorrelationId(message: Record<string, unknown>): string | undefined;
|
|
222
|
+
/**
|
|
223
|
+
* Build Kafka message headers containing the current correlation context.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* import { buildKafkaCorrelationHeaders } from 'logixia/correlation';
|
|
228
|
+
*
|
|
229
|
+
* await producer.send({
|
|
230
|
+
* topic: 'orders',
|
|
231
|
+
* messages: [{ value: JSON.stringify(order), headers: buildKafkaCorrelationHeaders() }],
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function buildKafkaCorrelationHeaders(): Record<string, string>;
|
|
236
|
+
//#endregion
|
|
237
|
+
export { CorrelationContext, CorrelationMiddlewareOptions, buildKafkaCorrelationHeaders, childFromRequest, correlationFastifyHook, correlationFetch, correlationMiddleware, createCorrelationAxiosInterceptor, extractMessageCorrelationId, generateCorrelationId, getCurrentCorrelationId, withCorrelationId };
|
|
238
|
+
//# sourceMappingURL=correlation.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation.d.mts","names":[],"sources":["../src/correlation.ts"],"sourcesContent":[],"mappings":";;AA4DA;AASA;AAoCA;AA2BA;AAkBA;AAoBA;;;;;AA+CA;;;;;AAsDA;;;;;;;AA4CA;AA4CA;;;;;;;AAmDA;AA4CA;;;;;;;;;;;;;;;;;;;;;UA1YiB,kBAAA;;;;;;;;UASA,4BAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoCD,qBAAA,CAAA;;;;;iBA2BA,uBAAA,CAAA;;;;;;;;;;;;;;iBAkBA,wEAAwE,IAAI;;;;;;;;;;;;;;iBAoB5E,qBAAA,WAA+B,qCAStC,8BACA;;;;;;;;;;;iBAqCO,sBAAA,WAAgC,yCASnC,gCACF;;;;;;;;;;;;;;;;;;iBA4CW,gBAAA,iBACJ,MAAM,gBAChB;;IAEL,QAAQ;;;;;;;;;;;;;iBAwCK,iCAAA;;aAEJ,4BAA4B;;;;;;;;;;;;;;;;;;;;;;;iBA0CxB;aAA8C,0BAA0B;QACjF,iCACG;;IAEP;;;;;;;;;;;;;;;;;;;iBA+Ca,2BAAA,UAAqC;;;;;;;;;;;;;;iBA4CrC,4BAAA,CAAA,GAAgC"}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
//#region src/correlation.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* logixia — Microservices Correlation ID Propagation
|
|
4
|
+
*
|
|
5
|
+
* Provides zero-boilerplate correlation ID generation and propagation for
|
|
6
|
+
* distributed systems. A single correlation ID travels through every service
|
|
7
|
+
* in a request fan-out, making it trivial to join logs across services.
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* - UUID v4 correlation ID generation (via `crypto.randomUUID()`)
|
|
11
|
+
* - Express / Fastify / NestJS incoming-request middleware
|
|
12
|
+
* - `correlationFetch()` — wraps global `fetch`, auto-injects `X-Correlation-ID`
|
|
13
|
+
* - `createCorrelationAxiosInterceptor()` — axios request interceptor
|
|
14
|
+
* - `childFromRequest(req, logger)` — child logger with all request identifiers
|
|
15
|
+
* - Kafka / SQS / RabbitMQ message context helpers
|
|
16
|
+
* - Integration with `LogixiaContext` (AsyncLocalStorage) so every `logger.info()`
|
|
17
|
+
* call automatically includes the correlationId without per-call passing.
|
|
18
|
+
*
|
|
19
|
+
* @example Express middleware
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { correlationMiddleware } from 'logixia/correlation';
|
|
22
|
+
* import { logger } from 'logixia';
|
|
23
|
+
*
|
|
24
|
+
* app.use(correlationMiddleware());
|
|
25
|
+
*
|
|
26
|
+
* app.get('/orders', (req, res) => {
|
|
27
|
+
* // correlationId is automatically in every log inside this request
|
|
28
|
+
* logger.info('Listing orders');
|
|
29
|
+
* res.json({ ok: true });
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example Outbound fetch with correlation
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { correlationFetch } from 'logixia/correlation';
|
|
36
|
+
*
|
|
37
|
+
* // Reads correlationId from current LogixiaContext and injects it as header
|
|
38
|
+
* const data = await correlationFetch('https://payments.internal/charge', {
|
|
39
|
+
* method: 'POST',
|
|
40
|
+
* body: JSON.stringify({ amount: 100 }),
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example Manual correlation context
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { withCorrelationId } from 'logixia/correlation';
|
|
47
|
+
*
|
|
48
|
+
* await withCorrelationId('trace-abc-123', async () => {
|
|
49
|
+
* // All logs and outbound fetch calls in here carry 'trace-abc-123'
|
|
50
|
+
* await processOrder();
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
/** Fields injected into the log context by the correlation middleware. */
|
|
55
|
+
interface CorrelationContext {
|
|
56
|
+
correlationId: string;
|
|
57
|
+
traceId?: string;
|
|
58
|
+
/** Service that originated the request (from `X-Origin-Service` header). */
|
|
59
|
+
originService?: string;
|
|
60
|
+
/** Index signature so CorrelationContext is assignable to LogContext. */
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
interface CorrelationMiddlewareOptions {
|
|
64
|
+
/**
|
|
65
|
+
* HTTP header to read/write the correlation ID from/to.
|
|
66
|
+
* @default 'x-correlation-id'
|
|
67
|
+
*/
|
|
68
|
+
header?: string;
|
|
69
|
+
/**
|
|
70
|
+
* HTTP header for the originating service name.
|
|
71
|
+
* @default 'x-origin-service'
|
|
72
|
+
*/
|
|
73
|
+
originServiceHeader?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Function to generate a new correlation ID when none is present in the request.
|
|
76
|
+
* @default () => crypto.randomUUID()
|
|
77
|
+
*/
|
|
78
|
+
generate?: () => string;
|
|
79
|
+
/**
|
|
80
|
+
* When `true`, the response will include the correlation ID header so
|
|
81
|
+
* clients can log it client-side.
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
setResponseHeader?: boolean;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Generate a new correlation ID using `crypto.randomUUID()` (available in
|
|
88
|
+
* Node.js 14.17+, all modern browsers, and Edge runtimes).
|
|
89
|
+
*
|
|
90
|
+
* When the Web Crypto global is unavailable (e.g. Node 18 without
|
|
91
|
+
* `--experimental-global-webcrypto`), fall back to `node:crypto`'s
|
|
92
|
+
* `randomUUID`, which returns a proper UUID v4 — matching the shape this
|
|
93
|
+
* function's JSDoc and README promise. `logixia/correlation` is Node-only
|
|
94
|
+
* (it builds on `AsyncLocalStorage`), so importing `node:crypto` is safe here.
|
|
95
|
+
*/
|
|
96
|
+
declare function generateCorrelationId(): string;
|
|
97
|
+
/**
|
|
98
|
+
* Return the correlation ID from the current `LogixiaContext`, or `undefined`
|
|
99
|
+
* when called outside any context scope.
|
|
100
|
+
*/
|
|
101
|
+
declare function getCurrentCorrelationId(): string | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Run `callback` inside a correlation context scope.
|
|
104
|
+
*
|
|
105
|
+
* If a correlation ID is not provided, a new UUID is generated.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* await withCorrelationId('cid-abc', async () => {
|
|
110
|
+
* logger.info('Processing message'); // → { correlationId: 'cid-abc', ... }
|
|
111
|
+
* await callDownstream(); // correlationFetch() picks up cid-abc
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function withCorrelationId<T>(correlationId: string | undefined, callback: () => T): T;
|
|
116
|
+
/**
|
|
117
|
+
* Express-compatible middleware that:
|
|
118
|
+
* 1. Reads `X-Correlation-ID` from the incoming request (or generates a new one)
|
|
119
|
+
* 2. Wraps the request in a `LogixiaContext` so all loggers in the handler
|
|
120
|
+
* automatically include `correlationId`
|
|
121
|
+
* 3. Writes the correlation ID back to the response header (opt-out via options)
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { correlationMiddleware } from 'logixia/correlation';
|
|
126
|
+
* app.use(correlationMiddleware());
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare function correlationMiddleware(options?: CorrelationMiddlewareOptions): (req: Record<string, unknown>, res: Record<string, unknown>, next: () => void) => void;
|
|
130
|
+
/**
|
|
131
|
+
* Fastify `onRequest` lifecycle hook — same functionality as `correlationMiddleware`
|
|
132
|
+
* but adapted to Fastify's hook API.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* import { correlationFastifyHook } from 'logixia/correlation';
|
|
137
|
+
* fastify.addHook('onRequest', correlationFastifyHook());
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
declare function correlationFastifyHook(options?: CorrelationMiddlewareOptions): (request: Record<string, unknown>, reply: Record<string, unknown>, done: () => void) => void;
|
|
141
|
+
/**
|
|
142
|
+
* A drop-in replacement for `fetch` that automatically injects the current
|
|
143
|
+
* `correlationId` from `LogixiaContext` as an `X-Correlation-ID` request header.
|
|
144
|
+
*
|
|
145
|
+
* Falls back to the global `fetch` when no correlation context is active.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { correlationFetch } from 'logixia/correlation';
|
|
150
|
+
*
|
|
151
|
+
* // Inside a request handler with correlationMiddleware active:
|
|
152
|
+
* const response = await correlationFetch('https://payments.internal/v1/charge', {
|
|
153
|
+
* method: 'POST',
|
|
154
|
+
* body: JSON.stringify({ amount: 100 }),
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
declare function correlationFetch(input: string | URL | Request, init?: RequestInit, options?: {
|
|
159
|
+
header?: string;
|
|
160
|
+
}): Promise<Response>;
|
|
161
|
+
/**
|
|
162
|
+
* Returns an axios request interceptor that automatically injects the current
|
|
163
|
+
* correlation ID into outgoing axios requests.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* import axios from 'axios';
|
|
168
|
+
* import { createCorrelationAxiosInterceptor } from 'logixia/correlation';
|
|
169
|
+
*
|
|
170
|
+
* axios.interceptors.request.use(createCorrelationAxiosInterceptor());
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function createCorrelationAxiosInterceptor(options?: {
|
|
174
|
+
header?: string;
|
|
175
|
+
}): (config: Record<string, unknown>) => Record<string, unknown>;
|
|
176
|
+
/**
|
|
177
|
+
* Create a child logger enriched with all correlation-relevant identifiers
|
|
178
|
+
* extracted from the incoming HTTP request.
|
|
179
|
+
*
|
|
180
|
+
* The child logger's every call automatically includes:
|
|
181
|
+
* `correlationId`, `requestId`, `method`, `url`, `userAgent`, `ip`
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* import { childFromRequest } from 'logixia/correlation';
|
|
186
|
+
* import { logger } from 'logixia';
|
|
187
|
+
*
|
|
188
|
+
* app.use((req, res, next) => {
|
|
189
|
+
* req.logger = childFromRequest(req, logger);
|
|
190
|
+
* next();
|
|
191
|
+
* });
|
|
192
|
+
*
|
|
193
|
+
* app.get('/orders', (req, res) => {
|
|
194
|
+
* req.logger.info('Listing orders'); // → correlationId, requestId, method, url ...
|
|
195
|
+
* });
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
declare function childFromRequest<TLogger extends {
|
|
199
|
+
child(ctx: Record<string, unknown>): TLogger;
|
|
200
|
+
}>(req: Record<string, unknown>, logger: TLogger, options?: {
|
|
201
|
+
correlationHeader?: string;
|
|
202
|
+
}): TLogger;
|
|
203
|
+
/**
|
|
204
|
+
* Extract a correlation context from a Kafka / RabbitMQ / SQS message object.
|
|
205
|
+
*
|
|
206
|
+
* Looks for `correlationId`, `correlation_id`, or `x-correlation-id` in
|
|
207
|
+
* `message.headers` (Kafka) or `message.MessageAttributes` (SQS).
|
|
208
|
+
*
|
|
209
|
+
* @example Kafka consumer
|
|
210
|
+
* ```ts
|
|
211
|
+
* import { extractMessageCorrelationId, withCorrelationId } from 'logixia/correlation';
|
|
212
|
+
*
|
|
213
|
+
* consumer.run({
|
|
214
|
+
* eachMessage: async ({ message }) => {
|
|
215
|
+
* const correlationId = extractMessageCorrelationId(message);
|
|
216
|
+
* await withCorrelationId(correlationId, () => processMessage(message));
|
|
217
|
+
* },
|
|
218
|
+
* });
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
declare function extractMessageCorrelationId(message: Record<string, unknown>): string | undefined;
|
|
222
|
+
/**
|
|
223
|
+
* Build Kafka message headers containing the current correlation context.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* import { buildKafkaCorrelationHeaders } from 'logixia/correlation';
|
|
228
|
+
*
|
|
229
|
+
* await producer.send({
|
|
230
|
+
* topic: 'orders',
|
|
231
|
+
* messages: [{ value: JSON.stringify(order), headers: buildKafkaCorrelationHeaders() }],
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function buildKafkaCorrelationHeaders(): Record<string, string>;
|
|
236
|
+
//#endregion
|
|
237
|
+
export { CorrelationContext, CorrelationMiddlewareOptions, buildKafkaCorrelationHeaders, childFromRequest, correlationFastifyHook, correlationFetch, correlationMiddleware, createCorrelationAxiosInterceptor, extractMessageCorrelationId, generateCorrelationId, getCurrentCorrelationId, withCorrelationId };
|
|
238
|
+
//# sourceMappingURL=correlation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correlation.d.ts","names":[],"sources":["../src/correlation.ts"],"sourcesContent":[],"mappings":";;AA4DA;AASA;AAoCA;AA2BA;AAkBA;AAoBA;;;;;AA+CA;;;;;AAsDA;;;;;;;AA4CA;AA4CA;;;;;;;AAmDA;AA4CA;;;;;;;;;;;;;;;;;;;;;UA1YiB,kBAAA;;;;;;;;UASA,4BAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoCD,qBAAA,CAAA;;;;;iBA2BA,uBAAA,CAAA;;;;;;;;;;;;;;iBAkBA,wEAAwE,IAAI;;;;;;;;;;;;;;iBAoB5E,qBAAA,WAA+B,qCAStC,8BACA;;;;;;;;;;;iBAqCO,sBAAA,WAAgC,yCASnC,gCACF;;;;;;;;;;;;;;;;;;iBA4CW,gBAAA,iBACJ,MAAM,gBAChB;;IAEL,QAAQ;;;;;;;;;;;;;iBAwCK,iCAAA;;aAEJ,4BAA4B;;;;;;;;;;;;;;;;;;;;;;;iBA0CxB;aAA8C,0BAA0B;QACjF,iCACG;;IAEP;;;;;;;;;;;;;;;;;;;iBA+Ca,2BAAA,UAAqC;;;;;;;;;;;;;;iBA4CrC,4BAAA,CAAA,GAAgC"}
|