streamtop 1.3.0

Terminal HLS, DASH, and IPTV stream monitor with wire probes, TR 101 290, SCTE-35, and Prometheus metrics
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
//! OTLP/HTTP JSON trace export with W3C `traceparent` propagation.

use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use color_eyre::eyre::{eyre, Result, WrapErr};
use serde_json::{json, Value};

use crate::engine::ip_pin::validate_outbound_url;
use crate::engine::metrics::MetricsSnapshot;
use crate::engine::network_trace::pinned_post_json;
use crate::engine::redact::redact_url;
use crate::models::{G2gMetrics, NetworkTiming, WireProbeInfo};

const SERVICE_NAME: &str = "streamtop";
const OTEL_PENDING_CAP: usize = 256;

#[derive(Debug, Clone)]
struct SpanRecord {
    name: String,
    start_ns: u128,
    end_ns: u128,
    attributes: Vec<(String, String)>,
    trace_id: String,
    span_id: String,
    parent_span_id: Option<String>,
}

#[derive(Debug, Clone)]
struct MetricPoint {
    name: String,
    kind: MetricKind,
    value: f64,
    attributes: Vec<(String, String)>,
}

#[derive(Debug, Clone, Copy)]
enum MetricKind {
    Gauge,
    Counter,
}

/// W3C trace context shared across outbound probe requests in one session.
#[derive(Debug, Clone)]
pub struct TraceContext {
    pub trace_id: String,
    pub root_span_id: String,
}

impl TraceContext {
    pub fn new() -> Self {
        Self {
            trace_id: random_hex_id(16),
            root_span_id: random_hex_id(8),
        }
    }

    /// Build `traceparent` header for child outbound spans (`00-trace-span-01`).
    pub fn traceparent(&self) -> String {
        let span = random_hex_id(8);
        format!("00-{}-{}-01", self.trace_id, span)
    }

    pub fn trace_id(&self) -> &str {
        &self.trace_id
    }
}

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

/// Buffers spans and POSTs OTLP JSON batches to `{endpoint}/v1/traces`.
pub struct OtelExporter {
    endpoint: String,
    allow_insecure: bool,
    pending: Mutex<Vec<SpanRecord>>,
    metrics: Mutex<Vec<MetricPoint>>,
    trace: Mutex<TraceContext>,
}

impl OtelExporter {
    pub fn new(endpoint: &str, allow_insecure: bool) -> Result<Arc<Self>> {
        let endpoint = endpoint.trim().trim_end_matches('/').to_string();
        if endpoint.is_empty() {
            return Err(eyre!("otel endpoint is empty"));
        }
        if !endpoint.starts_with("http://") && !endpoint.starts_with("https://") {
            return Err(eyre!("otel endpoint must be http(s) URL"));
        }
        validate_outbound_url(&endpoint, allow_insecure)?;
        Ok(Arc::new(Self {
            endpoint,
            allow_insecure,
            pending: Mutex::new(Vec::new()),
            metrics: Mutex::new(Vec::new()),
            trace: Mutex::new(TraceContext::new()),
        }))
    }

    pub fn traceparent(&self) -> String {
        self.trace
            .lock()
            .map(|t| t.traceparent())
            .unwrap_or_else(|_| "00-00000000000000000000000000000000-0000000000000000-01".into())
    }

    fn push_span(&self, name: &str, start_ns: u128, end_ns: u128, attrs: Vec<(String, String)>) {
        let (trace_id, parent) = self
            .trace
            .lock()
            .map(|t| (t.trace_id.clone(), Some(t.root_span_id.clone())))
            .unwrap_or_else(|_| (random_hex_id(16), None));
        let span_id = random_hex_id(8);
        if let Ok(mut pending) = self.pending.lock() {
            if pending.len() >= OTEL_PENDING_CAP {
                pending.remove(0);
            }
            pending.push(SpanRecord {
                name: name.into(),
                start_ns,
                end_ns,
                attributes: attrs,
                trace_id,
                span_id,
                parent_span_id: parent,
            });
        }
    }

