devlogbus 1.3.1

Rust SDK for publishing records to DevLogBus.
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
use std::fmt;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub const DEFAULT_HTTP_ENDPOINT: &str = "http://127.0.0.1:7423";
pub const REDACTED_VALUE: &str = "[REDACTED]";

pub type RecordFilter = Arc<dyn Fn(&Record) -> bool + Send + Sync>;
pub type RecordRedactor = Arc<dyn Fn(&mut Record) + Send + Sync>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Record {
    pub time: Option<String>,
    pub level: String,
    pub source: String,
    pub message: String,
    pub attrs_json: Option<String>,
}

impl Record {
    pub fn new(
        source: impl Into<String>,
        level: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        Self {
            time: None,
            level: level.into(),
            source: source.into(),
            message: message.into(),
            attrs_json: None,
        }
    }

    pub fn attrs_json(mut self, attrs_json: impl Into<String>) -> Self {
        self.attrs_json = Some(attrs_json.into());
        self
    }
}

#[derive(Clone)]
pub struct Client {
    endpoint: String,
    source: String,
    timeout: Duration,
    filter: Option<RecordFilter>,
    redactor: Option<RecordRedactor>,
}

#[derive(Default)]
pub struct ClientOptions {
    pub endpoint: Option<String>,
    pub source: Option<String>,
    pub timeout: Option<Duration>,
    pub filter: Option<RecordFilter>,
    pub redactor: Option<RecordRedactor>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublishResult {
    pub published: u32,
    pub filtered: bool,
}

#[derive(Debug)]
pub enum Error {
    InvalidEndpoint(String),
    InvalidRecord(String),
    Io(std::io::Error),
    Http(u16),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidEndpoint(message) => write!(f, "{message}"),
            Error::InvalidRecord(message) => write!(f, "{message}"),
            Error::Io(err) => write!(f, "{err}"),
            Error::Http(status) => write!(f, "DevLogBus publish failed: HTTP {status}"),
        }
    }
}

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

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Error::Io(value)
    }
}

impl Client {
    pub fn new(options: ClientOptions) -> Self {
        Self {
            endpoint: trim_endpoint(options.endpoint.as_deref().unwrap_or(DEFAULT_HTTP_ENDPOINT)),
            source: options.source.unwrap_or_default(),
            timeout: options.timeout.unwrap_or(Duration::from_secs(2)),
            filter: options.filter,
            redactor: options.redactor,
        }
    }

    pub fn publish_message(
        &self,
        level: impl Into<String>,
        message: impl Into<String>,
        attrs_json: Option<String>,
    ) -> Result<PublishResult, Error> {
        let mut record = Record::new(self.source.clone(), level, message);
        record.attrs_json = attrs_json;
        self.publish(record)
    }

    pub fn publish(&self, mut record: Record) -> Result<PublishResult, Error> {
        if record.source.trim().is_empty() {
            record.source = self.source.clone();
        }
        if let Some(filter) = &self.filter {
            if !filter(&record) {
                return Ok(PublishResult {
                    published: 0,
                    filtered: true,
                });
            }
        }
        if let Some(redactor) = &self.redactor {
            redactor(&mut record);
        }

        let payload = record_json(&record)?;
        let endpoint = parse_endpoint(&self.endpoint)?;
        let mut stream = TcpStream::connect((endpoint.host.as_str(), endpoint.port))?;
        stream.set_read_timeout(Some(self.timeout))?;
        stream.set_write_timeout(Some(self.timeout))?;

        let request = format!(
            "POST {} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            endpoint.path,
            endpoint.host_header(),
            payload.len(),
            payload
        );
        stream.write_all(request.as_bytes())?;
        stream.flush()?;

        let mut response = String::new();
        stream.read_to_string(&mut response)?;
        let status = parse_status(&response)?;
        if !(200..300).contains(&status) {
            return Err(Error::Http(status));
        }
        Ok(PublishResult {
            published: 1,
            filtered: false,
        })
    }
}

pub fn drop_sources<I, S>(sources: I) -> RecordFilter
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    let blocked: Vec<String> = sources
        .into_iter()
        .map(Into::into)
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();
    Arc::new(move |record: &Record| !blocked.iter().any(|source| source == &record.source))
}

