shodh-memory 0.2.0

Persistent cognitive memory for AI agents and robots — Hebbian learning, knowledge graph, spatial recall. Zenoh/ROS2 native. Single binary, runs offline.
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Production-grade metrics with Prometheus
//!
//! Exposes key operational metrics for monitoring and alerting:
//! - Request rates and latencies
//! - Memory usage and resource consumption
//! - Vector index performance
//! - Error rates and types
//!
//! NOTE: We intentionally avoid user_id in metric labels to prevent
//! high-cardinality explosion that can crash Prometheus.

use prometheus::{
    Histogram, HistogramOpts, HistogramVec, IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Opts,
    Registry,
};
use std::sync::{LazyLock, OnceLock};

/// Metrics initialization result
static METRICS_INIT: OnceLock<Result<(), MetricsError>> = OnceLock::new();

/// Error type for metrics initialization
#[derive(Debug, Clone)]
pub struct MetricsError {
    pub message: String,
}

impl std::fmt::Display for MetricsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Metrics initialization failed: {}", self.message)
    }
}

impl std::error::Error for MetricsError {}

/// Create histogram opts with standard latency buckets
fn latency_histogram_opts(name: &str, help: &str) -> HistogramOpts {
    HistogramOpts::new(name, help).buckets(vec![
        0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0,
    ])
}

/// Create histogram opts for fast operations (sub-millisecond)
fn fast_histogram_opts(name: &str, help: &str) -> HistogramOpts {
    HistogramOpts::new(name, help).buckets(vec![
        0.0001, 0.0005, 0.001, 0.0025, 0.005, 0.01, 0.025, 0.05,
    ])
}

/// Global metrics registry
pub static METRICS_REGISTRY: LazyLock<Registry> = LazyLock::new(Registry::new);

// ============================================================================
// Request Metrics
// ============================================================================

/// HTTP request duration in seconds
pub static HTTP_REQUEST_DURATION: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        latency_histogram_opts(
            "shodh_http_request_duration_seconds",
            "HTTP request duration in seconds",
        ),
        &["method", "endpoint", "status"],
    )
    .expect("HTTP_REQUEST_DURATION metric must be valid at compile time")
});

/// Total HTTP requests
pub static HTTP_REQUESTS_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new("shodh_http_requests_total", "Total HTTP requests"),
        &["method", "endpoint", "status"],
    )
    .expect("HTTP_REQUESTS_TOTAL metric must be valid at compile time")
});

// ============================================================================
// Memory Operation Metrics
// NOTE: No user_id in labels to prevent cardinality explosion
// ============================================================================

/// Memory store operations (record)
pub static MEMORY_STORE_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new("shodh_memory_store_total", "Total memory store operations"),
        &["result"],
    )
    .expect("MEMORY_STORE_TOTAL metric must be valid at compile time")
});

/// Memory store duration
pub static MEMORY_STORE_DURATION: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_memory_store_duration_seconds",
            "Memory store operation duration",
        )
        .buckets(vec![0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5]),
    )
    .expect("MEMORY_STORE_DURATION metric must be valid at compile time")
});

/// Memory retrieve operations
pub static MEMORY_RETRIEVE_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_memory_retrieve_total",
            "Total memory retrieve operations",
        ),
        &["retrieval_mode", "result"],
    )
    .expect("MEMORY_RETRIEVE_TOTAL metric must be valid at compile time")
});

/// Memory retrieve duration
pub static MEMORY_RETRIEVE_DURATION: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        HistogramOpts::new(
            "shodh_memory_retrieve_duration_seconds",
            "Memory retrieve operation duration",
        )
        .buckets(vec![0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0]),
        &["retrieval_mode"],
    )
    .expect("MEMORY_RETRIEVE_DURATION metric must be valid at compile time")
});

/// Results returned per query
pub static MEMORY_RETRIEVE_RESULTS: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        HistogramOpts::new(
            "shodh_memory_retrieve_results",
            "Number of results returned per query",
        )
        .buckets(vec![0.0, 1.0, 5.0, 10.0, 25.0, 50.0, 100.0]),
        &["retrieval_mode"],
    )
    .expect("MEMORY_RETRIEVE_RESULTS metric must be valid at compile time")
});

// ============================================================================
// Ontological Retrieval Metrics
// ============================================================================

/// Ontological intent confidence distribution per query
pub static ONTOLOGICAL_INTENT_CONFIDENCE: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_ontological_intent_confidence",
            "Distribution of inferred ontological intent confidence scores",
        )
        .buckets(vec![0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]),
    )
    .expect("ONTOLOGICAL_INTENT_CONFIDENCE metric must be valid at compile time")
});

