sozu-command-lib 2.3.1

configuration library to command a sozu instance
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
use std::fmt;

use rusty_ulid::Ulid;

use crate::{
    AsStr,
    logging::{
        EndpointRecord, FullTags, LogAddress, LogContext, LogDuration, LogLevel, LogMessage,
        LoggerBackend, Rfc3339Time,
    },
};

impl LogLevel {
    pub const fn as_str(&self, access: bool, colored: bool) -> &'static str {
        match (self, access, colored) {
            (LogLevel::Error, false, false) => "ERROR",
            (LogLevel::Warn, false, false) => "WARN ",
            (LogLevel::Info, false, false) => "INFO ",
            (LogLevel::Debug, false, false) => "DEBUG",
            (LogLevel::Trace, false, false) => "TRACE",

            (LogLevel::Error, false, true) => "\x1b[;31;1mERROR",
            (LogLevel::Warn, false, true) => "\x1b[;33;1mWARN ",
            (LogLevel::Info, false, true) => "\x1b[;32;1mINFO ",
            (LogLevel::Debug, false, true) => "\x1b[;36mDEBUG",
            (LogLevel::Trace, false, true) => "\x1b[;90mTRACE",

            (LogLevel::Error, true, false) => "ERROR-ACCESS",
            (LogLevel::Info, true, false) => "INFO-ACCESS ",
            (_, true, false) => "???",

            (LogLevel::Error, true, true) => "\x1b[;35;1mERROR-ACCESS",
            (LogLevel::Info, true, true) => "\x1b[;35;1mINFO-ACCESS ",
            (_, true, true) => "\x1b[;35;1m???",
        }
    }
}

impl AsRef<str> for LoggerBackend {
    fn as_ref(&self) -> &str {
        match self {
            LoggerBackend::Stdout(_) => "stdout",
            LoggerBackend::Unix(_) => "UNIX socket",
            LoggerBackend::Udp(_, _) => "UDP socket",
            LoggerBackend::Tcp(_) => "TCP socket",
            LoggerBackend::File(_) => "file",
        }
    }
}

impl fmt::Display for Rfc3339Time {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        let t = self.inner;
        write!(
            f,
            "{}-{:02}-{:02}T{:02}:{:02}:{:02}.{:06}Z",
            t.year(),
            t.month() as u8,
            t.day(),
            t.hour(),
            t.minute(),
            t.second(),
            t.microsecond()
        )
    }
}

impl fmt::Display for LogMessage<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            Some(message) => write!(f, " | {message}"),
            None => Ok(()),
        }
    }
}

impl fmt::Display for LogDuration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            None => write!(f, "-"),
            Some(duration) => {
                let secs = duration.as_secs();
                if secs >= 10 {
                    return write!(f, "{secs}s");
                }

                let ms = duration.as_millis();
                if ms < 10 {
                    let us = duration.as_micros();
                    if us >= 10 {
                        return write!(f, "{us}μs");
                    }

                    let ns = duration.as_nanos();
                    return write!(f, "{ns}ns");
                }

                write!(f, "{ms}ms")
            }
        }
    }
}

impl fmt::Display for LogAddress {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            // `SocketAddr`'s own `Display` formats on the stack.
            Some(address) => write!(f, "{address}"),
            None => f.write_str("-"),
        }
    }
}

/// Crockford base32 alphabet of a ULID, the one `rusty_ulid` encodes with.
const ULID_ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";

/// Write `ulid` in its 26-character canonical form.
///
/// `rusty_ulid`'s `Display` is `f.write_str(&self.to_string())`, one `String`
/// per ULID, and every log line carrying a [`LogContext`] renders one or two
/// of them. This encodes into a stack array instead, with the same alphabet
/// and the same bit layout: the first character carries the top 3 bits of the
/// 128-bit value, each of the other 25 the next 5.
fn write_ulid(f: &mut fmt::Formatter, ulid: Ulid) -> fmt::Result {
    let value = u128::from(ulid);
    let mut encoded = [0u8; 26];
    for (index, digit) in encoded.iter_mut().enumerate() {
        let shift = 125 - 5 * index;
        *digit = ULID_ALPHABET[((value >> shift) & 0x1f) as usize];
    }
    // Every byte comes from the ASCII alphabet above, so this never fails.
    f.write_str(std::str::from_utf8(&encoded).map_err(|_| fmt::Error)?)
}

impl fmt::Display for LogContext<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("[")?;
        write_ulid(f, self.session_id)?;
        f.write_str(" ")?;
        match self.request_id {
            Some(id) => write_ulid(f, id)?,
            None => f.write_str("-")?,
        }
        write!(
            f,
            " {} {}]",
            self.cluster_id.unwrap_or("-"),
            self.backend_id.unwrap_or("-")
        )
    }
}

