@lad-tech/nsc-toolkit 1.20.1 → 1.21.0
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/CHANGELOG.md +3 -3
- package/dist/Client.js +22 -10
- package/dist/Service.js +12 -4
- package/dist/types/Service.d.ts +5 -0
- package/dist/types/interfaces.d.ts +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
# [1.21.0](https://github.com/lad-tech/nsc-toolkit/compare/v1.20.1...v1.21.0) (2024-08-14)
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
###
|
|
4
|
+
### Features
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* Add new requestId field into baggage ([#113](https://github.com/lad-tech/nsc-toolkit/issues/113)) ([02c222d](https://github.com/lad-tech/nsc-toolkit/commit/02c222d13b7325211edc6049cad8301440b64b47))
|
package/dist/Client.js
CHANGED
|
@@ -110,7 +110,7 @@ class Client extends Root_1.Root {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
async request(subject, data, { options, request, response }) {
|
|
113
|
-
var _a, _b, _c, _d;
|
|
113
|
+
var _a, _b, _c, _d, _e;
|
|
114
114
|
const tracer = opentelemetry.trace.getTracer('');
|
|
115
115
|
const span = tracer.startSpan(subject, undefined, this.getContext(this.baggage));
|
|
116
116
|
try {
|
|
@@ -119,7 +119,8 @@ class Client extends Root_1.Root {
|
|
|
119
119
|
}
|
|
120
120
|
const { spanId, traceId, traceFlags } = span.spanContext();
|
|
121
121
|
const expired = this.getExpired((_b = this.baggage) === null || _b === void 0 ? void 0 : _b.expired, options === null || options === void 0 ? void 0 : options.timeout);
|
|
122
|
-
const
|
|
122
|
+
const requestId = (_c = this.baggage) === null || _c === void 0 ? void 0 : _c.requestId;
|
|
123
|
+
const message = { payload: data, baggage: { expired, traceId, spanId, traceFlags, requestId } };
|
|
123
124
|
const timeout = expired - Date.now();
|
|
124
125
|
if (timeout <= 0) {
|
|
125
126
|
throw new Error('Timeout request service ' + subject);
|
|
@@ -141,9 +142,9 @@ class Client extends Root_1.Root {
|
|
|
141
142
|
? await this.makeHttpRequest(subject, message, options, timeout)
|
|
142
143
|
: await this.makeBrokerRequest(subject, message, timeout);
|
|
143
144
|
if (result.error) {
|
|
144
|
-
throw new Error((
|
|
145
|
+
throw new Error((_d = result.error.message) !== null && _d !== void 0 ? _d : result.error);
|
|
145
146
|
}
|
|
146
|
-
if (((
|
|
147
|
+
if (((_e = options === null || options === void 0 ? void 0 : options.runTimeValidation) === null || _e === void 0 ? void 0 : _e.response) && response) {
|
|
147
148
|
this.validate(result.payload, response);
|
|
148
149
|
}
|
|
149
150
|
if ((options === null || options === void 0 ? void 0 : options.cache) && !this.isStream(result.payload) && this.cache) {
|
|
@@ -236,12 +237,23 @@ class Client extends Root_1.Root {
|
|
|
236
237
|
if (!baggage) {
|
|
237
238
|
return {};
|
|
238
239
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
'nsc-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
240
|
+
const headers = {};
|
|
241
|
+
if (baggage.expired) {
|
|
242
|
+
headers['nsc-expired'] = baggage.expired;
|
|
243
|
+
}
|
|
244
|
+
if (baggage.requestId) {
|
|
245
|
+
headers['x-request-id'] = baggage.requestId;
|
|
246
|
+
}
|
|
247
|
+
if (baggage.traceId) {
|
|
248
|
+
headers['nsc-trace-id'] = baggage.traceId;
|
|
249
|
+
}
|
|
250
|
+
if (baggage.spanId) {
|
|
251
|
+
headers['nsc-span-id'] = baggage.spanId;
|
|
252
|
+
}
|
|
253
|
+
if (baggage.traceFlags) {
|
|
254
|
+
headers['nsc-trace-flags'] = baggage.traceFlags;
|
|
255
|
+
}
|
|
256
|
+
return headers;
|
|
245
257
|
}
|
|
246
258
|
isJsMessage(message) {
|
|
247
259
|
return !!message.ack && !!message.nak;
|
package/dist/Service.js
CHANGED
|
@@ -192,7 +192,13 @@ class Service extends Root_1.Root {
|
|
|
192
192
|
*/
|
|
193
193
|
getNextBaggage(span, baggage) {
|
|
194
194
|
const { traceId, spanId, traceFlags } = span.spanContext();
|
|
195
|
-
return { traceId, spanId, traceFlags, expired: baggage === null || baggage === void 0 ? void 0 : baggage.expired };
|
|
195
|
+
return { traceId, spanId, traceFlags, expired: baggage === null || baggage === void 0 ? void 0 : baggage.expired, requestId: baggage === null || baggage === void 0 ? void 0 : baggage.requestId };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Guard for determine whether baggage contains trace information
|
|
199
|
+
*/
|
|
200
|
+
isBaggageContainTrace(params) {
|
|
201
|
+
return !!params && !!(params.traceId && params.spanId && params.traceFlags);
|
|
196
202
|
}
|
|
197
203
|
/**
|
|
198
204
|
* If there is no baggage. For example, in HTTP Gateway
|
|
@@ -200,7 +206,7 @@ class Service extends Root_1.Root {
|
|
|
200
206
|
getRootBaggage(subject, headers, ownTimeout) {
|
|
201
207
|
const baggage = headers ? this.getBaggageFromHTTPHeader(headers) : undefined;
|
|
202
208
|
const tracer = api_1.trace.getTracer('');
|
|
203
|
-
const context = this.getContext(baggage);
|
|
209
|
+
const context = this.getContext(this.isBaggageContainTrace(baggage) ? baggage : undefined);
|
|
204
210
|
const span = tracer.startSpan(subject, undefined, context);
|
|
205
211
|
const newBaggage = this.getNextBaggage(span, baggage);
|
|
206
212
|
this.rootSpans.set(newBaggage.traceId, span);
|
|
@@ -333,7 +339,7 @@ class Service extends Root_1.Root {
|
|
|
333
339
|
async handled(payload, Method, baggage) {
|
|
334
340
|
const subject = `${this.serviceName}.${Method.settings.action}`;
|
|
335
341
|
const tracer = api_1.trace.getTracer('');
|
|
336
|
-
const context = this.getContext(baggage);
|
|
342
|
+
const context = this.getContext(this.isBaggageContainTrace(baggage) ? baggage : undefined);
|
|
337
343
|
const span = tracer.startSpan(subject, undefined, context);
|
|
338
344
|
const logger = new toolbelt_1.Logs.Logger({
|
|
339
345
|
location: `${this.serviceName}.${Method.settings.action}`,
|
|
@@ -579,15 +585,17 @@ class Service extends Root_1.Root {
|
|
|
579
585
|
const traceId = headers['nsc-trace-id'];
|
|
580
586
|
const spanId = headers['nsc-span-id'];
|
|
581
587
|
const traceFlags = headers['nsc-trace-flags'] ? +headers['nsc-trace-flags'] : undefined;
|
|
588
|
+
const requestId = headers['x-request-id'] ? String(headers['x-request-id']) : undefined;
|
|
582
589
|
if (traceId && spanId && traceFlags) {
|
|
583
590
|
return {
|
|
584
591
|
traceId,
|
|
585
592
|
spanId,
|
|
586
593
|
traceFlags,
|
|
587
594
|
expired,
|
|
595
|
+
requestId,
|
|
588
596
|
};
|
|
589
597
|
}
|
|
590
|
-
return
|
|
598
|
+
return { expired, requestId };
|
|
591
599
|
}
|
|
592
600
|
}
|
|
593
601
|
exports.Service = Service;
|
package/dist/types/Service.d.ts
CHANGED
|
@@ -37,6 +37,10 @@ export declare class Service<E extends Emitter = Emitter> extends Root {
|
|
|
37
37
|
* Create Baggage from span. Expired one-on-one business logic call
|
|
38
38
|
*/
|
|
39
39
|
private getNextBaggage;
|
|
40
|
+
/**
|
|
41
|
+
* Guard for determine whether baggage contains trace information
|
|
42
|
+
*/
|
|
43
|
+
private isBaggageContainTrace;
|
|
40
44
|
/**
|
|
41
45
|
* If there is no baggage. For example, in HTTP Gateway
|
|
42
46
|
*/
|
|
@@ -45,6 +49,7 @@ export declare class Service<E extends Emitter = Emitter> extends Root {
|
|
|
45
49
|
traceId: string;
|
|
46
50
|
spanId: string;
|
|
47
51
|
traceFlags: number;
|
|
52
|
+
requestId: string | undefined;
|
|
48
53
|
};
|
|
49
54
|
/**
|
|
50
55
|
* End root baggage
|
|
@@ -30,6 +30,7 @@ export type Baggage = {
|
|
|
30
30
|
traceId: string;
|
|
31
31
|
spanId: string;
|
|
32
32
|
traceFlags: number;
|
|
33
|
+
requestId?: string;
|
|
33
34
|
expired?: number;
|
|
34
35
|
};
|
|
35
36
|
export type ExternalBaggage = {
|
|
@@ -37,6 +38,7 @@ export type ExternalBaggage = {
|
|
|
37
38
|
'nsc-trace-id'?: string;
|
|
38
39
|
'nsc-span-id'?: string;
|
|
39
40
|
'nsc-trace-flags'?: number;
|
|
41
|
+
'x-request-id'?: string;
|
|
40
42
|
};
|
|
41
43
|
export interface Message<M = any> {
|
|
42
44
|
payload: M;
|