    pub fn record_manifest_resolution(&self, url: &str, duration_ms: u64) {
        let end_ns = now_unix_nano();
        let start_ns = end_ns.saturating_sub(u128::from(duration_ms.max(1)) * 1_000_000);
        self.push_span(
            "manifest.resolve",
            start_ns,
            end_ns,
            vec![("url".into(), redact_url(url))],
        );
    }

    pub fn record_network(&self, span_name: &str, timing: &NetworkTiming, url: &str) {
        let end_ns = now_unix_nano();
        let span_ms = timing.ttfb_ms.max(1);
        let start_ns = end_ns.saturating_sub(u128::from(span_ms) * 1_000_000);
        let mut attrs = vec![
            ("url".into(), redact_url(url)),
            ("ttfb_ms".into(), timing.ttfb_ms.to_string()),
        ];
        if let Some(v) = timing.dns_ms {
            attrs.push(("dns_ms".into(), v.to_string()));
            self.record_stage("dns.lookup", v, url);
        }
        if let Some(v) = timing.tcp_ms {
            attrs.push(("tcp_ms".into(), v.to_string()));
            self.record_stage("tcp.connect", v, url);
        }
        if let Some(v) = timing.tls_ms {
            attrs.push(("tls_ms".into(), v.to_string()));
            self.record_stage("tls.handshake", v, url);
        }
        self.record_stage("http.ttfb", timing.ttfb_ms, url);
        self.push_span(span_name, start_ns, end_ns, attrs);
    }

    fn record_stage(&self, name: &str, ms: u64, url: &str) {
        let end_ns = now_unix_nano();
        let start_ns = end_ns.saturating_sub(u128::from(ms.max(1)) * 1_000_000);
        self.push_span(
            name,
            start_ns,
            end_ns,
            vec![
                ("url".into(), redact_url(url)),
                ("duration_ms".into(), ms.to_string()),
            ],
        );
    }

    pub fn record_segment_download(
        &self,
        url: &str,
        timing: &NetworkTiming,
        download_ms: u64,
        http_status: u16,
        chunked: bool,
    ) {
        let end_ns = now_unix_nano();
        let total_ms = download_ms.max(timing.ttfb_ms).max(1);
        let start_ns = end_ns.saturating_sub(u128::from(total_ms) * 1_000_000);
        let attrs = vec![
            ("url".into(), redact_url(url)),
            ("http.status_code".into(), http_status.to_string()),
            ("download_ms".into(), download_ms.to_string()),
            ("ttfb_ms".into(), timing.ttfb_ms.to_string()),
            ("chunked_transfer".into(), chunked.to_string()),
        ];
        self.push_span("segment.download", start_ns, end_ns, attrs);
    }

    pub fn record_wire_parse(&self, url: &str, wire: &WireProbeInfo) {
        let end_ns = now_unix_nano();
        let start_ns = end_ns.saturating_sub(2_000_000);
        let mut attrs = vec![("url".into(), redact_url(url))];
        if let Some(w) = wire.width {
            attrs.push(("video.width".into(), w.to_string()));
        }
        if let Some(h) = wire.height {
            attrs.push(("video.height".into(), h.to_string()));
        }
        if !wire.pssh.is_empty() {
            attrs.push(("pssh.count".into(), wire.pssh.entries.len().to_string()));
        }
        self.push_span("wire.parse", start_ns, end_ns, attrs);
    }

    pub fn record_g2g(&self, g2g: &G2gMetrics) {
        let end_ns = now_unix_nano();
        let start_ns = end_ns.saturating_sub(1_000_000);
        let mut attrs = Vec::new();
        if let Some(v) = g2g.g2g_total_ms {
            attrs.push(("g2g_total_ms".into(), v.to_string()));
        }
        if let Some(v) = g2g.ingestion_lag_ms {
            attrs.push(("ingestion_lag_ms".into(), v.to_string()));
        }
        if let Some(v) = g2g.edge_propagation_ms {
            attrs.push(("edge_propagation_ms".into(), v.to_string()));
        }
        if !attrs.is_empty() {
            self.push_span("g2g.latency", start_ns, end_ns, attrs);
        }
    }

