@@ -32,6 +32,8 @@ enum { k_bpf_traceparent_enabled = 1 };
3232enum { k_bpf_traceparent_enabled = 0 };
3333#endif
3434
35+ volatile const u64 max_transaction_time ;
36+
3537static __always_inline unsigned char * tp_char_buf () {
3638 int zero = 0 ;
3739 return bpf_map_lookup_elem (& tp_char_buf_mem , & zero );
@@ -109,8 +111,8 @@ static __always_inline unsigned char *bpf_strstr_tp_loop__legacy(unsigned char *
109111 return NULL ;
110112}
111113
112- static __always_inline const tp_info_pid_t *
113- find_nginx_parent_trace ( const pid_connection_info_t * p_conn , u16 orig_dport ) {
114+ static __always_inline tp_info_pid_t * find_nginx_parent_trace ( const pid_connection_info_t * p_conn ,
115+ u16 orig_dport ) {
114116 connection_info_part_t client_part = {};
115117 populate_ephemeral_info (& client_part , & p_conn -> conn , orig_dport , p_conn -> pid , FD_CLIENT );
116118 fd_info_t * fd_info = fd_info_for_conn (& client_part );
@@ -127,8 +129,8 @@ find_nginx_parent_trace(const pid_connection_info_t *p_conn, u16 orig_dport) {
127129 return NULL ;
128130}
129131
130- static __always_inline const tp_info_pid_t *
131- find_nodejs_parent_trace ( const pid_connection_info_t * p_conn , u16 orig_dport ) {
132+ static __always_inline tp_info_pid_t * find_nodejs_parent_trace ( const pid_connection_info_t * p_conn ,
133+ u16 orig_dport ) {
132134 connection_info_part_t client_part = {};
133135 populate_ephemeral_info (& client_part , & p_conn -> conn , orig_dport , p_conn -> pid , FD_CLIENT );
134136 fd_info_t * fd_info = fd_info_for_conn (& client_part );
@@ -161,12 +163,12 @@ find_nodejs_parent_trace(const pid_connection_info_t *p_conn, u16 orig_dport) {
161163 return trace_info_for_connection (conn , TRACE_TYPE_SERVER );
162164}
163165
164- static __always_inline const tp_info_pid_t * find_parent_process_trace (trace_key_t * t_key ) {
166+ static __always_inline tp_info_pid_t * find_parent_process_trace (trace_key_t * t_key ) {
165167 // Up to 5 levels of thread nesting allowed
166168 enum { k_max_depth = 5 };
167169
168170 for (u8 i = 0 ; i < k_max_depth ; ++ i ) {
169- const tp_info_pid_t * server_tp = bpf_map_lookup_elem (& server_traces , t_key );
171+ tp_info_pid_t * server_tp = bpf_map_lookup_elem (& server_traces , t_key );
170172
171173 if (server_tp ) {
172174 bpf_dbg_printk ("Found parent trace for pid=%d, ns=%lx, extra_id=%llx" ,
@@ -191,9 +193,9 @@ static __always_inline const tp_info_pid_t *find_parent_process_trace(trace_key_
191193 return NULL ;
192194}
193195
194- static __always_inline const tp_info_pid_t * find_parent_trace (const pid_connection_info_t * p_conn ,
195- u16 orig_dport ) {
196- const tp_info_pid_t * node_tp = find_nodejs_parent_trace (p_conn , orig_dport );
196+ static __always_inline tp_info_pid_t * find_parent_trace (const pid_connection_info_t * p_conn ,
197+ u16 orig_dport ) {
198+ tp_info_pid_t * node_tp = find_nodejs_parent_trace (p_conn , orig_dport );
197199
198200 if (node_tp ) {
199201 return node_tp ;
@@ -208,13 +210,13 @@ static __always_inline const tp_info_pid_t *find_parent_trace(const pid_connecti
208210 t_key .p_key .ns ,
209211 t_key .extra_id );
210212
211- const tp_info_pid_t * nginx_parent = find_nginx_parent_trace (p_conn , orig_dport );
213+ tp_info_pid_t * nginx_parent = find_nginx_parent_trace (p_conn , orig_dport );
212214
213215 if (nginx_parent ) {
214216 return nginx_parent ;
215217 }
216218
217- const tp_info_pid_t * proc_parent = find_parent_process_trace (& t_key );
219+ tp_info_pid_t * proc_parent = find_parent_process_trace (& t_key );
218220
219221 if (proc_parent ) {
220222 return proc_parent ;
@@ -382,13 +384,33 @@ static __always_inline u8 find_trace_for_server_request(connection_info_t *conn,
382384 return found_tp ;
383385}
384386
387+ static __always_inline u8 should_be_in_same_transaction (const tp_info_t * parent_tp ,
388+ const tp_info_t * child_tp ) {
389+ if (child_tp -> ts < parent_tp -> ts ) {
390+ return 0 ;
391+ }
392+
393+ u64 diff = child_tp -> ts - parent_tp -> ts ;
394+
395+ return diff < max_transaction_time ;
396+ }
397+
385398static __always_inline u8 find_trace_for_client_request (const pid_connection_info_t * p_conn ,
386399 u16 orig_dport ,
387400 tp_info_t * tp ) {
388- const tp_info_pid_t * server_tp = find_parent_trace (p_conn , orig_dport );
401+ tp_info_pid_t * server_tp = find_parent_trace (p_conn , orig_dport );
389402
390403 if (server_tp && server_tp -> valid && valid_trace (server_tp -> tp .trace_id )) {
391404 bpf_dbg_printk ("Found existing server tp for client call" );
405+
406+ if (!should_be_in_same_transaction (& server_tp -> tp , tp )) {
407+ bpf_dbg_printk ("Parent and child are too far apart, marking server trace as invalid" );
408+ bpf_dbg_printk (
409+ "%lld >>> %lld (max: %lld)" , tp -> ts , server_tp -> tp .ts , max_transaction_time );
410+ server_tp -> valid = 0 ;
411+ return 0 ;
412+ }
413+
392414 __builtin_memcpy (tp -> trace_id , server_tp -> tp .trace_id , sizeof (tp -> trace_id ));
393415 __builtin_memcpy (tp -> parent_id , server_tp -> tp .span_id , sizeof (tp -> parent_id ));
394416 return 1 ;
0 commit comments