/// Ontological re-rank boost applied to individual memories (Layer 4.9)
pub static ONTOLOGICAL_RERANK_BOOST_APPLIED: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_ontological_rerank_boost",
            "Distribution of ontological re-rank boost values applied to memories",
        )
        .buckets(vec![0.0, 0.02, 0.04, 0.08, 0.12, 0.16, 0.20, 0.25]),
    )
    .expect("ONTOLOGICAL_RERANK_BOOST_APPLIED metric must be valid at compile time")
});

/// Queries where ontological intent was below confidence threshold (fallback to unfiltered)
pub static ONTOLOGICAL_FALLBACK_TOTAL: LazyLock<IntCounter> = LazyLock::new(|| {
    IntCounter::new(
        "shodh_ontological_fallback_total",
        "Queries where ontological intent was below confidence threshold",
    )
    .expect("ONTOLOGICAL_FALLBACK_TOTAL metric must be valid at compile time")
});

/// Queries where ontological filtering was disabled due to high graph density
pub static ONTOLOGICAL_DENSITY_SKIP_TOTAL: LazyLock<IntCounter> = LazyLock::new(|| {
    IntCounter::new(
        "shodh_ontological_density_skip_total",
        "Queries where ontological filtering was disabled due to high graph density",
    )
    .expect("ONTOLOGICAL_DENSITY_SKIP_TOTAL metric must be valid at compile time")
});

// ============================================================================
// Embedding Metrics (P1.2: Instrument embed operations)
// ============================================================================

/// Embedding generation operations
pub static EMBEDDING_GENERATE_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_embedding_generate_total",
            "Total embedding generations",
        ),
        &["mode", "result"], // mode: "onnx" or "simplified"
    )
    .expect("EMBEDDING_GENERATE_TOTAL metric must be valid at compile time")
});

/// Embedding generation duration
pub static EMBEDDING_GENERATE_DURATION: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        HistogramOpts::new(
            "shodh_embedding_generate_duration_seconds",
            "Embedding generation duration",
        )
        .buckets(vec![
            0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 5.0,
        ]),
        &["mode"],
    )
    .expect("EMBEDDING_GENERATE_DURATION metric must be valid at compile time")
});

/// Embedding timeout count
pub static EMBEDDING_TIMEOUT_TOTAL: LazyLock<IntCounter> = LazyLock::new(|| {
    IntCounter::new(
        "shodh_embedding_timeout_total",
        "Total embedding generation timeouts",
    )
    .expect("EMBEDDING_TIMEOUT_TOTAL metric must be valid at compile time")
});

/// NER session lock timeout count
pub static NER_LOCK_TIMEOUT_TOTAL: LazyLock<IntCounter> = LazyLock::new(|| {
    IntCounter::new(
        "shodh_ner_lock_timeout_total",
        "Total NER session lock timeouts (degraded entity extraction)",
    )
    .expect("NER_LOCK_TIMEOUT_TOTAL metric must be valid at compile time")
});

// ============================================================================
// Memory Usage Metrics (aggregate, no per-user to avoid cardinality)
// ============================================================================

/// Active users in cache
pub static ACTIVE_USERS: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_active_users",
        "Number of users with active memory sessions",
    )
    .expect("ACTIVE_USERS metric must be valid at compile time")
});

/// Total memories stored by tier (aggregate across all users)
pub static MEMORIES_BY_TIER: LazyLock<IntGaugeVec> = LazyLock::new(|| {
    IntGaugeVec::new(
        Opts::new("shodh_memories_by_tier", "Total memories by tier"),
        &["tier"], // tier: "working", "session", "longterm"
    )
    .expect("MEMORIES_BY_TIER metric must be valid at compile time")
});

/// Total memory system heap usage (estimated, aggregate)
pub static MEMORY_HEAP_BYTES_TOTAL: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_memory_heap_bytes_total",
        "Total estimated heap usage across all users",
    )
    .expect("MEMORY_HEAP_BYTES_TOTAL metric must be valid at compile time")
});

// ============================================================================
// Vector Index Metrics (aggregate)
// ============================================================================

/// Total vector index size (number of vectors across all users)
pub static VECTOR_INDEX_SIZE_TOTAL: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_vector_index_size_total",
        "Total number of vectors in all indices",
    )
    .expect("VECTOR_INDEX_SIZE_TOTAL metric must be valid at compile time")
});

/// Vector search operations
pub static VECTOR_SEARCH_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_vector_search_total",
            "Total vector search operations",
        ),
        &["result"],
    )
    .expect("VECTOR_SEARCH_TOTAL metric must be valid at compile time")
});

