@pristine-ts/telemetry 2.0.3 → 2.0.5

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.
Files changed (59) hide show
  1. package/dist/lib/cjs/enums/span-keyname.enum.js +2 -30
  2. package/dist/lib/cjs/enums/span-keyname.enum.js.map +1 -1
  3. package/dist/lib/cjs/managers/tracing.manager.js +171 -42
  4. package/dist/lib/cjs/managers/tracing.manager.js.map +1 -1
  5. package/dist/lib/cjs/models/models.js +1 -0
  6. package/dist/lib/cjs/models/models.js.map +1 -1
  7. package/dist/lib/cjs/models/span-event.model.js +6 -0
  8. package/dist/lib/cjs/models/span-event.model.js.map +1 -0
  9. package/dist/lib/cjs/models/span.model.js +2 -68
  10. package/dist/lib/cjs/models/span.model.js.map +1 -1
  11. package/dist/lib/cjs/models/trace.model.js +2 -35
  12. package/dist/lib/cjs/models/trace.model.js.map +1 -1
  13. package/dist/lib/cjs/telemetry.module.js +0 -1
  14. package/dist/lib/cjs/telemetry.module.js.map +1 -1
  15. package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  16. package/dist/lib/cjs/utils/utils.js +0 -1
  17. package/dist/lib/cjs/utils/utils.js.map +1 -1
  18. package/dist/lib/esm/enums/span-keyname.enum.js +1 -30
  19. package/dist/lib/esm/enums/span-keyname.enum.js.map +1 -1
  20. package/dist/lib/esm/managers/tracing.manager.js +171 -42
  21. package/dist/lib/esm/managers/tracing.manager.js.map +1 -1
  22. package/dist/lib/esm/models/models.js +1 -0
  23. package/dist/lib/esm/models/models.js.map +1 -1
  24. package/dist/lib/esm/models/span-event.model.js +2 -0
  25. package/dist/lib/esm/models/span-event.model.js.map +1 -0
  26. package/dist/lib/esm/models/span.model.js +1 -67
  27. package/dist/lib/esm/models/span.model.js.map +1 -1
  28. package/dist/lib/esm/models/trace.model.js +1 -34
  29. package/dist/lib/esm/models/trace.model.js.map +1 -1
  30. package/dist/lib/esm/telemetry.module.js +0 -1
  31. package/dist/lib/esm/telemetry.module.js.map +1 -1
  32. package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
  33. package/dist/lib/esm/utils/utils.js +0 -1
  34. package/dist/lib/esm/utils/utils.js.map +1 -1
  35. package/dist/types/enums/span-keyname.enum.d.ts +1 -29
  36. package/dist/types/interfaces/tracing-manager.interface.d.ts +1 -50
  37. package/dist/types/managers/tracing.manager.d.ts +71 -13
  38. package/dist/types/models/models.d.ts +1 -0
  39. package/dist/types/models/span-event.model.d.ts +1 -0
  40. package/dist/types/models/span.model.d.ts +1 -73
  41. package/dist/types/models/trace.model.d.ts +1 -44
  42. package/dist/types/telemetry.module.d.ts +0 -1
  43. package/dist/types/utils/utils.d.ts +0 -1
  44. package/package.json +4 -4
  45. package/dist/lib/cjs/decorators/decorators.js +0 -18
  46. package/dist/lib/cjs/decorators/decorators.js.map +0 -1
  47. package/dist/lib/cjs/decorators/traced.decorator.js +0 -60
  48. package/dist/lib/cjs/decorators/traced.decorator.js.map +0 -1
  49. package/dist/lib/cjs/utils/span-runner.js +0 -112
  50. package/dist/lib/cjs/utils/span-runner.js.map +0 -1
  51. package/dist/lib/esm/decorators/decorators.js +0 -2
  52. package/dist/lib/esm/decorators/decorators.js.map +0 -1
  53. package/dist/lib/esm/decorators/traced.decorator.js +0 -57
  54. package/dist/lib/esm/decorators/traced.decorator.js.map +0 -1
  55. package/dist/lib/esm/utils/span-runner.js +0 -108
  56. package/dist/lib/esm/utils/span-runner.js.map +0 -1
  57. package/dist/types/decorators/decorators.d.ts +0 -1
  58. package/dist/types/decorators/traced.decorator.d.ts +0 -35
  59. package/dist/types/utils/span-runner.d.ts +0 -55
