embeddenator-obs 0.21.0

Observability: metrics, logging, and tracing for Embeddenator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Embeddenator-Obs Integration Guide

This guide shows how to integrate embeddenator-obs into other Embeddenator components.

## Quick Integration Checklist

- [ ] Add dependency to Cargo.toml
- [ ] Initialize observability at startup
- [ ] Add metrics to critical paths
- [ ] Instrument functions with spans
- [ ] Add test metrics to benchmarks
- [ ] Configure logging for development

## Step 1: Add Dependency

### In Component Cargo.toml

```toml
[dependencies]
embeddenator-obs = { version = "0.20.0-alpha.1", features = ["metrics"] }

# For development builds, add tracing
[dev-dependencies]
embeddenator-obs = { version = "0.20.0-alpha.1", features = ["full"] }
```

### For Local Development

```toml
[patch."https://github.com/tzervas/embeddenator-obs"]
embeddenator-obs = { path = "../embeddenator-obs" }
```

## Step 2: Initialize at Startup

### In main.rs or lib.rs

```rust
use embeddenator_obs::init_tracing;

fn main() {
    // Initialize once at startup
    init_tracing();
    
    // Your application code
}
```

### Configuration via Environment

```bash
# Set log level
export EMBEDDENATOR_LOG=info
export RUST_LOG=debug

# Set output format
export EMBEDDENATOR_LOG_FORMAT=json
```

## Step 3: Add Metrics to Critical Paths

### Example: VSA Operations

```rust
use embeddenator_obs::metrics;

pub fn bind(a: &Vector, b: &Vector) -> Vector {
    // Increment operation counter
    metrics().inc_sub_cache_miss();
    
    // Your implementation
    let result = compute_bind(a, b);
    
    result
}

pub fn bundle(a: &Vector, b: &Vector) -> Vector {
    metrics().inc_sub_cache_hit();
    // Implementation
}
```

### Example: Retrieval Operations

```rust
use embeddenator_obs::metrics;
use std::time::Instant;

pub fn query(index: &Index, query: &Vector, k: usize) -> Vec<Result> {
    let start = Instant::now();
    
    // Perform query
    let results = index.search(query, k);
    
    // Record timing
    metrics().record_retrieval_query(start.elapsed());
    
    results
}

pub fn rerank(candidates: &[Result], query: &Vector) -> Vec<Result> {
    let start = Instant::now();
    
    // Reranking logic
    let reranked = // ...
    
    metrics().record_rerank(start.elapsed());
    
    reranked
}
```

## Step 4: Instrument with Spans

### Function-Level Instrumentation

```rust
use embeddenator_obs::create_span;

pub fn process_batch(items: &[Item]) -> Result<Vec<Output>> {
    let _span = create_span("process_batch", &[
        ("item_count", &items.len().to_string())
    ]);
    
    // Your implementation
    // Span automatically records timing on drop
}
```

### Nested Spans

```rust
use embeddenator_obs::{create_span, create_debug_span};

pub fn complex_operation(data: &Data) -> Result<Output> {
    let _outer = create_span("complex_operation", &[]);
    
    {
        let _phase1 = create_debug_span("phase1_preprocessing", &[]);
        preprocess(data)?;
    }
    
    {
        let _phase2 = create_debug_span("phase2_computation", &[]);
        compute(data)?;
    }
    
    {
        let _phase3 = create_debug_span("phase3_postprocessing", &[]);
        postprocess(data)
    }
}
```

## Step 5: Add Test Metrics to Benchmarks

### Basic Performance Test

```rust
use embeddenator_obs::TestMetrics;

#[test]
fn benchmark_bind_operation() {
    let mut metrics = TestMetrics::new("bind");
    
    let a = create_test_vector(768);
    let b = create_test_vector(768);
    
    // Time 100 iterations
    for _ in 0..100 {
        metrics.start_timing();
        let _result = bind(&a, &b);
        metrics.stop_timing();
    }
    
    let stats = metrics.timing_stats();
    
    // Assert performance requirements
    assert!(stats.mean_ns < 10_000, "Bind too slow: {:.2}µs", stats.mean_ns / 1000.0);
    assert!(stats.p99_ns < 20_000, "P99 too high: {:.2}µs", stats.p99_ns as f64 / 1000.0);
    
    // Print summary
    println!("{}", metrics.summary());
}
```

### Advanced Benchmarking

```rust
use embeddenator_obs::TestMetrics;

#[test]
fn comprehensive_benchmark() {
    let mut metrics = TestMetrics::new("comprehensive_test");
    
    let iterations = 1000;
    let mut total_items = 0;
    
    for i in 0..iterations {
        let size = 100 + i;
        let data = generate_test_data(size);
        
        let result = metrics.time_operation(|| {
            process(&data)
        });
        
        total_items += result.len();
        metrics.inc_op("processed");
        
        if i % 100 == 0 {
            metrics.record_memory(estimate_memory_usage());
        }
    }
    
    // Record custom metrics
    metrics.record_metric("items_per_op", total_items as f64 / iterations as f64);
    metrics.record_metric("throughput_items_per_sec", 
        total_items as f64 / (metrics.timing_stats().total_ns as f64 / 1e9));
    
    // Verify and print
    println!("{}", metrics.summary());
    
    let stats = metrics.timing_stats();
    assert!(stats.ops_per_sec() >= 1000.0, "Throughput too low");
}
```

## Step 6: Telemetry for Production Monitoring

### Setup Telemetry Collector