/// Vector search duration
pub static VECTOR_SEARCH_DURATION: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(fast_histogram_opts(
        "shodh_vector_search_duration_seconds",
        "Vector search duration",
    ))
    .expect("VECTOR_SEARCH_DURATION metric must be valid at compile time")
});

// ============================================================================
// Storage Metrics
// ============================================================================

/// RocksDB operations
pub static ROCKSDB_OPS_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new("shodh_rocksdb_ops_total", "Total RocksDB operations"),
        &["operation", "result"], // operation: "get", "put", "delete"
    )
    .expect("ROCKSDB_OPS_TOTAL metric must be valid at compile time")
});

/// RocksDB operation duration
pub static ROCKSDB_OPS_DURATION: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        fast_histogram_opts(
            "shodh_rocksdb_ops_duration_seconds",
            "RocksDB operation duration",
        ),
        &["operation"],
    )
    .expect("ROCKSDB_OPS_DURATION metric must be valid at compile time")
});

/// Fallback deserialization branch hits (legacy migration observability)
pub static LEGACY_FALLBACK_BRANCH_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_legacy_fallback_branch_total",
            "Total fallback deserialization branch hits",
        ),
        &["branch"],
    )
    .expect("LEGACY_FALLBACK_BRANCH_TOTAL metric must be valid at compile time")
});

// ============================================================================
// Error Metrics
// ============================================================================

/// Total errors by type
pub static ERRORS_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new("shodh_errors_total", "Total errors by type"),
        &["error_type", "endpoint"],
    )
    .expect("ERRORS_TOTAL metric must be valid at compile time")
});

/// Resource limit rejections
pub static RESOURCE_LIMIT_REJECTIONS: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_resource_limit_rejections",
            "Requests rejected due to resource limits",
        ),
        &["resource"],
    )
    .expect("RESOURCE_LIMIT_REJECTIONS metric must be valid at compile time")
});

// ============================================================================
// Concurrency Metrics (P0.8)
// ============================================================================

/// Current concurrent requests
pub static CONCURRENT_REQUESTS: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_concurrent_requests",
        "Current number of concurrent requests",
    )
    .expect("CONCURRENT_REQUESTS metric must be valid at compile time")
});

/// Request queue size (if queuing implemented)
pub static REQUEST_QUEUE_SIZE: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new("shodh_request_queue_size", "Number of queued requests")
        .expect("REQUEST_QUEUE_SIZE metric must be valid at compile time")
});

// ============================================================================
// Hebbian Learning Metrics
// ============================================================================

/// Hebbian reinforcement operations
pub static HEBBIAN_REINFORCE_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_hebbian_reinforce_total",
            "Total Hebbian reinforcement operations",
        ),
        &["outcome", "result"], // outcome: "helpful", "misleading", "neutral"
    )
    .expect("HEBBIAN_REINFORCE_TOTAL metric must be valid at compile time")
});

/// Hebbian reinforcement duration
pub static HEBBIAN_REINFORCE_DURATION: LazyLock<HistogramVec> = LazyLock::new(|| {
    HistogramVec::new(
        HistogramOpts::new(
            "shodh_hebbian_reinforce_duration_seconds",
            "Hebbian reinforcement operation duration",
        )
        .buckets(vec![0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5]),
        &["outcome"],
    )
    .expect("HEBBIAN_REINFORCE_DURATION metric must be valid at compile time")
});

// ============================================================================
// Consolidation Metrics
// ============================================================================

/// Memory consolidation operations
pub static CONSOLIDATE_TOTAL: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_consolidate_total",
            "Total memory consolidation operations",
        ),
        &["result"],
    )
    .expect("CONSOLIDATE_TOTAL metric must be valid at compile time")
});

/// Memory consolidation duration
pub static CONSOLIDATE_DURATION: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_consolidate_duration_seconds",
            "Memory consolidation operation duration",
        )
        .buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]),
    )
    .expect("CONSOLIDATE_DURATION metric must be valid at compile time")
});

// ============================================================================
// Batch Operation Metrics
// ============================================================================

/// Batch store duration
pub static BATCH_STORE_DURATION: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_batch_store_duration_seconds",
            "Batch memory store operation duration",
        )
        .buckets(vec![0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0]),
    )
    .expect("BATCH_STORE_DURATION metric must be valid at compile time")
});

