@atrim/instrument-node 0.6.0 → 0.7.0-b9eaf74-20260108193056

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,278 @@
1
+ import { Layer, Supervisor, Effect, Context, Option, Fiber, Exit, FiberRef } from 'effect';
2
+ import { AutoInstrumentationConfig, InstrumentationConfig } from '@atrim/instrument-core';
3
+ export { AutoInstrumentationConfig } from '@atrim/instrument-core';
4
+
5
+ /**
6
+ * Auto-Tracing Supervisor for Effect-TS
7
+ *
8
+ * Provides automatic tracing of all Effect fibers without manual Effect.withSpan() calls.
9
+ * Uses Effect's Supervisor API to intercept fiber creation/termination and create
10
+ * OpenTelemetry spans automatically.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { AutoTracingLive } from '@atrim/instrument-node/effect/auto'
15
+ *
16
+ * const program = Effect.gen(function* () {
17
+ * yield* doWork() // Automatically traced!
18
+ * }).pipe(Effect.provide(AutoTracingLive))
19
+ * ```
20
+ */
21
+
22
+ /**
23
+ * FiberRef to enable/disable auto-tracing for specific fibers
24
+ * Use withoutAutoTracing() to disable
25
+ */
26
+ declare const AutoTracingEnabled: FiberRef.FiberRef<boolean>;
27
+ /**
28
+ * FiberRef to override auto-generated span name
29
+ * Use setSpanName() to override
30
+ */
31
+ declare const AutoTracingSpanName: FiberRef.FiberRef<Option.Option<string>>;
32
+ /**
33
+ * Supervisor that automatically creates OpenTelemetry spans for all Effect fibers
34
+ *
35
+ * This supervisor intercepts fiber creation and termination, creating spans
36
+ * based on configuration from instrumentation.yaml.
37
+ */
38
+ declare class AutoTracingSupervisor extends Supervisor.AbstractSupervisor<void> {
39
+ private readonly config;
40
+ private readonly fiberSpans;
41
+ private readonly fiberStartTimes;
42
+ private readonly tracer;
43
+ private readonly includePatterns;
44
+ private readonly excludePatterns;
45
+ private activeFiberCount;
46
+ constructor(config: AutoInstrumentationConfig);
47
+ /**
48
+ * Returns the current value (void for this supervisor)
49
+ */
50
+ get value(): Effect.Effect<void>;
51
+ /**
52
+ * Called when a fiber starts executing
53
+ */
54
+ 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;
55
+ /**
56
+ * Called when a fiber completes (success or failure)
57
+ */
58
+ onEnd<A, E>(exit: Exit.Exit<A, E>, fiber: Fiber.RuntimeFiber<A, E>): void;
59
+ /**
60
+ * Check if a span name should be traced based on filter patterns
61
+ */
62
+ private shouldTrace;
63
+ /**
64
+ * Get initial span attributes for a fiber
65
+ */
66
+ private getInitialAttributes;
67
+ /**
68
+ * Parse stack trace to get source info
69
+ */
70
+ private parseStackTrace;
71
+ /**
72
+ * Parse min_duration string to nanoseconds
73
+ */
74
+ private parseMinDuration;
75
+ }
76
+ /**
77
+ * Create a custom AutoTracingSupervisor with the given config
78
+ */
79
+ declare const createAutoTracingSupervisor: (config: AutoInstrumentationConfig) => AutoTracingSupervisor;
80
+ /**
81
+ * Layer that provides auto-tracing with custom configuration
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const CustomAutoTracing = createAutoTracingLayer({
86
+ * enabled: true,
87
+ * span_naming: {
88
+ * default: 'app.{function}',
89
+ * rules: [{ match: { file: 'src/services/.*' }, name: 'service.{function}' }]
90
+ * }
91
+ * })
92
+ * ```
93
+ */
94
+ declare const createAutoTracingLayer: (options?: {
95
+ config?: AutoInstrumentationConfig;
96
+ }) => Layer.Layer<never>;
97
+ /**
98
+ * Zero-config auto-tracing layer
99
+ *
100
+ * Loads configuration from instrumentation.yaml and automatically traces
101
+ * all Effect fibers based on the configuration.
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * import { AutoTracingLive } from '@atrim/instrument-node/effect/auto'
106
+ *
107
+ * const program = Effect.gen(function* () {
108
+ * yield* doWork() // Automatically traced!
109
+ * }).pipe(Effect.provide(AutoTracingLive))
110
+ * ```
111
+ */
112
+ declare const AutoTracingLive: Layer.Layer<never>;
113
+ /**
114
+ * Disable auto-tracing for a specific Effect
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * const program = Effect.gen(function* () {
119
+ * yield* publicWork() // Traced
120
+ * yield* withoutAutoTracing(internalWork()) // NOT traced
121
+ * })
122
+ * ```
123
+ */
124
+ declare const withoutAutoTracing: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
125
+ /**
126
+ * Override the auto-generated span name for a specific Effect
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const program = Effect.gen(function* () {
131
+ * yield* setSpanName('custom.operation.name')(myEffect)
132
+ * })
133
+ * ```
134
+ */
135
+ declare const setSpanName: (name: string) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
136
+
137
+ /**
138
+ * Node.js configuration loader
139
+ *
140
+ * Provides configuration loading using native Node.js APIs (fs, fetch)
141
+ * This module doesn't require Effect Platform, making it work without Effect installed.
142
+ */
143
+
144
+ /**
145
+ * Legacy options interface for backward compatibility
146
+ */
147
+ interface ConfigLoaderOptions {
148
+ configPath?: string;
149
+ configUrl?: string;
150
+ config?: InstrumentationConfig;
151
+ cacheTimeout?: number;
152
+ }
153
+
154
+ /**
155
+ * Auto-Tracing Configuration Loader
156
+ *
157
+ * Loads auto-tracing configuration from instrumentation.yaml
158
+ * and provides a typed Effect service for accessing it.
159
+ */
160
+
161
+ /**
162
+ * Default auto-tracing configuration when not specified in instrumentation.yaml
163
+ */
164
+ declare const defaultAutoTracingConfig: AutoInstrumentationConfig;
165
+ declare const AutoTracingConfig_base: Context.TagClass<AutoTracingConfig, "AutoTracingConfig", {
166
+ enabled: boolean;
167
+ filter: {
168
+ include: string[];
169
+ exclude: string[];
170
+ };
171
+ granularity: "fiber" | "operator";
172
+ span_naming: {
173
+ default: string;
174
+ infer_from_source: boolean;
175
+ rules: {
176
+ match: {
177
+ function?: string | undefined;
178
+ file?: string | undefined;
179
+ module?: string | undefined;
180
+ };
181
+ name: string;
182
+ }[];
183
+ };
184
+ performance: {
185
+ sampling_rate: number;
186
+ min_duration: string;
187
+ max_concurrent: number;
188
+ };
189
+ metadata: {
190
+ fiber_info: boolean;
191
+ source_location: boolean;
192
+ parent_fiber: boolean;
193
+ };
194
+ }>;
195
+ /**
196
+ * Service tag for auto-tracing configuration
197
+ */
198
+ declare class AutoTracingConfig extends AutoTracingConfig_base {
199
+ }
200
+ /**
201
+ * Load auto-tracing configuration from instrumentation.yaml
202
+ *
203
+ * Returns the config from effect.auto_instrumentation section,
204
+ * or defaults if not specified.
205
+ */
206
+ declare const loadAutoTracingConfig: (options?: ConfigLoaderOptions) => Effect.Effect<AutoInstrumentationConfig, never, never>;
207
+ /**
208
+ * Load auto-tracing configuration synchronously from cache or default
209
+ *
210
+ * Note: This is a synchronous fallback when Effect runtime isn't available.
211
+ * Prefer loadAutoTracingConfig() when possible.
212
+ */
213
+ declare const loadAutoTracingConfigSync: () => AutoInstrumentationConfig;
214
+ /**
215
+ * Layer that provides auto-tracing configuration
216
+ */
217
+ declare const AutoTracingConfigLive: Layer.Layer<AutoTracingConfig, never, never>;
218
+ /**
219
+ * Layer that provides custom auto-tracing configuration
220
+ */
221
+ declare const AutoTracingConfigLayer: (config: AutoInstrumentationConfig) => Layer.Layer<AutoTracingConfig>;
222
+
223
+ /**
224
+ * Span Name Inference for Auto-Tracing
225
+ *
226
+ * Provides intelligent span naming based on source code information
227
+ * and configuration rules from instrumentation.yaml.
228
+ */
229
+
230
+ /**
231
+ * Source code information extracted from stack traces
232
+ */
233
+ interface SourceInfo {
234
+ /** Function name (or 'anonymous') */
235
+ function: string;
236
+ /** Full file path */
237
+ file: string;
238
+ /** Line number */
239
+ line: number;
240
+ /** Column number */
241
+ column: number;
242
+ }
243
+ /**
244
+ * Template variables available for span naming
245
+ */
246
+ interface TemplateVariables {
247
+ fiber_id: string;
248
+ function: string;
249
+ module: string;
250
+ file: string;
251
+ line: string;
252
+ operator: string;
253
+ [key: string]: string;
254
+ }
255
+ /**
256
+ * Infer a span name based on fiber ID, source info, and configuration
257
+ *
258
+ * Priority:
259
+ * 1. Match against naming rules (first match wins)
260
+ * 2. Use default template with source info if available
261
+ * 3. Fallback to default template with fiber ID only
262
+ *
263
+ * @param fiberId - The fiber's numeric ID
264
+ * @param sourceInfo - Optional source code information from stack trace
265
+ * @param config - Auto-instrumentation configuration
266
+ * @returns The inferred span name
267
+ */
268
+ declare function inferSpanName(fiberId: number, sourceInfo: SourceInfo | undefined, config: AutoInstrumentationConfig): string;
269
+ /**
270
+ * Sanitize a span name to be OpenTelemetry compliant
271
+ *
272
+ * - Replaces invalid characters with underscores
273
+ * - Ensures name is not empty
274
+ * - Truncates to reasonable length
275
+ */
276
+ declare function sanitizeSpanName(name: string): string;
277
+
278
+ export { AutoTracingConfig, AutoTracingConfigLayer, AutoTracingConfigLive, AutoTracingEnabled, AutoTracingLive, AutoTracingSpanName, AutoTracingSupervisor, type SourceInfo, type TemplateVariables, createAutoTracingLayer, createAutoTracingSupervisor, defaultAutoTracingConfig, inferSpanName, loadAutoTracingConfig, loadAutoTracingConfigSync, sanitizeSpanName, setSpanName, withoutAutoTracing };