@pristine-ts/telemetry 2.0.2 → 2.0.4
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/lib/cjs/enums/span-keyname.enum.js +2 -30
- package/dist/lib/cjs/enums/span-keyname.enum.js.map +1 -1
- package/dist/lib/cjs/managers/tracing.manager.js +182 -37
- package/dist/lib/cjs/managers/tracing.manager.js.map +1 -1
- package/dist/lib/cjs/models/models.js +1 -0
- package/dist/lib/cjs/models/models.js.map +1 -1
- package/dist/lib/cjs/models/span-event.model.js +6 -0
- package/dist/lib/cjs/models/span-event.model.js.map +1 -0
- package/dist/lib/cjs/models/span.model.js +2 -68
- package/dist/lib/cjs/models/span.model.js.map +1 -1
- package/dist/lib/cjs/models/trace.model.js +2 -35
- package/dist/lib/cjs/models/trace.model.js.map +1 -1
- package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/lib/cjs/utils/span-runner.js +3 -77
- package/dist/lib/cjs/utils/span-runner.js.map +1 -1
- package/dist/lib/esm/enums/span-keyname.enum.js +1 -30
- package/dist/lib/esm/enums/span-keyname.enum.js.map +1 -1
- package/dist/lib/esm/managers/tracing.manager.js +184 -39
- package/dist/lib/esm/managers/tracing.manager.js.map +1 -1
- package/dist/lib/esm/models/models.js +1 -0
- package/dist/lib/esm/models/models.js.map +1 -1
- package/dist/lib/esm/models/span-event.model.js +2 -0
- package/dist/lib/esm/models/span-event.model.js.map +1 -0
- package/dist/lib/esm/models/span.model.js +1 -67
- package/dist/lib/esm/models/span.model.js.map +1 -1
- package/dist/lib/esm/models/trace.model.js +1 -34
- package/dist/lib/esm/models/trace.model.js.map +1 -1
- package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/lib/esm/utils/span-runner.js +1 -76
- package/dist/lib/esm/utils/span-runner.js.map +1 -1
- package/dist/types/enums/span-keyname.enum.d.ts +1 -29
- package/dist/types/interfaces/tracing-manager.interface.d.ts +1 -50
- package/dist/types/managers/tracing.manager.d.ts +73 -7
- package/dist/types/models/models.d.ts +1 -0
- package/dist/types/models/span-event.model.d.ts +1 -0
- package/dist/types/models/span.model.d.ts +1 -73
- package/dist/types/models/trace.model.d.ts +1 -44
- package/dist/types/utils/span-runner.d.ts +1 -59
- package/package.json +4 -4
|
@@ -1,50 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Trace } from "../models/trace.model";
|
|
3
|
-
/**
|
|
4
|
-
* This interface specifies what a tracing manager should implement.
|
|
5
|
-
*/
|
|
6
|
-
export interface TracingManagerInterface {
|
|
7
|
-
/**
|
|
8
|
-
* This property contains a reference to the active trace.
|
|
9
|
-
*/
|
|
10
|
-
trace?: Trace;
|
|
11
|
-
/**
|
|
12
|
-
* This methods starts the Tracing. This should be the first method called before doing anything else.
|
|
13
|
-
* @param spanRootKeyname The keyname of the span at the root.
|
|
14
|
-
* @param traceId The trace id if there is one.
|
|
15
|
-
* @param context The context if there is one.
|
|
16
|
-
*/
|
|
17
|
-
startTracing(spanRootKeyname?: string, traceId?: string, context?: {
|
|
18
|
-
[key: string]: string;
|
|
19
|
-
}): Span;
|
|
20
|
-
/**
|
|
21
|
-
* This method ends the trace entirely.
|
|
22
|
-
*/
|
|
23
|
-
endTrace(): void;
|
|
24
|
-
/**
|
|
25
|
-
* This method starts a new span.
|
|
26
|
-
* @param keyname The keyname for this new span.
|
|
27
|
-
* @param parentKeyname The keyname of the parent span.
|
|
28
|
-
* @param parentId The id of the parent span.
|
|
29
|
-
* @param context The context if there is one.
|
|
30
|
-
*/
|
|
31
|
-
startSpan(keyname: string, parentKeyname?: string, parentId?: string, context?: {
|
|
32
|
-
[key: string]: string;
|
|
33
|
-
}): Span;
|
|
34
|
-
/**
|
|
35
|
-
* This methods adds an already created Span to the trace. It assumes that it its hierarchy is correct.
|
|
36
|
-
* @param span The span to add.
|
|
37
|
-
*/
|
|
38
|
-
addSpan(span: Span): Span;
|
|
39
|
-
/**
|
|
40
|
-
* This methods ends the span by setting the end date and by calling the tracers.
|
|
41
|
-
* It will also end the trace if the rootspan is being ended.
|
|
42
|
-
* @param span The span to end.
|
|
43
|
-
*/
|
|
44
|
-
endSpan(span: Span): void;
|
|
45
|
-
/**
|
|
46
|
-
* This method ends the span using a keyname.
|
|
47
|
-
* @param keyname The keyname of the span to end.
|
|
48
|
-
*/
|
|
49
|
-
endSpanKeyname(keyname: string): void;
|
|
50
|
-
}
|
|
1
|
+
export { TracingManagerInterface } from "@pristine-ts/common";
|
|
@@ -8,6 +8,24 @@ import { LogHandlerInterface } from "@pristine-ts/logging";
|
|
|
8
8
|
* The Tracing Manager provides methods to help with tracing.
|
|
9
9
|
* It is tagged and can be injected using TracingManagerInterface which facilitates mocking.
|
|
10
10
|
* It is module scoped to the TelemetryModuleKeyname.
|
|
11
|
+
*
|
|
12
|
+
* **Where the active `Trace` lives.** The active trace is stored on `EventContext.trace`,
|
|
13
|
+
* propagated via `AsyncLocalStorage`. Every `TracingManager` instance — whether resolved
|
|
14
|
+
* from the root container (kernel boot) or from a per-event child container — reads and
|
|
15
|
+
* writes spans through the same `EventContext.trace` reference, so a span started in the
|
|
16
|
+
* kernel and an event added in a controller belong to the same trace tree.
|
|
17
|
+
*
|
|
18
|
+
* **Fallback `this.trace`.** During kernel boot (`Kernel.start()` and friends), there is
|
|
19
|
+
* no `EventContext` yet — the framework hasn't started handling an event. In that window
|
|
20
|
+
* the manager falls back to `this.trace`. This is the only time `this.trace` is touched.
|
|
21
|
+
*
|
|
22
|
+
* **Lifecycle: container-scoped, not singleton.** Each per-event DI child container gets
|
|
23
|
+
* its own `TracingManager` instance. The per-instance `this.trace` fallback is what keeps
|
|
24
|
+
* parallel events isolated *if* tracing is somehow started outside an EventContext.
|
|
25
|
+
* Resolving `TracingManager` from the root container returns the root instance (used for
|
|
26
|
+
* kernel-initialization spans); resolving from a child container returns the per-event
|
|
27
|
+
* instance — both will agree on what trace is active inside an event because they read
|
|
28
|
+
* from EventContext.
|
|
11
29
|
*/
|
|
12
30
|
export declare class TracingManager implements TracingManagerInterface {
|
|
13
31
|
private readonly tracers;
|
|
@@ -16,15 +34,10 @@ export declare class TracingManager implements TracingManagerInterface {
|
|
|
16
34
|
private readonly debug;
|
|
17
35
|
private readonly tracingContext;
|
|
18
36
|
/**
|
|
19
|
-
*
|
|
37
|
+
* Fallback trace used only during kernel boot when no `EventContext` is active.
|
|
38
|
+
* Inside an event, the active trace lives on `EventContext.trace` instead.
|
|
20
39
|
*/
|
|
21
40
|
trace?: Trace;
|
|
22
|
-
/**
|
|
23
|
-
* This object contains a map of all the spans sorted by their keyname.
|
|
24
|
-
*/
|
|
25
|
-
spans: {
|
|
26
|
-
[keyname: string]: Span[];
|
|
27
|
-
};
|
|
28
41
|
/**
|
|
29
42
|
* The Tracing Manager provides methods to help with tracing.
|
|
30
43
|
* It is tagged and can be injected using TracingManagerInterface which facilitates mocking.
|
|
@@ -37,6 +50,16 @@ export declare class TracingManager implements TracingManagerInterface {
|
|
|
37
50
|
* @param tracingContext The tracing context.
|
|
38
51
|
*/
|
|
39
52
|
constructor(tracers: TracerInterface[], loghandler: LogHandlerInterface, isActive: boolean, debug: boolean, tracingContext: TracingContext);
|
|
53
|
+
/**
|
|
54
|
+
* Returns the currently active trace from the EventContext, falling back to the
|
|
55
|
+
* per-instance `this.trace` when no EventContext is active (kernel boot).
|
|
56
|
+
*/
|
|
57
|
+
private getActiveTrace;
|
|
58
|
+
/**
|
|
59
|
+
* Stores `trace` as the active trace — on `EventContext.trace` when an event is active,
|
|
60
|
+
* otherwise on the instance fallback `this.trace`.
|
|
61
|
+
*/
|
|
62
|
+
private setActiveTrace;
|
|
40
63
|
/**
|
|
41
64
|
* This methods starts the Tracing. This should be the first method called before doing anything else.
|
|
42
65
|
* @param spanRootKeyname The keyname of the span at the root.
|
|
@@ -76,4 +99,47 @@ export declare class TracingManager implements TracingManagerInterface {
|
|
|
76
99
|
* This method ends the trace entirely.
|
|
77
100
|
*/
|
|
78
101
|
endTrace(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Attaches a named, timestamped event to the most-recently-started in-progress span.
|
|
104
|
+
* Use for noteworthy moments that don't warrant a child span — "validation passed",
|
|
105
|
+
* "found 50 rows", "rate limit ok". Cheap (just pushes onto an array); shows up in
|
|
106
|
+
* the trace rendered by tracers (ConsoleTracer, FileTracer, X-Ray, etc.).
|
|
107
|
+
*
|
|
108
|
+
* If no span is currently active (no trace started, or every span has already ended),
|
|
109
|
+
* a warning is logged and the marker is dropped. The warning is the explicit signal
|
|
110
|
+
* that "this marker call had nowhere to go" — usually the sign of a missing
|
|
111
|
+
* `startTracing()` call earlier in the flow.
|
|
112
|
+
*/
|
|
113
|
+
addEventToCurrentSpan(message: string, attributes?: {
|
|
114
|
+
[key: string]: string;
|
|
115
|
+
}): void;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the active trace's spans + their events as a flat, timestamp-sorted list of
|
|
118
|
+
* `{kind, name, date, attributes}` entries. Public utility for custom tracers, debug
|
|
119
|
+
* endpoints, test helpers, or anyone who wants "the active trace as a flat list."
|
|
120
|
+
* Returns an empty array when no active trace exists.
|
|
121
|
+
*/
|
|
122
|
+
getCurrentTrail(): Array<{
|
|
123
|
+
kind: "span" | "event";
|
|
124
|
+
name: string;
|
|
125
|
+
date: Date;
|
|
126
|
+
attributes: {
|
|
127
|
+
[key: string]: string;
|
|
128
|
+
};
|
|
129
|
+
}>;
|
|
130
|
+
/**
|
|
131
|
+
* Finds the deepest in-progress span — the leaf of the still-open subtree. Used as
|
|
132
|
+
* the target for `addEventToCurrentSpan`. Intuition: events attach to whatever is
|
|
133
|
+
* currently open at the bottom of the call stack.
|
|
134
|
+
*
|
|
135
|
+
* Walking the in-progress subtree (rather than ranking all spans by start date)
|
|
136
|
+
* avoids the edge case where a parent and child have the same `Date.now()` value —
|
|
137
|
+
* picking the "latest started" by raw timestamp would incorrectly attach to the
|
|
138
|
+
* parent. Depth-first traversal of in-progress children naturally lands on the
|
|
139
|
+
* deepest leaf. Tie-broken by latest start date when multiple leaves exist.
|
|
140
|
+
*
|
|
141
|
+
* Returns undefined when nothing is in progress (no trace, or every span has ended).
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
private findActiveLeafSpan;
|
|
79
145
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SpanEvent } from "@pristine-ts/common";
|
|
@@ -1,73 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { TracingManagerInterface } from "../interfaces/tracing-manager.interface";
|
|
3
|
-
/**
|
|
4
|
-
* This model represents a span.
|
|
5
|
-
*/
|
|
6
|
-
export declare class Span {
|
|
7
|
-
keyname: string;
|
|
8
|
-
/**
|
|
9
|
-
* The unique id of the span.
|
|
10
|
-
*/
|
|
11
|
-
id: string;
|
|
12
|
-
/**
|
|
13
|
-
* The tracing manager.
|
|
14
|
-
*/
|
|
15
|
-
tracingManager?: TracingManagerInterface;
|
|
16
|
-
/**
|
|
17
|
-
* The trace the span is attached to.
|
|
18
|
-
*/
|
|
19
|
-
trace?: Trace;
|
|
20
|
-
/**
|
|
21
|
-
* The timestamp in milliseconds at which the span was started.
|
|
22
|
-
*/
|
|
23
|
-
startDate: number;
|
|
24
|
-
/**
|
|
25
|
-
* The timestamp in milliseconds at which the span was ended.
|
|
26
|
-
*/
|
|
27
|
-
endDate?: number;
|
|
28
|
-
/**
|
|
29
|
-
* The parent span.
|
|
30
|
-
*/
|
|
31
|
-
parentSpan?: Span;
|
|
32
|
-
/**
|
|
33
|
-
* The children spans.
|
|
34
|
-
*/
|
|
35
|
-
children: Span[];
|
|
36
|
-
/**
|
|
37
|
-
* The context associated with the span.
|
|
38
|
-
*/
|
|
39
|
-
context: {
|
|
40
|
-
[key: string]: string;
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Whether or not the span is in progress, meaning it has not ended.
|
|
44
|
-
*/
|
|
45
|
-
inProgress: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* This model represents a span.
|
|
48
|
-
* @param keyname The keyname of the span.
|
|
49
|
-
* @param id The unique id of the span.
|
|
50
|
-
* @param context The context to associate with the span.
|
|
51
|
-
*/
|
|
52
|
-
constructor(keyname: string, id?: string, context?: {
|
|
53
|
-
[key: string]: string;
|
|
54
|
-
});
|
|
55
|
-
/**
|
|
56
|
-
* This method returns the duration of the span in milliseconds.
|
|
57
|
-
*/
|
|
58
|
-
getDuration(): number;
|
|
59
|
-
/**
|
|
60
|
-
* This method ends the span.
|
|
61
|
-
*/
|
|
62
|
-
end(): void;
|
|
63
|
-
/**
|
|
64
|
-
* This method sets the trace for the span and all of its children.
|
|
65
|
-
* @param trace The trace the span should be attached to.
|
|
66
|
-
*/
|
|
67
|
-
setTrace(trace: Trace): void;
|
|
68
|
-
/**
|
|
69
|
-
* This method adds a child span to the current span. It only adds it if it's not already part of the children.
|
|
70
|
-
* @param span The span to add as a child.
|
|
71
|
-
*/
|
|
72
|
-
addChild(span: Span): void;
|
|
73
|
-
}
|
|
1
|
+
export { Span, SpanLifecycleOwnerInterface } from "@pristine-ts/common";
|
|
@@ -1,44 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* This model represents a trace.
|
|
4
|
-
*/
|
|
5
|
-
export declare class Trace {
|
|
6
|
-
/**
|
|
7
|
-
* The unique id of the trace.
|
|
8
|
-
*/
|
|
9
|
-
id: string;
|
|
10
|
-
/**
|
|
11
|
-
* The timestamp in milliseconds at which the trace was started.
|
|
12
|
-
*/
|
|
13
|
-
startDate: number;
|
|
14
|
-
/**
|
|
15
|
-
* The timestamp in milliseconds at which the trace was ended.
|
|
16
|
-
*/
|
|
17
|
-
endDate?: number;
|
|
18
|
-
/**
|
|
19
|
-
* The span that is at the root.
|
|
20
|
-
*/
|
|
21
|
-
rootSpan?: Span;
|
|
22
|
-
/**
|
|
23
|
-
* The context associated with the trace.
|
|
24
|
-
*/
|
|
25
|
-
context?: {
|
|
26
|
-
[key: string]: string;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Whether or not the trace was ended.
|
|
30
|
-
*/
|
|
31
|
-
hasEnded: boolean;
|
|
32
|
-
/**
|
|
33
|
-
* This model represents a trace.
|
|
34
|
-
* @param id The unique id of the trace.
|
|
35
|
-
* @param context The context associated with the trace.
|
|
36
|
-
*/
|
|
37
|
-
constructor(id?: string, context?: {
|
|
38
|
-
[key: string]: string;
|
|
39
|
-
});
|
|
40
|
-
/**
|
|
41
|
-
* This returns the duration of the trace in miliseconds.
|
|
42
|
-
*/
|
|
43
|
-
getDuration(): number;
|
|
44
|
-
}
|
|
1
|
+
export { Trace } from "@pristine-ts/common";
|
|
@@ -1,59 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Options to scope a `runWithSpan` call beyond just naming the span.
|
|
4
|
-
*/
|
|
5
|
-
export interface SpanRunnerOptions {
|
|
6
|
-
/** When set, attach the new span as a child of the most recent span with this keyname. */
|
|
7
|
-
parentKeyname?: string;
|
|
8
|
-
/** Disambiguates parents when multiple spans share the same `parentKeyname`. */
|
|
9
|
-
parentId?: string;
|
|
10
|
-
/** Free-form context recorded on the span and surfaced in the rendered output. */
|
|
11
|
-
context?: {
|
|
12
|
-
[key: string]: string;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Runs a function inside a span that is automatically ended when the function returns
|
|
17
|
-
* or throws.
|
|
18
|
-
*
|
|
19
|
-
* Replaces the manual `start → try → finally end()` boilerplate:
|
|
20
|
-
*
|
|
21
|
-
* ```ts
|
|
22
|
-
* // Before:
|
|
23
|
-
* const span = this.tracingManager.startSpan("payment.charge");
|
|
24
|
-
* try {
|
|
25
|
-
* return await this.client.charge(amount);
|
|
26
|
-
* } finally {
|
|
27
|
-
* span.end();
|
|
28
|
-
* }
|
|
29
|
-
*
|
|
30
|
-
* // After:
|
|
31
|
-
* return spanRunner.runWithSpan(this.tracingManager, "payment.charge",
|
|
32
|
-
* () => this.client.charge(amount));
|
|
33
|
-
* ```
|
|
34
|
-
*
|
|
35
|
-
* Why pass `tracingManager` explicitly? The manager is registered per-container, and
|
|
36
|
-
* Pristine creates a child container per event. A "magic" lookup that finds the right
|
|
37
|
-
* one requires AsyncLocalStorage propagation — deferred until that infrastructure
|
|
38
|
-
* exists. For now, services inject `@inject("TracingManagerInterface") private readonly
|
|
39
|
-
* tracingManager: TracingManagerInterface` and pass it through.
|
|
40
|
-
*
|
|
41
|
-
* On thrown errors: the error's name/message is attached to the span's context (visible
|
|
42
|
-
* in the rendered tree/json output) and then re-thrown. The span is ended either way.
|
|
43
|
-
*
|
|
44
|
-
* Stateless — instantiate once and reuse, or use the exported singleton `spanRunner`.
|
|
45
|
-
*/
|
|
46
|
-
export declare class SpanRunner {
|
|
47
|
-
/**
|
|
48
|
-
* Runs `fn` inside a span. See class docs for full semantics.
|
|
49
|
-
*
|
|
50
|
-
* @typeParam T The return type of `fn`. Always wrapped in `Promise<T>` because the
|
|
51
|
-
* helper awaits internally — a sync `fn` works fine, but the return type loses its
|
|
52
|
-
* sync-ness.
|
|
53
|
-
*/
|
|
54
|
-
runWithSpan<T>(tracingManager: TracingManagerInterface, spanKeyname: string, fn: () => Promise<T> | T, options?: SpanRunnerOptions): Promise<T>;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Default singleton. Stateless so sharing is safe.
|
|
58
|
-
*/
|
|
59
|
-
export declare const spanRunner: SpanRunner;
|
|
1
|
+
export { SpanRunner, SpanRunnerOptions, spanRunner } from "@pristine-ts/common";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pristine-ts/telemetry",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/lib/esm/telemetry.module.js",
|
|
6
6
|
"main": "dist/lib/cjs/telemetry.module.js",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"test:cov": "jest --coverage"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@pristine-ts/common": "^2.0.
|
|
16
|
-
"@pristine-ts/logging": "^2.0.
|
|
15
|
+
"@pristine-ts/common": "^2.0.4",
|
|
16
|
+
"@pristine-ts/logging": "^2.0.4",
|
|
17
17
|
"uuid": "^9.0.1"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"src/*.{js,ts}"
|
|
62
62
|
]
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "f376d34e13c1b17b7764c0b47708148e4e51390b",
|
|
65
65
|
"repository": {
|
|
66
66
|
"type": "git",
|
|
67
67
|
"url": "https://github.com/magieno/pristine-ts.git",
|