/// Batch store size
pub static BATCH_STORE_SIZE: LazyLock<Histogram> = LazyLock::new(|| {
    Histogram::with_opts(
        HistogramOpts::new(
            "shodh_batch_store_size",
            "Number of memories in batch store operations",
        )
        .buckets(vec![
            1.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0,
        ]),
    )
    .expect("BATCH_STORE_SIZE metric must be valid at compile time")
});

// ============================================================================
// Embedding Cache Metrics (SHO-68)
// ============================================================================

/// Embedding cache operations (query cache)
pub static EMBEDDING_CACHE_QUERY: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_embedding_cache_query_total",
            "Query embedding cache operations",
        ),
        &["result"], // result: "hit" or "miss"
    )
    .expect("EMBEDDING_CACHE_QUERY metric must be valid at compile time")
});

/// Embedding cache operations (content cache)
pub static EMBEDDING_CACHE_CONTENT: LazyLock<IntCounterVec> = LazyLock::new(|| {
    IntCounterVec::new(
        Opts::new(
            "shodh_embedding_cache_content_total",
            "Content embedding cache operations",
        ),
        &["result"], // result: "hit" or "miss"
    )
    .expect("EMBEDDING_CACHE_CONTENT metric must be valid at compile time")
});

/// Current cache size (query cache)
pub static EMBEDDING_CACHE_QUERY_SIZE: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_embedding_cache_query_size",
        "Current number of entries in query embedding cache",
    )
    .expect("EMBEDDING_CACHE_QUERY_SIZE metric must be valid at compile time")
});

/// Current cache size (content cache)
pub static EMBEDDING_CACHE_CONTENT_SIZE: LazyLock<IntGauge> = LazyLock::new(|| {
    IntGauge::new(
        "shodh_embedding_cache_content_size",
        "Current number of entries in content embedding cache",
    )
    .expect("EMBEDDING_CACHE_CONTENT_SIZE metric must be valid at compile time")
});

/// Register all metrics with the global registry
///
/// # Returns
/// - `Ok(())` if all metrics registered successfully
/// - `Err(MetricsError)` if any metric fails to register
///
/// # Behavior
/// - Registration is idempotent - calling multiple times is safe
/// - On failure, server should log warning and continue (degraded mode)
/// - Prometheus scraping will simply return empty metrics if registration failed
pub fn register_metrics() -> Result<(), MetricsError> {
    // Check if already initialized
    if let Some(result) = METRICS_INIT.get() {
        return result.clone();
    }

    let result = do_register_metrics();
    let _ = METRICS_INIT.set(result.clone());
    result
}

