@vtvlive/interactive-apm 0.0.12 → 0.0.14
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/dist/interfaces/tracing-provider.interface.d.ts +5 -5
- package/dist/interfaces/tracing-provider.interface.d.ts.map +1 -1
- package/dist/providers/elastic-apm.tracing-provider.d.ts +8 -7
- package/dist/providers/elastic-apm.tracing-provider.d.ts.map +1 -1
- package/dist/providers/elastic-apm.tracing-provider.js +16 -8
- package/dist/providers/opentelemetry.tracing-provider.d.ts +5 -5
- package/dist/providers/opentelemetry.tracing-provider.d.ts.map +1 -1
- package/dist/providers/opentelemetry.tracing-provider.js +3 -3
- package/dist/services/tracing.service.d.ts +5 -5
- package/dist/services/tracing.service.d.ts.map +1 -1
- package/dist/services/tracing.service.js +2 -2
- package/dist/types/apm.types.d.ts +5 -3
- package/dist/types/apm.types.d.ts.map +1 -1
- package/dist/utils/tracing.helper.d.ts +7 -6
- package/dist/utils/tracing.helper.d.ts.map +1 -1
- package/dist/utils/tracing.helper.js +39 -22
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ISpan, SpanKind } from "../types/apm.types";
|
|
1
|
+
import { ISpan, SpanAttributes, SpanAttributeValue, SpanKind } from "../types/apm.types";
|
|
2
2
|
/**
|
|
3
3
|
* Interface chung cho APM Tracing Provider
|
|
4
4
|
* Cho phép switch giữa OpenTelemetry và Elastic APM thông qua DI
|
|
@@ -16,7 +16,7 @@ export interface ITracingProvider {
|
|
|
16
16
|
* @param spanKind Loại span (INTERNAL, SERVER, CLIENT, PRODUCER, CONSUMER, WEBSOCKET, OTHER)
|
|
17
17
|
* @returns Span object - cần gọi end() khi hoàn thành
|
|
18
18
|
*/
|
|
19
|
-
startSpan(name: string, attributes?:
|
|
19
|
+
startSpan(name: string, attributes?: SpanAttributes, spanKind?: SpanKind): ISpan | null;
|
|
20
20
|
/**
|
|
21
21
|
* Thực thi function với context tracing (auto-close span)
|
|
22
22
|
* @param name Tên của span
|
|
@@ -24,7 +24,7 @@ export interface ITracingProvider {
|
|
|
24
24
|
* @param attributes Các attributes metadata
|
|
25
25
|
* @returns Kết quả của function
|
|
26
26
|
*/
|
|
27
|
-
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?:
|
|
27
|
+
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?: SpanAttributes): Promise<T>;
|
|
28
28
|
/**
|
|
29
29
|
* Capture error vào active span hiện tại
|
|
30
30
|
* @param error Error object
|
|
@@ -35,7 +35,7 @@ export interface ITracingProvider {
|
|
|
35
35
|
* @param key Tên attribute
|
|
36
36
|
* @param value Giá trị attribute
|
|
37
37
|
*/
|
|
38
|
-
setAttribute(key: string, value:
|
|
38
|
+
setAttribute(key: string, value: SpanAttributeValue): void;
|
|
39
39
|
/**
|
|
40
40
|
* Force flush all pending spans
|
|
41
41
|
*/
|
|
@@ -44,7 +44,7 @@ export interface ITracingProvider {
|
|
|
44
44
|
* Kết thúc span manually
|
|
45
45
|
* @param span Span cần end (nếu không truyền, sẽ end active span)
|
|
46
46
|
*/
|
|
47
|
-
endSpan(span?: ISpan | null): void;
|
|
47
|
+
endSpan(span?: ISpan | null, result?: unknown): void;
|
|
48
48
|
/**
|
|
49
49
|
* Flush và shutdown provider (cho graceful shutdown)
|
|
50
50
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracing-provider.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/tracing-provider.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"tracing-provider.interface.d.ts","sourceRoot":"","sources":["../../src/interfaces/tracing-provider.interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEzF;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,UAAU,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7C;;;;;;OAMG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,cAAc,EAAE,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,IAAI,CAAC;IAExF;;;;;;OAMG;IACH,mBAAmB,CAAC,CAAC,EACnB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,EACtC,UAAU,CAAC,EAAE,cAAc,GAC1B,OAAO,CAAC,CAAC,CAAC,CAAC;IAEd;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAEjC;;;;OAIG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,IAAI,CAAC;IAE3D;;OAEG;IACH,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE7B;;;OAGG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAErD;;OAEG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { SpanKind } from "../types/apm-provider.type";
|
|
2
2
|
import { ITracingProvider } from "../interfaces/tracing-provider.interface";
|
|
3
|
-
import { ISpan } from "../types/apm.types";
|
|
3
|
+
import { ISpan, SpanAttributes, SpanAttributeValue } from "../types/apm.types";
|
|
4
4
|
/**
|
|
5
5
|
* Elastic APM specific span interface
|
|
6
6
|
*/
|
|
7
7
|
export interface IElasticApmSpan extends ISpan {
|
|
8
8
|
/** Set a label on the span */
|
|
9
|
-
setLabel(key: string, value: string): void;
|
|
9
|
+
setLabel(key: string, value: string | number | boolean): void;
|
|
10
10
|
/** Set the span type */
|
|
11
11
|
setType(type: string): void;
|
|
12
12
|
/** Set the outcome (success, failure) */
|
|
13
13
|
outcome?: string;
|
|
14
14
|
/** End the span */
|
|
15
|
-
end(): void;
|
|
15
|
+
end(result?: unknown): void;
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Elastic APM Tracing Provider Implementation
|
|
@@ -22,6 +22,7 @@ export declare class ElasticApmTracingProvider implements ITracingProvider {
|
|
|
22
22
|
private apm;
|
|
23
23
|
private readonly serviceName;
|
|
24
24
|
private readonly environment;
|
|
25
|
+
private static isElasticLabelValue;
|
|
25
26
|
constructor(config?: {
|
|
26
27
|
serviceName?: string;
|
|
27
28
|
environment?: string;
|
|
@@ -43,11 +44,11 @@ export declare class ElasticApmTracingProvider implements ITracingProvider {
|
|
|
43
44
|
/**
|
|
44
45
|
* Bắt đầu một span mới
|
|
45
46
|
*/
|
|
46
|
-
startSpan(name: string, attributes?:
|
|
47
|
+
startSpan(name: string, attributes?: SpanAttributes, spanKind?: SpanKind): IElasticApmSpan | null;
|
|
47
48
|
/**
|
|
48
49
|
* Thực thi function với tracing context
|
|
49
50
|
*/
|
|
50
|
-
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?:
|
|
51
|
+
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?: SpanAttributes): Promise<T>;
|
|
51
52
|
/**
|
|
52
53
|
* Capture error vào APM
|
|
53
54
|
*/
|
|
@@ -55,11 +56,11 @@ export declare class ElasticApmTracingProvider implements ITracingProvider {
|
|
|
55
56
|
/**
|
|
56
57
|
* Set label cho current transaction/span
|
|
57
58
|
*/
|
|
58
|
-
setAttribute(key: string, value:
|
|
59
|
+
setAttribute(key: string, value: SpanAttributeValue): void;
|
|
59
60
|
/**
|
|
60
61
|
* End span manually
|
|
61
62
|
*/
|
|
62
|
-
endSpan(span?: ISpan | null): void;
|
|
63
|
+
endSpan(span?: ISpan | null, result?: unknown): void;
|
|
63
64
|
/**
|
|
64
65
|
* Flush và shutdown gracefully
|
|
65
66
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elastic-apm.tracing-provider.d.ts","sourceRoot":"","sources":["../../src/providers/elastic-apm.tracing-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"elastic-apm.tracing-provider.d.ts","sourceRoot":"","sources":["../../src/providers/elastic-apm.tracing-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAU/E;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,KAAK;IAC5C,8BAA8B;IAC9B,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IAE9D,wBAAwB;IACxB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE5B,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,mBAAmB;IACnB,GAAG,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;CAC7B;AA0BD;;;GAGG;AACH,qBAAa,yBAA0B,YAAW,gBAAgB;IAChE,OAAO,CAAC,GAAG,CAAiC;IAC5C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACr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,CAAC,iBAAiB;IAmBzB;;OAEG;IACH,OAAO,CAAC,mBAAmB;CAe5B"}
|
|
@@ -9,6 +9,9 @@ const elastic_apm_compat_1 = require("../utils/elastic-apm-compat");
|
|
|
9
9
|
* Sử dụng elastic-apm-node agent để gửi traces về Elastic APM server
|
|
10
10
|
*/
|
|
11
11
|
class ElasticApmTracingProvider {
|
|
12
|
+
static isElasticLabelValue(value) {
|
|
13
|
+
return value !== undefined && !Array.isArray(value);
|
|
14
|
+
}
|
|
12
15
|
constructor(config = {}) {
|
|
13
16
|
this.apm = null;
|
|
14
17
|
this.serviceName =
|
|
@@ -93,7 +96,9 @@ class ElasticApmTracingProvider {
|
|
|
93
96
|
// Set labels từ attributes
|
|
94
97
|
if (attributes) {
|
|
95
98
|
for (const [key, value] of Object.entries(attributes)) {
|
|
96
|
-
|
|
99
|
+
if (ElasticApmTracingProvider.isElasticLabelValue(value)) {
|
|
100
|
+
span.setLabel(key, value);
|
|
101
|
+
}
|
|
97
102
|
}
|
|
98
103
|
}
|
|
99
104
|
// Set span type dựa trên kind
|
|
@@ -112,6 +117,9 @@ class ElasticApmTracingProvider {
|
|
|
112
117
|
}
|
|
113
118
|
return;
|
|
114
119
|
}
|
|
120
|
+
if (!ElasticApmTracingProvider.isElasticLabelValue(value)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
115
123
|
originalSetLabel(key, value);
|
|
116
124
|
};
|
|
117
125
|
// Override setAttribute (if it exists, maps to setLabel)
|
|
@@ -130,7 +138,7 @@ class ElasticApmTracingProvider {
|
|
|
130
138
|
}
|
|
131
139
|
// Override end() để end transaction sau
|
|
132
140
|
const originalEnd = span.end.bind(span);
|
|
133
|
-
span.end = () => {
|
|
141
|
+
span.end = (result) => {
|
|
134
142
|
if (isEnded) {
|
|
135
143
|
if ((0, debug_logger_1.isDebugEnabled)()) {
|
|
136
144
|
console.warn(`[ElasticAPM] Span "${name}" has already been ended. Ignoring duplicate end().`);
|
|
@@ -146,10 +154,10 @@ class ElasticApmTracingProvider {
|
|
|
146
154
|
startTime,
|
|
147
155
|
endTime,
|
|
148
156
|
});
|
|
149
|
-
originalEnd();
|
|
157
|
+
originalEnd(result);
|
|
150
158
|
// End transaction if we created it
|
|
151
159
|
if (createdTransaction && transaction) {
|
|
152
|
-
transaction.end();
|
|
160
|
+
transaction.end(result);
|
|
153
161
|
}
|
|
154
162
|
};
|
|
155
163
|
return span;
|
|
@@ -192,17 +200,17 @@ class ElasticApmTracingProvider {
|
|
|
192
200
|
*/
|
|
193
201
|
setAttribute(key, value) {
|
|
194
202
|
const span = this.apm?.currentSpan;
|
|
195
|
-
if (span) {
|
|
196
|
-
span.setLabel(key,
|
|
203
|
+
if (span && ElasticApmTracingProvider.isElasticLabelValue(value)) {
|
|
204
|
+
span.setLabel(key, value);
|
|
197
205
|
}
|
|
198
206
|
}
|
|
199
207
|
/**
|
|
200
208
|
* End span manually
|
|
201
209
|
*/
|
|
202
|
-
endSpan(span) {
|
|
210
|
+
endSpan(span, result) {
|
|
203
211
|
const activeSpan = (span || this.apm?.currentSpan);
|
|
204
212
|
if (activeSpan) {
|
|
205
|
-
activeSpan.end();
|
|
213
|
+
activeSpan.end(result);
|
|
206
214
|
}
|
|
207
215
|
}
|
|
208
216
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ITracingProvider } from "../interfaces/tracing-provider.interface";
|
|
2
2
|
import { OtlpTransport } from "../types/otlp-transport.type";
|
|
3
|
-
import { ISpan, SpanKind } from "../types/apm.types";
|
|
3
|
+
import { ISpan, SpanAttributes, SpanAttributeValue, SpanKind } from "../types/apm.types";
|
|
4
4
|
/**
|
|
5
5
|
* OpenTelemetry Tracing Provider Implementation
|
|
6
6
|
* Sử dụng OpenTelemetry SDK với OTLP exporter để gửi traces về Elastic APM
|
|
@@ -43,11 +43,11 @@ export declare class OpenTelemetryTracingProvider implements ITracingProvider {
|
|
|
43
43
|
/**
|
|
44
44
|
* Bắt đầu một span mới
|
|
45
45
|
*/
|
|
46
|
-
startSpan(name: string, attributes?:
|
|
46
|
+
startSpan(name: string, attributes?: SpanAttributes, spanKind?: SpanKind): ISpan | null;
|
|
47
47
|
/**
|
|
48
48
|
* Thực thi function với tracing context
|
|
49
49
|
*/
|
|
50
|
-
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?:
|
|
50
|
+
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?: SpanAttributes): Promise<T>;
|
|
51
51
|
/**
|
|
52
52
|
* Capture error vào active span
|
|
53
53
|
*/
|
|
@@ -55,7 +55,7 @@ export declare class OpenTelemetryTracingProvider implements ITracingProvider {
|
|
|
55
55
|
/**
|
|
56
56
|
* Set attribute cho active span
|
|
57
57
|
*/
|
|
58
|
-
setAttribute(key: string, value:
|
|
58
|
+
setAttribute(key: string, value: SpanAttributeValue): void;
|
|
59
59
|
/**
|
|
60
60
|
* Force flush all pending spans
|
|
61
61
|
*/
|
|
@@ -63,7 +63,7 @@ export declare class OpenTelemetryTracingProvider implements ITracingProvider {
|
|
|
63
63
|
/**
|
|
64
64
|
* End span manually
|
|
65
65
|
*/
|
|
66
|
-
endSpan(span?: ISpan | null): void;
|
|
66
|
+
endSpan(span?: ISpan | null, result?: unknown): void;
|
|
67
67
|
/**
|
|
68
68
|
* Shutdown gracefully
|
|
69
69
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opentelemetry.tracing-provider.d.ts","sourceRoot":"","sources":["../../src/providers/opentelemetry.tracing-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"opentelemetry.tracing-provider.d.ts","sourceRoot":"","sources":["../../src/providers/opentelemetry.tracing-provider.ts"],"names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AAa,CAAC,EAAE,MAAM,CAAC;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACrC,qBAAqB,CAAC,EAAE,OAAO,CAAC;QAChC,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,GAAG,OAAO,CAAC,IAAI,CAAC;IA+NjB;;OAEG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,cAAc,EAC3B,QAAQ,GAAE,QAA4B,GACrC,KAAK,GAAG,IAAI;IA+Gf;;OAEG;IACG,mBAAmB,CAAC,CAAC,EACzB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,EACtC,UAAU,CAAC,EAAE,cAAc,GAC1B,OAAO,CAAC,CAAC,CAAC;IAmCb;;OAEG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAiBhC;;OAEG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,IAAI;IAa1D;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAajC;;OAEG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI;IAapD;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAM/B;;OAEG;IACH,OAAO,CAAC,WAAW;CAmBpB"}
|
|
@@ -478,7 +478,7 @@ class OpenTelemetryTracingProvider {
|
|
|
478
478
|
* Set attribute cho active span
|
|
479
479
|
*/
|
|
480
480
|
setAttribute(key, value) {
|
|
481
|
-
if (!this.initialized) {
|
|
481
|
+
if (!this.initialized || value === undefined) {
|
|
482
482
|
return;
|
|
483
483
|
}
|
|
484
484
|
// @ts-ignore - Optional peer dependency
|
|
@@ -507,7 +507,7 @@ class OpenTelemetryTracingProvider {
|
|
|
507
507
|
/**
|
|
508
508
|
* End span manually
|
|
509
509
|
*/
|
|
510
|
-
endSpan(span) {
|
|
510
|
+
endSpan(span, result) {
|
|
511
511
|
if (!this.initialized) {
|
|
512
512
|
return;
|
|
513
513
|
}
|
|
@@ -515,7 +515,7 @@ class OpenTelemetryTracingProvider {
|
|
|
515
515
|
const { trace } = require("@opentelemetry/api");
|
|
516
516
|
const activeSpan = span || trace.getActiveSpan();
|
|
517
517
|
if (activeSpan) {
|
|
518
|
-
activeSpan.end();
|
|
518
|
+
activeSpan.end(result);
|
|
519
519
|
}
|
|
520
520
|
}
|
|
521
521
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ITracingProvider } from "../interfaces/tracing-provider.interface";
|
|
2
|
-
import { ISpan, SpanKind } from "../types/apm.types";
|
|
2
|
+
import { ISpan, SpanAttributes, SpanAttributeValue, SpanKind } from "../types/apm.types";
|
|
3
3
|
/**
|
|
4
4
|
* Tracing Service - Wrapper cho ITracingProvider
|
|
5
5
|
* Service này được inject vào controllers/services để sử dụng tracing
|
|
@@ -47,7 +47,7 @@ export declare class TracingService {
|
|
|
47
47
|
* span.end();
|
|
48
48
|
* }
|
|
49
49
|
*/
|
|
50
|
-
startSpan(name: string, attributes?:
|
|
50
|
+
startSpan(name: string, attributes?: SpanAttributes, spanKind?: SpanKind): ISpan | null;
|
|
51
51
|
/**
|
|
52
52
|
* Thực thi function với context tracing (auto-close span)
|
|
53
53
|
* @param name Tên của span
|
|
@@ -64,7 +64,7 @@ export declare class TracingService {
|
|
|
64
64
|
* }
|
|
65
65
|
* );
|
|
66
66
|
*/
|
|
67
|
-
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?:
|
|
67
|
+
startSpanWithParent<T>(name: string, fn: (span: ISpan | null) => Promise<T>, attributes?: SpanAttributes): Promise<T>;
|
|
68
68
|
/**
|
|
69
69
|
* Capture error vào active span hiện tại
|
|
70
70
|
* @param error Error object
|
|
@@ -75,12 +75,12 @@ export declare class TracingService {
|
|
|
75
75
|
* @param key Tên attribute
|
|
76
76
|
* @param value Giá trị attribute
|
|
77
77
|
*/
|
|
78
|
-
setAttribute(key: string, value:
|
|
78
|
+
setAttribute(key: string, value: SpanAttributeValue): void;
|
|
79
79
|
/**
|
|
80
80
|
* Kết thúc span manually
|
|
81
81
|
* @param span Span cần end (nếu không truyền, sẽ end active span)
|
|
82
82
|
*/
|
|
83
|
-
endSpan(span?: ISpan): void;
|
|
83
|
+
endSpan(span?: ISpan, result?: unknown): void;
|
|
84
84
|
/**
|
|
85
85
|
* Flush và shutdown APM provider (cho graceful shutdown)
|
|
86
86
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracing.service.d.ts","sourceRoot":"","sources":["../../src/services/tracing.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"tracing.service.d.ts","sourceRoot":"","sources":["../../src/services/tracing.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0CAA0C,CAAC;AAC5E,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEzF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,cAAc;IACb,OAAO,CAAC,QAAQ,CAAC,QAAQ;gBAAR,QAAQ,EAAE,gBAAgB;IAEvD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,cAAc,EAC3B,QAAQ,GAAE,QAA4B,GACrC,KAAK,GAAG,IAAI;IAIf;;;;;;;;;;;;;;;OAeG;IACH,mBAAmB,CAAC,CAAC,EACnB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,EACtC,UAAU,CAAC,EAAE,cAAc,GAC1B,OAAO,CAAC,CAAC,CAAC;IAIb;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAIhC;;;;OAIG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB,GAAG,IAAI;IAI1D;;;OAGG;IACH,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI;IAI7C;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAGhC"}
|
|
@@ -91,8 +91,8 @@ class TracingService {
|
|
|
91
91
|
* Kết thúc span manually
|
|
92
92
|
* @param span Span cần end (nếu không truyền, sẽ end active span)
|
|
93
93
|
*/
|
|
94
|
-
endSpan(span) {
|
|
95
|
-
this.provider.endSpan(span);
|
|
94
|
+
endSpan(span, result) {
|
|
95
|
+
this.provider.endSpan(span, result);
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
98
|
* Flush và shutdown APM provider (cho graceful shutdown)
|
|
@@ -10,13 +10,15 @@ export { ApmProvider, type ApmProviderType, SpanKind, OtlpTransport, type OtlpTr
|
|
|
10
10
|
/**
|
|
11
11
|
* Common Span interface - works with both Elastic APM and OpenTelemetry
|
|
12
12
|
*/
|
|
13
|
+
export type SpanAttributeValue = string | number | boolean | string[] | undefined;
|
|
14
|
+
export type SpanAttributes = Record<string, SpanAttributeValue>;
|
|
13
15
|
export interface ISpan {
|
|
14
16
|
/** Span name */
|
|
15
17
|
name: string;
|
|
16
18
|
/** End the span */
|
|
17
|
-
end(): void;
|
|
19
|
+
end(result?: unknown): void;
|
|
18
20
|
/** Set an attribute on the span */
|
|
19
|
-
setAttribute(key: string, value:
|
|
21
|
+
setAttribute(key: string, value: SpanAttributeValue): void;
|
|
20
22
|
/** Add an event to the span */
|
|
21
23
|
addEvent?(name: string, attributes?: Record<string, unknown>): void;
|
|
22
24
|
/** Set the span status */
|
|
@@ -47,7 +49,7 @@ export interface ITracer {
|
|
|
47
49
|
*/
|
|
48
50
|
export interface SpanOptions {
|
|
49
51
|
/** Span attributes */
|
|
50
|
-
attributes?:
|
|
52
|
+
attributes?: SpanAttributes;
|
|
51
53
|
/** Span kind (server, client, internal, etc.) */
|
|
52
54
|
kind?: SpanKind;
|
|
53
55
|
/** Parent span context */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apm.types.d.ts","sourceRoot":"","sources":["../../src/types/apm.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAClF,OAAO,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG9E,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,CAAC;AAE9F;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IAEb,mBAAmB;IACnB,GAAG,
|
|
1
|
+
{"version":3,"file":"apm.types.d.ts","sourceRoot":"","sources":["../../src/types/apm.types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAClF,OAAO,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAG9E,OAAO,EAAE,WAAW,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,CAAC;AAE9F;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAA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f;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,oCAAoC;IACpC,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAEpD,kCAAkC;IAClC,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,CAAC;IAEzB,oCAAoC;IACpC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5B,6BAA6B;IAC7B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,mBAAmB;IACnB,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,IAAI,CAAC;IAExE,4BAA4B;IAC5B,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1B,6BAA6B;IAC7B,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ISpan, SpanAttributes, SpanAttributeValue } from "../types/apm.types";
|
|
2
2
|
import { SpanKind } from "../types/apm-provider.type";
|
|
3
3
|
/**
|
|
4
4
|
* TracingHelper - Static utility class for unified APM usage
|
|
@@ -19,6 +19,7 @@ import { SpanKind } from "../types/apm-provider.type";
|
|
|
19
19
|
export declare class TracingHelper {
|
|
20
20
|
private static tracer;
|
|
21
21
|
private static apmProvider;
|
|
22
|
+
private static isElasticLabelValue;
|
|
22
23
|
/**
|
|
23
24
|
* Get the current APM provider (cached)
|
|
24
25
|
*/
|
|
@@ -34,7 +35,7 @@ export declare class TracingHelper {
|
|
|
34
35
|
* @param spanKind Loại span (mặc định: INTERNAL). Dùng SERVER cho API endpoints
|
|
35
36
|
* @returns Span object
|
|
36
37
|
*/
|
|
37
|
-
static startSpan(name: string, attributes?:
|
|
38
|
+
static startSpan(name: string, attributes?: SpanAttributes, spanKind?: SpanKind): ISpan;
|
|
38
39
|
private static mapSpanKindToOtlpSpanKind;
|
|
39
40
|
/**
|
|
40
41
|
* Start span using OpenTelemetry API
|
|
@@ -63,7 +64,7 @@ export declare class TracingHelper {
|
|
|
63
64
|
* @param attributes Các attributes metadata
|
|
64
65
|
* @returns Kết quả của function
|
|
65
66
|
*/
|
|
66
|
-
static startSpanWithParent<T>(name: string, fn: (span:
|
|
67
|
+
static startSpanWithParent<T>(name: string, fn: (span: ISpan | undefined) => Promise<T>, attributes?: SpanAttributes): Promise<T>;
|
|
67
68
|
/**
|
|
68
69
|
* Capture error vào active span hiện tại
|
|
69
70
|
* @param error Error object
|
|
@@ -74,17 +75,17 @@ export declare class TracingHelper {
|
|
|
74
75
|
* @param key Tên attribute
|
|
75
76
|
* @param value Giá trị attribute
|
|
76
77
|
*/
|
|
77
|
-
static setAttribute(key: string, value:
|
|
78
|
+
static setAttribute(key: string, value: SpanAttributeValue): void;
|
|
78
79
|
/**
|
|
79
80
|
* Kết thúc span manually
|
|
80
81
|
* @param span Span cần end
|
|
81
82
|
*/
|
|
82
|
-
static endSpan(span?:
|
|
83
|
+
static endSpan(span?: ISpan, result?: unknown): void;
|
|
83
84
|
/**
|
|
84
85
|
* Get the current active span
|
|
85
86
|
* @returns The active span or undefined
|
|
86
87
|
*/
|
|
87
|
-
static getActiveSpan():
|
|
88
|
+
static getActiveSpan(): ISpan | undefined;
|
|
88
89
|
/**
|
|
89
90
|
* Get the current trace ID
|
|
90
91
|
* @returns The trace ID or undefined
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracing.helper.d.ts","sourceRoot":"","sources":["../../src/utils/tracing.helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tracing.helper.d.ts","sourceRoot":"","sources":["../../src/utils/tracing.helper.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,kBAAkB,E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,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI;IA2BpD;;;OAGG;IACH,MAAM,CAAC,aAAa,IAAI,KAAK,GAAG,SAAS;IAqBzC;;;OAGG;IACH,MAAM,CAAC,UAAU,IAAI,MAAM,GAAG,SAAS;CAqBxC"}
|
|
@@ -52,6 +52,9 @@ function getApmProvider() {
|
|
|
52
52
|
* }
|
|
53
53
|
*/
|
|
54
54
|
class TracingHelper {
|
|
55
|
+
static isElasticLabelValue(value) {
|
|
56
|
+
return value !== undefined && !Array.isArray(value);
|
|
57
|
+
}
|
|
55
58
|
/**
|
|
56
59
|
* Get the current APM provider (cached)
|
|
57
60
|
*/
|
|
@@ -139,7 +142,9 @@ class TracingHelper {
|
|
|
139
142
|
// Set attributes as labels
|
|
140
143
|
if (attributes) {
|
|
141
144
|
for (const [key, value] of Object.entries(attributes)) {
|
|
142
|
-
|
|
145
|
+
if (TracingHelper.isElasticLabelValue(value)) {
|
|
146
|
+
apmSpan.setLabel(key, value);
|
|
147
|
+
}
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
// Set span type
|
|
@@ -153,25 +158,32 @@ class TracingHelper {
|
|
|
153
158
|
// Add setAttribute method that maps to setLabel
|
|
154
159
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
155
160
|
span.setAttribute = (key, value) => {
|
|
156
|
-
|
|
161
|
+
if (TracingHelper.isElasticLabelValue(value)) {
|
|
162
|
+
originalSetLabel(key, value);
|
|
163
|
+
}
|
|
157
164
|
return span; // Return span for chaining
|
|
158
165
|
};
|
|
159
166
|
// Add setAttributes method for batch setting
|
|
160
167
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
161
168
|
span.setAttributes = (attrs) => {
|
|
162
169
|
for (const [k, v] of Object.entries(attrs)) {
|
|
163
|
-
|
|
170
|
+
if (TracingHelper.isElasticLabelValue(v)) {
|
|
171
|
+
originalSetLabel(k, v);
|
|
172
|
+
}
|
|
164
173
|
}
|
|
165
174
|
return span; // Return span for chaining
|
|
166
175
|
};
|
|
167
176
|
}
|
|
168
|
-
// Override end() to also end transaction if we created it
|
|
177
|
+
// Override end() to also end transaction if we created it.
|
|
178
|
+
// Separation: span.end() accepts an optional timestamp (number) for when the span ended,
|
|
179
|
+
// while transaction.end() accepts a semantic result string (e.g. "HTTP 2xx", "success").
|
|
180
|
+
// We call originalEnd() without forwarding result, and pass result only to transaction.end().
|
|
169
181
|
if (createdTransaction) {
|
|
170
182
|
const originalEnd = apmSpan.end.bind(apmSpan);
|
|
171
|
-
apmSpan.end = () => {
|
|
183
|
+
apmSpan.end = (result) => {
|
|
172
184
|
originalEnd();
|
|
173
185
|
if (transaction) {
|
|
174
|
-
transaction.end();
|
|
186
|
+
transaction.end(result);
|
|
175
187
|
}
|
|
176
188
|
};
|
|
177
189
|
}
|
|
@@ -232,18 +244,23 @@ class TracingHelper {
|
|
|
232
244
|
* Create a no-op span that does nothing
|
|
233
245
|
*/
|
|
234
246
|
static createNoOpSpan(name) {
|
|
235
|
-
|
|
247
|
+
const noOpSpan = {
|
|
236
248
|
name,
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
249
|
+
setAttribute: function () {
|
|
250
|
+
return this;
|
|
251
|
+
},
|
|
252
|
+
addEvent: function () {
|
|
253
|
+
return this;
|
|
254
|
+
},
|
|
255
|
+
recordException: function () {
|
|
256
|
+
return this;
|
|
257
|
+
},
|
|
258
|
+
setStatus: function () {
|
|
259
|
+
return this;
|
|
260
|
+
},
|
|
261
|
+
end: () => { },
|
|
246
262
|
};
|
|
263
|
+
return noOpSpan;
|
|
247
264
|
}
|
|
248
265
|
/**
|
|
249
266
|
* Thực thi function với context tracing (auto-close span)
|
|
@@ -324,8 +341,8 @@ class TracingHelper {
|
|
|
324
341
|
// @ts-ignore - Optional dependency
|
|
325
342
|
const apm = require("elastic-apm-node");
|
|
326
343
|
const span = apm.currentSpan;
|
|
327
|
-
if (span) {
|
|
328
|
-
span.setLabel(key,
|
|
344
|
+
if (span && TracingHelper.isElasticLabelValue(value)) {
|
|
345
|
+
span.setLabel(key, value);
|
|
329
346
|
}
|
|
330
347
|
}
|
|
331
348
|
catch {
|
|
@@ -335,7 +352,7 @@ class TracingHelper {
|
|
|
335
352
|
}
|
|
336
353
|
// OpenTelemetry
|
|
337
354
|
const span = api_1.trace.getActiveSpan();
|
|
338
|
-
if (span) {
|
|
355
|
+
if (span && value !== undefined) {
|
|
339
356
|
span.setAttribute(key, value);
|
|
340
357
|
}
|
|
341
358
|
}
|
|
@@ -343,7 +360,7 @@ class TracingHelper {
|
|
|
343
360
|
* Kết thúc span manually
|
|
344
361
|
* @param span Span cần end
|
|
345
362
|
*/
|
|
346
|
-
static endSpan(span) {
|
|
363
|
+
static endSpan(span, result) {
|
|
347
364
|
if (!span) {
|
|
348
365
|
const provider = this.getProvider();
|
|
349
366
|
if (provider === "elastic-apm") {
|
|
@@ -352,7 +369,7 @@ class TracingHelper {
|
|
|
352
369
|
const apm = require("elastic-apm-node");
|
|
353
370
|
const apmSpan = apm.currentSpan;
|
|
354
371
|
if (apmSpan) {
|
|
355
|
-
apmSpan.end();
|
|
372
|
+
apmSpan.end(result);
|
|
356
373
|
}
|
|
357
374
|
}
|
|
358
375
|
catch {
|
|
@@ -364,7 +381,7 @@ class TracingHelper {
|
|
|
364
381
|
span = api_1.trace.getActiveSpan();
|
|
365
382
|
}
|
|
366
383
|
if (span) {
|
|
367
|
-
span.end();
|
|
384
|
+
span.end(result);
|
|
368
385
|
}
|
|
369
386
|
}
|
|
370
387
|
/**
|
package/package.json
CHANGED