pub fn redact_message() -> RecordRedactor {
    Arc::new(|record: &mut Record| {
        record.message = REDACTED_VALUE.to_string();
    })
}

pub fn normalize_level(level: &str) -> String {
    match level.trim().to_ascii_lowercase().as_str() {
        "debug" | "dbg" => "DEBUG".to_string(),
        "" | "info" => "INFO".to_string(),
        "warn" | "warning" => "WARN".to_string(),
        "error" | "err" => "ERROR".to_string(),
        _ => level.trim().to_ascii_uppercase(),
    }
}

pub fn record_json(record: &Record) -> Result<String, Error> {
    let source = record.source.trim();
    if source.is_empty() {
        return Err(Error::InvalidRecord(
            "DevLogBus source is required".to_string(),
        ));
    }
    if record.message.is_empty() {
        return Err(Error::InvalidRecord(
            "DevLogBus message is required".to_string(),
        ));
    }
    if let Some(attrs) = &record.attrs_json {
        if !attrs.trim_start().starts_with('{') {
            return Err(Error::InvalidRecord(
                "DevLogBus attrs_json must be a JSON object".to_string(),
            ));
        }
    }

    let time = record.time.clone().unwrap_or_else(now_iso);
    let mut out = String::new();
    out.push('{');
    out.push_str("\"time\":");
    push_json_string(&mut out, &time);
    out.push_str(",\"level\":");
    push_json_string(&mut out, &normalize_level(&record.level));
    out.push_str(",\"source\":");
    push_json_string(&mut out, source);
    out.push_str(",\"message\":");
    push_json_string(&mut out, &record.message);
    if let Some(attrs) = &record.attrs_json {
        out.push_str(",\"attrs\":");
        out.push_str(attrs);
    }
    out.push('}');
    Ok(out)
}

struct Endpoint {
    host: String,
    port: u16,
    path: String,
}

impl Endpoint {
    fn host_header(&self) -> String {
        format!("{}:{}", self.host, self.port)
    }
}

fn parse_endpoint(endpoint: &str) -> Result<Endpoint, Error> {
    let raw = endpoint.trim().trim_end_matches('/');
    let raw = raw.strip_prefix("http://").ok_or_else(|| {
        Error::InvalidEndpoint("DevLogBus Rust SDK only supports http:// endpoints".to_string())
    })?;
    let (authority, base_path) = match raw.split_once('/') {
        Some((authority, path)) => (authority, format!("/{path}")),
        None => (raw, String::new()),
    };
    let (host, port) = match authority.rsplit_once(':') {
        Some((host, port)) => {
            let parsed = port.parse::<u16>().map_err(|_| {
                Error::InvalidEndpoint("DevLogBus endpoint port is invalid".to_string())
            })?;
            (host.to_string(), parsed)
        }
        None => (authority.to_string(), 80),
    };
    if host.is_empty() {
        return Err(Error::InvalidEndpoint(
            "DevLogBus endpoint host is required".to_string(),
        ));
    }
    Ok(Endpoint {
        host,
        port,
        path: format!("{}/api/records", base_path.trim_end_matches('/')),
    })
}

fn parse_status(response: &str) -> Result<u16, Error> {
    let status = response
        .lines()
        .next()
        .and_then(|line| line.split_whitespace().nth(1))
        .and_then(|value| value.parse::<u16>().ok())
        .ok_or_else(|| {
            Error::InvalidEndpoint("DevLogBus response did not include an HTTP status".to_string())
        })?;
    Ok(status)
}

fn trim_endpoint(endpoint: &str) -> String {
    endpoint.trim().trim_end_matches('/').to_string()
}

fn push_json_string(out: &mut String, value: &str) {
    out.push('"');
    for ch in value.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            '\u{08}' => out.push_str("\\b"),
            '\u{0c}' => out.push_str("\\f"),
            ch if ch < '\u{20}' => out.push_str(&format!("\\u{:04x}", ch as u32)),
            ch => out.push(ch),
        }
    }
    out.push('"');
}

fn now_iso() -> String {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    format_unix_utc(secs)
}