impl fmt::Display for EndpointRecord<'_> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Http {
                method,
                authority,
                path,
                status,
                ..
            } => {
                let pretty = f.alternate();
                write!(
                    f,
                    "{} {} {} ",
                    authority.as_str_or("-"),
                    method.as_str_or("-"),
                    path.as_str_or("-"),
                )?;
                write_status(f, *status, pretty)
            }
            Self::Tcp => {
                write!(f, "-")
            }
        }
    }
}

/// Write the status code, colored by class when `pretty`, straight into the
/// formatter rather than through an intermediate `String`.
fn write_status(f: &mut fmt::Formatter, status: Option<u16>, pretty: bool) -> fmt::Result {
    match (status, pretty) {
        (Some(s @ 200..=299), true) => write!(f, "\x1b[32m{s}"),
        (Some(s @ 300..=399), true) => write!(f, "\x1b[34m{s}"),
        (Some(s @ 400..=499), true) => write!(f, "\x1b[33m{s}"),
        (Some(s @ 500..=599), true) => write!(f, "\x1b[31m{s}"),
        (Some(s), _) => write!(f, "{s}"),
        (None, _) => f.write_str("-"),
    }
}

impl fmt::Display for FullTags<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match (self.concatenated, self.user_agent) {
            (None, None) => Ok(()),
            (Some(tags), None) => write!(f, "{tags}"),
            (Some(tags), Some(ua)) if !tags.is_empty() => {
                write!(f, "{tags}, user-agent={}", EscapedUserAgent(ua))
            }
            (_, Some(ua)) => write!(f, "user-agent={}", EscapedUserAgent(ua)),
        }
    }
}

/// A user agent as the access log renders it: every space becomes `_`, `[`
/// becomes `{` and `]` becomes `}`, so the value can neither split the line
/// into more columns nor close the bracketed tag block early. Every other
/// byte is copied verbatim.
///
/// The spans between two replaced bytes are written straight into the
/// formatter. The three replaced bytes are ASCII and never occur inside a
/// multi-byte UTF-8 sequence, so splitting on them keeps every span valid.
struct EscapedUserAgent<'a>(&'a str);

impl fmt::Display for EscapedUserAgent<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut rest = self.0;
        while let Some(index) = rest.find([' ', '[', ']']) {
            f.write_str(&rest[..index])?;
            f.write_str(match rest.as_bytes()[index] {
                b' ' => "_",
                b'[' => "{",
                _ => "}",
            })?;
            rest = &rest[index + 1..];
        }
        f.write_str(rest)
    }
}

#[cfg(test)]
mod tests {
    use rusty_ulid::Ulid;

    use super::EscapedUserAgent;
    use crate::logging::{EndpointRecord, FullTags, LogAddress, LogContext};

    /// The allocating renderer this module used before it wrote into the
    /// formatter, kept as the byte-for-byte oracle of the escaping.
    fn replace_chain(user_agent: &str) -> String {
        user_agent
            .replace(' ', "_")
            .replace('[', "{")
            .replace(']', "}")
    }

    /// Edge cases of the user-agent escaping, each with its expected
    /// rendering. Only space, `[` and `]` are rewritten: control bytes,
    /// quotes, tabs, other whitespace and non-ASCII pass through unchanged.
    const USER_AGENTS: &[(&str, &str)] = &[
        ("", ""),
        ("curl/8.18.0", "curl/8.18.0"),
        (" ", "_"),
        ("[", "{"),
        ("]", "}"),
        ("  [[]]  ", "__{{}}__"),
        (
            "Mozilla/5.0 (X11; Linux x86_64) [en]",
            "Mozilla/5.0_(X11;_Linux_x86_64)_{en}",
        ),
        ("leading and trailing ", "leading_and_trailing_"),
        ("tab\there", "tab\there"),
        ("nul\0byte esc\x1b[31m", "nul\0byte_esc\x1b{31m"),
        ("cr\r\nlf del\x7f", "cr\r\nlf_del\x7f"),
        ("\"double\" 'single'", "\"double\"_'single'"),
        ("back\\slash", "back\\slash"),
        ("nbsp\u{a0}kept", "nbsp\u{a0}kept"),
        ("ideographic\u{3000}space", "ideographic\u{3000}space"),
        (
            "caf\u{e9} [\u{65e5}\u{672c}] \u{1f980}",
            "caf\u{e9}_{\u{65e5}\u{672c}}_\u{1f980}",
        ),
        ("{already} braces_", "{already}_braces_"),
    ];

    #[test]
    fn user_agent_escaping_matches_the_expected_rendering() {
        for (input, expected) in USER_AGENTS {
            assert_eq!(
                EscapedUserAgent(input).to_string(),
                *expected,
                "user agent {input:?}"
            );
        }
    }

    #[test]
    fn user_agent_escaping_is_byte_identical_to_the_replace_chain() {
        for (input, _) in USER_AGENTS {
            assert_eq!(
                EscapedUserAgent(input).to_string().as_bytes(),
                replace_chain(input).as_bytes(),
                "user agent {input:?}"
            );
        }
    }

