How to parse netlog events and handle sessions
masterNetlog files contain various events that can be parsed into Rust structures using Serde.
Workflow
- Read Header: Use
read_netlog_recordto get the raw bytes of a record. Useserde_json::from_sliceto parse the record into anEventHeader. - Populate Strings: Call
event_hdr.populate_strings(&constants)to resolve compressed strings using the constants extracted from the file header. - Handle Session Events: Netlogs often contain session-specific data. When an
EventHeaderindicates aPHASE_BEGINand a specific type (likeHTTP2_SESSIONorQUIC_SESSION), you can deserialize the same record into specialized event types likeHttp2SessionEventorQuicSessionEvent. - Generic Parsing: Alternatively, use
netlog::parse_event(&event_hdr, &record)to attempt to parse the event into a more general structure.
// Example of handling a specific session type
if event_hdr.phase_string == "PHASE_BEGIN" {
match event_hdr.ty_string.as_str() {
"HTTP2_SESSION" => {
let ev: Http2SessionEvent = serde_json::from_slice(&record).unwrap();
// Handle HTTP2 session
},
"QUIC_SESSION" => {
let ev: QuicSessionEvent = serde_json::from_slice(&record).unwrap();
// Handle QUIC session
},
_ => (),
}
}