aprender-cbtop 0.63.0

Compute Block Top - Real-time load testing and hardware monitoring TUI
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
//! Observability Backend Integrations (PMAT-046)
//!
//! Export metrics and traces to observability platforms.
//!
//! # Design
//!
//! - Datadog integration (DogStatsD protocol)
//! - New Relic Metrics API
//! - Honeycomb events and traces
//! - OpenTelemetry collector (OTLP)
//! - Generic webhook for custom backends
//!
//! # Falsification (FKR-047)
//!
//! Hâ‚€: Cannot export metrics to 3+ backends simultaneously
//! Test: Configure all backends, verify each receives metrics

mod types;
pub use types::*;

use std::collections::HashMap;
use std::time::Instant;

/// Configuration for the observability exporter
#[derive(Debug, Clone, Default)]
pub struct ObservabilityConfig {
    /// Datadog configuration
    pub datadog: Option<DatadogConfig>,
    /// New Relic configuration
    pub newrelic: Option<NewRelicConfig>,
    /// Honeycomb configuration
    pub honeycomb: Option<HoneycombConfig>,
    /// OTLP configuration
    pub otlp: Option<OtlpConfig>,
    /// Webhook configuration
    pub webhook: Option<WebhookConfig>,
    /// Batch size for exports
    pub batch_size: usize,
    /// Flush interval in milliseconds
    pub flush_interval_ms: u64,
}

impl ObservabilityConfig {
    /// Create new configuration
    pub fn new() -> Self {
        Self {
            batch_size: 100,
            flush_interval_ms: 10_000,
            ..Default::default()
        }
    }

    /// Enable Datadog
    pub fn with_datadog(mut self, config: DatadogConfig) -> Self {
        self.datadog = Some(config);
        self
    }

    /// Enable New Relic
    pub fn with_newrelic(mut self, config: NewRelicConfig) -> Self {
        self.newrelic = Some(config);
        self
    }

    /// Enable Honeycomb
    pub fn with_honeycomb(mut self, config: HoneycombConfig) -> Self {
        self.honeycomb = Some(config);
        self
    }

    /// Enable OTLP
    pub fn with_otlp(mut self, config: OtlpConfig) -> Self {
        self.otlp = Some(config);
        self
    }

    /// Enable webhook
    pub fn with_webhook(mut self, config: WebhookConfig) -> Self {
        self.webhook = Some(config);
        self
    }

    /// Get list of enabled backends
    pub fn enabled_backends(&self) -> Vec<ObservabilityBackend> {
        let mut backends = Vec::new();
        if self.datadog.is_some() {
            backends.push(ObservabilityBackend::Datadog);
        }
        if self.newrelic.is_some() {
            backends.push(ObservabilityBackend::NewRelic);
        }
        if self.honeycomb.is_some() {
            backends.push(ObservabilityBackend::Honeycomb);
        }
        if self.otlp.is_some() {
            backends.push(ObservabilityBackend::Otlp);
        }
        if self.webhook.is_some() {
            backends.push(ObservabilityBackend::Webhook);
        }
        backends
    }
}

/// Observability exporter for multiple backends
#[derive(Debug)]
pub struct ObservabilityExporter {
    /// Configuration
    config: ObservabilityConfig,
    /// Pending metrics buffer
    buffer: Vec<ExportMetric>,
    /// Backend health status
    health: HashMap<ObservabilityBackend, BackendHealth>,
    /// Total exports per backend
    export_counts: HashMap<ObservabilityBackend, u64>,
    /// Last flush time
    last_flush: Instant,
}

impl ObservabilityExporter {
    /// Create a new observability exporter
    pub fn new(config: ObservabilityConfig) -> Self {
        let mut health = HashMap::new();

        // Initialize health for each enabled backend
        for backend in config.enabled_backends() {
            health.insert(
                backend,
                BackendHealth {
                    backend,
                    healthy: true,
                    last_success: None,
                    consecutive_failures: 0,
                    avg_latency_ms: 0.0,
                },
            );
        }

        Self {
            config,
            buffer: Vec::new(),
            health,
            export_counts: HashMap::new(),
            last_flush: Instant::now(),
        }
    }

    /// Add a metric to the buffer
    pub fn record(&mut self, metric: ExportMetric) {
        self.buffer.push(metric);

        // Auto-flush if buffer is full
        if self.buffer.len() >= self.config.batch_size {
            let _ = self.flush();
        }
    }

