@atrim/instrument-node 0.7.0 → 0.7.1-dev.14fdea7.20260108224013

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.
@@ -0,0 +1,359 @@
1
+ import { Layer, Supervisor, Effect, Context, Option, Fiber, Exit, FiberRef } from 'effect';
2
+ import * as OtelApi from '@opentelemetry/api';
3
+ import { AutoInstrumentationConfig, InstrumentationConfig } from '@atrim/instrument-core';
4
+ export { AutoInstrumentationConfig } from '@atrim/instrument-core';
5
+
6
+ /**
7
+ * Auto-Tracing Supervisor for Effect-TS
8
+ *
9
+ * Provides automatic tracing of all Effect fibers without manual Effect.withSpan() calls.
10
+ * Uses Effect's Supervisor API to intercept fiber creation/termination and create
11
+ * OpenTelemetry spans automatically.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { AutoTracingLive } from '@atrim/instrument-node/effect/auto'
16
+ *
17
+ * const program = Effect.gen(function* () {
18
+ * yield* doWork() // Automatically traced!
19
+ * }).pipe(Effect.provide(AutoTracingLive))
20
+ * ```
21
+ */
22
+
23
+ /**
24
+ * FiberRef to enable/disable auto-tracing for specific fibers
25
+ * Use withoutAutoTracing() to disable
26
+ */
27
+ declare const AutoTracingEnabled: FiberRef.FiberRef<boolean>;
28
+ /**
29
+ * FiberRef to override auto-generated span name
30
+ * Use setSpanName() to override
31
+ */
32
+ declare const AutoTracingSpanName: FiberRef.FiberRef<Option.Option<string>>;
33
+ /**
34
+ * Supervisor that automatically creates OpenTelemetry spans for all Effect fibers
35
+ *
36
+ * This supervisor intercepts fiber creation and termination, creating spans
37
+ * based on configuration from instrumentation.yaml.
38
+ */
39
+ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void> {
40
+ private readonly config;
41
+ private readonly fiberSpans;
42
+ private readonly fiberStartTimes;
43
+ private _tracer;
44
+ private readonly includePatterns;
45
+ private readonly excludePatterns;
46
+ private activeFiberCount;
47
+ private _rootSpan;
48
+ constructor(config: AutoInstrumentationConfig);
49
+ /**
50
+ * Set the root span for parent context propagation
51
+ */
52
+ setRootSpan(span: OtelApi.Span): void;
53
+ /**
54
+ * Get the tracer lazily - this allows time for the NodeSdk layer to register the global provider
55
+ */
56
+ private get tracer();
57
+ /**
58
+ * Returns the current value (void for this supervisor)
59
+ */
60
+ get value(): Effect.Effect<void>;
61
+ /**
62
+ * Called when a fiber starts executing
63
+ */
64
+ onStart<A, E, R>(_context: Context.Context<R>, _effect: Effect.Effect<A, E, R>, parent: Option.Option<Fiber.RuntimeFiber<unknown, unknown>>, fiber: Fiber.RuntimeFiber<A, E>): void;
65
+ /**
66
+ * Called when a fiber completes (success or failure)
67
+ */
68
+ onEnd<A, E>(exit: Exit.Exit<A, E>, fiber: Fiber.RuntimeFiber<A, E>): void;
69
+ /**
70
+ * Check if a span name should be traced based on filter patterns
71
+ */
72
+ private shouldTrace;
73
+ /**
74
+ * Get initial span attributes for a fiber
75
+ */
76
+ private getInitialAttributes;
77
+ /**
78
+ * Parse stack trace to get source info
79
+ */
80
+ private parseStackTrace;
81
+ /**
82
+ * Parse min_duration string to nanoseconds
83
+ */
84
+ private parseMinDuration;
85
+ }
86
+ /**
87
+ * Create a custom AutoTracingSupervisor with the given config
88
+ */
89
+ declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig) => AutoTracingSupervisor;
90
+ /**
91
+ * Layer that provides auto-tracing with custom configuration
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * const CustomAutoTracing = createAutoTracingLayer({
96
+ * enabled: true,
97
+ * span_naming: {
98
+ * default: 'app.{function}',
99
+ * rules: [{ match: { file: 'src/services/.*' }, name: 'service.{function}' }]
100
+ * }
101
+ * })
102
+ * ```
103
+ */
104
+ declare const createAutoTracingLayer: (options?: {
105
+ config?: AutoInstrumentationConfig;
106
+ }) => Layer.Layer<never>;
107
+ /**
108
+ * Wrap an Effect with auto-tracing supervision
109
+ *
110
+ * This creates a span for the main effect AND supervises all child fibers.
111
+ * Use this when you need to ensure all fibers in an effect are traced.
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const program = Effect.gen(function* () {
116
+ * yield* doWork() // Automatically traced!
117
+ * })
118
+ *
119
+ * const tracedProgram = withAutoTracing(program, { enabled: true, ... })
120
+ * Effect.runPromise(tracedProgram)
121
+ * ```
122
+ */
123
+ declare const withAutoTracing: <A, E, R>(effect: Effect.Effect<A, E, R>, config: AutoInstrumentationConfig, mainSpanName?: string) => Effect.Effect<A, E, R>;
124
+ /**
125
+ * Zero-config auto-tracing layer
126
+ *
127
+ * Loads configuration from instrumentation.yaml and automatically traces
128
+ * all Effect fibers based on the configuration.
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * import { AutoTracingLive } from '@atrim/instrument-node/effect/auto'
133
+ *
134
+ * const program = Effect.gen(function* () {
135
+ * yield* doWork() // Automatically traced!
136
+ * }).pipe(Effect.provide(AutoTracingLive))
137
+ * ```
138
+ */
139
+ declare const AutoTracingLive: Layer.Layer<never>;
140
+ /**
141
+ * Disable auto-tracing for a specific Effect
142
+ *
143
+ * @example
144
+ * ```typescript
145
+ * const program = Effect.gen(function* () {
146
+ * yield* publicWork() // Traced
147
+ * yield* withoutAutoTracing(internalWork()) // NOT traced
148
+ * })
149
+ * ```
150
+ */
151
+ declare const withoutAutoTracing: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
152
+ /**
153
+ * Override the auto-generated span name for a specific Effect
154
+ *
155
+ * @example
156
+ * ```typescript
157
+ * const program = Effect.gen(function* () {
158
+ * yield* setSpanName('custom.operation.name')(myEffect)
159
+ * })
160
+ * ```
161
+ */
162
+ declare const setSpanName: (name: string) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
163
+ /**
164
+ * Create a fully YAML-driven auto-instrumentation layer
165
+ *
166
+ * This layer reads all configuration from instrumentation.yaml including:
167
+ * - Exporter configuration (type, endpoint, processor)
168
+ * - Auto-tracing configuration (naming rules, filters, performance)
169
+ * - Service metadata (name, version)
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * import { createFullAutoTracingLayer } from '@atrim/instrument-node/effect/auto'
174
+ *
175
+ * // Everything configured via instrumentation.yaml - no code config needed!
176
+ * const AppLive = createFullAutoTracingLayer()
177
+ *
178
+ * Effect.runPromise(program.pipe(Effect.provide(AppLive)))
179
+ * ```
180
+ */
181
+ declare const createFullAutoTracingLayer: () => Layer.Layer<never>;
182
+ /**
183
+ * Fully YAML-driven auto-instrumentation layer
184
+ *
185
+ * This is the recommended way to use auto-instrumentation. All configuration
186
+ * comes from instrumentation.yaml - no code configuration needed.
187
+ *
188
+ * @example
189
+ * ```yaml
190
+ * # instrumentation.yaml
191
+ * effect:
192
+ * auto_instrumentation:
193
+ * enabled: true
194
+ * span_naming:
195
+ * default: "effect.{function}"
196
+ * rules:
197
+ * - match: { function: "internal.*" }
198
+ * name: "internal.{function}"
199
+ * filter:
200
+ * exclude:
201
+ * - "^internal\\."
202
+ * exporter_config:
203
+ * type: otlp # or 'console' for dev
204
+ * endpoint: http://localhost:4318
205
+ * processor: batch # or 'simple' for dev
206
+ * ```
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * import { FullAutoTracingLive } from '@atrim/instrument-node/effect/auto'
211
+ *
212
+ * // Just provide the layer - everything else from YAML!
213
+ * Effect.runPromise(program.pipe(Effect.provide(FullAutoTracingLive)))
214
+ * ```
215
+ */
216
+ declare const FullAutoTracingLive: Layer.Layer<never>;
217
+
218
+ /**
219
+ * Node.js configuration loader
220
+ *
221
+ * Provides configuration loading using native Node.js APIs (fs, fetch)
222
+ * This module doesn't require Effect Platform, making it work without Effect installed.
223
+ */
224
+
225
+ /**
226
+ * Legacy options interface for backward compatibility
227
+ */
228
+ interface ConfigLoaderOptions {
229
+ configPath?: string;
230
+ configUrl?: string;
231
+ config?: InstrumentationConfig;
232
+ cacheTimeout?: number;
233
+ }
234
+
235
+ /**
236
+ * Auto-Tracing Configuration Loader
237
+ *
238
+ * Loads auto-tracing configuration from instrumentation.yaml
239
+ * and provides a typed Effect service for accessing it.
240
+ */
241
+
242
+ /**
243
+ * Default auto-tracing configuration when not specified in instrumentation.yaml
244
+ */
245
+ declare const defaultAutoTracingConfig: AutoInstrumentationConfig;
246
+ declare const AutoTracingConfig_base: Context.TagClass<AutoTracingConfig, "AutoTracingConfig", {
247
+ enabled: boolean;
248
+ filter: {
249
+ include: string[];
250
+ exclude: string[];
251
+ };
252
+ granularity: "fiber" | "operator";
253
+ span_naming: {
254
+ default: string;
255
+ infer_from_source: boolean;
256
+ rules: {
257
+ match: {
258
+ function?: string | undefined;
259
+ file?: string | undefined;
260
+ module?: string | undefined;
261
+ };
262
+ name: string;
263
+ }[];
264
+ };
265
+ performance: {
266
+ sampling_rate: number;
267
+ min_duration: string;
268
+ max_concurrent: number;
269
+ };
270
+ metadata: {
271
+ fiber_info: boolean;
272
+ source_location: boolean;
273
+ parent_fiber: boolean;
274
+ };
275
+ }>;
276
+ /**
277
+ * Service tag for auto-tracing configuration
278
+ */
279
+ declare class AutoTracingConfig extends AutoTracingConfig_base {
280
+ }
281
+ /**
282
+ * Load auto-tracing configuration from instrumentation.yaml
283
+ *
284
+ * Returns the config from effect.auto_instrumentation section,
285
+ * or defaults if not specified.
286
+ */
287
+ declare const loadAutoTracingConfig: (options?: ConfigLoaderOptions) => Effect.Effect<AutoInstrumentationConfig, never, never>;
288
+ /**
289
+ * Load auto-tracing configuration synchronously from cache or default
290
+ *
291
+ * Note: This is a synchronous fallback when Effect runtime isn't available.
292
+ * Prefer loadAutoTracingConfig() when possible.
293
+ */
294
+ declare const loadAutoTracingConfigSync: () => AutoInstrumentationConfig;
295
+ /**
296
+ * Layer that provides auto-tracing configuration
297
+ */
298
+ declare const AutoTracingConfigLive: Layer.Layer<AutoTracingConfig, never, never>;
299
+ /**
300
+ * Layer that provides custom auto-tracing configuration
301
+ */
302
+ declare const AutoTracingConfigLayer: (config: AutoInstrumentationConfig) => Layer.Layer<AutoTracingConfig>;
303
+
304
+ /**
305
+ * Span Name Inference for Auto-Tracing
306
+ *
307
+ * Provides intelligent span naming based on source code information
308
+ * and configuration rules from instrumentation.yaml.
309
+ */
310
+
311
+ /**
312
+ * Source code information extracted from stack traces
313
+ */
314
+ interface SourceInfo {
315
+ /** Function name (or 'anonymous') */
316
+ function: string;
317
+ /** Full file path */
318
+ file: string;
319
+ /** Line number */
320
+ line: number;
321
+ /** Column number */
322
+ column: number;
323
+ }
324
+ /**
325
+ * Template variables available for span naming
326
+ */
327
+ interface TemplateVariables {
328
+ fiber_id: string;
329
+ function: string;
330
+ module: string;
331
+ file: string;
332
+ line: string;
333
+ operator: string;
334
+ [key: string]: string;
335
+ }
336
+ /**
337
+ * Infer a span name based on fiber ID, source info, and configuration
338
+ *
339
+ * Priority:
340
+ * 1. Match against naming rules (first match wins)
341
+ * 2. Use default template with source info if available
342
+ * 3. Fallback to default template with fiber ID only
343
+ *
344
+ * @param fiberId - The fiber's numeric ID
345
+ * @param sourceInfo - Optional source code information from stack trace
346
+ * @param config - Auto-instrumentation configuration
347
+ * @returns The inferred span name
348
+ */
349
+ declare function inferSpanName(fiberId: number, sourceInfo: SourceInfo | undefined, config: AutoInstrumentationConfig): string;
350
+ /**
351
+ * Sanitize a span name to be OpenTelemetry compliant
352
+ *
353
+ * - Replaces invalid characters with underscores
354
+ * - Ensures name is not empty
355
+ * - Truncates to reasonable length
356
+ */
357
+ declare function sanitizeSpanName(name: string): string;
358
+
359
+ export { AutoTracingConfig, AutoTracingConfigLayer, AutoTracingConfigLive, AutoTracingEnabled, AutoTracingLive, AutoTracingSpanName, AutoTracingSupervisor, FullAutoTracingLive, type SourceInfo, type TemplateVariables, createAutoTracingLayer, createAutoTracingSupervisor, createFullAutoTracingLayer, defaultAutoTracingConfig, inferSpanName, loadAutoTracingConfig, loadAutoTracingConfigSync, sanitizeSpanName, setSpanName, withAutoTracing, withoutAutoTracing };