@nestjs/observe 0.0.1
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/LICENSE +23 -0
- package/README.md +135 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/agent/detached-observe-worker.d.ts +1 -0
- package/dist/agent/detached-observe-worker.js +104 -0
- package/dist/agent/observe-agent.shared-buffer.d.ts +42 -0
- package/dist/agent/observe-agent.shared-buffer.js +211 -0
- package/dist/agent/observe-agent.worker.d.ts +25 -0
- package/dist/agent/observe-agent.worker.js +213 -0
- package/dist/custom-metrics/counter.d.ts +39 -0
- package/dist/custom-metrics/counter.js +128 -0
- package/dist/custom-metrics/gauge.d.ts +40 -0
- package/dist/custom-metrics/gauge.js +134 -0
- package/dist/custom-metrics/index.d.ts +3 -0
- package/dist/custom-metrics/index.js +3 -0
- package/dist/custom-metrics/summary.d.ts +38 -0
- package/dist/custom-metrics/summary.js +106 -0
- package/dist/encoders/custom-metrics.encoder.d.ts +25 -0
- package/dist/encoders/custom-metrics.encoder.js +50 -0
- package/dist/encoders/job-snapshot.encoder.d.ts +28 -0
- package/dist/encoders/job-snapshot.encoder.js +28 -0
- package/dist/encoders/remap-keys.util.d.ts +1 -0
- package/dist/encoders/remap-keys.util.js +10 -0
- package/dist/encoders/request-snapshot.encoder.d.ts +33 -0
- package/dist/encoders/request-snapshot.encoder.js +32 -0
- package/dist/encoders/runtime-metrics.encoder.d.ts +63 -0
- package/dist/encoders/runtime-metrics.encoder.js +55 -0
- package/dist/encoders/trace.encoder.d.ts +19 -0
- package/dist/encoders/trace.encoder.js +20 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/instrument/create-instance-decorator.instrument.d.ts +7 -0
- package/dist/instrument/create-instance-decorator.instrument.js +165 -0
- package/dist/interfaces/custom-metric.interface.d.ts +18 -0
- package/dist/interfaces/custom-metric.interface.js +1 -0
- package/dist/interfaces/graphql.interfaces.d.ts +12 -0
- package/dist/interfaces/graphql.interfaces.js +1 -0
- package/dist/interfaces/index.d.ts +7 -0
- package/dist/interfaces/index.js +7 -0
- package/dist/interfaces/job-snapshot.interface.d.ts +23 -0
- package/dist/interfaces/job-snapshot.interface.js +1 -0
- package/dist/interfaces/node-runtime-metrics.interface.d.ts +37 -0
- package/dist/interfaces/node-runtime-metrics.interface.js +1 -0
- package/dist/interfaces/observe-options.interface.d.ts +93 -0
- package/dist/interfaces/observe-options.interface.js +1 -0
- package/dist/interfaces/request-snapshot.interface.d.ts +23 -0
- package/dist/interfaces/request-snapshot.interface.js +1 -0
- package/dist/interfaces/trace-events.interfaces.d.ts +17 -0
- package/dist/interfaces/trace-events.interfaces.js +1 -0
- package/dist/interfaces/trace-span.interface.d.ts +12 -0
- package/dist/interfaces/trace-span.interface.js +1 -0
- package/dist/observe.constants.d.ts +2 -0
- package/dist/observe.constants.js +2 -0
- package/dist/observe.module.d.ts +20 -0
- package/dist/observe.module.js +181 -0
- package/dist/profiling/cpu-profiler.service.d.ts +45 -0
- package/dist/profiling/cpu-profiler.service.js +146 -0
- package/dist/profiling/fold-profile.util.d.ts +28 -0
- package/dist/profiling/fold-profile.util.js +0 -0
- package/dist/profiling/profile-uploader.d.ts +19 -0
- package/dist/profiling/profile-uploader.js +88 -0
- package/dist/profiling/span-slice-recorder.d.ts +18 -0
- package/dist/profiling/span-slice-recorder.js +78 -0
- package/dist/protocols/graphql-observe-agent.service.d.ts +58 -0
- package/dist/protocols/graphql-observe-agent.service.js +261 -0
- package/dist/protocols/graphql-operation-parser.d.ts +10 -0
- package/dist/protocols/graphql-operation-parser.js +174 -0
- package/dist/protocols/http-observe-agent.service.d.ts +31 -0
- package/dist/protocols/http-observe-agent.service.js +186 -0
- package/dist/protocols/queue-observe-agent.service.d.ts +15 -0
- package/dist/protocols/queue-observe-agent.service.js +132 -0
- package/dist/protocols/rpc-observe-agent.service.d.ts +33 -0
- package/dist/protocols/rpc-observe-agent.service.js +183 -0
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +1 -0
- package/dist/services/logger-patcher.service.d.ts +12 -0
- package/dist/services/logger-patcher.service.js +76 -0
- package/dist/services/node-runtime-metrics.service.d.ts +23 -0
- package/dist/services/node-runtime-metrics.service.js +156 -0
- package/dist/services/operation-trace.registry.d.ts +49 -0
- package/dist/services/operation-trace.registry.js +364 -0
- package/dist/services/stdout-forwarder.service.d.ts +21 -0
- package/dist/services/stdout-forwarder.service.js +128 -0
- package/dist/services/trace-sampler.service.d.ts +19 -0
- package/dist/services/trace-sampler.service.js +38 -0
- package/dist/services/tracer.service.d.ts +48 -0
- package/dist/services/tracer.service.js +186 -0
- package/dist/trace-span.delegate.d.ts +10 -0
- package/dist/trace-span.delegate.js +24 -0
- package/dist/types/key-of.type.d.ts +1 -0
- package/dist/types/key-of.type.js +1 -0
- package/dist/types/path-value.type.d.ts +6 -0
- package/dist/types/path-value.type.js +1 -0
- package/dist/utils/assert-module-options.util.d.ts +2 -0
- package/dist/utils/assert-module-options.util.js +8 -0
- package/dist/utils/default-trace-id-generator.util.d.ts +1 -0
- package/dist/utils/default-trace-id-generator.util.js +11 -0
- package/dist/utils/log-line.parser.d.ts +9 -0
- package/dist/utils/log-line.parser.js +59 -0
- package/dist/utils/log-redactor.d.ts +11 -0
- package/dist/utils/log-redactor.js +150 -0
- package/dist/utils/redact-url-query.d.ts +2 -0
- package/dist/utils/redact-url-query.js +51 -0
- package/dist/utils/source-context.util.d.ts +11 -0
- package/dist/utils/source-context.util.js +130 -0
- package/dist/utils/source-map-resolver.util.d.ts +7 -0
- package/dist/utils/source-map-resolver.util.js +107 -0
- package/package.json +129 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { NodeRuntimeMetrics } from "../interfaces/index.js";
|
|
2
|
+
declare const RUNTIME_METRICS_KEY_MAP: {
|
|
3
|
+
readonly cpu: "c";
|
|
4
|
+
readonly memory: "m";
|
|
5
|
+
readonly gc: "g";
|
|
6
|
+
readonly eventLoop: "e";
|
|
7
|
+
};
|
|
8
|
+
declare const CPU_KEY_MAP: {
|
|
9
|
+
readonly user: "u";
|
|
10
|
+
readonly system: "s";
|
|
11
|
+
readonly percentageUsed: "p";
|
|
12
|
+
};
|
|
13
|
+
declare const MEMORY_KEY_MAP: {
|
|
14
|
+
readonly rss: "r";
|
|
15
|
+
readonly heapTotal: "ht";
|
|
16
|
+
readonly heapUsed: "hu";
|
|
17
|
+
readonly external: "e";
|
|
18
|
+
readonly arrayBuffers: "ab";
|
|
19
|
+
readonly percentageUsed: "p";
|
|
20
|
+
};
|
|
21
|
+
declare const GC_KEY_MAP: {
|
|
22
|
+
readonly count: "c";
|
|
23
|
+
readonly totalDuration: "td";
|
|
24
|
+
readonly breakdown: "b";
|
|
25
|
+
};
|
|
26
|
+
declare const GC_BREAKDOWN_KEY_MAP: {
|
|
27
|
+
readonly minor: "m";
|
|
28
|
+
readonly major: "j";
|
|
29
|
+
readonly incremental: "i";
|
|
30
|
+
};
|
|
31
|
+
declare const EVENT_LOOP_KEY_MAP: {
|
|
32
|
+
readonly lag: "l";
|
|
33
|
+
readonly utilization: "u";
|
|
34
|
+
};
|
|
35
|
+
type EncodedCpu = {
|
|
36
|
+
[K in keyof NodeRuntimeMetrics["cpu"] as K extends keyof typeof CPU_KEY_MAP ? (typeof CPU_KEY_MAP)[K] : never]: NodeRuntimeMetrics["cpu"][K];
|
|
37
|
+
};
|
|
38
|
+
type EncodedMemory = {
|
|
39
|
+
[K in keyof NodeRuntimeMetrics["memory"] as K extends keyof typeof MEMORY_KEY_MAP ? (typeof MEMORY_KEY_MAP)[K] : never]: NodeRuntimeMetrics["memory"][K];
|
|
40
|
+
};
|
|
41
|
+
type EncodedGcBreakdown = {
|
|
42
|
+
[K in keyof NonNullable<NodeRuntimeMetrics["gc"]["breakdown"]> as K extends keyof typeof GC_BREAKDOWN_KEY_MAP ? (typeof GC_BREAKDOWN_KEY_MAP)[K] : never]: NonNullable<NodeRuntimeMetrics["gc"]["breakdown"]>[K];
|
|
43
|
+
};
|
|
44
|
+
type EncodedGc = Omit<{
|
|
45
|
+
[K in keyof NodeRuntimeMetrics["gc"] as K extends keyof typeof GC_KEY_MAP ? (typeof GC_KEY_MAP)[K] : never]: NodeRuntimeMetrics["gc"][K];
|
|
46
|
+
}, "b"> & {
|
|
47
|
+
b?: EncodedGcBreakdown;
|
|
48
|
+
};
|
|
49
|
+
type EncodedEventLoop = {
|
|
50
|
+
[K in keyof NodeRuntimeMetrics["eventLoop"] as K extends keyof typeof EVENT_LOOP_KEY_MAP ? (typeof EVENT_LOOP_KEY_MAP)[K] : never]: NodeRuntimeMetrics["eventLoop"][K];
|
|
51
|
+
};
|
|
52
|
+
export type EncodedNodeRuntimeMetrics = Omit<{
|
|
53
|
+
[K in keyof NodeRuntimeMetrics as K extends keyof typeof RUNTIME_METRICS_KEY_MAP ? (typeof RUNTIME_METRICS_KEY_MAP)[K] : never]: NodeRuntimeMetrics[K];
|
|
54
|
+
}, "c" | "m" | "g" | "e"> & {
|
|
55
|
+
c?: EncodedCpu;
|
|
56
|
+
m?: EncodedMemory;
|
|
57
|
+
g?: EncodedGc;
|
|
58
|
+
e?: EncodedEventLoop;
|
|
59
|
+
};
|
|
60
|
+
export declare class RuntimeMetricsEncoder {
|
|
61
|
+
static encode(metrics: NodeRuntimeMetrics): EncodedNodeRuntimeMetrics;
|
|
62
|
+
}
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { remapKeys } from "./remap-keys.util.js";
|
|
2
|
+
const RUNTIME_METRICS_KEY_MAP = {
|
|
3
|
+
cpu: "c",
|
|
4
|
+
memory: "m",
|
|
5
|
+
gc: "g",
|
|
6
|
+
eventLoop: "e",
|
|
7
|
+
};
|
|
8
|
+
const CPU_KEY_MAP = {
|
|
9
|
+
user: "u",
|
|
10
|
+
system: "s",
|
|
11
|
+
percentageUsed: "p",
|
|
12
|
+
};
|
|
13
|
+
const MEMORY_KEY_MAP = {
|
|
14
|
+
rss: "r",
|
|
15
|
+
heapTotal: "ht",
|
|
16
|
+
heapUsed: "hu",
|
|
17
|
+
external: "e",
|
|
18
|
+
arrayBuffers: "ab",
|
|
19
|
+
percentageUsed: "p",
|
|
20
|
+
};
|
|
21
|
+
const GC_KEY_MAP = {
|
|
22
|
+
count: "c",
|
|
23
|
+
totalDuration: "td",
|
|
24
|
+
breakdown: "b",
|
|
25
|
+
};
|
|
26
|
+
const GC_BREAKDOWN_KEY_MAP = {
|
|
27
|
+
minor: "m",
|
|
28
|
+
major: "j",
|
|
29
|
+
incremental: "i",
|
|
30
|
+
};
|
|
31
|
+
const EVENT_LOOP_KEY_MAP = {
|
|
32
|
+
lag: "l",
|
|
33
|
+
utilization: "u",
|
|
34
|
+
};
|
|
35
|
+
export class RuntimeMetricsEncoder {
|
|
36
|
+
static encode(metrics) {
|
|
37
|
+
const encoded = remapKeys(metrics, RUNTIME_METRICS_KEY_MAP);
|
|
38
|
+
if (metrics.cpu) {
|
|
39
|
+
encoded.c = remapKeys(metrics.cpu, CPU_KEY_MAP);
|
|
40
|
+
}
|
|
41
|
+
if (metrics.memory) {
|
|
42
|
+
encoded.m = remapKeys(metrics.memory, MEMORY_KEY_MAP);
|
|
43
|
+
}
|
|
44
|
+
if (metrics.gc) {
|
|
45
|
+
encoded.g = remapKeys(metrics.gc, GC_KEY_MAP);
|
|
46
|
+
if (metrics.gc.breakdown) {
|
|
47
|
+
encoded.g.b = remapKeys(metrics.gc.breakdown, GC_BREAKDOWN_KEY_MAP);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (metrics.eventLoop) {
|
|
51
|
+
encoded.e = remapKeys(metrics.eventLoop, EVENT_LOOP_KEY_MAP);
|
|
52
|
+
}
|
|
53
|
+
return encoded;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CompleteTraceEventNode } from "../interfaces/trace-events.interfaces.js";
|
|
2
|
+
export declare const TRACE_KEY_MAP: {
|
|
3
|
+
readonly name: "n";
|
|
4
|
+
readonly origin: "o";
|
|
5
|
+
readonly tags: "t";
|
|
6
|
+
readonly duration: "d";
|
|
7
|
+
readonly error: "e";
|
|
8
|
+
readonly className: "c";
|
|
9
|
+
readonly methodKey: "m";
|
|
10
|
+
readonly children: "ch";
|
|
11
|
+
readonly spanId: "s";
|
|
12
|
+
readonly startOffset: "so";
|
|
13
|
+
};
|
|
14
|
+
export type RecursiveEncodedTrace = Omit<{
|
|
15
|
+
[K in keyof Omit<CompleteTraceEventNode, "children"> as K extends keyof typeof TRACE_KEY_MAP ? (typeof TRACE_KEY_MAP)[K] : never]: CompleteTraceEventNode[K];
|
|
16
|
+
}, "ch"> & {
|
|
17
|
+
ch?: Array<RecursiveEncodedTrace>;
|
|
18
|
+
};
|
|
19
|
+
export declare function encodeTrace(trace: CompleteTraceEventNode): RecursiveEncodedTrace;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { remapKeys } from "./remap-keys.util.js";
|
|
2
|
+
export const TRACE_KEY_MAP = {
|
|
3
|
+
name: "n",
|
|
4
|
+
origin: "o",
|
|
5
|
+
tags: "t",
|
|
6
|
+
duration: "d",
|
|
7
|
+
error: "e",
|
|
8
|
+
className: "c",
|
|
9
|
+
methodKey: "m",
|
|
10
|
+
children: "ch",
|
|
11
|
+
spanId: "s",
|
|
12
|
+
startOffset: "so",
|
|
13
|
+
};
|
|
14
|
+
export function encodeTrace(trace) {
|
|
15
|
+
const encoded = remapKeys(trace, TRACE_KEY_MAP);
|
|
16
|
+
if (trace.children && trace.children.length > 0) {
|
|
17
|
+
encoded.ch = trace.children.map((child) => encodeTrace(child));
|
|
18
|
+
}
|
|
19
|
+
return encoded;
|
|
20
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./custom-metrics/index.js";
|
|
2
|
+
export * from "./instrument/create-instance-decorator.instrument.js";
|
|
3
|
+
export * from "./interfaces/index.js";
|
|
4
|
+
export * from "./observe.constants.js";
|
|
5
|
+
export * from "./observe.module.js";
|
|
6
|
+
export * from "./services/index.js";
|
|
7
|
+
export * from "./trace-span.delegate.js";
|
|
8
|
+
export * from "./types/key-of.type.js";
|
|
9
|
+
export * from "./types/path-value.type.js";
|
|
10
|
+
export * from "./utils/default-trace-id-generator.util.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./custom-metrics/index.js";
|
|
2
|
+
export * from "./instrument/create-instance-decorator.instrument.js";
|
|
3
|
+
export * from "./interfaces/index.js";
|
|
4
|
+
export * from "./observe.constants.js";
|
|
5
|
+
export * from "./observe.module.js";
|
|
6
|
+
export * from "./services/index.js";
|
|
7
|
+
export * from "./trace-span.delegate.js";
|
|
8
|
+
export * from "./types/key-of.type.js";
|
|
9
|
+
export * from "./types/path-value.type.js";
|
|
10
|
+
export * from "./utils/default-trace-id-generator.util.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
import { OperationTraceRegistry } from "../services/operation-trace.registry.js";
|
|
3
|
+
import { KeyOf } from "../types/key-of.type.js";
|
|
4
|
+
export declare function createInstanceDecorator<T extends Record<string, unknown>>(als: AsyncLocalStorage<Map<KeyOf<T>, any>>, operationTraceRegistry: OperationTraceRegistry, options: {
|
|
5
|
+
traceIdKey: string;
|
|
6
|
+
skipInstrumentation: (instance: unknown) => boolean;
|
|
7
|
+
}): (instance: unknown) => any;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { CALLER_METADATA_KEY } from "../observe.constants.js";
|
|
2
|
+
const STANDALONE_FUNCTION_LABEL = "Function";
|
|
3
|
+
export function createInstanceDecorator(als, operationTraceRegistry, options) {
|
|
4
|
+
const createTracedWrapper = (spec) => {
|
|
5
|
+
const { className, methodName, spanId } = spec;
|
|
6
|
+
const frameName = `${className}.${methodName}`;
|
|
7
|
+
const call = spec.call;
|
|
8
|
+
const callUntraced = spec.callUntraced;
|
|
9
|
+
return named(frameName, function (...args) {
|
|
10
|
+
if (spec.isActive()) {
|
|
11
|
+
return callUntraced(this, args);
|
|
12
|
+
}
|
|
13
|
+
const store = als.getStore();
|
|
14
|
+
const requestId = store?.get(options.traceIdKey);
|
|
15
|
+
if (!requestId) {
|
|
16
|
+
return callUntraced(this, args);
|
|
17
|
+
}
|
|
18
|
+
spec.setActive(true);
|
|
19
|
+
const callerId = store?.get(CALLER_METADATA_KEY);
|
|
20
|
+
const newStepId = operationTraceRegistry.internalStartTraceStep(requestId, className, methodName, callerId);
|
|
21
|
+
const onReturnValue = (res) => {
|
|
22
|
+
operationTraceRegistry.internalEndTraceStep(requestId, spanId, className, methodName, newStepId);
|
|
23
|
+
return res;
|
|
24
|
+
};
|
|
25
|
+
const onError = (err) => {
|
|
26
|
+
relabelProxyFrame(err, className, methodName);
|
|
27
|
+
operationTraceRegistry.internalEndTraceStep(requestId, spanId, className, methodName, newStepId, err);
|
|
28
|
+
throw err;
|
|
29
|
+
};
|
|
30
|
+
return als.run(new Map([
|
|
31
|
+
...(store?.entries() ?? []),
|
|
32
|
+
[CALLER_METADATA_KEY, newStepId],
|
|
33
|
+
]), named(frameName, () => {
|
|
34
|
+
try {
|
|
35
|
+
const result = call(this, args);
|
|
36
|
+
if (result instanceof Promise) {
|
|
37
|
+
return result.then(onReturnValue).catch(onError);
|
|
38
|
+
}
|
|
39
|
+
return onReturnValue(result);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
onError(err);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
spec.setActive(false);
|
|
46
|
+
}
|
|
47
|
+
}));
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
const instrumentFunction = (fn) => {
|
|
51
|
+
if (isClass(fn) || options.skipInstrumentation(fn)) {
|
|
52
|
+
return fn;
|
|
53
|
+
}
|
|
54
|
+
const methodName = fn.name || "anonymous";
|
|
55
|
+
const className = STANDALONE_FUNCTION_LABEL;
|
|
56
|
+
const frameName = `${className}.${methodName}`;
|
|
57
|
+
let active = false;
|
|
58
|
+
const invoke = named(frameName, (thisArg, args) => Reflect.apply(fn, thisArg, args));
|
|
59
|
+
const tracedFn = createTracedWrapper({
|
|
60
|
+
className,
|
|
61
|
+
methodName,
|
|
62
|
+
spanId: `${className}#${methodName}`,
|
|
63
|
+
isActive: () => active,
|
|
64
|
+
setActive: (value) => (active = value),
|
|
65
|
+
call: invoke,
|
|
66
|
+
callUntraced: invoke,
|
|
67
|
+
});
|
|
68
|
+
return new Proxy(fn, {
|
|
69
|
+
apply: named(frameName, (_target, thisArg, args) => Reflect.apply(tracedFn, thisArg, args)),
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const instrumentObject = (instance) => {
|
|
73
|
+
if (options.skipInstrumentation(instance)) {
|
|
74
|
+
return instance;
|
|
75
|
+
}
|
|
76
|
+
const methodRefsCache = new WeakMap();
|
|
77
|
+
const currentlyTracing = new WeakMap();
|
|
78
|
+
return new Proxy(instance, {
|
|
79
|
+
get: (target, prop, receiver) => {
|
|
80
|
+
const attributeValue = Reflect.get(target, prop);
|
|
81
|
+
const isFunction = typeof attributeValue === "function" && prop !== "constructor";
|
|
82
|
+
const shouldProxy = isFunction && !isClass(attributeValue);
|
|
83
|
+
const isAxios = attributeValue?.Axios;
|
|
84
|
+
if (!shouldProxy || isAxios) {
|
|
85
|
+
return Reflect.get(target, prop);
|
|
86
|
+
}
|
|
87
|
+
const className = target.constructor.name;
|
|
88
|
+
const methodName = String(prop);
|
|
89
|
+
const frameName = `${className}.${methodName}`;
|
|
90
|
+
if (methodRefsCache.has(attributeValue)) {
|
|
91
|
+
return methodRefsCache.get(attributeValue);
|
|
92
|
+
}
|
|
93
|
+
const originalMethod = Reflect.get(target, prop, receiver);
|
|
94
|
+
const proxyFn = createTracedWrapper({
|
|
95
|
+
className,
|
|
96
|
+
methodName,
|
|
97
|
+
spanId: generateSpanId(target, methodName),
|
|
98
|
+
isActive: () => currentlyTracing.get(target)?.has(methodName) ?? false,
|
|
99
|
+
setActive: (active) => {
|
|
100
|
+
if (!currentlyTracing.has(target)) {
|
|
101
|
+
currentlyTracing.set(target, new Set());
|
|
102
|
+
}
|
|
103
|
+
const activeMethods = currentlyTracing.get(target);
|
|
104
|
+
if (active) {
|
|
105
|
+
activeMethods.add(methodName);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
activeMethods.delete(methodName);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
call: named(frameName, (_thisArg, args) => originalMethod.apply(receiver, args)),
|
|
112
|
+
callUntraced: named(frameName, (_thisArg, args) => originalMethod.apply(target, args)),
|
|
113
|
+
});
|
|
114
|
+
const boundFn = proxyFn.bind(undefined);
|
|
115
|
+
Object.defineProperty(boundFn, "name", {
|
|
116
|
+
value: originalMethod.name,
|
|
117
|
+
configurable: true,
|
|
118
|
+
});
|
|
119
|
+
const metadataKeys = Reflect.getMetadataKeys(originalMethod);
|
|
120
|
+
for (const key of metadataKeys) {
|
|
121
|
+
const metadata = Reflect.getMetadata(key, originalMethod);
|
|
122
|
+
Reflect.defineMetadata(key, metadata, boundFn);
|
|
123
|
+
}
|
|
124
|
+
methodRefsCache.set(attributeValue, boundFn);
|
|
125
|
+
return boundFn;
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
return (instance) => {
|
|
130
|
+
if (typeof instance === "function") {
|
|
131
|
+
return instrumentFunction(instance);
|
|
132
|
+
}
|
|
133
|
+
if (typeof instance === "object" && instance) {
|
|
134
|
+
return instrumentObject(instance);
|
|
135
|
+
}
|
|
136
|
+
return instance;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function named(name, fn) {
|
|
140
|
+
Object.defineProperty(fn, "name", { value: name, configurable: true });
|
|
141
|
+
return fn;
|
|
142
|
+
}
|
|
143
|
+
function relabelProxyFrame(err, className, methodName) {
|
|
144
|
+
if (!(err instanceof Error) || typeof err.stack !== "string") {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const frame = new RegExp(`(\\n\\s*at (?:async )?)Proxy\\.${escapeRegExp(methodName)}\\b`);
|
|
148
|
+
if (!frame.test(err.stack)) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
err.stack = err.stack.replace(frame, `$1${className}.${methodName}`);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function escapeRegExp(value) {
|
|
158
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
159
|
+
}
|
|
160
|
+
function isClass(value) {
|
|
161
|
+
return /^\s*class\s+/.test(value.toString());
|
|
162
|
+
}
|
|
163
|
+
function generateSpanId(instance, methodKey) {
|
|
164
|
+
return `${instance.constructor.name}#${methodKey}`;
|
|
165
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { GaugeKind } from "../custom-metrics/index.js";
|
|
2
|
+
export interface CustomMetric<TLabel extends string = "default"> {
|
|
3
|
+
name: string;
|
|
4
|
+
value: Record<string, number>;
|
|
5
|
+
description?: string;
|
|
6
|
+
type: "counter" | "gauge" | "summary";
|
|
7
|
+
labels?: TLabel[];
|
|
8
|
+
kind?: GaugeKind;
|
|
9
|
+
tags?: Record<string, string>;
|
|
10
|
+
lastUpdated?: number;
|
|
11
|
+
increase?: Record<string, number>;
|
|
12
|
+
p50?: Record<string, number>;
|
|
13
|
+
p95?: Record<string, number>;
|
|
14
|
+
p99?: Record<string, number>;
|
|
15
|
+
observations?: Record<string, number>;
|
|
16
|
+
total?: Record<string, number>;
|
|
17
|
+
maximum?: Record<string, number>;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./custom-metric.interface.js";
|
|
2
|
+
export * from "./graphql.interfaces.js";
|
|
3
|
+
export * from "./job-snapshot.interface.js";
|
|
4
|
+
export * from "./node-runtime-metrics.interface.js";
|
|
5
|
+
export * from "./request-snapshot.interface.js";
|
|
6
|
+
export * from "./trace-span.interface.js";
|
|
7
|
+
export * from "./observe-options.interface.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./custom-metric.interface.js";
|
|
2
|
+
export * from "./graphql.interfaces.js";
|
|
3
|
+
export * from "./job-snapshot.interface.js";
|
|
4
|
+
export * from "./node-runtime-metrics.interface.js";
|
|
5
|
+
export * from "./request-snapshot.interface.js";
|
|
6
|
+
export * from "./trace-span.interface.js";
|
|
7
|
+
export * from "./observe-options.interface.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CompleteTraceEventNode, OngoingTraceEventNode } from "./trace-events.interfaces.js";
|
|
2
|
+
export interface JobSnapshot {
|
|
3
|
+
id: string;
|
|
4
|
+
traceId: string;
|
|
5
|
+
name: string;
|
|
6
|
+
queueName: string;
|
|
7
|
+
status?: "completed" | "failed";
|
|
8
|
+
calledAt?: string;
|
|
9
|
+
startTimestamp?: number;
|
|
10
|
+
duration?: number;
|
|
11
|
+
enqueuedAt?: string;
|
|
12
|
+
waitDuration?: number;
|
|
13
|
+
attemptsMade?: number;
|
|
14
|
+
maxAttempts?: number;
|
|
15
|
+
traces: Array<OngoingTraceEventNode | CompleteTraceEventNode>;
|
|
16
|
+
tags?: Record<string, string | number | boolean>;
|
|
17
|
+
error?: {
|
|
18
|
+
cls?: string;
|
|
19
|
+
message: string;
|
|
20
|
+
stack?: string;
|
|
21
|
+
tags?: Record<string, string | number | boolean>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface NodeRuntimeMetrics {
|
|
2
|
+
cpu: {
|
|
3
|
+
user: number;
|
|
4
|
+
system: number;
|
|
5
|
+
percentageUsed: number;
|
|
6
|
+
};
|
|
7
|
+
memory: {
|
|
8
|
+
rss: number;
|
|
9
|
+
heapTotal: number;
|
|
10
|
+
heapUsed: number;
|
|
11
|
+
external: number;
|
|
12
|
+
arrayBuffers?: number;
|
|
13
|
+
percentageUsed: number;
|
|
14
|
+
};
|
|
15
|
+
gc: {
|
|
16
|
+
count: number;
|
|
17
|
+
totalDuration: number;
|
|
18
|
+
breakdown?: {
|
|
19
|
+
minor?: {
|
|
20
|
+
count: number;
|
|
21
|
+
duration: number;
|
|
22
|
+
};
|
|
23
|
+
major?: {
|
|
24
|
+
count: number;
|
|
25
|
+
duration: number;
|
|
26
|
+
};
|
|
27
|
+
incremental?: {
|
|
28
|
+
count: number;
|
|
29
|
+
duration: number;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
eventLoop: {
|
|
34
|
+
lag: number;
|
|
35
|
+
utilization: number;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { FactoryProvider, ModuleMetadata, Provider, Type } from "@nestjs/common";
|
|
2
|
+
import { BaseRpcContext, Transport } from "@nestjs/microservices";
|
|
3
|
+
import { GraphQLResolveInfoLike } from "./graphql.interfaces.js";
|
|
4
|
+
export interface RedactionOptions {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
patterns?: RegExp[];
|
|
7
|
+
keys?: string[];
|
|
8
|
+
replacement?: string;
|
|
9
|
+
useDefaultPatterns?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface CreateObserveModuleOptions {
|
|
12
|
+
traceIdKey?: string;
|
|
13
|
+
traceIdGenerator?: (req: unknown) => string;
|
|
14
|
+
attachTraceIdToLogs?: boolean;
|
|
15
|
+
sourceContext?: boolean | {
|
|
16
|
+
linesOfContext?: number;
|
|
17
|
+
maxFrames?: number;
|
|
18
|
+
sourceMaps?: boolean;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export interface ObserveOptions {
|
|
22
|
+
appKey: string;
|
|
23
|
+
appSecret: string;
|
|
24
|
+
endpoint?: string;
|
|
25
|
+
serviceId: string;
|
|
26
|
+
serviceVersion?: string;
|
|
27
|
+
flushInterval?: number;
|
|
28
|
+
runtimeMetrics?: boolean;
|
|
29
|
+
runtimeMetricsInterval?: number;
|
|
30
|
+
tracesSampleRate?: number | ((protocal: "http" | "rpc" | "grpc" | "graphql", attributes: any) => boolean);
|
|
31
|
+
maxTracesPerBatch?: number;
|
|
32
|
+
forwardLogs?: boolean;
|
|
33
|
+
redaction?: RedactionOptions;
|
|
34
|
+
debug?: boolean;
|
|
35
|
+
http?: {
|
|
36
|
+
ignore?: Array<string | {
|
|
37
|
+
method: string;
|
|
38
|
+
path: string | RegExp;
|
|
39
|
+
} | RegExp> | ((req: any) => boolean);
|
|
40
|
+
tags?: Record<string, string>;
|
|
41
|
+
setAttributes?: (req: any) => {
|
|
42
|
+
[key: string]: string | number | boolean;
|
|
43
|
+
};
|
|
44
|
+
queryParamsObfuscateRegex?: RegExp;
|
|
45
|
+
getUserId?: (req: any) => string;
|
|
46
|
+
};
|
|
47
|
+
rpc?: {
|
|
48
|
+
ignore?: (transportId: Transport | symbol, ctx: BaseRpcContext) => boolean;
|
|
49
|
+
tags?: Record<string, string>;
|
|
50
|
+
setAttributes?: (transportId: Transport | symbol, ctx: BaseRpcContext) => {
|
|
51
|
+
[key: string]: string | number | boolean;
|
|
52
|
+
};
|
|
53
|
+
getUserId?: (transportId: Transport | symbol, ctx: BaseRpcContext) => string;
|
|
54
|
+
};
|
|
55
|
+
graphql?: {
|
|
56
|
+
ignore?: (info: GraphQLResolveInfoLike) => boolean;
|
|
57
|
+
tags?: Record<string, string>;
|
|
58
|
+
setAttributes?: (info: GraphQLResolveInfoLike, context: any) => {
|
|
59
|
+
[key: string]: string | number | boolean;
|
|
60
|
+
};
|
|
61
|
+
getUserId?: (context: any) => string;
|
|
62
|
+
};
|
|
63
|
+
grpc?: {
|
|
64
|
+
ignore?: (call: any) => boolean;
|
|
65
|
+
tags?: Record<string, string>;
|
|
66
|
+
setAttributes?: (call: any) => {
|
|
67
|
+
[key: string]: string | number | boolean;
|
|
68
|
+
};
|
|
69
|
+
getUserId?: (call: any) => string;
|
|
70
|
+
};
|
|
71
|
+
jobs?: {
|
|
72
|
+
tags?: Record<string, string>;
|
|
73
|
+
setAttributes?: (job: {
|
|
74
|
+
queueName: string;
|
|
75
|
+
name: string;
|
|
76
|
+
id: string | undefined;
|
|
77
|
+
}) => {
|
|
78
|
+
[key: string]: string | number | boolean;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export type ObserveModuleOptionsWithDefaults = Required<CreateObserveModuleOptions> & ObserveOptions;
|
|
83
|
+
export interface ObserveOptionsFactory {
|
|
84
|
+
createObserveOptions(): Promise<ObserveOptions> | ObserveOptions;
|
|
85
|
+
}
|
|
86
|
+
export interface ObserveModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
|
|
87
|
+
useExisting?: Type<ObserveOptionsFactory>;
|
|
88
|
+
useClass?: Type<ObserveOptionsFactory>;
|
|
89
|
+
useFactory?: (...args: any[]) => Promise<ObserveOptions> | ObserveOptions;
|
|
90
|
+
inject?: FactoryProvider["inject"];
|
|
91
|
+
extraProviders?: Provider[];
|
|
92
|
+
global?: boolean;
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CompleteTraceEventNode, OngoingTraceEventNode } from "./trace-events.interfaces.js";
|
|
2
|
+
export interface RequestSnapshot {
|
|
3
|
+
calledAt?: string;
|
|
4
|
+
traceId: string;
|
|
5
|
+
startTimestamp?: number;
|
|
6
|
+
duration?: number;
|
|
7
|
+
protocol: string;
|
|
8
|
+
operationId?: string;
|
|
9
|
+
traces: Array<OngoingTraceEventNode | CompleteTraceEventNode>;
|
|
10
|
+
tags?: Record<string, string | number | boolean>;
|
|
11
|
+
error?: {
|
|
12
|
+
cls?: string;
|
|
13
|
+
message: string;
|
|
14
|
+
stack?: string;
|
|
15
|
+
tags?: Record<string, string | number | boolean>;
|
|
16
|
+
};
|
|
17
|
+
userId?: string;
|
|
18
|
+
attributes?: {
|
|
19
|
+
method?: string;
|
|
20
|
+
statusCode?: number;
|
|
21
|
+
originalUrl?: string;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TraceSpan } from "./trace-span.interface.js";
|
|
2
|
+
export interface OngoingTraceEventNode extends TraceSpan {
|
|
3
|
+
type: "start";
|
|
4
|
+
className: string;
|
|
5
|
+
methodKey: string;
|
|
6
|
+
startTime: number;
|
|
7
|
+
startOffset?: number;
|
|
8
|
+
children: Array<CompleteTraceEventNode | OngoingTraceEventNode>;
|
|
9
|
+
parent?: OngoingTraceEventNode;
|
|
10
|
+
}
|
|
11
|
+
export interface CompleteTraceEventNode extends TraceSpan {
|
|
12
|
+
className: string;
|
|
13
|
+
methodKey: string;
|
|
14
|
+
duration: number;
|
|
15
|
+
startOffset?: number;
|
|
16
|
+
children: Array<CompleteTraceEventNode>;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface TraceSpan {
|
|
2
|
+
spanId?: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
origin: "manual" | "auto";
|
|
5
|
+
tags?: Record<string, string | number | boolean>;
|
|
6
|
+
error?: boolean | {
|
|
7
|
+
cls?: string;
|
|
8
|
+
message: string;
|
|
9
|
+
stack?: string;
|
|
10
|
+
tags?: Record<string, string | number | boolean>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|