    /// Record multiple metrics
    pub fn record_batch(&mut self, metrics: Vec<ExportMetric>) {
        for metric in metrics {
            self.record(metric);
        }
    }

    /// Flush buffer to all backends
    pub fn flush(&mut self) -> Vec<ExportResult> {
        if self.buffer.is_empty() {
            return Vec::new();
        }

        let metrics = std::mem::take(&mut self.buffer);
        let mut results = Vec::new();

        // Export to each enabled backend
        for backend in self.config.enabled_backends() {
            let result = self.export_to_backend(backend, &metrics);
            self.update_health(backend, &result);
            results.push(result);
        }

        self.last_flush = Instant::now();
        results
    }

    /// Export metrics to a specific backend
    fn export_to_backend(
        &mut self,
        backend: ObservabilityBackend,
        metrics: &[ExportMetric],
    ) -> ExportResult {
        let start = Instant::now();

        // Simulate export (in production, this would make actual HTTP/UDP calls)
        let (success, error) = match backend {
            ObservabilityBackend::Datadog => self.export_to_datadog(metrics),
            ObservabilityBackend::NewRelic => self.export_to_newrelic(metrics),
            ObservabilityBackend::Honeycomb => self.export_to_honeycomb(metrics),
            ObservabilityBackend::Otlp => self.export_to_otlp(metrics),
            ObservabilityBackend::Webhook => self.export_to_webhook(metrics),
        };

        let duration_ms = start.elapsed().as_millis() as u64;

        if success {
            *self.export_counts.entry(backend).or_insert(0) += metrics.len() as u64;
        }

        ExportResult {
            backend,
            success,
            metrics_exported: if success { metrics.len() } else { 0 },
            duration_ms,
            error,
        }
    }

    /// Export to Datadog (DogStatsD format)
    fn export_to_datadog(&self, metrics: &[ExportMetric]) -> (bool, Option<String>) {
        let Some(config) = &self.config.datadog else {
            return (false, Some("Datadog not configured".to_string()));
        };

        // Format metrics in DogStatsD format
        let mut formatted = Vec::new();
        for metric in metrics {
            let tags: Vec<String> = metric
                .tags
                .iter()
                .map(|(k, v)| format!("{}:{}", k, v))
                .chain(config.default_tags.iter().cloned())
                .collect();

            let tag_str = if tags.is_empty() {
                String::new()
            } else {
                format!("|#{}", tags.join(","))
            };

            let metric_type = match metric.metric_type {
                MetricExportType::Gauge => "g",
                MetricExportType::Counter => "c",
                MetricExportType::Histogram => "h",
            };

            formatted.push(format!(
                "{}.{}:{}|{}{}",
                config.prefix, metric.name, metric.value, metric_type, tag_str
            ));
        }

        // In production, send to UDP socket at config.host:config.port
        let _ = formatted;
        (true, None)
    }

    /// Export to New Relic (Metrics API)
    fn export_to_newrelic(&self, metrics: &[ExportMetric]) -> (bool, Option<String>) {
        let Some(_config) = &self.config.newrelic else {
            return (false, Some("New Relic not configured".to_string()));
        };

        // Format metrics for New Relic Metrics API
        let mut payload = Vec::new();
        for metric in metrics {
            let metric_data = format!(
                r#"{{"name":"{}","type":"{}","value":{},"timestamp":{}}}"#,
                metric.name,
                match metric.metric_type {
                    MetricExportType::Gauge => "gauge",
                    MetricExportType::Counter => "count",
                    MetricExportType::Histogram => "summary",
                },
                metric.value,
                metric.timestamp_ns / 1_000_000_000
            );
            payload.push(metric_data);
        }

        // In production, POST to config.endpoint with API key header
        let _ = payload;
        (true, None)
    }

    /// Export to Honeycomb (Events API)
    fn export_to_honeycomb(&self, metrics: &[ExportMetric]) -> (bool, Option<String>) {
        let Some(config) = &self.config.honeycomb else {
            return (false, Some("Honeycomb not configured".to_string()));
        };

        // Format as Honeycomb events
        let mut events = Vec::new();
        for metric in metrics {
            let mut event: HashMap<&str, String> = HashMap::new();
            event.insert("name", metric.name.clone());
            event.insert("value", metric.value.to_string());
            event.insert("service.name", config.service_name.clone());

            for (k, v) in &metric.tags {
                event.insert(k.as_str(), v.clone());
            }

            events.push(event);
        }

        // In production, POST to config.endpoint with API key
        let _ = events;
        (true, None)
    }