```rust
use embeddenator_obs::{Telemetry, TelemetryConfig};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

// Create shared telemetry instance
lazy_static! {
    static ref TELEMETRY: Arc<Mutex<Telemetry>> = 
        Arc::new(Mutex::new(Telemetry::default_config()));
}

// Background export thread
fn start_telemetry_export() {
    let telemetry = TELEMETRY.clone();
    
    thread::spawn(move || {
        loop {
            thread::sleep(Duration::from_secs(60));
            
            let snapshot = telemetry.lock().unwrap().snapshot();
            
            // Export to monitoring system
            export_to_prometheus(&snapshot);
            // or
            send_to_cloudwatch(&snapshot.to_json());
        }
    });
}
```

### Record Operations

```rust
fn handle_request(req: Request) -> Response {
    let start = Instant::now();
    
    let response = process_request(req);
    
    TELEMETRY.lock().unwrap()
        .record_operation("handle_request", start.elapsed().as_micros() as u64);
    
    TELEMETRY.lock().unwrap()
        .increment_counter("requests_total");
    
    response
}
```

## Integration Patterns by Component

### embeddenator-vsa

```rust
// In lib.rs
pub use embeddenator_obs::{metrics, create_span};

// In operations
pub fn bind(a: &Vector, b: &Vector) -> Vector {
    let _span = create_span("vsa_bind", &[("dim", &a.dim().to_string())]);
    metrics().inc_sub_cache_miss();
    
    // Implementation
}

pub fn bundle(a: &Vector, b: &Vector) -> Vector {
    let _span = create_span("vsa_bundle", &[]);
    metrics().inc_sub_cache_hit();
    
    // Implementation
}
```

### embeddenator-retrieval

```rust
use embeddenator_obs::{metrics, create_span};
use std::time::Instant;

pub fn hierarchical_query(
    index: &HierarchicalIndex,
    query: &Vector,
    k: usize
) -> Vec<Result> {
    let _span = create_span("hier_query", &[("k", &k.to_string())]);
    
    let start = Instant::now();
    let results = index.query_with_hierarchy(query, k);
    metrics().record_hier_query(start.elapsed());
    
    results
}
```

### embeddenator-fs

```rust
use embeddenator_obs::{metrics, create_span, info};

pub fn scan_directory(path: &Path) -> Result<Vec<Entry>> {
    let _span = create_span("fs_scan", &[("path", &path.display().to_string())]);
    
    info(&format!("Scanning directory: {}", path.display()));
    
    let entries = read_dir_recursive(path)?;
    
    metrics().inc_index_cache_hit();
    
    Ok(entries)
}
```

## Testing Observability Integration

### Unit Test

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use embeddenator_obs::metrics;
    
    #[test]
    fn test_metrics_recorded() {
        let before = metrics().snapshot();
        
        // Call instrumented function
        let _ = my_function();
        
        let after = metrics().snapshot();
        
        // Verify metrics changed
        #[cfg(feature = "metrics")]
        assert!(after.sub_cache_hits > before.sub_cache_hits);
    }
}
```

### Integration Test

```rust
#[test]
fn test_full_observability_stack() {
    use embeddenator_obs::{init_tracing, metrics, TestMetrics};
    
    init_tracing();
    
    let mut test_metrics = TestMetrics::new("integration");
    
    test_metrics.time_operation(|| {
        // Perform operations
        my_function();
        
        // Verify metrics
        let snapshot = metrics().snapshot();
        assert!(snapshot.sub_cache_hits > 0 || snapshot.sub_cache_misses > 0);
    });
    
    println!("{}", test_metrics.summary());
}
```

## Troubleshooting

### Metrics Not Recording

```rust
// Check if metrics feature is enabled
#[cfg(feature = "metrics")]
println!("Metrics enabled");

#[cfg(not(feature = "metrics"))]
println!("Metrics disabled - no-op");
```

### No Tracing Output

```bash
# Enable tracing
export EMBEDDENATOR_LOG=debug
export RUST_LOG=trace

# Run your program
cargo run
```

### Performance Regression

```rust
// Add benchmark to catch regressions
#[test]
fn performance_baseline() {
    let mut metrics = TestMetrics::new("baseline");
    
    for _ in 0..1000 {
        metrics.time_operation(|| {
            // Critical path
        });
    }
    
    let stats = metrics.timing_stats();
    
    // Set baseline thresholds
    assert!(stats.p50_ns < 1000, "P50 regression");
    assert!(stats.p99_ns < 5000, "P99 regression");
}
```

## Best Practices

1. **Initialize Once**: Call `init_tracing()` once at startup
2. **Metrics in Production**: Use metrics for production monitoring
3. **Spans for Development**: Use spans for debugging and development
4. **TestMetrics for Benchmarks**: Use TestMetrics for performance testing
5. **Feature Gates**: Respect feature gates for zero-cost in release
6. **Avoid Allocations**: Metrics are lock-free and allocation-free
7. **Span Scope**: Let spans drop naturally for automatic timing
8. **Counter Increments**: Use for counting, not for timing
9. **Duration Recording**: Use for operation timing
10. **Telemetry Export**: Export periodically, not per-operation

## Performance Targets

| Operation | Target Overhead | Actual (Release) |
|-----------|----------------|------------------|
| Counter increment | <20ns | 4ns ✅ |
| Span creation | <200ns | 6ns ✅ |
| TestMetrics timing | <100ns | 24ns ✅ |
| Hi-res measurement | <100ns | 20ns ✅ |

## Additional Resources

- [README.md]README.md - Full documentation
- [MIGRATION_SUMMARY.md]MIGRATION_SUMMARY.md - Migration details
- [examples/performance_benchmark.rs]examples/performance_benchmark.rs - Performance benchmarks
- [tests/integration_test.rs]tests/integration_test.rs - Integration examples

## Support

For issues or questions, see the main Embeddenator repository.