@pristine-ts/telemetry 2.0.2 → 2.0.3

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 (28) hide show
  1. package/dist/lib/cjs/decorators/decorators.js +18 -0
  2. package/dist/lib/cjs/decorators/decorators.js.map +1 -0
  3. package/dist/lib/cjs/decorators/traced.decorator.js +60 -0
  4. package/dist/lib/cjs/decorators/traced.decorator.js.map +1 -0
  5. package/dist/lib/cjs/managers/tracing.manager.js +18 -2
  6. package/dist/lib/cjs/managers/tracing.manager.js.map +1 -1
  7. package/dist/lib/cjs/telemetry.module.js +1 -0
  8. package/dist/lib/cjs/telemetry.module.js.map +1 -1
  9. package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  10. package/dist/lib/cjs/utils/span-runner.js +55 -24
  11. package/dist/lib/cjs/utils/span-runner.js.map +1 -1
  12. package/dist/lib/esm/decorators/decorators.js +2 -0
  13. package/dist/lib/esm/decorators/decorators.js.map +1 -0
  14. package/dist/lib/esm/decorators/traced.decorator.js +57 -0
  15. package/dist/lib/esm/decorators/traced.decorator.js.map +1 -0
  16. package/dist/lib/esm/managers/tracing.manager.js +20 -4
  17. package/dist/lib/esm/managers/tracing.manager.js.map +1 -1
  18. package/dist/lib/esm/telemetry.module.js +1 -0
  19. package/dist/lib/esm/telemetry.module.js.map +1 -1
  20. package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
  21. package/dist/lib/esm/utils/span-runner.js +55 -24
  22. package/dist/lib/esm/utils/span-runner.js.map +1 -1
  23. package/dist/types/decorators/decorators.d.ts +1 -0
  24. package/dist/types/decorators/traced.decorator.d.ts +35 -0
  25. package/dist/types/managers/tracing.manager.d.ts +8 -0
  26. package/dist/types/telemetry.module.d.ts +1 -0
  27. package/dist/types/utils/span-runner.d.ts +19 -23
  28. package/package.json +4 -4
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Method decorator that wraps the decorated method in a span. The span is automatically
3
+ * ended when the method returns or throws, and the `TracingManager` is auto-resolved
4
+ * from the active `EventContext` — no need to inject it manually.
5
+ *
6
+ * ```ts
7
+ * class PaymentService {
8
+ * @traced() // span name = "PaymentService.charge"
9
+ * async charge(amount: number) { ... }
10
+ *
11
+ * @traced("payment.refund.action") // explicit name
12
+ * async refund(chargeId: string) { ... }
13
+ * }
14
+ * ```
15
+ *
16
+ * **Behavior outside an event context.** When the decorated method runs without an
17
+ * active `EventContext` (e.g. a unit test calling the method directly, or background
18
+ * work that escaped the event lifecycle), the decorator is a no-op — the original
19
+ * method runs unchanged. Tracing must never throw or alter semantics.
20
+ *
21
+ * **Sync methods become async.** The decorator awaits the wrapped method internally,
22
+ * so any decorated method returns a `Promise`. Decorating a sync method changes its
23
+ * signature visible to callers — apply `@traced` to methods you'd already be awaiting
24
+ * (DB calls, HTTP, expensive computations), not to tight sync helpers.
25
+ *
26
+ * **Type-level caveat.** TypeScript's parameter and method decorator types don't carry
27
+ * enough information for the compiler to verify that the wrapped method's return type
28
+ * is compatible with `Promise<T>`. The wrapper is type-safe at runtime; the call site's
29
+ * static types are unchanged. Pair with explicit `: Promise<T>` annotations on the
30
+ * method when you want the static type to match what's actually returned.
31
+ *
32
+ * @param spanName Optional explicit name for the span. Defaults to
33
+ * `${ClassName}.${methodName}`.
34
+ */
35
+ export declare function traced(spanName?: string): MethodDecorator;
@@ -8,6 +8,14 @@ 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
+ * **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.
11
19
  */
12
20
  export declare class TracingManager implements TracingManagerInterface {
13
21
  private readonly tracers;
@@ -1,4 +1,5 @@
1
1
  import { ModuleInterface } from "@pristine-ts/common";
2
+ export * from "./decorators/decorators";
2
3
  export * from "./enums/enums";
3
4
  export * from "./interfaces/interfaces";
4
5
  export * from "./managers/managers";
@@ -16,42 +16,38 @@ export interface SpanRunnerOptions {
16
16
  * Runs a function inside a span that is automatically ended when the function returns
17
17
  * or throws.
18
18
  *
19
- * Replaces the manual `start → try → finally end()` boilerplate:
19
+ * Two call shapes are supported:
20
+ *
21
+ * **Explicit form** (`runWithSpan(tracingManager, name, fn)`) — pass the manager you've
22
+ * already injected. Works anywhere, including outside an event context. Use this when
23
+ * you have a `TracingManager` reference in hand:
20
24
  *
21
25
  * ```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
26
  * return spanRunner.runWithSpan(this.tracingManager, "payment.charge",
32
27
  * () => this.client.charge(amount));
33
28
  * ```
34
29
  *
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.
30
+ * **ALS form** (`runWithSpan(name, fn)`) auto-resolves the `TracingManager` from the
31
+ * active `EventContext`'s container. Concise; works when called from anywhere inside
32
+ * a Pristine event (controller, service, etc.) without needing the manager threaded
33
+ * through:
34
+ *
35
+ * ```ts
36
+ * return spanRunner.runWithSpan("payment.charge", () => this.client.charge(amount));
37
+ * ```
38
+ *
39
+ * If the ALS form is used outside any `EventContext` (e.g. a unit test that doesn't
40
+ * boot a kernel), no span is created and `fn` runs unchanged — same no-throw contract
41
+ * as the rest of the tracing layer.
40
42
  *
41
43
  * 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.
44
+ * in the rendered tree/JSON output) and then re-thrown. The span is ended either way.
43
45
  *
44
46
  * Stateless — instantiate once and reuse, or use the exported singleton `spanRunner`.
45
47
  */
46
48
  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
49
  runWithSpan<T>(tracingManager: TracingManagerInterface, spanKeyname: string, fn: () => Promise<T> | T, options?: SpanRunnerOptions): Promise<T>;
50
+ runWithSpan<T>(spanKeyname: string, fn: () => Promise<T> | T, options?: SpanRunnerOptions): Promise<T>;
55
51
  }
56
52
  /**
57
53
  * Default singleton. Stateless so sharing is safe.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pristine-ts/telemetry",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
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.2",
16
- "@pristine-ts/logging": "^2.0.2",
15
+ "@pristine-ts/common": "^2.0.3",
16
+ "@pristine-ts/logging": "^2.0.3",
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": "b0899c7eb6b34a99c6df0739507b1458549a0009",
64
+ "gitHead": "c741bb430ab8f6286068dc3870fcadf4cefd0023",
65
65
  "repository": {
66
66
  "type": "git",
67
67
  "url": "https://github.com/magieno/pristine-ts.git",