@@ -9,13 +9,23 @@ import { LogHandlerInterface } from "@pristine-ts/logging";
9
9
  * It is tagged and can be injected using TracingManagerInterface which facilitates mocking.
10
10
  * It is module scoped to the TelemetryModuleKeyname.
11
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
+ *
12
22
  * **Lifecycle: container-scoped, not singleton.** Each per-event DI child container gets
13
- * its own `TracingManager` instance with its own `trace` and `spans` state. Earlier
14
- * versions used `@singleton()` a single instance shared across every event — which
15
- * was a latent bug: parallel events would clobber each other's `this.trace`. Resolving
16
- * `TracingManager` from the root container still returns the root instance (used for
17
- * kernel-initialization spans before any event has started); resolving from a child
18
- * container returns the per-event instance, which is what application code wants.
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.
19
29
  */
20
30
  export declare class TracingManager implements TracingManagerInterface {
21
31
  private readonly tracers;
@@ -24,15 +34,10 @@ export declare class TracingManager implements TracingManagerInterface {
24
34
  private readonly debug;
25
35
  private readonly tracingContext;
26
36
  /**
27
- * This property contains a reference to the active trace.
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.
28
39
  */
29
40
  trace?: Trace;
30
- /**
31
- * This object contains a map of all the spans sorted by their keyname.
32
- */
33
- spans: {
34
- [keyname: string]: Span[];
35
- };
36
41
  /**
37
42
  * The Tracing Manager provides methods to help with tracing.
38
43
  * It is tagged and can be injected using TracingManagerInterface which facilitates mocking.
@@ -45,6 +50,16 @@ export declare class TracingManager implements TracingManagerInterface {
45
50
  * @param tracingContext The tracing context.
46
51
  */
47
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;
48
63
  /**
49
64
  * This methods starts the Tracing. This should be the first method called before doing anything else.
50
65
  * @param spanRootKeyname The keyname of the span at the root.
@@ -84,4 +99,47 @@ export declare class TracingManager implements TracingManagerInterface {
84
99
  * This method ends the trace entirely.
85
100
  */
86
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;
87
145
  }
@@ -1,2 +1,3 @@
1
+ export * from "./span-event.model";
1
2
  export * from "./span.model";
2
3
  export * from "./trace.model";
@@ -0,0 +1 @@
1
+ export { SpanEvent } from "@pristine-ts/common";
@@ -1,73 +1 @@
1
- import { Trace } from "./trace.model";
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
- import { Span } from "./span.model";
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,5 +1,4 @@
1
1
  import { ModuleInterface } from "@pristine-ts/common";
2
- export * from "./decorators/decorators";
3
2
  export * from "./enums/enums";
4
3
  export * from "./interfaces/interfaces";
5
4
  export * from "./managers/managers";
@@ -1,2 +1 @@
1
- export * from "./span-runner";
2
1
  export * from "./trace-renderer";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pristine-ts/telemetry",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
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.3",
16
- "@pristine-ts/logging": "^2.0.3",
15
+ "@pristine-ts/common": "^2.0.5",
16
+ "@pristine-ts/logging": "^2.0.5",
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": "c741bb430ab8f6286068dc3870fcadf4cefd0023",
64
+ "gitHead": "9a960b330ee1961bb9a6151ce50257c4d8ccd48d",
65
65
  "repository": {
66
66
  "type": "git",
67
67
  "url": "https://github.com/magieno/pristine-ts.git",
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./traced.decorator"), exports);
18
- //# sourceMappingURL=decorators.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../src/decorators/decorators.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC"}
@@ -1,60 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.traced = traced;
4
- const span_runner_1 = require("../utils/span-runner");
5
- /**
6
- * Method decorator that wraps the decorated method in a span. The span is automatically
7
- * ended when the method returns or throws, and the `TracingManager` is auto-resolved
8
- * from the active `EventContext` — no need to inject it manually.
9
- *
10
- * ```ts
11
- * class PaymentService {
12
- * @traced() // span name = "PaymentService.charge"
13
- * async charge(amount: number) { ... }
14
- *
15
- * @traced("payment.refund.action") // explicit name
16
- * async refund(chargeId: string) { ... }
17
- * }
18
- * ```
19
- *
20
- * **Behavior outside an event context.** When the decorated method runs without an
21
- * active `EventContext` (e.g. a unit test calling the method directly, or background
22
- * work that escaped the event lifecycle), the decorator is a no-op — the original
23
- * method runs unchanged. Tracing must never throw or alter semantics.
24
- *
25
- * **Sync methods become async.** The decorator awaits the wrapped method internally,
26
- * so any decorated method returns a `Promise`. Decorating a sync method changes its
27
- * signature visible to callers — apply `@traced` to methods you'd already be awaiting
28
- * (DB calls, HTTP, expensive computations), not to tight sync helpers.
29
- *
30
- * **Type-level caveat.** TypeScript's parameter and method decorator types don't carry
31
- * enough information for the compiler to verify that the wrapped method's return type
32
- * is compatible with `Promise<T>`. The wrapper is type-safe at runtime; the call site's
33
- * static types are unchanged. Pair with explicit `: Promise<T>` annotations on the
34
- * method when you want the static type to match what's actually returned.
35
- *
36
- * @param spanName Optional explicit name for the span. Defaults to
37
- * `${ClassName}.${methodName}`.
38
- */
39
- function traced(spanName) {
40
- return (target, propertyKey, descriptor) => {
41
- var _a, _b, _c;
42
- const original = descriptor.value;
43
- if (typeof original !== "function") {
44
- // Decorator was applied to something other than a method (a getter, a property
45
- // with no initial value, etc.). Leave it alone — silently no-op rather than
46
- // throw at decoration time.
47
- return descriptor;
48
- }
49
- // Resolve a stable default name at decoration time. `target.constructor.name` is
50
- // the class name for instance methods; for static methods, `target.name` is the
51
- // class. We cover both via the fallback chain.
52
- const className = (_c = (_b = (_a = target === null || target === void 0 ? void 0 : target.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : target === null || target === void 0 ? void 0 : target.name) !== null && _c !== void 0 ? _c : "anonymous";
53
- const resolvedName = spanName !== null && spanName !== void 0 ? spanName : `${className}.${String(propertyKey)}`;
54
- descriptor.value = function (...args) {
55
- return span_runner_1.spanRunner.runWithSpan(resolvedName, () => original.apply(this, args));
56
- };
57
- return descriptor;
58
- };
59
- }
60
- //# sourceMappingURL=traced.decorator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"traced.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/traced.decorator.ts"],"names":[],"mappings":";;AAoCA,wBAsBC;AA1DD,sDAAgD;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,SAAgB,MAAM,CAAC,QAAiB;IACtC,OAAO,CAAC,MAAW,EAAE,WAA4B,EAAE,UAA8B,EAAE,EAAE;;QACnF,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;QAClC,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YACnC,+EAA+E;YAC/E,4EAA4E;YAC5E,4BAA4B;YAC5B,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,iFAAiF;QACjF,gFAAgF;QAChF,+CAA+C;QAC/C,MAAM,SAAS,GAAW,MAAA,MAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,WAAW,0CAAE,IAAI,mCAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,mCAAI,WAAW,CAAC;QACnF,MAAM,YAAY,GAAG,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,GAAG,SAAS,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC;QAEvE,UAAU,CAAC,KAAK,GAAG,UAAyB,GAAG,IAAW;YACxD,OAAO,wBAAU,CAAC,WAAW,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QAChF,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
@@ -1,112 +0,0 @@
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.spanRunner = exports.SpanRunner = void 0;
13
- const common_1 = require("@pristine-ts/common");
14
- /**
15
- * Runs a function inside a span that is automatically ended when the function returns
16
- * or throws.
17
- *
18
- * Two call shapes are supported:
19
- *
20
- * **Explicit form** (`runWithSpan(tracingManager, name, fn)`) — pass the manager you've
21
- * already injected. Works anywhere, including outside an event context. Use this when
22
- * you have a `TracingManager` reference in hand:
23
- *
24
- * ```ts
25
- * return spanRunner.runWithSpan(this.tracingManager, "payment.charge",
26
- * () => this.client.charge(amount));
27
- * ```
28
- *
29
- * **ALS form** (`runWithSpan(name, fn)`) — auto-resolves the `TracingManager` from the
30
- * active `EventContext`'s container. Concise; works when called from anywhere inside
31
- * a Pristine event (controller, service, etc.) without needing the manager threaded
32
- * through:
33
- *
34
- * ```ts
35
- * return spanRunner.runWithSpan("payment.charge", () => this.client.charge(amount));
36
- * ```
37
- *
38
- * If the ALS form is used outside any `EventContext` (e.g. a unit test that doesn't
39
- * boot a kernel), no span is created and `fn` runs unchanged — same no-throw contract
40
- * as the rest of the tracing layer.
41
- *
42
- * On thrown errors: the error's name/message is attached to the span's context (visible
43
- * in the rendered tree/JSON output) and then re-thrown. The span is ended either way.
44
- *
45
- * Stateless — instantiate once and reuse, or use the exported singleton `spanRunner`.
46
- */
47
- class SpanRunner {
48
- runWithSpan(tracingManagerOrSpanKeyname, spanKeynameOrFn, fnOrOptions, maybeOptions) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- // Disambiguate by the first argument's shape. A string means "ALS form"; an object
51
- // with `startSpan` means the explicit-manager form.
52
- let tracingManager;
53
- let spanKeyname;
54
- let fn;
55
- let options;
56
- if (typeof tracingManagerOrSpanKeyname === "string") {
57
- spanKeyname = tracingManagerOrSpanKeyname;
58
- fn = spanKeynameOrFn;
59
- options = fnOrOptions;
60
- // Resolve the manager from the active event context. If there isn't one (e.g. a
61
- // unit test calling this directly), fall back to running the function without a
62
- // span — tracing must never throw or change semantics.
63
- const container = common_1.EventContextManager.container();
64
- if (container !== undefined) {
65
- try {
66
- tracingManager = container.resolve("TracingManagerInterface");
67
- }
68
- catch (_a) {
69
- tracingManager = undefined;
70
- }
71
- }
72
- }
73
- else {
74
- tracingManager = tracingManagerOrSpanKeyname;
75
- spanKeyname = spanKeynameOrFn;
76
- fn = fnOrOptions;
77
- options = maybeOptions;
78
- }
79
- if (tracingManager === undefined) {
80
- // No manager available: ALS form called outside any event context, or no
81
- // TracingManager registered in the container. Run the function unchanged so
82
- // callers get the same behavior they'd see without `runWithSpan` wrapping at all.
83
- return yield fn();
84
- }
85
- const span = tracingManager.startSpan(spanKeyname, options === null || options === void 0 ? void 0 : options.parentKeyname, options === null || options === void 0 ? void 0 : options.parentId, options === null || options === void 0 ? void 0 : options.context);
86
- try {
87
- return yield fn();
88
- }
89
- catch (error) {
90
- // Annotate the span with error info so it surfaces in the rendered output without
91
- // forcing the caller to remember to add it themselves. Span.context is string-typed
92
- // by design (cheap to serialize), so we coerce.
93
- if (error instanceof Error) {
94
- span.context = Object.assign(Object.assign({}, span.context), { error: "true", errorName: error.name, errorMessage: error.message });
95
- }
96
- else {
97
- span.context = Object.assign(Object.assign({}, span.context), { error: "true", errorName: "non-error-throw", errorMessage: String(error) });
98
- }
99
- throw error;
100
- }
101
- finally {
102
- span.end();
103
- }
104
- });
105
- }
106
- }
107
- exports.SpanRunner = SpanRunner;
108
- /**
109
- * Default singleton. Stateless so sharing is safe.
110
- */
111
- exports.spanRunner = new SpanRunner();
112
- //# sourceMappingURL=span-runner.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"span-runner.js","sourceRoot":"","sources":["../../../../src/utils/span-runner.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,gDAAwD;AAexD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAa,UAAU;IAcf,WAAW,CACf,2BAA6D,EAC7D,eAAgD,EAChD,WAAwD,EACxD,YAAgC;;YAEhC,mFAAmF;YACnF,oDAAoD;YACpD,IAAI,cAAmD,CAAC;YACxD,IAAI,WAAmB,CAAC;YACxB,IAAI,EAAwB,CAAC;YAC7B,IAAI,OAAsC,CAAC;YAE3C,IAAI,OAAO,2BAA2B,KAAK,QAAQ,EAAE,CAAC;gBACpD,WAAW,GAAG,2BAA2B,CAAC;gBAC1C,EAAE,GAAG,eAAuC,CAAC;gBAC7C,OAAO,GAAG,WAA4C,CAAC;gBACvD,gFAAgF;gBAChF,gFAAgF;gBAChF,uDAAuD;gBACvD,MAAM,SAAS,GAAG,4BAAmB,CAAC,SAAS,EAAE,CAAC;gBAClD,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;oBAC5B,IAAI,CAAC;wBACH,cAAc,GAAG,SAAS,CAAC,OAAO,CAA0B,yBAAyB,CAAC,CAAC;oBACzF,CAAC;oBAAC,WAAM,CAAC;wBACP,cAAc,GAAG,SAAS,CAAC;oBAC7B,CAAC;gBACH,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,cAAc,GAAG,2BAA2B,CAAC;gBAC7C,WAAW,GAAG,eAAyB,CAAC;gBACxC,EAAE,GAAG,WAAmC,CAAC;gBACzC,OAAO,GAAG,YAAY,CAAC;YACzB,CAAC;YAED,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;gBACjC,yEAAyE;gBACzE,4EAA4E;gBAC5E,kFAAkF;gBAClF,OAAO,MAAM,EAAE,EAAE,CAAC;YACpB,CAAC;YAED,MAAM,IAAI,GAAG,cAAc,CAAC,SAAS,CACnC,WAAW,EACX,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,aAAa,EACtB,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,EACjB,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CACjB,CAAC;YAEF,IAAI,CAAC;gBACH,OAAO,MAAM,EAAE,EAAE,CAAC;YACpB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,kFAAkF;gBAClF,oFAAoF;gBACpF,gDAAgD;gBAChD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;oBAC3B,IAAI,CAAC,OAAO,mCACP,IAAI,CAAC,OAAO,KACf,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,KAAK,CAAC,IAAI,EACrB,YAAY,EAAE,KAAK,CAAC,OAAO,GAC5B,CAAC;gBACJ,CAAC;qBAAM,CAAC;oBACN,IAAI,CAAC,OAAO,mCACP,IAAI,CAAC,OAAO,KACf,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,iBAAiB,EAC5B,YAAY,EAAE,MAAM,CAAC,KAAK,CAAC,GAC5B,CAAC;gBACJ,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,GAAG,EAAE,CAAC;YACb,CAAC;QACH,CAAC;KAAA;CACF;AAzFD,gCAyFC;AAED;;GAEG;AACU,QAAA,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC"}
@@ -1,2 +0,0 @@
1
- export * from "./traced.decorator";
2
- //# sourceMappingURL=decorators.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../src/decorators/decorators.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
@@ -1,57 +0,0 @@
1
- import { spanRunner } from "../utils/span-runner";
2
- /**
3
- * Method decorator that wraps the decorated method in a span. The span is automatically
4
- * ended when the method returns or throws, and the `TracingManager` is auto-resolved
5
- * from the active `EventContext` — no need to inject it manually.
6
- *
7
- * ```ts
8
- * class PaymentService {
9
- * @traced() // span name = "PaymentService.charge"
10
- * async charge(amount: number) { ... }
11
- *
12
- * @traced("payment.refund.action") // explicit name
13
- * async refund(chargeId: string) { ... }
14
- * }
15
- * ```
16
- *
17
- * **Behavior outside an event context.** When the decorated method runs without an
18
- * active `EventContext` (e.g. a unit test calling the method directly, or background
19
- * work that escaped the event lifecycle), the decorator is a no-op — the original
20
- * method runs unchanged. Tracing must never throw or alter semantics.
21
- *
22
- * **Sync methods become async.** The decorator awaits the wrapped method internally,
23
- * so any decorated method returns a `Promise`. Decorating a sync method changes its
24
- * signature visible to callers — apply `@traced` to methods you'd already be awaiting
25
- * (DB calls, HTTP, expensive computations), not to tight sync helpers.
26
- *
27
- * **Type-level caveat.** TypeScript's parameter and method decorator types don't carry
28
- * enough information for the compiler to verify that the wrapped method's return type
29
- * is compatible with `Promise<T>`. The wrapper is type-safe at runtime; the call site's
30
- * static types are unchanged. Pair with explicit `: Promise<T>` annotations on the
31
- * method when you want the static type to match what's actually returned.
32
- *
33
- * @param spanName Optional explicit name for the span. Defaults to
34
- * `${ClassName}.${methodName}`.
35
- */
36
- export function traced(spanName) {
37
- return (target, propertyKey, descriptor) => {
38
- var _a, _b, _c;
39
- const original = descriptor.value;
40
- if (typeof original !== "function") {
41
- // Decorator was applied to something other than a method (a getter, a property
42
- // with no initial value, etc.). Leave it alone — silently no-op rather than
43
- // throw at decoration time.
44
- return descriptor;
45
- }
46
- // Resolve a stable default name at decoration time. `target.constructor.name` is
47
- // the class name for instance methods; for static methods, `target.name` is the
48
- // class. We cover both via the fallback chain.
49
- const className = (_c = (_b = (_a = target === null || target === void 0 ? void 0 : target.constructor) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : target === null || target === void 0 ? void 0 : target.name) !== null && _c !== void 0 ? _c : "anonymous";
50
- const resolvedName = spanName !== null && spanName !== void 0 ? spanName : `${className}.${String(propertyKey)}`;
51
- descriptor.value = function (...args) {
52
- return spanRunner.runWithSpan(resolvedName, () => original.apply(this, args));
53
- };
54
- return descriptor;
55
- };
56
- }
57
- //# sourceMappingURL=traced.decorator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"traced.decorator.js","sourceRoot":"","sources":["../../../../src/decorators/traced.decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,sBAAsB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,MAAM,CAAC,QAAiB;IACtC,OAAO,CAAC,MAAW,EAAE,WAA4B,EAAE,UAA8B,EAAE,EAAE;;QACnF,MAAM,QAAQ,GAAG,UAAU,CAAC,KAAK,CAAC;QAClC,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YACnC,+EAA+E;YAC/E,4EAA4E;YAC5E,4BAA4B;YAC5B,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,iFAAiF;QACjF,gFAAgF;QAChF,+CAA+C;QAC/C,MAAM,SAAS,GAAW,MAAA,MAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,WAAW,0CAAE,IAAI,mCAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,mCAAI,WAAW,CAAC;QACnF,MAAM,YAAY,GAAG,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,GAAG,SAAS,IAAI,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC;QAEvE,UAAU,CAAC,KAAK,GAAG,UAAyB,GAAG,IAAW;YACxD,OAAO,UAAU,CAAC,WAAW,CAAC,YAAY,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QAChF,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}