    /// Snapshot Prometheus-style gauges/counters for OTLP `/v1/metrics`.
    pub fn record_metrics_snapshot(&self, snap: &MetricsSnapshot) {
        let url = redact_url(&snap.url);
        let attrs = vec![("url".into(), url)];
        let mut batch = vec![
            metric_point(
                "streamtop_stream_health_score",
                MetricKind::Gauge,
                f64::from(snap.health_score),
                attrs.clone(),
            ),
            metric_point(
                "streamtop_latency_seconds",
                MetricKind::Gauge,
                snap.latency_secs,
                attrs.clone(),
            ),
            metric_point(
                "streamtop_ad_mismatch_total",
                MetricKind::Counter,
                snap.ad_mismatch_total as f64,
                attrs.clone(),
            ),
            metric_point(
                "streamtop_inband_emsg_total",
                MetricKind::Counter,
                snap.inband_emsg_total as f64,
                attrs.clone(),
            ),
            metric_point(
                "streamtop_clearkey_decrypt_ok",
                MetricKind::Gauge,
                snap.clearkey_decrypt_ok,
                attrs.clone(),
            ),
            metric_point(
                "streamtop_tr101290_p1_violations_total",
                MetricKind::Counter,
                snap.tr101290_p1_total as f64,
                attrs.clone(),
            ),
            metric_point(
                "streamtop_tr101290_p2_violations_total",
                MetricKind::Counter,
                snap.tr101290_p2_total as f64,
                attrs,
            ),
        ];
        if let Ok(mut guard) = self.metrics.lock() {
            if guard.len() + batch.len() > OTEL_PENDING_CAP {
                let drain = guard.len() + batch.len() - OTEL_PENDING_CAP;
                guard.drain(0..drain);
            }
            guard.append(&mut batch);
        }
    }

    pub async fn flush(&self) -> Result<()> {
        let spans = {
            let mut guard = self
                .pending
                .lock()
                .map_err(|_| eyre!("otel span buffer poisoned"))?;
            std::mem::take(&mut *guard)
        };
        if spans.is_empty() {
            return Ok(());
        }
        validate_outbound_url(&self.endpoint, self.allow_insecure)?;
        let payload = build_otlp_payload(&spans);
        let url = format!("{}/v1/traces", self.endpoint);
        let status = pinned_post_json(&url, &payload, self.allow_insecure, Duration::from_secs(10))
            .await
            .wrap_err("otel trace export failed")?;
        if !(200..300).contains(&status) {
            return Err(eyre!("otel trace export HTTP {status}"));
        }
        Ok(())
    }

    pub async fn flush_metrics(&self) -> Result<()> {
        let points = {
            let mut guard = self
                .metrics
                .lock()
                .map_err(|_| eyre!("otel metric buffer poisoned"))?;
            std::mem::take(&mut *guard)
        };
        if points.is_empty() {
            return Ok(());
        }
        validate_outbound_url(&self.endpoint, self.allow_insecure)?;
        let payload = build_otlp_metrics_payload(&points);
        let url = format!("{}/v1/metrics", self.endpoint);
        let status = pinned_post_json(&url, &payload, self.allow_insecure, Duration::from_secs(10))
            .await
            .wrap_err("otel metric export failed")?;
        if !(200..300).contains(&status) {
            return Err(eyre!("otel metric export HTTP {status}"));
        }
        Ok(())
    }

    pub async fn flush_all(&self) -> Result<()> {
        self.flush().await?;
        self.flush_metrics().await?;
        Ok(())
    }
}

fn now_unix_nano() -> u128 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0)
}

fn random_hex_id(bytes: usize) -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static CTR: AtomicU64 = AtomicU64::new(1);
    let n = CTR.fetch_add(1, Ordering::Relaxed);
    format!("{n:0width$x}", width = bytes * 2)
}

