@@ -98,6 +98,7 @@ struct InstrumentContext<'a> {
9898 time_hms_ms : String ,
9999 time_datetime : String ,
100100 time_datetime_ms : String ,
101+ process_token : String ,
101102 process_parent_all_json : String ,
102103}
103104
@@ -131,6 +132,11 @@ impl<'a> InstrumentContext<'a> {
131132 time_hms_ms : now. format ( "%H:%M:%S%.3f" ) . to_string ( ) ,
132133 time_datetime : now. format ( "%Y-%m-%d %H:%M:%S" ) . to_string ( ) ,
133134 time_datetime_ms : now. format ( "%Y-%m-%d %H:%M:%S%.3f" ) . to_string ( ) ,
135+ process_token : info
136+ . process_info
137+ . as_ref ( )
138+ . and_then ( |p| p. token . clone ( ) )
139+ . unwrap_or_else ( || NA_STR . to_string ( ) ) ,
134140 process_parent_all_json : Self :: serialize_all_parents ( info. process_info . as_ref ( ) ) ,
135141 }
136142 }
@@ -275,6 +281,7 @@ impl interpolator::Context for InstrumentContext<'_> {
275281 . as_ref ( )
276282 . map_or_else ( Self :: na, |info| Formattable :: display ( & info. pid ) ) ,
277283 ) ,
284+ "process.token" => Some ( Formattable :: display ( & self . process_token ) ) ,
278285 "process.parent.pid" => self . process_parent_field ( 0 , "pid" ) ,
279286 "process.parent.name" => self . process_parent_field ( 0 , "name" ) ,
280287 "process.parent.path" => self . process_parent_field ( 0 , "path" ) ,
@@ -295,7 +302,7 @@ fn test_instrument_formatting() {
295302 local_ip: {ip.local}, conn_type: {conn.type}, \
296303 inbound_type: {inbound.type}, inbound_port: {inbound.port}, inbound_user: {inbound.user}, \
297304 process_name: {process.name}, process_cmdline: {process.cmdline}, process_path: {process.path}, \
298- process_pid: {process.pid}, process_parent_pid: {process.parent.pid}, \
305+ process_pid: {process.pid}, process_token: {process.token}, process_parent_pid: {process.parent.pid}, \
299306 process_parent_name: {process.parent.name}, process_parent_path: {process.parent.path}, \
300307 process_parent_cmdline: {process.parent.cmdline}, process_parent_indexed_name: {process.parents.0.name}, \
301308 process_parent_all_json: {process.parent.all.json}, \
@@ -324,7 +331,7 @@ time: {time.hms_ms}";
324331 local_ip: N/A, conn_type: tcp, \
325332 inbound_type: tun, inbound_port: N/A, inbound_user: N/A, \
326333 process_name: N/A, process_cmdline: N/A, process_path: N/A, \
327- process_pid: N/A, process_parent_pid: N/A, \
334+ process_pid: N/A, process_token: N/A, process_parent_pid: N/A, \
328335 process_parent_name: N/A, process_parent_path: N/A, \
329336 process_parent_cmdline: N/A, process_parent_indexed_name: N/A, \
330337 process_parent_all_json: [], "
@@ -340,6 +347,7 @@ fn mock_process(pid: i32, name: &str, parent: ParentProcess) -> ProcessInfo {
340347 name : name. to_string ( ) ,
341348 cmdline : format ! ( "{name} --serve" ) ,
342349 cwd : format ! ( "/tmp/{name}" ) ,
350+ token : None ,
343351 }
344352}
345353
@@ -436,3 +444,24 @@ fn test_instrument_template_validation_accepts_parent_json() {
436444 let template = "ancestor={process.parent.all.json}" ;
437445 assert ! ( FormattingObject :: new( template. to_string( ) ) . is_ok( ) ) ;
438446}
447+
448+ #[ test]
449+ fn test_instrument_formatting_process_token ( ) {
450+ // token absent → N/A
451+ let info = mock_conn_info ( None ) ;
452+ let rendered = FormattingObject :: format_inner ( "token={process.token}" , & info) . unwrap ( ) ;
453+ assert_eq ! ( rendered, "token=N/A" ) ;
454+
455+ // process present but no token → N/A
456+ let process_no_token = mock_process ( 42 , "curl" , ParentProcess :: None ) ;
457+ let info = mock_conn_info ( Some ( process_no_token) ) ;
458+ let rendered = FormattingObject :: format_inner ( "token={process.token}" , & info) . unwrap ( ) ;
459+ assert_eq ! ( rendered, "token=N/A" ) ;
460+
461+ // process present with token → token value
462+ let mut process_with_token = mock_process ( 42 , "curl" , ParentProcess :: None ) ;
463+ process_with_token. token = Some ( "python" . to_string ( ) ) ;
464+ let info = mock_conn_info ( Some ( process_with_token) ) ;
465+ let rendered = FormattingObject :: format_inner ( "token={process.token}" , & info) . unwrap ( ) ;
466+ assert_eq ! ( rendered, "token=python" ) ;
467+ }
0 commit comments