sonda-server 1.28.0

HTTP control plane for Sonda — synthetic telemetry generator
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
//! POST /events — synchronous single-event emission.

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

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sonda_core::emit::{emit_log, emit_metric};
use sonda_core::encoder::EncoderConfig;
use sonda_core::model::log::{LogEvent, Severity};
use sonda_core::model::metric::{Labels, MetricEvent};
use sonda_core::sink::SinkConfig;
use sonda_core::{EncoderError, SondaError};
use tracing::info;

use crate::routes::sink_warnings::{collect_warnings_for_sink, log_warnings};
use crate::state::AppState;

#[derive(Debug, Deserialize)]
pub struct LogPayload {
    pub severity: Severity,
    pub message: String,
    #[serde(default)]
    pub fields: HashMap<String, String>,
}

#[derive(Debug, Deserialize)]
pub struct MetricPayload {
    pub name: String,
    pub value: f64,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "signal_type")]
pub enum EventRequest {
    #[serde(rename = "logs")]
    Logs {
        #[serde(default)]
        labels: HashMap<String, String>,
        log: LogPayload,
        encoder: EncoderConfig,
        sink: SinkConfig,
    },
    #[serde(rename = "metrics")]
    Metrics {
        #[serde(default)]
        labels: HashMap<String, String>,
        metric: MetricPayload,
        encoder: EncoderConfig,
        sink: SinkConfig,
    },
}

#[derive(Debug, Serialize)]
pub struct EventAck {
    pub sent: bool,
    pub signal_type: &'static str,
    pub latency_ms: u128,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub warnings: Vec<String>,
}

pub async fn post_events(State(_state): State<AppState>, body: axum::body::Bytes) -> Response {
    let req: EventRequest = match serde_json::from_slice::<EventRequest>(&body) {
        Ok(r) => r,
        Err(e) => return bad_request(format!("invalid event body: {e}")),
    };

    let mut warnings: Vec<String> = Vec::new();
    let warning_label = match &req {
        EventRequest::Logs { .. } => "events.logs",
        EventRequest::Metrics { .. } => "events.metrics",
    };
    match &req {
        EventRequest::Logs { sink, .. } | EventRequest::Metrics { sink, .. } => {
            collect_warnings_for_sink(sink, warning_label, &mut warnings);
        }
    }
    log_warnings("POST /events", &warnings);

    let started = Instant::now();
    let (signal_type, sink_type, emit_result) = match req {
        EventRequest::Logs {
            labels,
            log,
            encoder,
            sink,
        } => {
            let event = match build_log_event(log, &labels) {
                Ok(e) => e,
                Err(e) => return error_response(e),
            };
            let sink_type = sink.kind();
            let labels_for_sink = (!labels.is_empty()).then_some(labels);
            let result = emit_log(&event, &encoder, &sink, labels_for_sink.as_ref()).await;
            ("logs", sink_type, result)
        }
        EventRequest::Metrics {
            labels,
            metric,
            encoder,
            sink,
        } => {
            let event = match build_metric_event(metric, &labels) {
                Ok(e) => e,
                Err(e) => return error_response(e),
            };
            let sink_type = sink.kind();
            let labels_for_sink = (!labels.is_empty()).then_some(labels);
            let result = emit_metric(&event, &encoder, &sink, labels_for_sink.as_ref()).await;
            ("metrics", sink_type, result)
        }
    };
    let latency_ms = started.elapsed().as_millis();

    match emit_result {
        Ok(()) => {
            info!(
                signal_type = signal_type,
                sink_type = sink_type,
                latency_ms = latency_ms,
                result = "ok",
                "POST /events: event delivered"
            );
            (
                StatusCode::OK,
                Json(EventAck {
                    sent: true,
                    signal_type,
                    latency_ms,
                    warnings,
                }),
            )
                .into_response()
        }
        Err(err) => {
            info!(
                signal_type = signal_type,
                sink_type = sink_type,
                latency_ms = latency_ms,
                result = "error",
                error = %err,
                "POST /events: event delivery failed"
            );
            error_response(err)
        }
    }
}

fn build_log_event(
    log: LogPayload,
    labels: &HashMap<String, String>,
) -> Result<LogEvent, SondaError> {
    let labels = labels_from_map(labels)?;
    let fields: std::collections::BTreeMap<String, String> = log.fields.into_iter().collect();
    Ok(LogEvent::new(log.severity, log.message, labels, fields))
}