fn format_unix_utc(secs: u64) -> String {
    let days = (secs / 86_400) as i64;
    let seconds_of_day = secs % 86_400;
    let hour = seconds_of_day / 3_600;
    let minute = (seconds_of_day % 3_600) / 60;
    let second = seconds_of_day % 60;
    let (year, month, day) = civil_from_days(days);
    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z")
}

fn civil_from_days(days_since_epoch: i64) -> (i64, u64, u64) {
    let z = days_since_epoch + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = z - era * 146_097;
    let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = doy - (153 * mp + 2) / 5 + 1;
    let month = mp + if mp < 10 { 3 } else { -9 };
    let year = y + if month <= 2 { 1 } else { 0 };
    (year, month as u64, day as u64)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::thread;

    #[test]
    fn normalizes_levels() {
        assert_eq!(normalize_level("warning"), "WARN");
        assert_eq!(normalize_level("dbg"), "DEBUG");
        assert_eq!(normalize_level("custom"), "CUSTOM");
    }

    #[test]
    fn formats_unix_time_as_rfc3339_utc() {
        assert_eq!(format_unix_utc(0), "1970-01-01T00:00:00Z");
        assert_eq!(format_unix_utc(1_704_067_199), "2023-12-31T23:59:59Z");
    }

    #[test]
    fn builds_record_json() {
        let record = Record {
            time: Some("2026-05-31T12:00:00Z".to_string()),
            level: "warning".to_string(),
            source: "rust_test".to_string(),
            message: "quote \" newline\n".to_string(),
            attrs_json: Some("{\"request\":{\"id\":\"req-1\"}}".to_string()),
        };

        let json = record_json(&record).unwrap();

        assert!(json.contains("\"level\":\"WARN\""));
        assert!(json.contains("\"source\":\"rust_test\""));
        assert!(json.contains("quote \\\" newline\\n"));
        assert!(json.contains("\"attrs\":{\"request\":{\"id\":\"req-1\"}}"));
    }

    #[test]
    fn rejects_non_object_attrs_json() {
        let record = Record::new("rust_test", "INFO", "hello").attrs_json("\"nope\"");
        assert!(record_json(&record).is_err());
    }

    #[test]
    fn filters_before_http_publish() {
        let client = Client::new(ClientOptions {
            endpoint: Some("http://127.0.0.1:1".to_string()),
            source: Some("hidden".to_string()),
            filter: Some(drop_sources(["hidden"])),
            ..Default::default()
        });

        let result = client.publish_message("INFO", "drop me", None).unwrap();

        assert_eq!(
            result,
            PublishResult {
                published: 0,
                filtered: true
            }
        );
    }

    #[test]
    fn publishes_to_http_endpoint() {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        let handle = thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            let mut buf = [0_u8; 1024];
            let mut bytes = Vec::new();
            loop {
                let n = stream.read(&mut buf).unwrap();
                bytes.extend_from_slice(&buf[..n]);
                if bytes.windows(4).any(|window| window == b"\r\n\r\n") {
                    break;
                }
            }
            let header_end = bytes
                .windows(4)
                .position(|window| window == b"\r\n\r\n")
                .unwrap()
                + 4;
            let headers = String::from_utf8_lossy(&bytes[..header_end]).to_string();
            let content_length = headers
                .lines()
                .find_map(|line| line.strip_prefix("Content-Length: "))
                .and_then(|value| value.parse::<usize>().ok())
                .unwrap_or(0);
            while bytes.len() < header_end + content_length {
                let n = stream.read(&mut buf).unwrap();
                bytes.extend_from_slice(&buf[..n]);
            }
            let request = String::from_utf8_lossy(&bytes).to_string();
            stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 15\r\nConnection: close\r\n\r\n{\"published\":1}").unwrap();
            request
        });

        let client = Client::new(ClientOptions {
            endpoint: Some(format!("http://127.0.0.1:{port}")),
            source: Some("rust_test".to_string()),
            redactor: Some(redact_message()),
            ..Default::default()
        });

        let result = client.publish_message("INFO", "secret", None).unwrap();
        let request = handle.join().unwrap();

        assert_eq!(
            result,
            PublishResult {
                published: 1,
                filtered: false
            }
        );
        assert!(request.contains("POST /api/records HTTP/1.1"));
        assert!(request.contains(REDACTED_VALUE));
    }
}