@rayondigital/nest-dapr 0.9.59 → 0.9.61
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.
|
@@ -73,8 +73,9 @@ class ActorProxyBuilder {
|
|
|
73
73
|
}
|
|
74
74
|
const originalBody = args.length > 0 ? args : null;
|
|
75
75
|
const correlationId = this.daprContextService.getCorrelationId(true);
|
|
76
|
+
const traceId = this.daprContextService.getTraceId(true);
|
|
76
77
|
const body = yield this.prepareBody(this.daprContextService, args, originalBody);
|
|
77
|
-
return yield this.actorClient.actor.invoke(actorTypeClassName, actorId, methodName, body, correlationId);
|
|
78
|
+
return yield this.actorClient.actor.invoke(actorTypeClassName, actorId, methodName, body, correlationId, traceId);
|
|
78
79
|
});
|
|
79
80
|
}
|
|
80
81
|
prepareBody(daprContextService, args, body) {
|
|
@@ -70,15 +70,17 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
setupReentrancy(options) {
|
|
73
|
-
ActorClientHTTP_1.default.prototype.invoke = function (actorType, actorId, methodName, body, reentrancyId) {
|
|
73
|
+
ActorClientHTTP_1.default.prototype.invoke = function (actorType, actorId, methodName, body, reentrancyId, traceParent) {
|
|
74
|
+
var _a;
|
|
74
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
75
76
|
const urlSafeId = actorId.getURLSafeId();
|
|
76
77
|
const result = yield this.client.execute(`/actors/${actorType}/${urlSafeId}/method/${methodName}`, {
|
|
77
78
|
method: 'POST',
|
|
78
79
|
body,
|
|
79
80
|
headers: {
|
|
80
|
-
'Dapr-Reentrancy-Id': reentrancyId !== null && reentrancyId !== void 0 ? reentrancyId : (0, crypto_1.randomUUID)(),
|
|
81
|
+
'Dapr-Reentrancy-Id': (_a = reentrancyId !== null && reentrancyId !== void 0 ? reentrancyId : traceParent) !== null && _a !== void 0 ? _a : (0, crypto_1.randomUUID)(),
|
|
81
82
|
'X-Correlation-ID': reentrancyId,
|
|
83
|
+
traceparent: traceParent,
|
|
82
84
|
},
|
|
83
85
|
});
|
|
84
86
|
return result;
|
|
@@ -119,6 +121,10 @@ let NestActorManager = NestActorManager_1 = class NestActorManager {
|
|
|
119
121
|
if (correlationId) {
|
|
120
122
|
contextService.setCorrelationId(correlationId);
|
|
121
123
|
}
|
|
124
|
+
const traceId = context[dapr_context_service_1.DAPR_TRACE_ID_KEY];
|
|
125
|
+
if (traceId) {
|
|
126
|
+
contextService.setTraceId(traceId);
|
|
127
|
+
}
|
|
122
128
|
}
|
|
123
129
|
if (invokeWrapperFn) {
|
|
124
130
|
return yield invokeWrapperFn(actorId, methodName, data, originalCallActor.bind(this));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ClsService } from 'nestjs-cls';
|
|
2
2
|
export declare const DAPR_CONTEXT_KEY = "context";
|
|
3
3
|
export declare const DAPR_CORRELATION_ID_KEY = "correlationId";
|
|
4
|
+
export declare const DAPR_TRACE_ID_KEY = "traceId";
|
|
4
5
|
export declare class DaprContextService {
|
|
5
6
|
private readonly cls;
|
|
6
7
|
constructor(cls: ClsService);
|
|
@@ -8,11 +9,15 @@ export declare class DaprContextService {
|
|
|
8
9
|
getId(): string;
|
|
9
10
|
setIdIfNotDefined(id?: string): void;
|
|
10
11
|
getCorrelationId(createIfNotDefined?: boolean): any;
|
|
12
|
+
getTraceId(createIfNotDefined?: boolean): any;
|
|
11
13
|
setCorrelationId(value?: string): void;
|
|
12
14
|
setCorrelationIdIfNotDefined(value?: string): string;
|
|
15
|
+
setTraceId(value: string): void;
|
|
16
|
+
setTraceIdIfNotDefined(value?: string): string;
|
|
13
17
|
setByKey<T>(key: string | symbol, value: T): void;
|
|
14
18
|
setByKeyIfNotDefined<T>(key: string | symbol, value: T): any;
|
|
15
19
|
set<T>(value: T): void;
|
|
16
20
|
getByKey<T>(key: string): T | undefined;
|
|
17
21
|
get<T>(): T | undefined;
|
|
22
|
+
private randomTraceId;
|
|
18
23
|
}
|
|
@@ -9,12 +9,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.DaprContextService = exports.DAPR_CORRELATION_ID_KEY = exports.DAPR_CONTEXT_KEY = void 0;
|
|
12
|
+
exports.DaprContextService = exports.DAPR_TRACE_ID_KEY = exports.DAPR_CORRELATION_ID_KEY = exports.DAPR_CONTEXT_KEY = void 0;
|
|
13
13
|
const crypto_1 = require("crypto");
|
|
14
14
|
const common_1 = require("@nestjs/common");
|
|
15
15
|
const nestjs_cls_1 = require("nestjs-cls");
|
|
16
16
|
exports.DAPR_CONTEXT_KEY = 'context';
|
|
17
17
|
exports.DAPR_CORRELATION_ID_KEY = 'correlationId';
|
|
18
|
+
exports.DAPR_TRACE_ID_KEY = 'traceId';
|
|
18
19
|
let DaprContextService = class DaprContextService {
|
|
19
20
|
constructor(cls) {
|
|
20
21
|
this.cls = cls;
|
|
@@ -46,6 +47,18 @@ let DaprContextService = class DaprContextService {
|
|
|
46
47
|
return this.setCorrelationIdIfNotDefined();
|
|
47
48
|
return undefined;
|
|
48
49
|
}
|
|
50
|
+
getTraceId(createIfNotDefined = false) {
|
|
51
|
+
const byKey = this.getByKey(exports.DAPR_TRACE_ID_KEY);
|
|
52
|
+
if (byKey)
|
|
53
|
+
return byKey;
|
|
54
|
+
const context = this.get();
|
|
55
|
+
if (context && context[exports.DAPR_TRACE_ID_KEY]) {
|
|
56
|
+
return context[exports.DAPR_TRACE_ID_KEY];
|
|
57
|
+
}
|
|
58
|
+
if (createIfNotDefined)
|
|
59
|
+
return this.setTraceIdIfNotDefined();
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
49
62
|
setCorrelationId(value) {
|
|
50
63
|
this.setByKey(exports.DAPR_CORRELATION_ID_KEY, value !== null && value !== void 0 ? value : (0, crypto_1.randomUUID)().toString());
|
|
51
64
|
}
|
|
@@ -55,6 +68,15 @@ let DaprContextService = class DaprContextService {
|
|
|
55
68
|
this.setByKeyIfNotDefined(exports.DAPR_CORRELATION_ID_KEY, value);
|
|
56
69
|
return value;
|
|
57
70
|
}
|
|
71
|
+
setTraceId(value) {
|
|
72
|
+
this.setByKey(exports.DAPR_TRACE_ID_KEY, value !== null && value !== void 0 ? value : (0, crypto_1.randomUUID)().toString());
|
|
73
|
+
}
|
|
74
|
+
setTraceIdIfNotDefined(value) {
|
|
75
|
+
if (!value)
|
|
76
|
+
value = this.randomTraceId();
|
|
77
|
+
this.setByKeyIfNotDefined(exports.DAPR_TRACE_ID_KEY, value);
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
58
80
|
setByKey(key, value) {
|
|
59
81
|
this.getService().set(key, value);
|
|
60
82
|
}
|
|
@@ -84,12 +106,22 @@ let DaprContextService = class DaprContextService {
|
|
|
84
106
|
if (!contextObject[exports.DAPR_CORRELATION_ID_KEY]) {
|
|
85
107
|
contextObject[exports.DAPR_CORRELATION_ID_KEY] = this.getByKey(exports.DAPR_CORRELATION_ID_KEY);
|
|
86
108
|
}
|
|
109
|
+
if (!contextObject[exports.DAPR_TRACE_ID_KEY]) {
|
|
110
|
+
contextObject[exports.DAPR_TRACE_ID_KEY] = this.getByKey(exports.DAPR_TRACE_ID_KEY);
|
|
111
|
+
}
|
|
87
112
|
return contextObject;
|
|
88
113
|
}
|
|
89
114
|
catch (error) {
|
|
90
115
|
return undefined;
|
|
91
116
|
}
|
|
92
117
|
}
|
|
118
|
+
randomTraceId() {
|
|
119
|
+
const version = '00';
|
|
120
|
+
const traceId = (0, crypto_1.randomBytes)(16).toString('hex');
|
|
121
|
+
const spanId = (0, crypto_1.randomBytes)(8).toString('hex');
|
|
122
|
+
const traceFlags = '01';
|
|
123
|
+
return `${version}-${traceId}-${spanId}-${traceFlags}`;
|
|
124
|
+
}
|
|
93
125
|
};
|
|
94
126
|
DaprContextService = __decorate([
|
|
95
127
|
(0, common_1.Injectable)(),
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Attributes, SpanKind } from '@opentelemetry/api';
|
|
2
|
+
import { ClsService } from 'nestjs-cls';
|
|
3
|
+
import { DaprContextService } from '../dapr-context-service';
|
|
4
|
+
export declare function withTracedContext<T>(contextService: ClsService | DaprContextService | undefined, operationName: string, operation: () => Promise<T>, spanKind?: SpanKind, spanAttributes?: Attributes, tracerName?: string): Promise<T>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.withTracedContext = void 0;
|
|
13
|
+
const api_1 = require("@opentelemetry/api");
|
|
14
|
+
const nestjs_cls_1 = require("nestjs-cls");
|
|
15
|
+
const dapr_context_service_1 = require("../dapr-context-service");
|
|
16
|
+
function withTracedContext(contextService, operationName, operation, spanKind = api_1.SpanKind.INTERNAL, spanAttributes = undefined, tracerName = 'nest-dapr') {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const tracer = api_1.trace.getTracer(tracerName);
|
|
19
|
+
const activeContext = api_1.context.active();
|
|
20
|
+
if (!activeContext) {
|
|
21
|
+
return yield operation();
|
|
22
|
+
}
|
|
23
|
+
const currentSpan = api_1.trace.getSpan(api_1.context.active());
|
|
24
|
+
const span = currentSpan !== null && currentSpan !== void 0 ? currentSpan : tracer.startSpan(operationName, { attributes: spanAttributes, kind: spanKind !== null && spanKind !== void 0 ? spanKind : api_1.SpanKind.SERVER });
|
|
25
|
+
const isNewSpan = !currentSpan;
|
|
26
|
+
if (contextService === undefined) {
|
|
27
|
+
contextService = nestjs_cls_1.ClsServiceManager.getClsService();
|
|
28
|
+
}
|
|
29
|
+
const spanContext = span.spanContext();
|
|
30
|
+
const traceId = `00-${spanContext.traceId}-${spanContext.spanId}-0${spanContext.traceFlags.toString(16)}`;
|
|
31
|
+
if (contextService instanceof nestjs_cls_1.ClsService) {
|
|
32
|
+
contextService.set(dapr_context_service_1.DAPR_TRACE_ID_KEY, traceId);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
contextService.setTraceId(traceId);
|
|
36
|
+
}
|
|
37
|
+
return api_1.context.with(api_1.trace.setSpan(activeContext, span), () => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
try {
|
|
39
|
+
const result = yield operation();
|
|
40
|
+
if (isNewSpan) {
|
|
41
|
+
span.end();
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
span.recordException(error);
|
|
47
|
+
if (isNewSpan) {
|
|
48
|
+
span.end();
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
exports.withTracedContext = withTracedContext;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rayondigital/nest-dapr",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.61",
|
|
4
4
|
"description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -24,9 +24,12 @@
|
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"@nestjs/common": "^10.0.0",
|
|
26
26
|
"@nestjs/core": "^10.0.0",
|
|
27
|
+
"@opentelemetry/api": "^1.0.0",
|
|
28
|
+
"@opentelemetry/core": "^1.0.0",
|
|
29
|
+
"@opentelemetry/sdk-trace-base": "^1.0.0",
|
|
30
|
+
"eventemitter2": "^6.0.0",
|
|
27
31
|
"nestjs-cls": "^3.4.0",
|
|
28
|
-
"rxjs": "^7.1.0"
|
|
29
|
-
"eventemitter2": "^6.0.0"
|
|
32
|
+
"rxjs": "^7.1.0"
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
35
|
"@dapr/dapr": "^3.3.1",
|
|
@@ -35,6 +38,9 @@
|
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"@nestjs/platform-express": "^10.0.0",
|
|
37
40
|
"@nestjs/testing": "^10.0.0",
|
|
41
|
+
"@opentelemetry/auto-instrumentations-node": "^0.44.0",
|
|
42
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.50.0",
|
|
43
|
+
"@opentelemetry/sdk-trace-node": "^1.23.0",
|
|
38
44
|
"@types/jest": "^29.5.12",
|
|
39
45
|
"@typescript-eslint/eslint-plugin": "^5.29.0",
|
|
40
46
|
"eslint": "^8.4.1",
|