// // Created by Carl.Guo on 2024/5/14. // #include "bpf_base.h" #ifndef EUSPACES_APM_TRACE_H #define EUSPACES_APM_TRACE_H #define APM_REMOTE_ADDR_MAX_LEN 256 #define APM_TYPE_FROM_STRING_SIZE 2 #define APM_SAMPLE_STRING_SIZE 2 #define APM_HOST_ID_STRING_SIZE 16 #define APM_APP_ID_STRING_SIZE 16 #define APM_INSTANCE_ID_STRING_SIZE 16 #define APM_TRACE_ID_STRING_SIZE 32 #define APM_ASSUMED_APP_ID_STRING_SIZE 16 #define APM_SPAN_ID_STRING_SIZE 16 #define APM_TYPE_FROM_SIZE 1 #define APM_SAMPLE_SIZE 1 #define APM_HOST_ID_SIZE 8 #define APM_APP_ID_SIZE 8 #define APM_INSTANCE_ID_SIZE 8 #define APM_TRACE_ID_SIZE 16 #define APM_ASSUMED_APP_ID_SIZE 8 #define APM_SPAN_ID_SIZE 8 // cwtrace #define CW_HEADER_KEY_LENGTH 7 #define CW_HEADER_KEY_VAL "cwtrace" #define CW_HEADER_KEY_UFIRST_VAL "Cwtrace" #define CW_HEADER_VAL_LENGTH 123 #define CW_HEADER_LENGTH 123 //#define CW_STREAM_HEADER_LEN 135 // CW_HEADER_KEY_LENGTH + 2 + CW_HEADER_VAL_LENGTH + 2 + 1 #define CW_STREAM_HEADER_LEN (CW_HEADER_KEY_LENGTH + 2 + CW_HEADER_VAL_LENGTH + 2 + 1) #define MAX_MQ_TOPIC_SIZE 256 // Max MQ topic size (e.g., Kafka topic) #define MAX_MQ_KEY_SIZE 256 // Max MQ key size (e.g., Kafka message key) /*********************************************************** * Trace struct ***********************************************************/ struct apm_trace_key_t { __u32 tgid; __u32 pid; __u64 goid; __u64 connectid; }; struct fd_trace_key_t { __u32 tgid; __u32 fd; }; struct thread_trace_key_t { __u64 pid_tgid; }; struct goid_trace_key_t { __u32 tgid; __u64 goid; }; struct fd_trace_poll_times_t { __u32 start_count; __u32 end_count; }; //struct apm_span_context { // unsigned char TraceID[APM_TRACE_ID_SIZE]; // unsigned char SpanID[APM_SPAN_ID_SIZE]; //}; struct apm_span_context { unsigned char type_from[APM_TYPE_FROM_SIZE]; unsigned char sample[APM_SAMPLE_SIZE]; unsigned char host_id[APM_HOST_ID_SIZE]; unsigned char app_id[APM_APP_ID_SIZE]; unsigned char instance_id[APM_INSTANCE_ID_SIZE]; unsigned char trace_id[APM_TRACE_ID_SIZE]; unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE]; unsigned char span_id[APM_SPAN_ID_SIZE]; }; // MQ 信息结构体(用于 Kafka、RabbitMQ 等消息队列) struct mq_info_t { char topic[MAX_MQ_TOPIC_SIZE]; char key[MAX_MQ_KEY_SIZE]; }; /* * Whether traceID is zero ? * For the client to actively send request, set traceID to zero. */ // bool is_trace_id_zero; // __u32 update_time; // 从系统开机开始到创建/更新时的间隔时间单位是秒 // __u32 peer_fd; // 用于socket之间的关联 // __u64 thread_trace_id; // 线程追踪ID // __u64 socket_id; // Records the socket associated when tracing was created (记录创建追踪时关联的socket) struct apm_trace_info_t { __u64 trace_id; __u8 type; bool routine_reuse; struct apm_trace_key_t trace_key; struct fd_trace_key_t fd_trace_key; struct thread_trace_key_t thread_trace_key; struct goid_trace_key_t goid_trace_key; // struct apm_span_context psc; // struct apm_span_context sc; }; // ---------- 业务上下文(thread_ctx) ---------- struct thread_ctx_t { __u64 token; // 追踪 token / trace_id(入口处写入) __u64 ts_ns; // 最近一次刷新时间 __u64 exp_ns; // 过期时间(now + TTL) __u32 root_thread; // 主线程 __u32 tgid; // 所属进程 __u32 parent_tid; // 父线程(用于调试/回溯) __u8 is_main_thread;// 主线程 __u16 level; // 继承层数 struct apm_trace_info_t *trace_info; struct apm_trace_key_t trace_key; }; static __always_inline void cw_copy_byte_arrays(unsigned char *src, unsigned char *dst, __u32 size) { for (int i = 0; i < size; i++) { dst[i] = src[i]; } } #endif //EUSPACES_APM_TRACE_H