@pristine-ts/telemetry 2.0.1 → 2.0.2

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 (63) hide show
  1. package/dist/lib/cjs/enums/console-tracer-output-mode.enum.js +16 -0
  2. package/dist/lib/cjs/enums/console-tracer-output-mode.enum.js.map +1 -0
  3. package/dist/lib/cjs/enums/enums.js +1 -0
  4. package/dist/lib/cjs/enums/enums.js.map +1 -1
  5. package/dist/lib/cjs/managers/tracing.manager.js +38 -39
  6. package/dist/lib/cjs/managers/tracing.manager.js.map +1 -1
  7. package/dist/lib/cjs/telemetry.configuration-keys.js +7 -0
  8. package/dist/lib/cjs/telemetry.configuration-keys.js.map +1 -1
  9. package/dist/lib/cjs/telemetry.module.js +89 -0
  10. package/dist/lib/cjs/telemetry.module.js.map +1 -1
  11. package/dist/lib/cjs/tracers/console.tracer.js +106 -0
  12. package/dist/lib/cjs/tracers/console.tracer.js.map +1 -0
  13. package/dist/lib/cjs/tracers/file.tracer.js +161 -0
  14. package/dist/lib/cjs/tracers/file.tracer.js.map +1 -0
  15. package/dist/lib/cjs/tracers/tracers.js +2 -1
  16. package/dist/lib/cjs/tracers/tracers.js.map +1 -1
  17. package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  18. package/dist/lib/cjs/utils/span-runner.js +81 -0
  19. package/dist/lib/cjs/utils/span-runner.js.map +1 -0
  20. package/dist/lib/cjs/utils/trace-renderer.js +128 -0
  21. package/dist/lib/cjs/utils/trace-renderer.js.map +1 -0
  22. package/dist/lib/cjs/utils/utils.js +19 -0
  23. package/dist/lib/cjs/utils/utils.js.map +1 -0
  24. package/dist/lib/esm/enums/console-tracer-output-mode.enum.js +13 -0
  25. package/dist/lib/esm/enums/console-tracer-output-mode.enum.js.map +1 -0
  26. package/dist/lib/esm/enums/enums.js +1 -0
  27. package/dist/lib/esm/enums/enums.js.map +1 -1
  28. package/dist/lib/esm/managers/tracing.manager.js +38 -39
  29. package/dist/lib/esm/managers/tracing.manager.js.map +1 -1
  30. package/dist/lib/esm/telemetry.configuration-keys.js +7 -0
  31. package/dist/lib/esm/telemetry.configuration-keys.js.map +1 -1
  32. package/dist/lib/esm/telemetry.module.js +90 -1
  33. package/dist/lib/esm/telemetry.module.js.map +1 -1
  34. package/dist/lib/esm/tracers/console.tracer.js +103 -0
  35. package/dist/lib/esm/tracers/console.tracer.js.map +1 -0
  36. package/dist/lib/esm/tracers/file.tracer.js +125 -0
  37. package/dist/lib/esm/tracers/file.tracer.js.map +1 -0
  38. package/dist/lib/esm/tracers/tracers.js +2 -1
  39. package/dist/lib/esm/tracers/tracers.js.map +1 -1
  40. package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
  41. package/dist/lib/esm/utils/span-runner.js +77 -0
  42. package/dist/lib/esm/utils/span-runner.js.map +1 -0
  43. package/dist/lib/esm/utils/trace-renderer.js +124 -0
  44. package/dist/lib/esm/utils/trace-renderer.js.map +1 -0
  45. package/dist/lib/esm/utils/utils.js +3 -0
  46. package/dist/lib/esm/utils/utils.js.map +1 -0
  47. package/dist/types/enums/console-tracer-output-mode.enum.d.ts +11 -0
  48. package/dist/types/enums/enums.d.ts +1 -0
  49. package/dist/types/managers/tracing.manager.d.ts +3 -1
  50. package/dist/types/telemetry.configuration-keys.d.ts +15 -0
  51. package/dist/types/telemetry.module.d.ts +1 -0
  52. package/dist/types/tracers/console.tracer.d.ts +28 -0
  53. package/dist/types/tracers/file.tracer.d.ts +34 -0
  54. package/dist/types/tracers/tracers.d.ts +2 -1
  55. package/dist/types/utils/span-runner.d.ts +59 -0
  56. package/dist/types/utils/trace-renderer.d.ts +41 -0
  57. package/dist/types/utils/utils.d.ts +2 -0
  58. package/package.json +4 -4
  59. package/dist/lib/cjs/tracers/basic.tracer.js +0 -50
  60. package/dist/lib/cjs/tracers/basic.tracer.js.map +0 -1
  61. package/dist/lib/esm/tracers/basic.tracer.js +0 -47
  62. package/dist/lib/esm/tracers/basic.tracer.js.map +0 -1
  63. package/dist/types/tracers/basic.tracer.d.ts +0 -12
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Pure-function renderers for `Trace` objects. Used by both `ConsoleTracer` (writes to
3
+ * stdout) and `FileTracer` (writes to disk) so the visual output is identical regardless
4
+ * of destination. All methods are stateless — instantiate once and reuse, or use the
5
+ * exported singleton `traceRenderer` if you don't care.
6
+ */
7
+ export class TraceRenderer {
8
+ /**
9
+ * Indented ASCII tree mirroring the span hierarchy. Right-aligned durations, slowest
10
+ * leaf span flagged as `← bottleneck`. Best for human readers.
11
+ *
12
+ * Internal/parent spans are skipped for bottleneck detection on purpose: a parent's
13
+ * duration is the sum of its children's work, so it would always dwarf any single
14
+ * leaf and the flag would never highlight where the actual time was spent.
15
+ */
16
+ renderTree(trace) {
17
+ const lines = [];
18
+ lines.push(`Trace ${trace.id} — ${trace.getDuration()}ms`);
19
+ if (trace.rootSpan === undefined) {
20
+ lines.push(" (no rootSpan)");
21
+ return lines.join("\n");
22
+ }
23
+ // Pre-walk to find the slowest leaf for the bottleneck flag.
24
+ let slowestSpan;
25
+ let slowestDuration = -1;
26
+ this.walkSpans(trace.rootSpan, (s) => {
27
+ if (s.children.length > 0)
28
+ return;
29
+ const d = s.getDuration();
30
+ if (d > slowestDuration) {
31
+ slowestDuration = d;
32
+ slowestSpan = s;
33
+ }
34
+ });
35
+ const rows = [];
36
+ const renderNode = (span, ancestorIsLast) => {
37
+ const prefix = ancestorIsLast.length === 0
38
+ ? ""
39
+ : ancestorIsLast.slice(0, -1).map(isLast => isLast ? " " : "│ ").join("")
40
+ + (ancestorIsLast[ancestorIsLast.length - 1] ? "└── " : "├── ");
41
+ rows.push({
42
+ prefix,
43
+ name: span.keyname,
44
+ duration: span.getDuration(),
45
+ isBottleneck: span === slowestSpan && slowestDuration > 0,
46
+ });
47
+ span.children.forEach((child, idx) => {
48
+ const isLast = idx === span.children.length - 1;
49
+ renderNode(child, [...ancestorIsLast, isLast]);
50
+ });
51
+ };
52
+ renderNode(trace.rootSpan, []);
53
+ // Pad name column so durations line up.
54
+ const nameColumnWidth = Math.max(...rows.map(r => r.prefix.length + r.name.length));
55
+ for (const row of rows) {
56
+ const padding = " ".repeat(Math.max(1, nameColumnWidth - row.prefix.length - row.name.length + 2));
57
+ const durationCell = `${row.duration}ms`.padStart(8);
58
+ const marker = row.isBottleneck ? " ← bottleneck" : "";
59
+ lines.push(`${row.prefix}${row.name}${padding}${durationCell}${marker}`);
60
+ }
61
+ return lines.join("\n");
62
+ }
63
+ /**
64
+ * One line per span sorted by start time, no indentation. Best for grep / log
65
+ * aggregation.
66
+ */
67
+ renderFlat(trace) {
68
+ const lines = [`Trace ${trace.id} — ${trace.getDuration()}ms`];
69
+ const all = [];
70
+ if (trace.rootSpan !== undefined) {
71
+ this.walkSpans(trace.rootSpan, (s) => all.push(s));
72
+ }
73
+ all.sort((a, b) => a.startDate - b.startDate);
74
+ for (const span of all) {
75
+ lines.push(` ${span.keyname.padEnd(48)} ${`${span.getDuration()}ms`.padStart(8)}`);
76
+ }
77
+ return lines.join("\n");
78
+ }
79
+ /**
80
+ * Pretty-printed JSON dump of the trace + every span. Best for piping into a downstream
81
+ * tool that wants structured access.
82
+ */
83
+ renderJson(trace) {
84
+ return JSON.stringify(this.serialize(trace), null, 2);
85
+ }
86
+ /**
87
+ * Serializes a trace to a plain JSON-friendly object. Exposed for callers that want to
88
+ * embed the structured form in a larger payload (e.g. a NDJSON line).
89
+ */
90
+ serialize(trace) {
91
+ return {
92
+ id: trace.id,
93
+ startDate: trace.startDate,
94
+ endDate: trace.endDate,
95
+ duration: trace.getDuration(),
96
+ context: trace.context,
97
+ rootSpan: trace.rootSpan ? this.serializeSpan(trace.rootSpan) : undefined,
98
+ };
99
+ }
100
+ serializeSpan(span) {
101
+ return {
102
+ id: span.id,
103
+ keyname: span.keyname,
104
+ startDate: span.startDate,
105
+ endDate: span.endDate,
106
+ duration: span.getDuration(),
107
+ context: span.context,
108
+ children: span.children.map(s => this.serializeSpan(s)),
109
+ };
110
+ }
111
+ /** Depth-first traversal helper. Visits the root and every descendant once. */
112
+ walkSpans(span, visit) {
113
+ visit(span);
114
+ for (const child of span.children) {
115
+ this.walkSpans(child, visit);
116
+ }
117
+ }
118
+ }
119
+ /**
120
+ * Default singleton. Use this from any caller that doesn't care to construct its own —
121
+ * the class is stateless so sharing is safe.
122
+ */
123
+ export const traceRenderer = new TraceRenderer();
124
+ //# sourceMappingURL=trace-renderer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"trace-renderer.js","sourceRoot":"","sources":["../../../../src/utils/trace-renderer.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,OAAO,aAAa;IACxB;;;;;;;OAOG;IACH,UAAU,CAAC,KAAY;QACrB,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,EAAE,MAAM,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;QAE3D,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACjC,KAAK,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;YAC9B,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QAED,6DAA6D;QAC7D,IAAI,WAA6B,CAAC;QAClC,IAAI,eAAe,GAAG,CAAC,CAAC,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE;YACnC,IAAI,CAAC,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;gBAAE,OAAO;YAClC,MAAM,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;YAC1B,IAAI,CAAC,GAAG,eAAe,EAAE,CAAC;gBACxB,eAAe,GAAG,CAAC,CAAC;gBACpB,WAAW,GAAG,CAAC,CAAC;YAClB,CAAC;QACH,CAAC,CAAC,CAAC;QAGH,MAAM,IAAI,GAA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pD,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./span-runner";
2
+ export * from "./trace-renderer";
3
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * How `ConsoleTracer` renders a completed trace.
3
+ */
4
+ export declare enum ConsoleTracerOutputModeEnum {
5
+ /** Indented ASCII tree mirroring the span hierarchy. Best for human readers. */
6
+ Tree = "tree",
7
+ /** Pretty-printed JSON dump of the trace + every span. Best for piping into a tool. */
8
+ Json = "json",
9
+ /** One span per line, no indentation, sorted by start time. Best for grep. */
10
+ Flat = "flat"
11
+ }
@@ -1 +1,2 @@
1
+ export * from "./console-tracer-output-mode.enum";
1
2
  export * from "./span-keyname.enum";