fn build_metric_event(
    metric: MetricPayload,
    labels: &HashMap<String, String>,
) -> Result<MetricEvent, SondaError> {
    let labels = labels_from_map(labels)?;
    MetricEvent::new(metric.name, metric.value, labels)
}

fn labels_from_map(map: &HashMap<String, String>) -> Result<Labels, SondaError> {
    if map.is_empty() {
        return Ok(Labels::default());
    }
    let pairs: Vec<(&str, &str)> = map.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
    Labels::from_pairs(&pairs)
}

fn error_response(err: SondaError) -> Response {
    match err {
        SondaError::Config(e) => unprocessable(format!("{e}")),
        SondaError::Sink(e) => bad_gateway(format!("sink error: {e}")),
        SondaError::Encoder(EncoderError::EventRejected(e)) => {
            unprocessable(format!("event rejected: {e}"))
        }
        SondaError::Encoder(e) => internal_error(format!("encoder error: {e}")),
        SondaError::Generator(e) => internal_error(format!("generator error: {e}")),
        SondaError::Runtime(e) => internal_error(format!("runtime error: {e}")),
        _ => internal_error("unexpected error variant"),
    }
}

fn bad_request(detail: impl std::fmt::Display) -> Response {
    let body = json!({ "error": "bad_request", "detail": detail.to_string() });
    (StatusCode::BAD_REQUEST, Json(body)).into_response()
}

fn unprocessable(detail: impl std::fmt::Display) -> Response {
    let body = json!({ "error": "unprocessable_entity", "detail": detail.to_string() });
    (StatusCode::UNPROCESSABLE_ENTITY, Json(body)).into_response()
}

fn bad_gateway(detail: impl std::fmt::Display) -> Response {
    let body = json!({ "error": "bad_gateway", "detail": detail.to_string() });
    (StatusCode::BAD_GATEWAY, Json(body)).into_response()
}