fn build_otlp_payload(spans: &[SpanRecord]) -> Value {
    let otel_spans: Vec<Value> = spans
        .iter()
        .map(|s| {
            let attrs: Vec<Value> = s
                .attributes
                .iter()
                .map(|(k, v)| {
                    json!({
                        "key": k,
                        "value": { "stringValue": v }
                    })
                })
                .collect();
            let mut span = json!({
                "traceId": s.trace_id,
                "spanId": s.span_id,
                "name": s.name,
                "kind": 1,
                "startTimeUnixNano": s.start_ns.to_string(),
                "endTimeUnixNano": s.end_ns.to_string(),
                "attributes": attrs,
            });
            if let Some(parent) = &s.parent_span_id {
                span["parentSpanId"] = json!(parent);
            }
            span
        })
        .collect();

    json!({
        "resourceSpans": [{
            "resource": {
                "attributes": [{
                    "key": "service.name",
                    "value": { "stringValue": SERVICE_NAME }
                }]
            },
            "scopeSpans": [{
                "scope": { "name": SERVICE_NAME },
                "spans": otel_spans
            }]
        }]
    })
}

fn metric_point(
    name: &str,
    kind: MetricKind,
    value: f64,
    attributes: Vec<(String, String)>,
) -> MetricPoint {
    MetricPoint {
        name: name.into(),
        kind,
        value,
        attributes,
    }
}

fn otlp_attributes(attrs: &[(String, String)]) -> Vec<Value> {
    attrs
        .iter()
        .map(|(k, v)| {
            json!({
                "key": k,
                "value": { "stringValue": v }
            })
        })
        .collect()
}

fn build_otlp_metrics_payload(points: &[MetricPoint]) -> Value {
    let ts = now_unix_nano().to_string();
    let metrics: Vec<Value> = points
        .iter()
        .map(|p| {
            let attrs = otlp_attributes(&p.attributes);
            let data_point = json!({
                "attributes": attrs,
                "timeUnixNano": ts,
                "asDouble": p.value,
            });
            match p.kind {
                MetricKind::Gauge => json!({
                    "name": p.name,
                    "gauge": { "dataPoints": [data_point] }
                }),
                MetricKind::Counter => json!({
                    "name": p.name,
                    "sum": {
                        "aggregationTemporality": 2,
                        "isMonotonic": true,
                        "dataPoints": [data_point]
                    }
                }),
            }
        })
        .collect();

    json!({
        "resourceMetrics": [{
            "resource": {
                "attributes": [{
                    "key": "service.name",
                    "value": { "stringValue": SERVICE_NAME }
                }]
            },
            "scopeMetrics": [{
                "scope": { "name": SERVICE_NAME },
                "metrics": metrics
            }]
        }]
    })
}

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

    #[test]
    fn traceparent_format() {
        let ctx = TraceContext::new();
        let tp = ctx.traceparent();
        assert!(tp.starts_with("00-"));
        assert_eq!(tp.matches('-').count(), 3);
    }

    #[test]
    fn otlp_payload_has_spans() {
        let spans = vec![SpanRecord {
            name: "dns.lookup".into(),
            start_ns: 100,
            end_ns: 200,
            attributes: vec![("ttfb_ms".into(), "5".into())],
            trace_id: "abc".into(),
            span_id: "def".into(),
            parent_span_id: None,
        }];
        let payload = build_otlp_payload(&spans);
        assert!(payload["resourceSpans"][0]["scopeSpans"][0]["spans"]
            .as_array()
            .is_some_and(|a| !a.is_empty()));
    }

    #[test]
    fn otlp_metrics_payload_has_points() {
        let points = vec![metric_point(
            "streamtop_inband_emsg_total",
            MetricKind::Counter,
            3.0,
            vec![("url".into(), "https://example.com".into())],
        )];
        let payload = build_otlp_metrics_payload(&points);
        assert!(payload["resourceMetrics"][0]["scopeMetrics"][0]["metrics"]
            .as_array()
            .is_some_and(|a| !a.is_empty()));
    }
}