Debian Codegraph @

third_party/rust/qlog/README.md

raw
1 The qlog crate is an implementation of the qlog [main logging schema],
2 [QUIC event definitions], and [HTTP/3 and QPACK event definitions].
3 The crate provides a qlog data model that can be used for traces with
4 events. It supports serialization and deserialization but defers logging IO
5 choices to applications.
6
7 The crate uses Serde for conversion between Rust and JSON.
8
9 [main logging schema]: https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-main-schema
10 [QUIC event definitions]:
11 https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-quic-events.html
12 [HTTP/3 and QPACK event definitions]:
13 https://datatracker.ietf.org/doc/html/draft-ietf-quic-qlog-h3-events.html
14
15 Overview
16 --------
17 qlog is a hierarchical logging format, with a rough structure of:
18
19 * Log
20 * Trace(s)
21 * Event(s)
22
23 In practice, a single QUIC connection maps to a single Trace file with one
24 or more Events. Applications can decide whether to combine Traces from
25 different connections into the same Log.
26
27 ## Buffered Traces with standard JSON
28
29 A [`Trace`] is a single JSON object. It contains metadata such as the
30 [`VantagePoint`] of capture and the [`Configuration`], and protocol event
31 data in the [`Event`] array.
32
33 JSON Traces allow applications to appends events to them before eventually
34 being serialized as a complete JSON object.
35
36 ### Creating a Trace
37
38 ```rust
39 let mut trace = qlog::Trace::new(
40 qlog::VantagePoint {
41 name: Some("Example client".to_string()),
42 ty: qlog::VantagePointType::Client,
43 flow: None,
44 },
45 Some("Example qlog trace".to_string()),
46 Some("Example qlog trace description".to_string()),
47 Some(qlog::Configuration {
48 time_offset: Some(0.0),
49 original_uris: None,
50 }),
51 None,
52 );
53 ```
54
55 ### Adding events to a Trace
56
57 Qlog `Event` objects are added to `qlog::Trace.events`.
58
59 The following example demonstrates how to log a qlog QUIC `packet_sent` event
60 containing a single Crypto frame. It constructs the necessary elements of the
61 [`Event`], then appends it to the trace with [`push_event()`].
62
63 ```rust
64 let scid = [0x7e, 0x37, 0xe4, 0xdc, 0xc6, 0x68, 0x2d, 0xa8];
65 let dcid = [0x36, 0xce, 0x10, 0x4e, 0xee, 0x50, 0x10, 0x1c];
66
67 let pkt_hdr = qlog::events::quic::PacketHeader::new(
68 qlog::events::quic::PacketType::Initial,
69 0, // packet_number
70 None, // flags
71 None, // token
72 None, // length
73 Some(0x00000001), // version
74 Some(&scid),
75 Some(&dcid),
76 );
77
78 let frames = vec![qlog::events::quic::QuicFrame::Crypto {
79 offset: 0,
80 length: 0,
81 }];
82
83 let raw = qlog::events::RawInfo {
84 length: Some(1251),
85 payload_length: Some(1224),
86 data: None,
87 };
88
89 let event_data =
90 qlog::events::EventData::PacketSent(qlog::events::quic::PacketSent {
91 header: pkt_hdr,
92 frames: Some(frames.into()),
93 is_coalesced: None,
94 retry_token: None,
95 stateless_reset_token: None,
96 supported_versions: None,
97 raw: Some(raw),
98 datagram_id: None,
99 });
100
101 trace.push_event(qlog::events::Event::with_time(0.0, event_data));
102 ```
103
104 ### Serializing
105
106 The qlog crate has only been tested with `serde_json`, however other serializer
107 targets might work.
108
109 For example, serializing the trace created above:
110
111 ```rust
112 serde_json::to_string_pretty(&trace).unwrap();
113 ```
114
115 would generate the following:
116
117 ```
118 {
119 "vantage_point": {
120 "name": "Example client",
121 "type": "client"
122 },
123 "title": "Example qlog trace",
124 "description": "Example qlog trace description",
125 "configuration": {
126 "time_offset": 0.0
127 },
128 "events": [
129 {
130 "time": 0.0,
131 "name": "transport:packet_sent",
132 "data": {
133 "header": {
134 "packet_type": "initial",
135 "packet_number": 0,
136 "version": "1",
137 "scil": 8,
138 "dcil": 8,
139 "scid": "7e37e4dcc6682da8",
140 "dcid": "36ce104eee50101c"
141 },
142 "raw": {
143 "length": 1251,
144 "payload_length": 1224
145 },
146 "frames": [
147 {
148 "frame_type": "crypto",
149 "offset": 0,
150 "length": 0
151 }
152 ]
153 }
154 }
155 ]
156 }
157 ```
158
159 ## Streaming Traces JSON Text Sequences (JSON-SEQ)
160
161 To help support streaming serialization of qlogs,
162 draft-ietf-quic-qlog-main-schema-01 introduced support for RFC 7464 JSON
163 Text Sequences (JSON-SEQ). The qlog crate supports this format and provides
164 utilities that aid streaming.
165
166 A [`TraceSeq`] contains metadata such as the [`VantagePoint`] of capture and
167 the [`Configuration`]. However, protocol event data is handled as separate
168 lines containing a record separator character, a serialized [`Event`], and a
169 newline.
170
171 ### Creating a TraceSeq
172
173 ``` rust
174 let mut trace = qlog::TraceSeq::new(
175 qlog::VantagePoint {
176 name: Some("Example client".to_string()),
177 ty: qlog::VantagePointType::Client,
178 flow: None,
179 },
180 Some("Example qlog trace".to_string()),
181 Some("Example qlog trace description".to_string()),
182 Some(qlog::Configuration {
183 time_offset: Some(0.0),
184 original_uris: None,
185 }),
186 None,
187 );
188 ```
189
190 Create an object with the [`Write`] trait:
191 ```
192 let mut file = std::fs::File::create("foo.sqlog").unwrap();
193 ```
194
195 Create a [`QlogStreamer`] and start serialization to foo.sqlog
196 using [`start_log()`]:
197
198 ```rust
199 let mut streamer = qlog::QlogStreamer::new(
200 qlog::QLOG_VERSION.to_string(),
201 Some("Example qlog".to_string()),
202 Some("Example qlog description".to_string()),
203 None,
204 std::time::Instant::now(),
205 trace,
206 qlog::EventImportance::Base,
207 Box::new(file),
208 );
209
210 streamer.start_log().ok();
211 ```
212
213 ### Adding simple events
214
215 Once logging has started you can stream events. Simple events can be written in
216 one step using [`add_event()`]:
217
218 ```rust
219 let event_data = qlog::events::EventData::MetricsUpdated(
220 qlog::events::quic::MetricsUpdated {
221 min_rtt: Some(1.0),
222 smoothed_rtt: Some(1.0),
223 latest_rtt: Some(1.0),
224 rtt_variance: Some(1.0),
225 pto_count: Some(1),
226 congestion_window: Some(1234),
227 bytes_in_flight: Some(5678),
228 ssthresh: None,
229 packets_in_flight: None,
230 pacing_rate: None,
231 },
232 );
233
234 let event = qlog::events::Event::with_time(0.0, event_data);
235 streamer.add_event(event).ok();
236 ```
237
238 ### Adding events with frames
239 Some events contain optional arrays of QUIC frames. If the event has
240 `Some(Vec<QuicFrame>)`, even if it is empty, the streamer enters a frame
241 serializing mode that must be finalized before other events can be logged.
242
243 In this example, a `PacketSent` event is created with an empty frame array and
244 frames are written out later:
245
246 ```rust
247 let scid = [0x7e, 0x37, 0xe4, 0xdc, 0xc6, 0x68, 0x2d, 0xa8];
248 let dcid = [0x36, 0xce, 0x10, 0x4e, 0xee, 0x50, 0x10, 0x1c];
249
250 let pkt_hdr = qlog::events::quic::PacketHeader::with_type(
251 qlog::events::quic::PacketType::OneRtt,
252 0,
253 Some(0x00000001),
254 Some(&scid),
255 Some(&dcid),
256 );
257
258 let event_data =
259 qlog::events::EventData::PacketSent(qlog::events::quic::PacketSent {
260 header: pkt_hdr,
261 frames: Some(vec![]),
262 is_coalesced: None,
263 retry_token: None,
264 stateless_reset_token: None,
265 supported_versions: None,
266 raw: None,
267 datagram_id: None,
268 };
269
270 let event = qlog::events::Event::with_time(0.0, event_data);
271
272 streamer.add_event(event).ok();
273 ```
274
275 In this example, the frames contained in the QUIC packet
276 are PING and PADDING. Each frame is written using the
277 [`add_frame()`] method. Frame writing is concluded with
278 [`finish_frames()`].
279
280 ```rust
281 let ping = qlog::events::quic::QuicFrame::Ping;
282 let padding = qlog::events::quic::QuicFrame::Padding;
283
284 streamer.add_frame(ping, false).ok();
285 streamer.add_frame(padding, false).ok();
286
287 streamer.finish_frames().ok();
288 ```
289
290 Once all events have been written, the log
291 can be finalized with [`finish_log()`]:
292
293 ```rust
294 streamer.finish_log().ok();
295 ```
296
297 ### Serializing
298
299 Serialization to JSON occurs as methods on the [`QlogStreamer`]
300 are called. No additional steps are required.
301
302 [`Trace`]: struct.Trace.html
303 [`TraceSeq`]: struct.TraceSeq.html
304 [`VantagePoint`]: struct.VantagePoint.html
305 [`Configuration`]: struct.Configuration.html
306 [`qlog::Trace.events`]: struct.Trace.html#structfield.events
307 [`push_event()`]: struct.Trace.html#method.push_event
308 [`packet_sent_min()`]: event/struct.Event.html#method.packet_sent_min
309 [`QuicFrame::crypto()`]: enum.QuicFrame.html#variant.Crypto
310 [`QlogStreamer`]: struct.QlogStreamer.html
311 [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
312 [`start_log()`]: struct.QlogStreamer.html#method.start_log
313 [`add_event()`]: struct.QlogStreamer.html#method.add_event
314 [`add_frame()`]: struct.QlogStreamer.html#method.add_frame
315 [`finish_frames()`]: struct.QlogStreamer.html#method.finish_frames
316 [`finish_log()`]: struct.QlogStreamer.html#method.finish_log