@@ -53,7 +53,9 @@ export declare class TracingManager implements TracingManagerInterface {
53
53
  * @param parentId The id of the parent span.
54
54
  * @param context The context if there is one.
55
55
  */
56
- startSpan(keyname: string, parentKeyname?: string, parentId?: string, context?: any): Span;
56
+ startSpan(keyname: string, parentKeyname?: string, parentId?: string, context?: {
57
+ [key: string]: string;
58
+ }): Span;
57
59
  /**
58
60
  * This methods adds an already created Span to the trace. It assumes that it its hierarchy is correct.
59
61
  * @param span The span to add.
@@ -12,7 +12,15 @@
12
12
  export declare const TelemetryConfigurationKeys: {
13
13
  readonly Active: "pristine.telemetry.active";
14
14
  readonly Debug: "pristine.telemetry.debug";
15
+ readonly ConsoleTracerActivated: "pristine.telemetry.console-tracer.activated";
16
+ readonly ConsoleTracerOutputMode: "pristine.telemetry.console-tracer.output-mode";
17
+ readonly ConsoleTracerMinimumDurationMs: "pristine.telemetry.console-tracer.minimum-duration-ms";
18
+ readonly FileTracerActivated: "pristine.telemetry.file-tracer.activated";
19
+ readonly FileTracerOutputMode: "pristine.telemetry.file-tracer.output-mode";
20
+ readonly FileTracerDirectory: "pristine.telemetry.file-tracer.directory";
21
+ readonly FileTracerFilenamePattern: "pristine.telemetry.file-tracer.filename-pattern";
15
22
  };
23
+ import { ConsoleTracerOutputModeEnum } from "./enums/console-tracer-output-mode.enum";
16
24
  /**
17
25
  * The expected runtime types for each configuration value defined by `@pristine-ts/telemetry`.
18
26
  * See `AwsConfigurationValueMap` in `@pristine-ts/aws` for the full pattern + caveats.
@@ -20,6 +28,13 @@ export declare const TelemetryConfigurationKeys: {
20
28
  export interface TelemetryConfigurationValueMap {
21
29
  "pristine.telemetry.active": boolean;
22
30
  "pristine.telemetry.debug": boolean;
31
+ "pristine.telemetry.console-tracer.activated": boolean;
32
+ "pristine.telemetry.console-tracer.output-mode": ConsoleTracerOutputModeEnum;
33
+ "pristine.telemetry.console-tracer.minimum-duration-ms": number;
34
+ "pristine.telemetry.file-tracer.activated": boolean;
35
+ "pristine.telemetry.file-tracer.output-mode": ConsoleTracerOutputModeEnum;
36
+ "pristine.telemetry.file-tracer.directory": string;
37
+ "pristine.telemetry.file-tracer.filename-pattern": string;
23
38
  }
24
39
  /**
25
40
  * Augments the global `PristineConfigurationValueMap` (defined in `@pristine-ts/common`)
@@ -4,5 +4,6 @@ export * from "./interfaces/interfaces";
4
4
  export * from "./managers/managers";
5
5
  export * from "./models/models";
6
6
  export * from "./tracers/tracers";
7
+ export * from "./utils/utils";
7
8
  export * from "./telemetry.configuration-keys";
8
9
  export declare const TelemetryModule: ModuleInterface;
@@ -0,0 +1,28 @@
1
+ import { Readable } from "stream";
2
+ import { LogHandlerInterface } from "@pristine-ts/logging";
3
+ import { TracerInterface } from "../interfaces/tracer.interface";
4
+ import { ConsoleTracerOutputModeEnum } from "../enums/console-tracer-output-mode.enum";
5
+ /**
6
+ * `ConsoleTracer` prints a completed trace to stdout when the trace ends. Its main purpose
7
+ * is **developer-readable feedback during local development**: see what spans ran, in what
8
+ * order, and which one was the bottleneck — without wiring up an external tracing backend.
9
+ *
10
+ * Off by default. Enable via `pristine.telemetry.console-tracer.activated = true` (or
11
+ * `PRISTINE_TELEMETRY_CONSOLE_TRACER_ACTIVATED=true`). Three output modes:
12
+ * - `tree` (default): indented ASCII tree, durations right-aligned, slowest leaf flagged.
13
+ * - `json`: pretty-printed JSON dump.
14
+ * - `flat`: one line per span, no indentation, start-time-sorted.
15
+ *
16
+ * Crash-isolated: a throw inside the formatting code becomes a stderr line, never an
17
+ * unhandled error or process crash.
18
+ */
19
+ export declare class ConsoleTracer implements TracerInterface {
20
+ private readonly activated;
21
+ private readonly outputMode;
22
+ private readonly minimumDurationMs;
23
+ private readonly logHandler;
24
+ traceEndedStream: Readable;
25
+ constructor(activated: boolean, outputMode: ConsoleTracerOutputModeEnum, minimumDurationMs: number, logHandler: LogHandlerInterface);
26
+ private handleTraceEnded;
27
+ private reportFailure;
28
+ }
@@ -0,0 +1,34 @@
1
+ import { Readable } from "stream";
2
+ import { LogHandlerInterface } from "@pristine-ts/logging";
3
+ import { TracerInterface } from "../interfaces/tracer.interface";
4
+ import { ConsoleTracerOutputModeEnum } from "../enums/console-tracer-output-mode.enum";
5
+ /**
6
+ * `FileTracer` writes one file per completed trace to a configured directory. Files are
7
+ * named after the trace's id (= eventId throughout Pristine), so locating a request's
8
+ * trace is `cat traces/<eventId>.json`.
9
+ *
10
+ * Off by default. Enable via `pristine.telemetry.file-tracer.activated = true`.
11
+ *
12
+ * Configuration:
13
+ * - `file-tracer.directory` — defaults to `./traces`
14
+ * - `file-tracer.filename-pattern` — defaults to `<traceId>.json`. Placeholders:
15
+ * `<traceId>`, `<date>` (UTC YYYY-MM-DD), `<timestamp>` (ms epoch).
16
+ * - `file-tracer.output-mode` — `json` (default), `tree`, or `flat`.
17
+ *
18
+ * Crash-isolated: a write failure (read-only fs, missing permissions, etc.) becomes a
19
+ * stderr line, never an unhandled error. The framework continues and the next trace is
20
+ * attempted independently.
21
+ */
22
+ export declare class FileTracer implements TracerInterface {
23
+ private readonly activated;
24
+ private readonly outputMode;
25
+ private readonly directory;
26
+ private readonly filenamePattern;
27
+ private readonly logHandler;
28
+ traceEndedStream: Readable;
29
+ constructor(activated: boolean, outputMode: ConsoleTracerOutputModeEnum, directory: string, filenamePattern: string, logHandler: LogHandlerInterface);
30
+ private handleTraceEnded;
31
+ private renderTrace;
32
+ private expandFilenamePattern;
33
+ private reportFailure;
34
+ }
@@ -1 +1,2 @@
1
- export * from "./basic.tracer";
1
+ export * from "./console.tracer";
2
+ export * from "./file.tracer";
@@ -0,0 +1,59 @@
1
+ import { TracingManagerInterface } from "../interfaces/tracing-manager.interface";
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;
@@ -0,0 +1,41 @@
1
+ import { Trace } from "../models/trace.model";
2
+ /**
3
+ * Pure-function renderers for `Trace` objects. Used by both `ConsoleTracer` (writes to
4
+ * stdout) and `FileTracer` (writes to disk) so the visual output is identical regardless
5
+ * of destination. All methods are stateless — instantiate once and reuse, or use the
6
+ * exported singleton `traceRenderer` if you don't care.
7
+ */
8
+ export declare class TraceRenderer {
9
+ /**
10
+ * Indented ASCII tree mirroring the span hierarchy. Right-aligned durations, slowest
11
+ * leaf span flagged as `← bottleneck`. Best for human readers.
12
+ *
13
+ * Internal/parent spans are skipped for bottleneck detection on purpose: a parent's
14
+ * duration is the sum of its children's work, so it would always dwarf any single
15
+ * leaf and the flag would never highlight where the actual time was spent.
16
+ */
17
+ renderTree(trace: Trace): string;
18
+ /**
19
+ * One line per span sorted by start time, no indentation. Best for grep / log
20
+ * aggregation.
21
+ */
22
+ renderFlat(trace: Trace): string;
23
+ /**
24
+ * Pretty-printed JSON dump of the trace + every span. Best for piping into a downstream
25
+ * tool that wants structured access.
26
+ */
27
+ renderJson(trace: Trace): string;
28
+ /**
29
+ * Serializes a trace to a plain JSON-friendly object. Exposed for callers that want to
30
+ * embed the structured form in a larger payload (e.g. a NDJSON line).
31
+ */
32
+ serialize(trace: Trace): Record<string, unknown>;
33
+ private serializeSpan;
34
+ /** Depth-first traversal helper. Visits the root and every descendant once. */
35
+ private walkSpans;
36
+ }
37
+ /**
38
+ * Default singleton. Use this from any caller that doesn't care to construct its own —
39
+ * the class is stateless so sharing is safe.
40
+ */
41
+ export declare const traceRenderer: TraceRenderer;
@@ -0,0 +1,2 @@
1
+ export * from "./span-runner";
2
+ export * from "./trace-renderer";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pristine-ts/telemetry",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
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.1",
16
- "@pristine-ts/logging": "^2.0.1",
15
+ "@pristine-ts/common": "^2.0.2",
16
+ "@pristine-ts/logging": "^2.0.2",
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": "a5e40ea0fcdeaf743666c1981a9315bfde9c4c00",
64
+ "gitHead": "b0899c7eb6b34a99c6df0739507b1458549a0009",
65
65
  "repository": {
66
66
  "type": "git",
67
67
  "url": "https://github.com/magieno/pristine-ts.git",
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.XrayTracer = void 0;
10
- const tsyringe_1 = require("tsyringe");
11
- const common_1 = require("@pristine-ts/common");
12
- const stream_1 = require("stream");
13
- /**
14
- * We need this to have at least one tracer so the @injectAll(ServiceDefinitionTagEnum.Tracer) does not fail
15
- * Until there's a fix for: https://github.com/microsoft/tsyringe/issues/63
16
- */
17
- let XrayTracer = class XrayTracer {
18
- constructor() {
19
- this.spanStartedStream = new stream_1.Readable({
20
- objectMode: true,
21
- read(size) {
22
- return true;
23
- }
24
- });
25
- this.spanEndedStream = new stream_1.Readable({
26
- objectMode: true,
27
- read(size) {
28
- return true;
29
- }
30
- });
31
- this.traceStartedStream = new stream_1.Readable({
32
- objectMode: true,
33
- read(size) {
34
- return true;
35
- }
36
- });
37
- this.traceEndedStream = new stream_1.Readable({
38
- objectMode: true,
39
- read(size) {
40
- return true;
41
- }
42
- });
43
- }
44
- };
45
- exports.XrayTracer = XrayTracer;
46
- exports.XrayTracer = XrayTracer = __decorate([
47
- (0, common_1.tag)(common_1.ServiceDefinitionTagEnum.Tracer),
48
- (0, tsyringe_1.injectable)()
49
- ], XrayTracer);
50
- //# sourceMappingURL=basic.tracer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"basic.tracer.js","sourceRoot":"","sources":["../../../../src/tracers/basic.tracer.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uCAAoC;AACpC,gDAAkE;AAClE,mCAAgC;AAGhC;;;GAGG;AAGI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAAhB;QACL,sBAAiB,GAAc,IAAI,iBAAQ,CAAC;YAC1C,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,oBAAe,GAAc,IAAI,iBAAQ,CAAC;YACxC,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,uBAAkB,GAAc,IAAI,iBAAQ,CAAC;YAC3C,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,qBAAgB,GAAc,IAAI,iBAAQ,CAAC;YACzC,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CAAA,CAAA;AAzBY,gCAAU;qBAAV,UAAU;IAFtB,IAAA,YAAG,EAAC,iCAAwB,CAAC,MAAM,CAAC;IACpC,IAAA,qBAAU,GAAE;GACA,UAAU,CAyBtB"}
@@ -1,47 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { injectable } from "tsyringe";
8
- import { ServiceDefinitionTagEnum, tag } from "@pristine-ts/common";
9
- import { Readable } from "stream";
10
- /**
11
- * We need this to have at least one tracer so the @injectAll(ServiceDefinitionTagEnum.Tracer) does not fail
12
- * Until there's a fix for: https://github.com/microsoft/tsyringe/issues/63
13
- */
14
- let XrayTracer = class XrayTracer {
15
- constructor() {
16
- this.spanStartedStream = new Readable({
17
- objectMode: true,
18
- read(size) {
19
- return true;
20
- }
21
- });
22
- this.spanEndedStream = new Readable({
23
- objectMode: true,
24
- read(size) {
25
- return true;
26
- }
27
- });
28
- this.traceStartedStream = new Readable({
29
- objectMode: true,
30
- read(size) {
31
- return true;
32
- }
33
- });
34
- this.traceEndedStream = new Readable({
35
- objectMode: true,
36
- read(size) {
37
- return true;
38
- }
39
- });
40
- }
41
- };
42
- XrayTracer = __decorate([
43
- tag(ServiceDefinitionTagEnum.Tracer),
44
- injectable()
45
- ], XrayTracer);
46
- export { XrayTracer };
47
- //# sourceMappingURL=basic.tracer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"basic.tracer.js","sourceRoot":"","sources":["../../../../src/tracers/basic.tracer.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AACpC,OAAO,EAAC,wBAAwB,EAAE,GAAG,EAAC,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAC,QAAQ,EAAC,MAAM,QAAQ,CAAC;AAGhC;;;GAGG;AAGI,IAAM,UAAU,GAAhB,MAAM,UAAU;IAAhB;QACL,sBAAiB,GAAc,IAAI,QAAQ,CAAC;YAC1C,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,oBAAe,GAAc,IAAI,QAAQ,CAAC;YACxC,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,uBAAkB,GAAc,IAAI,QAAQ,CAAC;YAC3C,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QACH,qBAAgB,GAAc,IAAI,QAAQ,CAAC;YACzC,UAAU,EAAE,IAAI;YAChB,IAAI,CAAC,IAAY;gBACf,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;IACL,CAAC;CAAA,CAAA;AAzBY,UAAU;IAFtB,GAAG,CAAC,wBAAwB,CAAC,MAAM,CAAC;IACpC,UAAU,EAAE;GACA,UAAU,CAyBtB"}
@@ -1,12 +0,0 @@
1
- import { Readable } from "stream";
2
- import { TracerInterface } from "../interfaces/tracer.interface";
3
- /**
4
- * We need this to have at least one tracer so the @injectAll(ServiceDefinitionTagEnum.Tracer) does not fail
5
- * Until there's a fix for: https://github.com/microsoft/tsyringe/issues/63
6
- */
7
- export declare class XrayTracer implements TracerInterface {
8
- spanStartedStream?: Readable;
9
- spanEndedStream?: Readable;
10
- traceStartedStream?: Readable;
11
- traceEndedStream?: Readable;
12
- }