@zudojs/observability 0.1.0 → 1.0.0

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 (157) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +260 -13
  3. package/dist/errors/index.d.ts +5 -0
  4. package/dist/errors/index.js +5 -0
  5. package/dist/errors/observabilityError.core.d.ts +32 -0
  6. package/dist/errors/observabilityError.core.js +56 -0
  7. package/dist/exporter/exporter.console.d.ts +51 -3
  8. package/dist/exporter/exporter.console.js +110 -23
  9. package/dist/exporter/index.d.ts +2 -2
  10. package/dist/exporter/index.js +2 -2
  11. package/dist/index.d.ts +17 -11
  12. package/dist/index.js +20 -12
  13. package/dist/internal/ids.core.d.ts +26 -0
  14. package/dist/internal/ids.core.js +51 -0
  15. package/dist/internal/index.d.ts +5 -0
  16. package/dist/internal/index.js +5 -0
  17. package/dist/logLevel/index.d.ts +1 -1
  18. package/dist/logLevel/index.js +1 -1
  19. package/dist/logLevel/logLevel.type.d.ts +8 -2
  20. package/dist/logLevel/logLevel.type.js +27 -20
  21. package/dist/logRecord/index.d.ts +1 -1
  22. package/dist/logRecord/index.js +1 -1
  23. package/dist/logRecord/logRecord.core.d.ts +15 -3
  24. package/dist/logRecord/logRecord.core.js +48 -26
  25. package/dist/logger/logger.core.d.ts +16 -8
  26. package/dist/logger/logger.core.js +54 -23
  27. package/dist/metrics/counter/counter.core.d.ts +4 -1
  28. package/dist/metrics/counter/counter.core.js +11 -3
  29. package/dist/metrics/gauge/gauge.core.d.ts +1 -0
  30. package/dist/metrics/gauge/gauge.core.js +9 -0
  31. package/dist/metrics/histogram/histogram.core.d.ts +22 -11
  32. package/dist/metrics/histogram/histogram.core.js +102 -7
  33. package/dist/metrics/histogram/index.d.ts +1 -1
  34. package/dist/metrics/histogram/index.js +1 -1
  35. package/dist/metrics/index.d.ts +4 -3
  36. package/dist/metrics/index.js +4 -3
  37. package/dist/metrics/metrics.reader.d.ts +39 -0
  38. package/dist/metrics/metrics.reader.js +81 -0
  39. package/dist/metrics/metrics.registry.d.ts +55 -4
  40. package/dist/metrics/metrics.registry.js +136 -49
  41. package/dist/noop/index.d.ts +1 -1
  42. package/dist/noop/index.js +1 -1
  43. package/dist/noop/noopObservability.core.d.ts +14 -2
  44. package/dist/noop/noopObservability.core.js +48 -11
  45. package/dist/observability/observability.core.d.ts +57 -3
  46. package/dist/observability/observability.core.js +206 -50
  47. package/dist/processor/index.d.ts +4 -3
  48. package/dist/processor/index.js +4 -3
  49. package/dist/processor/processor.batch.d.ts +66 -11
  50. package/dist/processor/processor.batch.js +130 -29
  51. package/dist/processor/processor.log.d.ts +56 -0
  52. package/dist/processor/processor.log.js +119 -0
  53. package/dist/propagation/index.d.ts +1 -1
  54. package/dist/propagation/index.js +1 -1
  55. package/dist/propagation/propagation.core.d.ts +26 -6
  56. package/dist/propagation/propagation.core.js +35 -19
  57. package/dist/redaction/index.d.ts +1 -1
  58. package/dist/redaction/index.js +1 -1
  59. package/dist/redaction/redaction.core.d.ts +37 -5
  60. package/dist/redaction/redaction.core.js +180 -36
  61. package/dist/sampling/index.d.ts +1 -1
  62. package/dist/sampling/index.js +1 -1
  63. package/dist/sampling/sampler.type.d.ts +38 -6
  64. package/dist/sampling/sampler.type.js +64 -23
  65. package/dist/tracing/index.d.ts +2 -2
  66. package/dist/tracing/index.js +2 -2
  67. package/dist/tracing/span/index.d.ts +1 -1
  68. package/dist/tracing/span/index.js +1 -1
  69. package/dist/tracing/span/span.core.d.ts +51 -8
  70. package/dist/tracing/span/span.core.js +90 -10
  71. package/dist/tracing/span/spanContext.type.d.ts +12 -3
  72. package/dist/tracing/span/spanContext.type.js +19 -16
  73. package/dist/tracing/tracer/index.d.ts +2 -2
  74. package/dist/tracing/tracer/index.js +2 -2
  75. package/dist/tracing/tracer/tracer.core.d.ts +44 -14
  76. package/dist/tracing/tracer/tracer.core.js +81 -25
  77. package/dist/types/config.types.d.ts +138 -0
  78. package/dist/types/config.types.js +5 -0
  79. package/dist/types/logging.types.d.ts +94 -0
  80. package/dist/types/logging.types.js +20 -0
  81. package/dist/types/metrics.types.d.ts +93 -0
  82. package/dist/types/metrics.types.js +5 -0
  83. package/dist/types/tracing.types.d.ts +115 -0
  84. package/dist/types/tracing.types.js +28 -0
  85. package/package.json +23 -14
  86. package/dist/exporter/exporter.console.d.ts.map +0 -1
  87. package/dist/exporter/exporter.console.js.map +0 -1
  88. package/dist/exporter/index.d.ts.map +0 -1
  89. package/dist/exporter/index.js.map +0 -1
  90. package/dist/index.d.ts.map +0 -1
  91. package/dist/index.js.map +0 -1
  92. package/dist/logLevel/index.d.ts.map +0 -1
  93. package/dist/logLevel/index.js.map +0 -1
  94. package/dist/logLevel/logLevel.type.d.ts.map +0 -1
  95. package/dist/logLevel/logLevel.type.js.map +0 -1
  96. package/dist/logRecord/index.d.ts.map +0 -1
  97. package/dist/logRecord/index.js.map +0 -1
  98. package/dist/logRecord/logRecord.core.d.ts.map +0 -1
  99. package/dist/logRecord/logRecord.core.js.map +0 -1
  100. package/dist/logger/index.d.ts.map +0 -1
  101. package/dist/logger/index.js.map +0 -1
  102. package/dist/logger/logger.core.d.ts.map +0 -1
  103. package/dist/logger/logger.core.js.map +0 -1
  104. package/dist/metrics/counter/counter.core.d.ts.map +0 -1
  105. package/dist/metrics/counter/counter.core.js.map +0 -1
  106. package/dist/metrics/counter/index.d.ts.map +0 -1
  107. package/dist/metrics/counter/index.js.map +0 -1
  108. package/dist/metrics/gauge/gauge.core.d.ts.map +0 -1
  109. package/dist/metrics/gauge/gauge.core.js.map +0 -1
  110. package/dist/metrics/gauge/index.d.ts.map +0 -1
  111. package/dist/metrics/gauge/index.js.map +0 -1
  112. package/dist/metrics/histogram/histogram.core.d.ts.map +0 -1
  113. package/dist/metrics/histogram/histogram.core.js.map +0 -1
  114. package/dist/metrics/histogram/index.d.ts.map +0 -1
  115. package/dist/metrics/histogram/index.js.map +0 -1
  116. package/dist/metrics/index.d.ts.map +0 -1
  117. package/dist/metrics/index.js.map +0 -1
  118. package/dist/metrics/metrics.registry.d.ts.map +0 -1
  119. package/dist/metrics/metrics.registry.js.map +0 -1
  120. package/dist/noop/index.d.ts.map +0 -1
  121. package/dist/noop/index.js.map +0 -1
  122. package/dist/noop/noopObservability.core.d.ts.map +0 -1
  123. package/dist/noop/noopObservability.core.js.map +0 -1
  124. package/dist/observability/index.d.ts.map +0 -1
  125. package/dist/observability/index.js.map +0 -1
  126. package/dist/observability/observability.core.d.ts.map +0 -1
  127. package/dist/observability/observability.core.js.map +0 -1
  128. package/dist/processor/index.d.ts.map +0 -1
  129. package/dist/processor/index.js.map +0 -1
  130. package/dist/processor/processor.batch.d.ts.map +0 -1
  131. package/dist/processor/processor.batch.js.map +0 -1
  132. package/dist/propagation/index.d.ts.map +0 -1
  133. package/dist/propagation/index.js.map +0 -1
  134. package/dist/propagation/propagation.core.d.ts.map +0 -1
  135. package/dist/propagation/propagation.core.js.map +0 -1
  136. package/dist/redaction/index.d.ts.map +0 -1
  137. package/dist/redaction/index.js.map +0 -1
  138. package/dist/redaction/redaction.core.d.ts.map +0 -1
  139. package/dist/redaction/redaction.core.js.map +0 -1
  140. package/dist/sampling/index.d.ts.map +0 -1
  141. package/dist/sampling/index.js.map +0 -1
  142. package/dist/sampling/sampler.type.d.ts.map +0 -1
  143. package/dist/sampling/sampler.type.js.map +0 -1
  144. package/dist/tracing/index.d.ts.map +0 -1
  145. package/dist/tracing/index.js.map +0 -1
  146. package/dist/tracing/span/index.d.ts.map +0 -1
  147. package/dist/tracing/span/index.js.map +0 -1
  148. package/dist/tracing/span/span.core.d.ts.map +0 -1
  149. package/dist/tracing/span/span.core.js.map +0 -1
  150. package/dist/tracing/span/spanContext.type.d.ts.map +0 -1
  151. package/dist/tracing/span/spanContext.type.js.map +0 -1
  152. package/dist/tracing/tracer/index.d.ts.map +0 -1
  153. package/dist/tracing/tracer/index.js.map +0 -1
  154. package/dist/tracing/tracer/tracer.core.d.ts.map +0 -1
  155. package/dist/tracing/tracer/tracer.core.js.map +0 -1
  156. package/dist/types.d.ts.map +0 -1
  157. package/dist/types.js.map +0 -1
