opencrates 3.0.1

Enterprise-grade AI-powered Rust development companion with comprehensive automation, monitoring, and deployment capabilities
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
//! FIXED VERSION - Complete metrics system for OpenCrates

use anyhow::Result;
use prometheus::{
    proto::MetricFamily, Counter as PrometheusCounter, Encoder, Gauge as PrometheusGauge,
    Histogram as PrometheusHistogram, HistogramOpts, IntCounter, IntGauge, Opts, Registry,
    TextEncoder,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex as TokioMutex;
use tokio::sync::RwLock;
use tracing::{debug, error, instrument};
use crate::utils::error::OpenCratesError;
use once_cell::sync::Lazy;
#[cfg(feature = "metrics")]
use prometheus::{
    opts, register_counter_vec, register_gauge, register_histogram_vec, CounterVec, Gauge,
    HistogramVec,
};

// Add structs for with_opts methods
#[derive(Debug, Clone)]
pub struct CustomHistogramOpts {
    pub name: String,
    pub help: String,
    pub buckets: Option<Vec<f64>>,
}

#[derive(Debug, Clone)]
pub struct CustomGaugeOpts {
    pub name: String,
    pub help: String,
}

/// Custom error type for metrics
#[derive(Debug, thiserror::Error)]
pub enum MetricsError {
    #[error("Metric not found: {0}")]
    MetricNotFound(String),
    #[error("Invalid metric value: {0}")]
    InvalidValue(String),
    #[error("Prometheus error: {0}")]
    PrometheusError(String),
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Metric types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetricType {
    Counter,
    Gauge,
    Histogram,
    Summary,
}

/// Metric value types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value")]
pub enum MetricValue {
    Counter(u64),
    Gauge(f64),
    Histogram(HistogramData),
    Timer(TimerData),
}

/// FIXED: Histogram data structure
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HistogramData {
    pub buckets: Vec<f64>,
    pub counts: Vec<u64>,
    pub sum: f64,
    pub count: u64,
}

/// FIXED: Timer data structure
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TimerData {
    pub count: u64,
    pub sum: Duration,
    pub min: Duration,
    pub max: Duration,
    pub avg: Duration,
}

/// Metric labels
pub type Labels = HashMap<String, String>;

/// FIXED: Metric sample with proper error handling
#[derive(Debug, Clone, Serialize)]
pub struct MetricSample {
    pub name: String,
    pub labels: Labels,
    pub value: MetricValue,
    pub timestamp: SystemTime,
    pub help: Option<String>,
}

impl MetricSample {
    #[must_use]
    pub fn new(name: String, value: MetricValue) -> Self {
        Self {
            name,
            labels: HashMap::new(),
            value,
            timestamp: SystemTime::now(),
            help: None,
        }
    }

    #[must_use]
    pub fn with_labels(mut self, labels: Labels) -> Self {
        self.labels = labels;
        self
    }

    #[must_use]
    pub fn with_help(mut self, help: String) -> Self {
        self.help = Some(help);
        self
    }

    #[must_use]
    pub fn with_timestamp(mut self, timestamp: SystemTime) -> Self {
        self.timestamp = timestamp;
        self
    }
}

/// FIXED: Counter with proper thread safety
#[derive(Debug, Clone)]
pub struct Counter {
    value: Arc<RwLock<u64>>,
    name: String,
    help: String,
    labels: Labels,
}

impl Counter {
    #[must_use]
    pub fn new(name: String, help: String) -> Self {
        Self {
            value: Arc::new(RwLock::new(0)),
            name,
            help,
            labels: HashMap::new(),
        }
    }

    #[must_use]
    pub fn with_labels(mut self, labels: HashMap<String, String>) -> Self {
        self.labels = labels;
        self
    }

    pub async fn increment(&self) -> Result<(), MetricsError> {
        let mut value = self.value.write().await;
        *value = value
            .checked_add(1)
            .ok_or_else(|| MetricsError::InvalidValue("Counter overflow".to_string()))?;
        Ok(())
    }

    pub async fn add(&self, amount: u64) -> Result<(), MetricsError> {
        let mut value = self.value.write().await;
        *value = value
            .checked_add(amount)
            .ok_or_else(|| MetricsError::InvalidValue("Counter overflow".to_string()))?;
        Ok(())
    }

    pub async fn get(&self) -> u64 {
        *self.value.read().await
    }

    pub async fn reset(&self) {
        let mut value = self.value.write().await;
        *value = 0;
    }

    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[must_use]
    pub fn help(&self) -> &str {
        &self.help
    }

    #[must_use]
    pub fn labels(&self) -> &Labels {
        &self.labels
    }
}

/// FIXED: Gauge with validation
#[derive(Debug, Clone)]
pub struct Gauge {
    value: Arc<RwLock<f64>>,
    name: String,
    help: String,
    labels: Labels,
}

impl Gauge {
    #[must_use]
    pub fn new(name: String, help: String) -> Self {
        Self {
            value: Arc::new(RwLock::new(0.0)),
            name,
            help,
            labels: HashMap::new(),
        }
    }

    #[must_use]
    pub fn with_labels(mut self, labels: HashMap<String, String>) -> Self {
        self.labels = labels;
        self
    }

    pub async fn set(&self, value: f64) -> Result<(), MetricsError> {
        if !value.is_finite() {
            return Err(MetricsError::InvalidValue(
                "Gauge value must be finite".to_string(),
            ));
        }
        let mut v = self.value.write().await;
        *v = value;
        Ok(())
    }

    pub async fn increment(&self) -> Result<(), MetricsError> {
        self.add(1.0).await
    }

    pub async fn decrement(&self) -> Result<(), MetricsError> {
        self.add(-1.0).await
    }

    pub async fn add(&self, amount: f64) -> Result<(), MetricsError> {
        if !amount.is_finite() {
            return Err(MetricsError::InvalidValue(
                "Amount must be finite".to_string(),
            ));
        }
        let mut value = self.value.write().await;
        let new_value = *value + amount;
        if !new_value.is_finite() {
            return Err(MetricsError::InvalidValue(
                "Resulting gauge value would be infinite".to_string(),
            ));
        }
        *value = new_value;
        Ok(())
    }

    pub async fn get(&self) -> f64 {
        *self.value.read().await
    }

    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[must_use]
    pub fn help(&self) -> &str {
        &self.help
    }

    #[must_use]
    pub fn labels(&self) -> &Labels {
        &self.labels
    }
}

/// FIXED: Histogram with proper bucket logic
#[derive(Debug, Clone)]
pub struct Histogram {
    name: String,
    buckets: Vec<f64>,
    counts: Arc<RwLock<Vec<u64>>>,
    sum: Arc<RwLock<f64>>,
    count: Arc<RwLock<u64>>,
    labels: Labels,
    help: Option<String>,
}

impl Histogram {
    #[must_use]
    pub fn new(name: String) -> Self {
        Self::with_buckets(name, Self::default_buckets())
    }

    #[must_use]
    pub fn with_buckets(name: String, mut buckets: Vec<f64>) -> Self {
        // FIXED: Ensure buckets are sorted
        buckets.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let counts = vec![0; buckets.len() + 1];
        Self {
            name,
            buckets: buckets.clone(),
            counts: Arc::new(RwLock::new(counts)),
            sum: Arc::new(RwLock::new(0.0)),
            count: Arc::new(RwLock::new(0)),
            labels: HashMap::new(),
            help: None,
        }
    }

    #[must_use]
    pub fn with_help(mut self, help: String) -> Self {
        self.help = Some(help);
        self
    }

    #[must_use]
    pub fn with_labels(mut self, labels: Labels) -> Self {
        self.labels = labels;
        self
    }

    fn default_buckets() -> Vec<f64> {
        vec![
            0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
        ]
    }

    /// FIXED: Proper bucket counting logic
    pub async fn observe(&self, value: f64) -> Result<(), MetricsError> {
        if !value.is_finite() {
            return Err(MetricsError::InvalidValue(
                "Histogram value must be finite".to_string(),
            ));
        }

        // Update sum and count atomically
        {
            let mut sum = self.sum.write().await;
            *sum += value;
        }
        {
            let mut count = self.count.write().await;
            *count += 1;
        }

        // FIXED: Update bucket counts correctly
        {
            let mut counts = self.counts.write().await;
            let mut index = self.buckets.len();
            for (i, &bucket) in self.buckets.iter().enumerate() {
                if value <= bucket {
                    index = i;
                    break;
                }
            }
            counts[index] += 1;
        }

        Ok(())
    }

    pub async fn time<F, R>(&self, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let start = Instant::now();
        let result = f();
        let duration = start.elapsed();
        let _ = self.observe(duration.as_secs_f64()).await; // Ignore errors in timing
        result
    }

    pub async fn time_async<F, Fut, R>(&self, f: F) -> R
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = R>,
    {
        let start = Instant::now();
        let result = f().await;
        let duration = start.elapsed();
        let _ = self.observe(duration.as_secs_f64()).await; // FIXED: Added await
        result
    }

    /// Retrieves the sum of all observed values.
    pub async fn get_sum(&self) -> f64 {
        *self.sum.read().await
    }

    /// Retrieves the total number of observations.
    pub async fn get_count(&self) -> u64 {
        *self.count.read().await
    }

    /// Retrieves the counts for each bucket.
    pub async fn bucket_counts(&self) -> Vec<u64> {
        self.counts.read().await.clone()
    }

    /// Get the name of the histogram
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the buckets used by this histogram
    #[must_use]
    pub fn buckets(&self) -> &[f64] {
        &self.buckets
    }

    /// Get the help text for this histogram
    #[must_use]
    pub fn help(&self) -> Option<&str> {
        self.help.as_deref()
    }

    /// Get the labels for this histogram
    #[must_use]
    pub fn labels(&self) -> &Labels {
        &self.labels
    }
}

/// Timer utility for measuring durations
#[derive(Debug)]
pub struct Timer {
    start: Instant,
    histogram: Option<Arc<Histogram>>,
}

impl Timer {
    #[must_use]
    pub fn new() -> Self {
        Self {
            start: Instant::now(),
            histogram: None,
        }
    }

    #[must_use]
    pub fn with_histogram(histogram: Arc<Histogram>) -> Self {
        Self {
            start: Instant::now(),
            histogram: Some(histogram),
        }
    }

    #[must_use]
    pub fn elapsed(&self) -> Duration {
        self.start.elapsed()
    }

    pub async fn finish(self) -> Duration {
        let duration = self.elapsed();
        if let Some(histogram) = &self.histogram {
            let _ = histogram.observe(duration.as_secs_f64()).await;
        }
        duration
    }
}

impl Default for Timer {
    fn default() -> Self {
        Self::new()
    }
}

/// Token usage tracking for API calls
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct TokenUsage {
    pub prompt_tokens: usize,
    pub completion_tokens: usize,
    pub total_tokens: usize,
}

impl TokenUsage {
    #[must_use]
    pub fn new(prompt_tokens: usize, completion_tokens: usize) -> Self {
        Self {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens + completion_tokens,
        }
    }
}

/// Provider metrics for tracking LLM usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderMetrics {
    pub provider_name: String,
    pub model_name: String,
    pub duration: Duration,
    pub usage: Option<TokenUsage>,
    #[cfg(feature = "metrics")]
    pub requests: CounterVec,
    #[cfg(feature = "metrics")]
    pub latency: HistogramVec,
    #[cfg(feature = "metrics")]
    pub errors: CounterVec,
}

impl ProviderMetrics {
    #[cfg(feature = "metrics")]
    pub fn new(provider_name: &str) -> Self {
        let requests = register_counter_vec!(
            opts!("opencrates_provider_requests_total", "Total number of requests"),
            &["provider_name"]
        )
        .unwrap();

        let latency = register_histogram_vec!(
            opts!("opencrates_provider_latency_seconds", "Latency of requests"),
            &["provider_name"]
        )
        .unwrap();

        let errors = register_counter_vec!(
            opts!("opencrates_provider_errors_total", "Total number of errors"),
            &["provider_name"]
        )
        .unwrap();

        Self {
            provider_name: provider_name.to_string(),
            model_name: String::new(),
            duration: Duration::default(),
            usage: None,
            requests,
            latency,
            errors,
        }
    }

    #[cfg(not(feature = "metrics"))]
    pub fn new(provider_name: &str) -> Self {
        Self {
            provider_name: provider_name.to_string(),
            model_name: String::new(),
            duration: Duration::default(),
            usage: None,
        }
    }

    #[instrument(skip(self))]
    pub fn record_request(&self) {
        #[cfg(feature = "metrics")]
        self.requests.with_label_values(&[&self.provider_name]).inc();
    }

    #[instrument(skip(self))]
    pub fn record_latency(&self, duration: f64) {
        #[cfg(feature = "metrics")]
        self.latency.with_label_values(&[&self.provider_name]).observe(duration);
    }

    #[instrument(skip(self))]
    pub fn record_error(&self) {
        #[cfg(feature = "metrics")]
        self.errors.with_label_values(&[&self.provider_name]).inc();
    }
}

/// Comprehensive metrics registry
#[derive(Debug, Clone)]
pub struct MetricRegistry {
    counters: Arc<RwLock<HashMap<String, Arc<Counter>>>>,
    gauges: Arc<RwLock<HashMap<String, Arc<Gauge>>>>,
    histograms: Arc<RwLock<HashMap<String, Arc<Histogram>>>>,
    prometheus_registry: Arc<Registry>,
}

impl MetricRegistry {
    #[must_use]
    pub fn new() -> Self {
        Self {
            counters: Arc::new(RwLock::new(HashMap::new())),
            gauges: Arc::new(RwLock::new(HashMap::new())),
            histograms: Arc::new(RwLock::new(HashMap::new())),
            prometheus_registry: Arc::new(Registry::new()),
        }
    }

    pub async fn register_counter(
        &self,
        name: &str,
        help: &str,
    ) -> Result<Arc<Counter>, MetricsError> {
        let counter = Arc::new(Counter::new(name.to_string(), help.to_string()));
        self.counters
            .write()
            .await
            .insert(name.to_string(), counter.clone());
        Ok(counter)
    }

    pub async fn register_gauge(&self, name: &str, help: &str) -> Result<Arc<Gauge>, MetricsError> {
        let gauge = Arc::new(Gauge::new(name.to_string(), help.to_string()));
        self.gauges
            .write()
            .await
            .insert(name.to_string(), gauge.clone());
        Ok(gauge)
    }

    pub async fn register_histogram(
        &self,
        name: &str,
        help: &str,
    ) -> Result<Arc<Histogram>, MetricsError> {
        let histogram = Arc::new(Histogram::new(name.to_string()).with_help(help.to_string()));
        self.histograms
            .write()
            .await
            .insert(name.to_string(), histogram.clone());
        Ok(histogram)
    }

    pub async fn get_counter(&self, name: &str) -> Option<Arc<Counter>> {
        self.counters.read().await.get(name).cloned()
    }

    pub async fn get_gauge(&self, name: &str) -> Option<Arc<Gauge>> {
        self.gauges.read().await.get(name).cloned()
    }

    pub async fn get_histogram(&self, name: &str) -> Option<Arc<Histogram>> {
        self.histograms.read().await.get(name).cloned()
    }

    pub async fn collect_metrics(&self) -> Vec<MetricSample> {
        let mut samples = Vec::new();

        // Collect counter metrics
        for (name, counter) in self.counters.read().await.iter() {
            let value = counter.get().await;
            samples.push(MetricSample::new(name.clone(), MetricValue::Counter(value)));
        }

        // Collect gauge metrics
        for (name, gauge) in self.gauges.read().await.iter() {
            let value = gauge.get().await;
            samples.push(MetricSample::new(name.clone(), MetricValue::Gauge(value)));
        }

        // Collect histogram metrics
        for (name, histogram) in self.histograms.read().await.iter() {
            let buckets = histogram.buckets().to_vec();
            let counts = histogram.bucket_counts().await;
            let sum = histogram.get_sum().await;
            let count = histogram.get_count().await;

            let histogram_data = HistogramData {
                buckets,
                counts,
                sum,
                count,
            };

            samples.push(MetricSample::new(
                name.clone(),
                MetricValue::Histogram(histogram_data),
            ));
        }

        samples
    }

    pub async fn export_prometheus(&self) -> Result<String, MetricsError> {
        let encoder = TextEncoder::new();
        let metric_families = self.prometheus_registry.gather();

        encoder
            .encode_to_string(&metric_families)
            .map_err(|e| MetricsError::PrometheusError(e.to_string()))
    }
}

impl Default for MetricRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// OpenCrates-specific metrics collection
#[derive(Debug, Clone)]
pub struct OpenCratesMetrics {
    registry: Arc<MetricRegistry>,
    // Core metrics
    pub crate_generations: Arc<Counter>,
    pub api_requests: Arc<Counter>,
    pub generation_duration: Arc<Histogram>,
    pub active_connections: Arc<Gauge>,
    pub cache_hits: Arc<Counter>,
    pub cache_misses: Arc<Counter>,
}

impl OpenCratesMetrics {
    pub async fn new() -> Result<Self, MetricsError> {
        let registry = Arc::new(MetricRegistry::new());

        let crate_generations = registry
            .register_counter(
                "opencrates_crate_generations_total",
                "Total number of crate generations",
            )
            .await?;

        let api_requests = registry
            .register_counter(
                "opencrates_api_requests_total",
                "Total number of API requests",
            )
            .await?;

        let generation_duration = registry
            .register_histogram(
                "opencrates_generation_duration_seconds",
                "Duration of crate generation in seconds",
            )
            .await?;

        let active_connections = registry
            .register_gauge(
                "opencrates_active_connections",
                "Number of active connections",
            )
            .await?;

        let cache_hits = registry
            .register_counter("opencrates_cache_hits_total", "Total number of cache hits")
            .await?;

        let cache_misses = registry
            .register_counter(
                "opencrates_cache_misses_total",
                "Total number of cache misses",
            )
            .await?;

        Ok(Self {
            registry,
            crate_generations,
            api_requests,
            generation_duration,
            active_connections,
            cache_hits,
            cache_misses,
        })
    }

    pub async fn record_generation(&self, duration: Duration) -> Result<(), MetricsError> {
        self.crate_generations.increment().await?;
        self.generation_duration
            .observe(duration.as_secs_f64())
            .await?;
        Ok(())
    }

    pub async fn record_api_request(&self) -> Result<(), MetricsError> {
        self.api_requests.increment().await
    }

    pub async fn set_active_connections(&self, count: i64) -> Result<(), MetricsError> {
        self.active_connections.set(count as f64).await
    }

    pub async fn record_cache_hit(&self) -> Result<(), MetricsError> {
        self.cache_hits.increment().await
    }

    pub async fn record_cache_miss(&self) -> Result<(), MetricsError> {
        self.cache_misses.increment().await
    }

    #[must_use]
    pub fn registry(&self) -> &Arc<MetricRegistry> {
        &self.registry
    }

    pub async fn export_metrics(&self) -> Result<String, MetricsError> {
        let samples = self.registry.collect_metrics().await;
        serde_json::to_string_pretty(&samples).map_err(MetricsError::SerializationError)
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SystemMetrics {
    pub cpu_usage: f32,
    pub memory_usage_mb: u64,
}

#[cfg(feature = "metrics")]
pub static CRATE_GENERATION_COUNTER: Lazy<CounterVec> = Lazy::new(|| {
    register_counter_vec!(
        opts!("opencrates_crate_generations_total", "Total number of crate generations"),
        &[]
    )
    .unwrap()
});

#[cfg(feature = "metrics")]
pub static ACTIVE_AGENTS_GAUGE: Lazy<Gauge> =
    Lazy::new(|| register_gauge!(opts!("opencrates_active_agents", "Active AI agents")).unwrap());