    /// Export to OTLP collector
    fn export_to_otlp(&self, metrics: &[ExportMetric]) -> (bool, Option<String>) {
        let Some(_config) = &self.config.otlp else {
            return (false, Some("OTLP not configured".to_string()));
        };

        // Format as OTLP metrics (simplified)
        let mut otlp_metrics = Vec::new();
        for metric in metrics {
            otlp_metrics.push(format!(
                "metric={{name={},value={},type={:?}}}",
                metric.name, metric.value, metric.metric_type
            ));
        }

        // In production, send via gRPC or HTTP to config.endpoint
        let _ = otlp_metrics;
        (true, None)
    }

    /// Export to webhook
    fn export_to_webhook(&self, metrics: &[ExportMetric]) -> (bool, Option<String>) {
        let Some(_config) = &self.config.webhook else {
            return (false, Some("Webhook not configured".to_string()));
        };

        // Format as JSON array
        let mut json_metrics = Vec::new();
        for metric in metrics {
            let tags_json: Vec<String> = metric
                .tags
                .iter()
                .map(|(k, v)| format!(r#""{}":"{}""#, k, v))
                .collect();

            json_metrics.push(format!(
                r#"{{"name":"{}","value":{},"type":"{:?}","tags":{{{}}},"timestamp_ns":{}}}"#,
                metric.name,
                metric.value,
                metric.metric_type,
                tags_json.join(","),
                metric.timestamp_ns
            ));
        }

        let _payload = format!("[{}]", json_metrics.join(","));

        // In production, send HTTP request to config.url
        (true, None)
    }

    /// Update backend health based on export result
    fn update_health(&mut self, backend: ObservabilityBackend, result: &ExportResult) {
        if let Some(health) = self.health.get_mut(&backend) {
            if result.success {
                health.healthy = true;
                health.last_success = Some(Instant::now());
                health.consecutive_failures = 0;

                // Update average latency
                let n = self.export_counts.get(&backend).copied().unwrap_or(1) as f64;
                health.avg_latency_ms =
                    health.avg_latency_ms * ((n - 1.0) / n) + (result.duration_ms as f64) / n;
            } else {
                health.consecutive_failures += 1;
                if health.consecutive_failures >= 3 {
                    health.healthy = false;
                }
            }
        }
    }

    /// Get health status for a backend
    pub fn get_health(&self, backend: ObservabilityBackend) -> Option<&BackendHealth> {
        self.health.get(&backend)
    }

    /// Get health status for all backends
    pub fn all_health(&self) -> impl Iterator<Item = &BackendHealth> {
        self.health.values()
    }

    /// Get enabled backends
    pub fn enabled_backends(&self) -> Vec<ObservabilityBackend> {
        self.config.enabled_backends()
    }

    /// Get buffer size
    pub fn buffer_size(&self) -> usize {
        self.buffer.len()
    }

    /// Get total exports for a backend
    pub fn export_count(&self, backend: ObservabilityBackend) -> u64 {
        self.export_counts.get(&backend).copied().unwrap_or(0)
    }

    /// Check if auto-flush is needed based on time
    pub fn should_flush(&self) -> bool {
        let elapsed = self.last_flush.elapsed().as_millis() as u64;
        !self.buffer.is_empty() && elapsed >= self.config.flush_interval_ms
    }

    /// Get configuration
    pub fn config(&self) -> &ObservabilityConfig {
        &self.config
    }
}

/// Format metric for DogStatsD protocol
pub fn format_dogstatsd(metric: &ExportMetric, prefix: &str, default_tags: &[String]) -> String {
    let tags: Vec<String> = metric
        .tags
        .iter()
        .map(|(k, v)| format!("{}:{}", k, v))
        .chain(default_tags.iter().cloned())
        .collect();

    let tag_str = if tags.is_empty() {
        String::new()
    } else {
        format!("|#{}", tags.join(","))
    };

    let metric_type = match metric.metric_type {
        MetricExportType::Gauge => "g",
        MetricExportType::Counter => "c",
        MetricExportType::Histogram => "h",
    };

    format!(
        "{}.{}:{}|{}{}",
        prefix, metric.name, metric.value, metric_type, tag_str
    )
}

/// Default batch size
pub const DEFAULT_BATCH_SIZE: usize = 100;

/// Default flush interval in milliseconds
pub const DEFAULT_FLUSH_INTERVAL_MS: u64 = 10_000;

#[cfg(test)]
mod tests;