    #[test]
    fn full_tags_render_the_escaped_user_agent() {
        let ua = Some("a b [c]");
        let render = |concatenated| {
            FullTags {
                concatenated,
                user_agent: ua,
            }
            .to_string()
        };
        assert_eq!(render(Some("k=v")), "k=v, user-agent=a_b_{c}");
        assert_eq!(render(Some("")), "user-agent=a_b_{c}");
        assert_eq!(render(None), "user-agent=a_b_{c}");
    }

    #[test]
    fn ulid_rendering_is_byte_identical_to_rusty_ulid() {
        let values = [
            0u128,
            1,
            0x1f,
            0x20,
            u128::MAX,
            u128::MAX >> 3,
            1 << 125,
            0x0123_4567_89AB_CDEF_FEDC_BA98_7654_3210,
            0x0192_3A4B_5C6D_7E8F_9012_3456_7890_ABCD,
        ];
        for value in values {
            let ulid = Ulid::from(value);
            let ctx = LogContext {
                session_id: ulid,
                request_id: Some(ulid),
                cluster_id: None,
                backend_id: None,
            };
            let expected = ulid.to_string();
            assert_eq!(
                ctx.to_string(),
                format!("[{expected} {expected} - -]"),
                "ULID {value:#x}"
            );
        }
        // A generated ULID carries a real timestamp and random bits.
        for _ in 0..64 {
            let ulid = Ulid::generate();
            let ctx = LogContext {
                session_id: ulid,
                request_id: None,
                cluster_id: None,
                backend_id: None,
            };
            assert_eq!(ctx.to_string(), format!("[{ulid} - - -]"));
        }
    }

    #[test]
    fn socket_addresses_render_like_their_display_or_a_dash() {
        let v4: std::net::SocketAddr = "127.0.0.1:49312".parse().unwrap();
        let v6: std::net::SocketAddr = "[2001:db8::1]:8443".parse().unwrap();
        assert_eq!(LogAddress(Some(v4)).to_string(), "127.0.0.1:49312");
        assert_eq!(LogAddress(Some(v6)).to_string(), "[2001:db8::1]:8443");
        assert_eq!(LogAddress(None).to_string(), "-");
    }

    #[test]
    fn endpoint_status_renders_plain_and_colored() {
        let endpoint = |status| EndpointRecord::Http {
            method: Some("GET"),
            authority: Some("example.com"),
            path: Some("/"),
            status,
            reason: None,
        };
        assert_eq!(format!("{}", endpoint(Some(200))), "example.com GET / 200");
        assert_eq!(format!("{}", endpoint(Some(101))), "example.com GET / 101");
        assert_eq!(format!("{}", endpoint(None)), "example.com GET / -");
        assert_eq!(
            format!("{:#}", endpoint(Some(204))),
            "example.com GET / \x1b[32m204"
        );
        assert_eq!(
            format!("{:#}", endpoint(Some(302))),
            "example.com GET / \x1b[34m302"
        );
        assert_eq!(
            format!("{:#}", endpoint(Some(404))),
            "example.com GET / \x1b[33m404"
        );
        assert_eq!(
            format!("{:#}", endpoint(Some(503))),
            "example.com GET / \x1b[31m503"
        );
        assert_eq!(
            format!("{:#}", endpoint(Some(101))),
            "example.com GET / 101"
        );
        assert_eq!(format!("{:#}", endpoint(None)), "example.com GET / -");
        assert_eq!(format!("{}", EndpointRecord::Tcp), "-");
    }

    #[test]
    fn log_context_display_all_fields_present() {
        let session = Ulid::from(0x01_23_45_67_89_AB_CD_EF_FE_DC_BA_98_76_54_32_10_u128);
        let request = Ulid::from(0x01_23_45_67_89_AB_CD_EF_FE_DC_BA_98_76_54_32_11_u128);
        let ctx = LogContext {
            session_id: session,
            request_id: Some(request),
            cluster_id: Some("cluster-abc"),
            backend_id: Some("backend-1"),
        };
        let rendered = format!("{ctx}");
        assert_eq!(
            rendered,
            format!("[{session} {request} cluster-abc backend-1]")
        );
    }

    #[test]
    fn log_context_display_dashes_when_missing() {
        let session = Ulid::from(0xABCDu128);
        let ctx = LogContext {
            session_id: session,
            request_id: None,
            cluster_id: None,
            backend_id: None,
        };
        assert_eq!(format!("{ctx}"), format!("[{session} - - -]"));
    }

    #[test]
    fn log_context_display_partial() {
        let session = Ulid::from(0x42u128);
        let request = Ulid::from(0x43u128);
        let ctx = LogContext {
            session_id: session,
            request_id: Some(request),
            cluster_id: None,
            backend_id: Some("backend-2"),
        };
        assert_eq!(
            format!("{ctx}"),
            format!("[{session} {request} - backend-2]")
        );
    }
}