fn internal_error(detail: impl std::fmt::Display) -> Response {
    let body = json!({ "error": "internal_server_error", "detail": detail.to_string() });
    (StatusCode::INTERNAL_SERVER_ERROR, Json(body)).into_response()
}

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

    #[test]
    fn deserializes_logs_branch() {
        let payload = serde_json::json!({
            "signal_type": "logs",
            "labels": {"event": "deploy_start"},
            "log": {"severity": "info", "message": "go", "fields": {}},
            "encoder": {"type": "json_lines"},
            "sink": {"type": "stdout"},
        });
        let req: EventRequest = serde_json::from_value(payload).expect("must parse");
        match req {
            EventRequest::Logs { log, .. } => {
                assert_eq!(log.message, "go");
                assert_eq!(log.severity, Severity::Info);
            }
            _ => panic!("expected Logs branch"),
        }
    }

    #[test]
    fn deserializes_metrics_branch() {
        let payload = serde_json::json!({
            "signal_type": "metrics",
            "labels": {},
            "metric": {"name": "x", "value": 1.0},
            "encoder": {"type": "prometheus_text"},
            "sink": {"type": "stdout"},
        });
        let req: EventRequest = serde_json::from_value(payload).expect("must parse");
        match req {
            EventRequest::Metrics { metric, .. } => {
                assert_eq!(metric.name, "x");
                assert_eq!(metric.value, 1.0);
            }
            _ => panic!("expected Metrics branch"),
        }
    }

    #[test]
    fn unknown_signal_type_fails_to_deserialize() {
        let payload = serde_json::json!({
            "signal_type": "traces",
            "encoder": {"type": "json_lines"},
            "sink": {"type": "stdout"},
        });
        let err = serde_json::from_value::<EventRequest>(payload)
            .expect_err("unknown signal_type must error");
        let msg = err.to_string();
        assert!(
            msg.contains("traces") || msg.to_lowercase().contains("variant"),
            "error must mention the bad tag, got: {msg}"
        );
    }

    #[test]
    fn missing_log_message_fails_to_deserialize() {
        let payload = serde_json::json!({
            "signal_type": "logs",
            "log": {"severity": "info"},
            "encoder": {"type": "json_lines"},
            "sink": {"type": "stdout"},
        });
        let err = serde_json::from_value::<EventRequest>(payload)
            .expect_err("missing message must error");
        assert!(
            err.to_string().to_lowercase().contains("message"),
            "error must mention the missing field, got: {err}"
        );
    }

    #[test]
    fn error_response_maps_config_to_422() {
        let err = SondaError::Config(sonda_core::ConfigError::InvalidValue("bad".to_string()));
        let resp = error_response(err);
        assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
    }

    #[test]
    fn error_response_maps_sink_to_502() {
        let err = SondaError::Sink(std::io::Error::new(
            std::io::ErrorKind::ConnectionRefused,
            "nope",
        ));
        let resp = error_response(err);
        assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);
    }

    #[test]
    fn error_response_maps_runtime_to_500() {
        let err = SondaError::Runtime(sonda_core::RuntimeError::ThreadPanicked);
        let resp = error_response(err);
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[test]
    fn labels_from_empty_map_returns_default() {
        let map: HashMap<String, String> = HashMap::new();
        let labels = labels_from_map(&map).expect("empty must succeed");
        assert!(labels.is_empty());
    }

    #[test]
    fn labels_from_map_rejects_invalid_keys() {
        let mut map = HashMap::new();
        map.insert("1bad".to_string(), "value".to_string());
        let err = labels_from_map(&map).expect_err("invalid key must fail");
        assert!(matches!(err, SondaError::Config(_)));
    }

    #[test]
    fn error_response_maps_a_rejected_event_to_422() {
        let err = SondaError::Encoder(EncoderError::EventRejected("no ifName".to_string()));
        let resp = error_response(err);
        assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
    }

    #[test]
    fn error_response_maps_other_encoder_errors_to_500() {
        let err = SondaError::Encoder(EncoderError::Other("protobuf".to_string()));
        let resp = error_response(err);
        assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
    }

    #[cfg(feature = "gnmi")]
    mod gnmi {
        use std::sync::Arc;

        use http_body_util::BodyExt;
        use hyper::Request;
        use sonda_core::sink::gnmi::{registry, GnmiTarget, GnmiTargetConfig, GnmiTargetSink};
        use sonda_core::sink::Sink;
        use tower::ServiceExt;

        use super::*;
        use crate::routes::GNMI_REGISTRY as REGISTRY;

        const TEMPLATE: &str = "/interfaces/interface[name={ifName}]/{name}";

        /// POST a metrics event with the given encoder, on a gnmi_target
        /// sink listening on `listen`; returns the status and the body.
        async fn post_metric(
            labels: serde_json::Value,
            encoder: serde_json::Value,
            listen: &str,
        ) -> (StatusCode, serde_json::Value) {
            let body = json!({
                "signal_type": "metrics",
                "labels": labels,
                "metric": {"name": "in_octets", "value": 42.0},
                "encoder": encoder,
                "sink": {"type": "gnmi_target", "listen": listen},
            });
            let request = Request::builder()
                .method("POST")
                .uri("/events")
                .header("content-type", "application/json")
                .body(axum::body::Body::from(body.to_string()))
                .expect("request");
            let response = crate::routes::router(AppState::new())
                .oneshot(request)
                .await
                .expect("infallible");
            let status = response.status();
            let bytes = response
                .into_body()
                .collect()
                .await
                .expect("body")
                .to_bytes();
            let json = serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null);
            (status, json)
        }

        /// A target held by the test, standing in for a running scenario.
        async fn hold_target() -> (GnmiTargetSink, Arc<GnmiTarget>) {
            let sink = GnmiTargetSink::new(&GnmiTargetConfig::new("127.0.0.1:0"))
                .await
                .expect("bind an ephemeral port");
            let target = Arc::clone(sink.target());
            (sink, target)
        }

        fn template(path: &str) -> serde_json::Value {
            json!({"type": "gnmi", "path": path})
        }

        /// `in_octets` with `ifName` = `if_name` and `value`, as the gnmi
        /// encoder for [`TEMPLATE`] encodes it.
        fn notification(if_name: &str, value: f64) -> (MetricEvent, Vec<u8>) {
            let encoder: EncoderConfig =
                serde_json::from_value(template(TEMPLATE)).expect("a gnmi encoder");
            let event = MetricEvent::new(
                "in_octets".to_string(),
                value,
                Labels::from_pairs(&[("ifName", if_name)]).expect("labels"),
            )
            .expect("event");
            let mut buf = Vec::new();
            sonda_core::encoder::create_encoder(&encoder)
                .expect("valid encoder")
                .encode_metric(&event, &mut buf)
                .expect("encodes");
            (event, buf)
        }

        /// A target held by the test and serving `ifName` = `if_name`,
        /// standing in for a running scenario.
        async fn serving_target(if_name: &str) -> (GnmiTargetSink, Arc<GnmiTarget>) {
            let (mut sink, target) = hold_target().await;
            let (event, encoded) = notification(if_name, 1.0);
            sink.write_metric_event(&event, &encoded)
                .await
                .expect("the scenario publishes its series");
            (sink, target)
        }

        /// An event for a series the running target serves replaces its
        /// latest value; the 200 is that replacement.
        #[tokio::test]
        async fn a_valid_event_reaches_the_target_with_200() {
            let _registry = REGISTRY.lock().await;
            let (_held, target) = serving_target("Gi0").await;
            let listen = target.local_addr().to_string();
            assert_eq!(target.series_count(), 1, "the scenario serves one series");

            let (status, body) =
                post_metric(json!({"ifName": "Gi0"}), template(TEMPLATE), &listen).await;
            assert_eq!(status, StatusCode::OK, "body: {body}");
            assert_eq!(body["sent"], json!(true), "body: {body}");
            assert_eq!(target.series_count(), 1, "the served series was replaced");
        }

        /// An event for a path the running target does not serve is a 422
        /// naming the path and the address, and adds nothing.
        #[tokio::test]
        async fn an_event_for_a_path_nothing_serves_is_422() {
            let _registry = REGISTRY.lock().await;
            let (_held, target) = serving_target("Gi0").await;
            let addr = target.local_addr().to_string();

            let (status, body) =
                post_metric(json!({"ifName": "Gi1"}), template(TEMPLATE), &addr).await;
            assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "body: {body}");
            let text = body.to_string();
            assert!(
                text.contains("interface[name=Gi1]/in-octets"),
                "body: {body}"
            );
            assert!(text.contains(&addr), "body: {body}");
            assert_eq!(target.series_count(), 1, "nothing was added");
        }

        #[tokio::test]
        async fn an_invalid_template_is_422() {
            let _registry = REGISTRY.lock().await;
            let (_held, target) = hold_target().await;
            let (status, body) = post_metric(
                json!({"ifName": "Gi0"}),
                template("/interfaces/interface[name={ifName}"),
                &target.local_addr().to_string(),
            )
            .await;
            assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "body: {body}");
            assert_eq!(target.series_count(), 0, "nothing was published");
        }

        #[tokio::test]
        async fn an_event_the_template_cannot_place_is_422() {
            let _registry = REGISTRY.lock().await;
            let (_held, target) = hold_target().await;
            // The template is valid, but this event carries no `ifName`.
            let (status, body) = post_metric(
                json!({"device": "rtr1"}),
                template("/interfaces/interface[name={ifName}]/{name}"),
                &target.local_addr().to_string(),
            )
            .await;
            assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "body: {body}");
            assert!(
                body.to_string().contains("event rejected"),
                "the 422 is the per-event one: {body}"
            );
            assert_eq!(target.series_count(), 0, "nothing was published");
        }

        /// With no running scenario on the address the event is refused,
        /// and the server binds nothing.
        #[tokio::test]
        async fn no_live_target_is_422_and_binds_nothing() {
            let _registry = REGISTRY.lock().await;
            // A port a target held and released, so nothing listens on it now.
            let addr = {
                let (held, target) = hold_target().await;
                let stopped = target.wait();
                let addr = target.local_addr();
                drop((held, target));
                stopped.await;
                addr
            };
            assert!(registry().is_empty(), "the precondition: no live target");

            let (status, body) = post_metric(
                json!({"ifName": "Gi0"}),
                template("/interfaces/interface[name={ifName}]/{name}"),
                &addr.to_string(),
            )
            .await;
            assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "body: {body}");
            assert!(
                body.to_string().contains("no running scenario listens on"),
                "body: {body}"
            );
            assert!(registry().is_empty(), "the request bound no target");
            tokio::net::TcpStream::connect(addr)
                .await
                .expect_err("nothing listens on the address");
        }
    }
}