fn do_register_metrics() -> Result<(), MetricsError> {
    let mut errors = Vec::new();

    // Helper macro to reduce boilerplate
    macro_rules! register {
        ($metric:expr, $name:expr) => {
            if let Err(e) = METRICS_REGISTRY.register(Box::new($metric.clone())) {
                errors.push(format!("{}: {}", $name, e));
            }
        };
    }

    // Request metrics
    register!(HTTP_REQUEST_DURATION, "HTTP_REQUEST_DURATION");
    register!(HTTP_REQUESTS_TOTAL, "HTTP_REQUESTS_TOTAL");

    // Memory operation metrics
    register!(MEMORY_STORE_TOTAL, "MEMORY_STORE_TOTAL");
    register!(MEMORY_STORE_DURATION, "MEMORY_STORE_DURATION");
    register!(MEMORY_RETRIEVE_TOTAL, "MEMORY_RETRIEVE_TOTAL");
    register!(MEMORY_RETRIEVE_DURATION, "MEMORY_RETRIEVE_DURATION");
    register!(MEMORY_RETRIEVE_RESULTS, "MEMORY_RETRIEVE_RESULTS");

    // Ontological retrieval metrics
    register!(
        ONTOLOGICAL_INTENT_CONFIDENCE,
        "ONTOLOGICAL_INTENT_CONFIDENCE"
    );
    register!(
        ONTOLOGICAL_RERANK_BOOST_APPLIED,
        "ONTOLOGICAL_RERANK_BOOST_APPLIED"
    );
    register!(ONTOLOGICAL_FALLBACK_TOTAL, "ONTOLOGICAL_FALLBACK_TOTAL");
    register!(
        ONTOLOGICAL_DENSITY_SKIP_TOTAL,
        "ONTOLOGICAL_DENSITY_SKIP_TOTAL"
    );

    // Embedding metrics
    register!(EMBEDDING_GENERATE_TOTAL, "EMBEDDING_GENERATE_TOTAL");
    register!(EMBEDDING_GENERATE_DURATION, "EMBEDDING_GENERATE_DURATION");
    register!(EMBEDDING_TIMEOUT_TOTAL, "EMBEDDING_TIMEOUT_TOTAL");
    register!(NER_LOCK_TIMEOUT_TOTAL, "NER_LOCK_TIMEOUT_TOTAL");

    // Memory usage metrics (aggregate)
    register!(ACTIVE_USERS, "ACTIVE_USERS");
    register!(MEMORIES_BY_TIER, "MEMORIES_BY_TIER");
    register!(MEMORY_HEAP_BYTES_TOTAL, "MEMORY_HEAP_BYTES_TOTAL");

    // Vector index metrics (aggregate)
    register!(VECTOR_INDEX_SIZE_TOTAL, "VECTOR_INDEX_SIZE_TOTAL");
    register!(VECTOR_SEARCH_TOTAL, "VECTOR_SEARCH_TOTAL");
    register!(VECTOR_SEARCH_DURATION, "VECTOR_SEARCH_DURATION");

    // Storage metrics
    register!(ROCKSDB_OPS_TOTAL, "ROCKSDB_OPS_TOTAL");
    register!(ROCKSDB_OPS_DURATION, "ROCKSDB_OPS_DURATION");
    register!(LEGACY_FALLBACK_BRANCH_TOTAL, "LEGACY_FALLBACK_BRANCH_TOTAL");

    // Error metrics
    register!(ERRORS_TOTAL, "ERRORS_TOTAL");
    register!(RESOURCE_LIMIT_REJECTIONS, "RESOURCE_LIMIT_REJECTIONS");

    // Concurrency metrics
    register!(CONCURRENT_REQUESTS, "CONCURRENT_REQUESTS");
    register!(REQUEST_QUEUE_SIZE, "REQUEST_QUEUE_SIZE");

    // Hebbian learning metrics
    register!(HEBBIAN_REINFORCE_TOTAL, "HEBBIAN_REINFORCE_TOTAL");
    register!(HEBBIAN_REINFORCE_DURATION, "HEBBIAN_REINFORCE_DURATION");

    // Consolidation metrics
    register!(CONSOLIDATE_TOTAL, "CONSOLIDATE_TOTAL");
    register!(CONSOLIDATE_DURATION, "CONSOLIDATE_DURATION");

    // Batch operation metrics
    register!(BATCH_STORE_DURATION, "BATCH_STORE_DURATION");
    register!(BATCH_STORE_SIZE, "BATCH_STORE_SIZE");

    // Embedding cache metrics (SHO-68)
    register!(EMBEDDING_CACHE_QUERY, "EMBEDDING_CACHE_QUERY");
    register!(EMBEDDING_CACHE_CONTENT, "EMBEDDING_CACHE_CONTENT");
    register!(EMBEDDING_CACHE_QUERY_SIZE, "EMBEDDING_CACHE_QUERY_SIZE");
    register!(EMBEDDING_CACHE_CONTENT_SIZE, "EMBEDDING_CACHE_CONTENT_SIZE");

    if errors.is_empty() {
        Ok(())
    } else {
        Err(MetricsError {
            message: errors.join("; "),
        })
    }
}

/// Helper to time operations with histogram (RAII pattern)
/// Usage: let _timer = Timer::new(SOME_HISTOGRAM.clone());
pub struct Timer {
    histogram: Histogram,
    start: std::time::Instant,
}

impl Timer {
    /// Create timer that records duration to histogram on drop
    pub fn new(histogram: Histogram) -> Self {
        Self {
            histogram,
            start: std::time::Instant::now(),
        }
    }
}

impl Drop for Timer {
    fn drop(&mut self) {
        let duration = self.start.elapsed().as_secs_f64();
        self.histogram.observe(duration);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use prometheus::core::Metric;

    #[test]
    fn test_metrics_registration_is_idempotent() {
        // First registration should succeed
        let result1 = register_metrics();
        // Second registration should also succeed (returns cached result)
        let result2 = register_metrics();

        // Both should have same result
        assert_eq!(result1.is_ok(), result2.is_ok());
    }

    #[test]
    fn test_timer_records_duration() {
        // Create a test histogram
        let histogram = Histogram::with_opts(HistogramOpts::new(
            "test_timer_histogram",
            "Test histogram for timer",
        ))
        .unwrap();

        {
            let _timer = Timer::new(histogram.clone());
            std::thread::sleep(std::time::Duration::from_millis(10));
        }

        // Histogram should have recorded one observation
        let metric = histogram.metric();
        assert_eq!(metric.get_histogram().get_sample_count(), 1);
        // Duration should be at least 10ms
        assert!(metric.get_histogram().get_sample_sum() >= 0.01);
    }
}