@@ -1,9 +1,13 @@
1
1
  /**
2
2
  * @zudojs/observability — Tracer
3
3
  *
4
- * Creates spans and notifies processors on start/end.
4
+ * Creates spans, applies the sampling decision, and notifies processors on
5
+ * start and end.
5
6
  */
6
- import { DefaultSpan, createSpan } from "../span/index.js";
7
+ import { TraceFlags } from "../../types.js";
8
+ import { DefaultSpan } from "../span/span.core.js";
9
+ import { createChildSpanContext, createSpanContext, } from "../span/spanContext.type.js";
10
+ import { AlwaysOnSampler } from "../../sampling/index.js";
7
11
  /**
8
12
  * Default tracer that creates spans and notifies processors on start/end.
9
13
  */
@@ -11,47 +15,99 @@ export class DefaultTracer {
11
15
  processors;
12
16
  exporter;
13
17
  resource;
18
+ sampler;
19
+ limits;
20
+ captureStackTraces;
21
+ redactAttribute;
22
+ onError;
14
23
  constructor(options) {
15
24
  this.processors = [...(options?.processors ?? [])];
16
25
  this.exporter = options?.exporter;
17
26
  this.resource = { ...(options?.resource ?? {}) };
27
+ this.sampler = options?.sampler ?? new AlwaysOnSampler();
28
+ this.limits = options?.limits;
29
+ this.captureStackTraces = options?.captureStackTraces ?? true;
30
+ this.redactAttribute = options?.redactAttribute;
31
+ this.onError = options?.onError;
18
32
  }
19
33
  startSpan(name, options) {
20
- const tracer = this;
21
- const span = createSpan(name, {
34
+ // Build the context first so the sampler can key on the real trace ID,
35
+ // then stamp the decision into traceFlags so children inherit it.
36
+ const base = options?.parent
37
+ ? createChildSpanContext(options.parent)
38
+ : createSpanContext();
39
+ const decision = this.sampler.shouldSample(options?.parent, base.traceId);
40
+ const sampled = decision.decision === "RECORD_AND_SAMPLE";
41
+ const recording = decision.decision !== "DO_NOT_RECORD";
42
+ const context = {
43
+ ...base,
44
+ traceFlags: sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
45
+ };
46
+ const span = new DefaultSpan(name, context, {
22
47
  ...options,
23
48
  resource: this.resource,
49
+ limits: this.limits,
50
+ captureStackTraces: this.captureStackTraces,
51
+ redactAttribute: this.redactAttribute,
52
+ recording,
53
+ // Only a sampled span reaches the processors; a RECORD_ONLY span is
54
+ // readable in-process but is not exported.
55
+ onEnd: sampled ? (readable) => this.notifyEnd(readable) : undefined,
24
56
  });
25
- for (const processor of this.processors) {
26
- processor.onStart(span);
57
+ if (decision.attributes) {
58
+ for (const [key, value] of Object.entries(decision.attributes)) {
59
+ span.setAttribute(key, value);
60
+ }
27
61
  }
28
- return new Proxy(span, {
29
- get(target, prop) {
30
- if (prop === "end") {
31
- return () => {
32
- target.end();
33
- for (const processor of tracer.processors) {
34
- processor.onEnd(target.toReadableSpan());
35
- }
36
- };
62
+ if (sampled) {
63
+ for (const processor of this.processors) {
64
+ try {
65
+ processor.onStart(span);
37
66
  }
38
- return Reflect.get(target, prop);
39
- },
40
- });
67
+ catch (error) {
68
+ this.onError?.(error, "SpanProcessor.onStart");
69
+ }
70
+ }
71
+ }
72
+ return span;
73
+ }
74
+ notifyEnd(readable) {
75
+ for (const processor of this.processors) {
76
+ try {
77
+ processor.onEnd(readable);
78
+ }
79
+ catch (error) {
80
+ this.onError?.(error, "SpanProcessor.onEnd");
81
+ }
82
+ }
41
83
  }
42
- /** Exports a completed span directly. */
84
+ /** Exports a completed span directly, bypassing the processors. */
43
85
  async exportSpan(span) {
44
86
  if (this.exporter) {
45
87
  await this.exporter.export([span]);
46
88
  }
47
89
  }
48
- /** Shuts down all processors and the exporter. */
49
- async shutdown() {
50
- for (const processor of this.processors) {
51
- await processor.shutdown();
90
+ /** Drains every processor without shutting anything down. */
91
+ async forceFlush() {
92
+ const results = await Promise.allSettled(this.processors.map((processor) => processor.forceFlush?.()));
93
+ for (const result of results) {
94
+ if (result.status === "rejected") {
95
+ this.onError?.(result.reason, "SpanProcessor.forceFlush");
96
+ }
52
97
  }
53
- if (this.exporter) {
54
- await this.exporter.shutdown();
98
+ }
99
+ /**
100
+ * Shuts down every processor.
101
+ *
102
+ * The exporter is not shut down here: the owner that supplied it shuts it
103
+ * down, and doing it in both places closed the same connection twice.
104
+ */
105
+ async shutdown() {
106
+ const results = await Promise.allSettled(this.processors.map((processor) => processor.shutdown()));
107
+ for (const result of results) {
108
+ if (result.status === "rejected") {
109
+ this.onError?.(result.reason, "SpanProcessor.shutdown");
110
+ }
55
111
  }
56
112
  }
57
113
  }
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Configuration types for the observability package.
3
+ */
4
+ import type { LogLevel, Logger, LogExporter } from "./logging.types.js";
5
+ import type { MetricsRegistry, MetricExporter } from "./metrics.types.js";
6
+ import type { MetricsRegistryOptions } from "../metrics/metrics.registry.js";
7
+ import type { Tracer, SpanExporter, SpanProcessor, SpanLimits, Sampler } from "./tracing.types.js";
8
+ /** Distributed tracing context carried through execution. */
9
+ export interface PropagationContext {
10
+ readonly traceId: string;
11
+ readonly spanId: string;
12
+ readonly parentSpanId?: string;
13
+ readonly requestId?: string;
14
+ readonly correlationId?: string;
15
+ readonly userId?: string;
16
+ readonly service?: string;
17
+ /** @see import("./tracing.types.js").TraceFlags */
18
+ readonly traceFlags?: number;
19
+ readonly baggage?: Record<string, string>;
20
+ }
21
+ /** Options for creating a propagation context. */
22
+ export interface PropagationContextOptions {
23
+ readonly traceId?: string;
24
+ readonly spanId?: string;
25
+ readonly parentSpanId?: string;
26
+ readonly requestId?: string;
27
+ readonly correlationId?: string;
28
+ readonly userId?: string;
29
+ readonly service?: string;
30
+ readonly traceFlags?: number;
31
+ readonly baggage?: Record<string, string>;
32
+ }
33
+ /** Manages propagation contexts. */
34
+ export interface PropagationManager {
35
+ /**
36
+ * The active context, or `undefined` outside a {@link PropagationManager.run}
37
+ * scope. Callers that want a context regardless should create one
38
+ * explicitly, so that "no context" stays distinguishable from a real one.
39
+ */
40
+ current(): PropagationContext | undefined;
41
+ /** Runs a function with a new propagation context. */
42
+ run<T>(context: PropagationContext, fn: () => T | Promise<T>): Promise<T>;
43
+ /** Runs a function synchronously with a new propagation context. */
44
+ runSync<T>(context: PropagationContext, fn: () => T): T;
45
+ /** Creates a new context derived from the current one. */
46
+ derive(overrides?: PropagationContextOptions): PropagationContext;
47
+ }
48
+ /** How a field name is matched against the sensitive-field list. */
49
+ export type RedactionMatchMode = "exact" | "contains";
50
+ /** Configuration for redacting sensitive fields from logs and traces. */
51
+ export interface RedactionConfig {
52
+ /**
53
+ * Field names to redact (case-insensitive). Defaults to a built-in list
54
+ * covering passwords, tokens, cookies, keys and card numbers.
55
+ */
56
+ readonly fields?: readonly string[];
57
+ /**
58
+ * Additional patterns tested against the field name. Useful for
59
+ * conventions a name list cannot express, such as `/^x-.*-token$/i`.
60
+ */
61
+ readonly patterns?: readonly RegExp[];
62
+ /**
63
+ * `"contains"` (the default) matches on word boundaries: a field is
64
+ * sensitive when any run of its words spells a listed term, so
65
+ * `userPassword`, `x-api-key` and `accessToken` are all caught while
66
+ * `shippingAddress` and `authorId` — which a raw substring test redacts
67
+ * because they contain `pin` and `auth` — are not. `"exact"` matches only
68
+ * whole names.
69
+ */
70
+ readonly matchMode?: RedactionMatchMode;
71
+ /** Custom redaction function, consulted before the field list. */
72
+ readonly customRedactor?: (key: string, value: unknown) => unknown;
73
+ /** Replacement text. Defaults to `"[REDACTED]"`. */
74
+ readonly replacement?: string;
75
+ /** How deep to walk nested structures. Default: 8. */
76
+ readonly maxDepth?: number;
77
+ }
78
+ /** Central observability facade. */
79
+ export interface Observability {
80
+ readonly logger: Logger;
81
+ readonly metrics: MetricsRegistry;
82
+ readonly tracer: Tracer;
83
+ readonly propagation: PropagationManager;
84
+ /** Creates a scoped observability instance with resource attributes. */
85
+ resource(attributes: Record<string, unknown>): Observability;
86
+ /** Drains every buffer without shutting anything down. */
87
+ flush(): Promise<void>;
88
+ /** Shuts down all exporters and processors. Safe to call more than once. */
89
+ shutdown(): Promise<void>;
90
+ }
91
+ /** Configuration for the observability system. */
92
+ export interface ObservabilityConfig {
93
+ readonly serviceName: string;
94
+ readonly serviceVersion?: string;
95
+ readonly environment?: string;
96
+ readonly logLevel?: LogLevel;
97
+ readonly logExporter?: LogExporter;
98
+ readonly spanExporter?: SpanExporter;
99
+ readonly metricExporter?: MetricExporter;
100
+ readonly sampler?: Sampler;
101
+ readonly processors?: readonly SpanProcessor[];
102
+ /**
103
+ * Redacts sensitive fields from log contexts *and* from span attributes and
104
+ * span event attributes. Omit it and neither is redacted.
105
+ */
106
+ readonly redaction?: RedactionConfig;
107
+ /**
108
+ * Metrics registry tuning: the series cap that bounds cardinality, and the
109
+ * histogram bucket boundaries. Without this the defaults were unreachable
110
+ * from the facade, so an application could not lower the 10,000-series cap
111
+ * that is its only protection against a label carrying a user ID.
112
+ */
113
+ readonly metrics?: MetricsRegistryOptions;
114
+ readonly resource?: Record<string, unknown>;
115
+ /** Caps on what a single span may accumulate. */
116
+ readonly spanLimits?: SpanLimits;
117
+ /**
118
+ * Fall back to the console exporters when no exporter is supplied.
119
+ * Defaults to `true`, which prints logs and spans to stdout — convenient in
120
+ * development and rarely wanted in production, so set it to `false` (or
121
+ * pass real exporters) when deploying.
122
+ */
123
+ readonly useConsoleExporters?: boolean;
124
+ /** How often metrics are exported, in ms. Default: 60,000. `0` disables it. */
125
+ readonly metricExportIntervalMs?: number;
126
+ /** How often buffered log records are flushed, in ms. Default: 1,000. */
127
+ readonly logFlushIntervalMs?: number;
128
+ /** How many log records are buffered before an eager flush. Default: 256. */
129
+ readonly logBatchSize?: number;
130
+ /**
131
+ * Record `exception.stacktrace` on spans. Stack traces reach the telemetry
132
+ * backend unredacted, so this is opt-out. Default: `true`.
133
+ */
134
+ readonly captureStackTraces?: boolean;
135
+ /** Reports a telemetry failure that would otherwise be swallowed. */
136
+ readonly onError?: (error: unknown, source: string) => void;
137
+ }
138
+ //# sourceMappingURL=config.types.d.ts.map
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Configuration types for the observability package.
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=config.types.js.map
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Logging types for the observability package.
3
+ */
4
+ /**
5
+ * Numeric log level hierarchy. Higher = more severe.
6
+ *
7
+ * A record is emitted when its level is at or above the logger's threshold,
8
+ * so a threshold of {@link LogLevel.OFF} silences everything.
9
+ */
10
+ export declare enum LogLevel {
11
+ TRACE = 0,
12
+ DEBUG = 1,
13
+ INFO = 2,
14
+ WARN = 3,
15
+ ERROR = 4,
16
+ FATAL = 5,
17
+ OFF = 6
18
+ }
19
+ /** Human-readable log level name. */
20
+ export type LogLevelName = "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "off";
21
+ /** A serialized view of a thrown value. */
22
+ export interface LogRecordError {
23
+ readonly name: string;
24
+ readonly message: string;
25
+ readonly stack?: string;
26
+ readonly cause?: unknown;
27
+ }
28
+ /** A structured log record produced by a logger. */
29
+ export interface LogRecord {
30
+ readonly level: LogLevel;
31
+ readonly levelName: LogLevelName;
32
+ readonly message: string;
33
+ readonly timestamp: Date;
34
+ readonly loggerName: string;
35
+ readonly context?: Record<string, unknown>;
36
+ readonly error?: LogRecordError;
37
+ /** Trace this record was emitted under, when a propagation context is active. */
38
+ readonly traceId?: string;
39
+ /** Span this record was emitted under, when a propagation context is active. */
40
+ readonly spanId?: string;
41
+ }
42
+ /** Structured logger interface. */
43
+ export interface Logger {
44
+ readonly name: string;
45
+ /** Current threshold. Changes when {@link Logger.setLevel} is called. */
46
+ readonly level: LogLevel;
47
+ trace(message: string, context?: Record<string, unknown>, error?: unknown): void;
48
+ debug(message: string, context?: Record<string, unknown>, error?: unknown): void;
49
+ info(message: string, context?: Record<string, unknown>, error?: unknown): void;
50
+ warn(message: string, context?: Record<string, unknown>, error?: unknown): void;
51
+ error(message: string, context?: Record<string, unknown>, error?: unknown): void;
52
+ fatal(message: string, context?: Record<string, unknown>, error?: unknown): void;
53
+ /** Creates a child logger with persistent context. */
54
+ child(name: string, context?: Record<string, unknown>): Logger;
55
+ /** Checks if a level would be logged. */
56
+ isLevelEnabled(level: LogLevel): boolean;
57
+ /**
58
+ * Change the threshold at runtime. Child loggers created before the change
59
+ * keep their own threshold.
60
+ */
61
+ setLevel(level: LogLevel): void;
62
+ /** Flushes any buffered log records. */
63
+ flush(): Promise<void>;
64
+ }
65
+ /** Options for creating a logger. */
66
+ export interface LoggerOptions {
67
+ readonly name: string;
68
+ readonly level?: LogLevel;
69
+ readonly context?: Record<string, unknown>;
70
+ readonly transport?: LogTransport;
71
+ /**
72
+ * Stamps `traceId` and `spanId` onto every record from the ambient
73
+ * propagation context. Defaults to `true`.
74
+ */
75
+ readonly correlate?: boolean;
76
+ /**
77
+ * Redacts the merged context and the error before the record is written.
78
+ * Applied by the logger, so every transport sees redacted records.
79
+ */
80
+ readonly redact?: (context: Record<string, unknown>) => Record<string, unknown>;
81
+ }
82
+ /** A log transport writes records to a destination. */
83
+ export interface LogTransport {
84
+ readonly name: string;
85
+ write(record: LogRecord): void | Promise<void>;
86
+ /** Drains anything buffered. Optional: a direct transport has nothing to do. */
87
+ flush?(): Promise<void>;
88
+ }
89
+ /** Exports log records to a backend. */
90
+ export interface LogExporter {
91
+ export(records: readonly LogRecord[]): Promise<void>;
92
+ shutdown(): Promise<void>;
93
+ }
94
+ //# sourceMappingURL=logging.types.d.ts.map
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Logging types for the observability package.
3
+ */
4
+ /**
5
+ * Numeric log level hierarchy. Higher = more severe.
6
+ *
7
+ * A record is emitted when its level is at or above the logger's threshold,
8
+ * so a threshold of {@link LogLevel.OFF} silences everything.
9
+ */
10
+ export var LogLevel;
11
+ (function (LogLevel) {
12
+ LogLevel[LogLevel["TRACE"] = 0] = "TRACE";
13
+ LogLevel[LogLevel["DEBUG"] = 1] = "DEBUG";
14
+ LogLevel[LogLevel["INFO"] = 2] = "INFO";
15
+ LogLevel[LogLevel["WARN"] = 3] = "WARN";
16
+ LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
17
+ LogLevel[LogLevel["FATAL"] = 5] = "FATAL";
18
+ LogLevel[LogLevel["OFF"] = 6] = "OFF";
19
+ })(LogLevel || (LogLevel = {}));
20
+ //# sourceMappingURL=logging.types.js.map
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Metrics types for the observability package.
3
+ */
4
+ /** A monotonically increasing counter. */
5
+ export interface Counter {
6
+ readonly name: string;
7
+ readonly labels?: Record<string, string>;
8
+ /**
9
+ * Adds to the counter. Rejects negative, NaN and infinite values by
10
+ * throwing — a counter that silently ignored them would report a number
11
+ * nobody can reconcile with the code that produced it.
12
+ */
13
+ increment(value?: number): void;
14
+ getValue(): number;
15
+ reset(): void;
16
+ }
17
+ /** A value that can go up and down. */
18
+ export interface Gauge {
19
+ readonly name: string;
20
+ readonly labels?: Record<string, string>;
21
+ setValue(value: number): void;
22
+ increment(value?: number): void;
23
+ decrement(value?: number): void;
24
+ getValue(): number;
25
+ reset(): void;
26
+ }
27
+ /** The distribution a histogram has observed. */
28
+ export interface HistogramValue {
29
+ readonly count: number;
30
+ readonly sum: number;
31
+ readonly min: number;
32
+ readonly max: number;
33
+ /** Mean of the observed values, or 0 when nothing has been recorded. */
34
+ readonly mean: number;
35
+ /** Cumulative bucket counts: every value `<= le` observed so far. */
36
+ readonly buckets: readonly {
37
+ readonly le: number;
38
+ readonly count: number;
39
+ }[];
40
+ /** Median, interpolated from the bucket boundaries. */
41
+ readonly p50: number;
42
+ readonly p90: number;
43
+ readonly p95: number;
44
+ readonly p99: number;
45
+ }
46
+ /** A distribution of observed values (latencies, sizes, etc.). */
47
+ export interface Histogram {
48
+ readonly name: string;
49
+ readonly labels?: Record<string, string>;
50
+ /** Records one observation. Rejects NaN and infinite values by throwing. */
51
+ record(value: number): void;
52
+ getValue(): HistogramValue;
53
+ /** Estimates a quantile from the bucket boundaries. `q` is in [0, 1]. */
54
+ percentile(q: number): number;
55
+ reset(): void;
56
+ }
57
+ /** Registry for all metrics. */
58
+ export interface MetricsRegistry {
59
+ counter(name: string, labels?: Record<string, string>): Counter;
60
+ gauge(name: string, labels?: Record<string, string>): Gauge;
61
+ histogram(name: string, labels?: Record<string, string>): Histogram;
62
+ /**
63
+ * Looks up one series. Omitting `labels` matches the unlabelled series;
64
+ * it does not mean "any labels", because returning an arbitrary one of
65
+ * several series is never what a caller wants.
66
+ */
67
+ getCounter(name: string, labels?: Record<string, string>): Counter | undefined;
68
+ getGauge(name: string, labels?: Record<string, string>): Gauge | undefined;
69
+ getHistogram(name: string, labels?: Record<string, string>): Histogram | undefined;
70
+ /** Every series registered under `name`, across all label sets. */
71
+ getSeries(name: string): readonly MetricSnapshot[];
72
+ getAll(): MetricSnapshot[];
73
+ /** Number of series currently held. */
74
+ size(): number;
75
+ reset(): void;
76
+ /** Drops every series. Unlike {@link MetricsRegistry.reset}, forgets them. */
77
+ clear(): void;
78
+ }
79
+ /** A point-in-time snapshot of a metric. */
80
+ export interface MetricSnapshot {
81
+ readonly name: string;
82
+ readonly type: "counter" | "gauge" | "histogram";
83
+ readonly value: number | HistogramValue;
84
+ readonly labels?: Record<string, string>;
85
+ /** When the snapshot was taken. */
86
+ readonly timestamp: Date;
87
+ }
88
+ /** Exports metric snapshots to a backend. */
89
+ export interface MetricExporter {
90
+ export(snapshots: readonly MetricSnapshot[]): Promise<void>;
91
+ shutdown(): Promise<void>;
92
+ }
93
+ //# sourceMappingURL=metrics.types.d.ts.map
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Metrics types for the observability package.
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=metrics.types.js.map
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Tracing types for the observability package.
3
+ */
4
+ /** Status of a span. */
5
+ export declare enum SpanStatus {
6
+ UNSET = "UNSET",
7
+ OK = "OK",
8
+ ERROR = "ERROR"
9
+ }
10
+ /**
11
+ * W3C trace flags. Bit 0 carries the sampling decision, which is what a
12
+ * downstream service reads to keep a trace whole.
13
+ */
14
+ export declare const TraceFlags: {
15
+ readonly NONE: 0;
16
+ readonly SAMPLED: 1;
17
+ };
18
+ /** A span event (timestamped annotation). */
19
+ export interface SpanEvent {
20
+ readonly name: string;
21
+ readonly timestamp: Date;
22
+ readonly attributes?: Record<string, unknown>;
23
+ }
24
+ /** Context identifying a specific span within a trace. */
25
+ export interface SpanContext {
26
+ readonly traceId: string;
27
+ readonly spanId: string;
28
+ readonly parentSpanId?: string;
29
+ /** @see TraceFlags */
30
+ readonly traceFlags?: number;
31
+ }
32
+ /** A single unit of work within a distributed trace. */
33
+ export interface Span {
34
+ readonly name: string;
35
+ readonly context: SpanContext;
36
+ readonly startTime: Date;
37
+ setAttribute(key: string, value: unknown): void;
38
+ addEvent(name: string, attributes?: Record<string, unknown>): void;
39
+ setStatus(status: SpanStatus, message?: string): void;
40
+ recordError(error: Error): void;
41
+ end(): void;
42
+ getDuration(): number;
43
+ isRecording(): boolean;
44
+ }
45
+ /** Creates new spans. */
46
+ export interface Tracer {
47
+ startSpan(name: string, options?: SpanOptions): Span;
48
+ }
49
+ /** Options for starting a span. */
50
+ export interface SpanOptions {
51
+ readonly parent?: SpanContext;
52
+ readonly attributes?: Record<string, unknown>;
53
+ readonly kind?: SpanKind;
54
+ }
55
+ /** Semantic kind of a span. */
56
+ export declare enum SpanKind {
57
+ INTERNAL = "INTERNAL",
58
+ SERVER = "SERVER",
59
+ CLIENT = "CLIENT",
60
+ PRODUCER = "PRODUCER",
61
+ CONSUMER = "CONSUMER"
62
+ }
63
+ /** Caps on what a single span may accumulate before export. */
64
+ export interface SpanLimits {
65
+ /** Maximum attributes retained. Default: 128. */
66
+ readonly maxAttributes?: number;
67
+ /** Maximum events retained. Default: 128. */
68
+ readonly maxEvents?: number;
69
+ /** Maximum attributes retained per event. Default: 128. */
70
+ readonly maxAttributesPerEvent?: number;
71
+ /** Strings longer than this are truncated. Default: 4096. */
72
+ readonly maxAttributeValueLength?: number;
73
+ }
74
+ /** Decision on whether a span should be recorded. */
75
+ export interface SamplingResult {
76
+ readonly decision: "RECORD_AND_SAMPLE" | "RECORD_ONLY" | "DO_NOT_RECORD";
77
+ readonly attributes?: Record<string, unknown>;
78
+ }
79
+ /** Determines which traces to sample. */
80
+ export interface Sampler {
81
+ shouldSample(parentContext?: SpanContext, traceId?: string): SamplingResult;
82
+ }
83
+ /** A span that has been completed and is ready for export. */
84
+ export interface ReadableSpan {
85
+ readonly name: string;
86
+ readonly context: SpanContext;
87
+ readonly kind: SpanKind;
88
+ readonly startTime: Date;
89
+ readonly endTime: Date;
90
+ readonly duration: number;
91
+ readonly status: SpanStatus;
92
+ /** The message passed alongside the status, when one was given. */
93
+ readonly statusMessage?: string;
94
+ readonly attributes: Record<string, unknown>;
95
+ readonly events: readonly SpanEvent[];
96
+ readonly resource: Record<string, unknown>;
97
+ /** Attributes discarded because the span hit its limit. */
98
+ readonly droppedAttributes: number;
99
+ /** Events discarded because the span hit its limit. */
100
+ readonly droppedEvents: number;
101
+ }
102
+ /** Exports completed spans to a backend. */
103
+ export interface SpanExporter {
104
+ export(spans: readonly ReadableSpan[]): Promise<void>;
105
+ shutdown(): Promise<void>;
106
+ }
107
+ /** Processes spans before export (batching, filtering, enrichment). */
108
+ export interface SpanProcessor {
109
+ onStart(span: Span): void;
110
+ onEnd(span: ReadableSpan): void;
111
+ /** Drains anything buffered without shutting the processor down. */
112
+ forceFlush?(): Promise<void>;
113
+ shutdown(): Promise<void>;
114
+ }
115
+ //# sourceMappingURL=tracing.types.d.ts.map
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Tracing types for the observability package.
3
+ */
4
+ /** Status of a span. */
5
+ export var SpanStatus;
6
+ (function (SpanStatus) {
7
+ SpanStatus["UNSET"] = "UNSET";
8
+ SpanStatus["OK"] = "OK";
9
+ SpanStatus["ERROR"] = "ERROR";
10
+ })(SpanStatus || (SpanStatus = {}));
11
+ /**
12
+ * W3C trace flags. Bit 0 carries the sampling decision, which is what a
13
+ * downstream service reads to keep a trace whole.
14
+ */
15
+ export const TraceFlags = {
16
+ NONE: 0,
17
+ SAMPLED: 1,
18
+ };
19
+ /** Semantic kind of a span. */
20
+ export var SpanKind;
21
+ (function (SpanKind) {
22
+ SpanKind["INTERNAL"] = "INTERNAL";
23
+ SpanKind["SERVER"] = "SERVER";
24
+ SpanKind["CLIENT"] = "CLIENT";
25
+ SpanKind["PRODUCER"] = "PRODUCER";
26
+ SpanKind["CONSUMER"] = "CONSUMER";
27
+ })(SpanKind || (SpanKind = {}));
28
+ //# sourceMappingURL=tracing.types.js.map