apm_trace.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Created by Carl.Guo on 2024/5/14.
  3. //
  4. #include "bpf_base.h"
  5. #ifndef EUSPACES_APM_TRACE_H
  6. #define EUSPACES_APM_TRACE_H
  7. #define APM_REMOTE_ADDR_MAX_LEN 256
  8. #define APM_TYPE_FROM_STRING_SIZE 2
  9. #define APM_SAMPLE_STRING_SIZE 2
  10. #define APM_HOST_ID_STRING_SIZE 16
  11. #define APM_APP_ID_STRING_SIZE 16
  12. #define APM_INSTANCE_ID_STRING_SIZE 16
  13. #define APM_TRACE_ID_STRING_SIZE 32
  14. #define APM_ASSUMED_APP_ID_STRING_SIZE 16
  15. #define APM_SPAN_ID_STRING_SIZE 16
  16. #define APM_TYPE_FROM_SIZE 1
  17. #define APM_SAMPLE_SIZE 1
  18. #define APM_HOST_ID_SIZE 8
  19. #define APM_APP_ID_SIZE 8
  20. #define APM_INSTANCE_ID_SIZE 8
  21. #define APM_TRACE_ID_SIZE 16
  22. #define APM_ASSUMED_APP_ID_SIZE 8
  23. #define APM_SPAN_ID_SIZE 8
  24. // cwtrace
  25. #define CW_HEADER_KEY_LENGTH 7
  26. #define CW_HEADER_KEY_VAL "cwtrace"
  27. #define CW_HEADER_KEY_UFIRST_VAL "Cwtrace"
  28. #define CW_HEADER_VAL_LENGTH 123
  29. #define CW_HEADER_LENGTH 123
  30. //#define CW_STREAM_HEADER_LEN 135 // CW_HEADER_KEY_LENGTH + 2 + CW_HEADER_VAL_LENGTH + 2 + 1
  31. #define CW_STREAM_HEADER_LEN (CW_HEADER_KEY_LENGTH + 2 + CW_HEADER_VAL_LENGTH + 2 + 1)
  32. // cwother
  33. // 格式:NN:ParentServiceName:MM[:ParentServiceSysTag]
  34. // NN: 2位数字(ASCII),表示 ParentServiceName 的长度(0-32)
  35. // MM: 2位数字(ASCII),表示 ParentServiceSysTag 的长度(0-32)
  36. // 当 MM=00 时,格式为 NN:ParentServiceName:00(不包含最后的冒号和SysTag)
  37. #define CW_SYS_HEADER_KEY_LENGTH 7
  38. #define CW_SYS_HEADER_KEY_VAL "cwother"
  39. #define CW_SYS_HEADER_KEY_UFIRST_VAL "Cwother"
  40. #define CW_SYS_LEN_PREFIX_LENGTH 2 // 每个长度字段的长度(NN 或 MM)
  41. #define CW_SYS_HEADER_VAL_LENGTH 76
  42. #define MAX_MQ_TOPIC_SIZE 256 // Max MQ topic size (e.g., Kafka topic)
  43. #define MAX_MQ_KEY_SIZE 256 // Max MQ key size (e.g., Kafka message key)
  44. /***********************************************************
  45. * Trace struct
  46. ***********************************************************/
  47. struct apm_trace_key_t {
  48. __u32 tgid;
  49. __u32 pid;
  50. __u64 goid;
  51. __u64 connectid;
  52. };
  53. struct fd_trace_key_t {
  54. __u32 tgid;
  55. __u32 fd;
  56. };
  57. struct thread_trace_key_t {
  58. __u64 pid_tgid;
  59. };
  60. struct goid_trace_key_t {
  61. __u32 tgid;
  62. __u64 goid;
  63. };
  64. struct fd_trace_poll_times_t {
  65. __u32 start_count;
  66. __u32 end_count;
  67. };
  68. //struct apm_span_context {
  69. // unsigned char TraceID[APM_TRACE_ID_SIZE];
  70. // unsigned char SpanID[APM_SPAN_ID_SIZE];
  71. //};
  72. struct apm_span_context {
  73. unsigned char type_from[APM_TYPE_FROM_SIZE];
  74. unsigned char sample[APM_SAMPLE_SIZE];
  75. unsigned char host_id[APM_HOST_ID_SIZE];
  76. unsigned char app_id[APM_APP_ID_SIZE];
  77. unsigned char instance_id[APM_INSTANCE_ID_SIZE];
  78. unsigned char trace_id[APM_TRACE_ID_SIZE];
  79. unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
  80. unsigned char span_id[APM_SPAN_ID_SIZE];
  81. unsigned char sysvs[CW_SYS_HEADER_VAL_LENGTH];
  82. };
  83. // MQ 信息结构体(用于 Kafka、RabbitMQ 等消息队列)
  84. struct mq_info_t {
  85. char topic[MAX_MQ_TOPIC_SIZE];
  86. char key[MAX_MQ_KEY_SIZE];
  87. };
  88. /*
  89. * Whether traceID is zero ?
  90. * For the client to actively send request, set traceID to zero.
  91. */
  92. // bool is_trace_id_zero;
  93. // __u32 update_time; // 从系统开机开始到创建/更新时的间隔时间单位是秒
  94. // __u32 peer_fd; // 用于socket之间的关联
  95. // __u64 thread_trace_id; // 线程追踪ID
  96. // __u64 socket_id; // Records the socket associated when tracing was created (记录创建追踪时关联的socket)
  97. struct apm_trace_info_t {
  98. __u64 trace_id;
  99. __u8 type;
  100. bool routine_reuse;
  101. struct apm_trace_key_t trace_key;
  102. struct fd_trace_key_t fd_trace_key;
  103. struct thread_trace_key_t thread_trace_key;
  104. struct goid_trace_key_t goid_trace_key;
  105. // struct apm_span_context psc;
  106. // struct apm_span_context sc;
  107. };
  108. // ---------- 业务上下文(thread_ctx) ----------
  109. struct thread_ctx_t {
  110. __u64 token; // 追踪 token / trace_id(入口处写入)
  111. __u64 ts_ns; // 最近一次刷新时间
  112. __u64 exp_ns; // 过期时间(now + TTL)
  113. __u32 root_thread; // 主线程
  114. __u32 tgid; // 所属进程
  115. __u32 parent_tid; // 父线程(用于调试/回溯)
  116. __u8 is_main_thread;// 主线程
  117. __u16 level; // 继承层数
  118. struct apm_trace_info_t *trace_info;
  119. struct apm_trace_key_t trace_key;
  120. };
  121. static __always_inline void cw_copy_byte_arrays(unsigned char *src, unsigned char *dst, __u32 size) {
  122. for (int i = 0; i < size; i++) {
  123. dst[i] = src[i];
  124. }
  125. }
  126. // 设置 trace 开始标记
  127. // 用法: SET_TRACE_START(e, PROTOCOL_HTTP)
  128. #define SET_TRACE_START(e, p) do { \
  129. (e)->trace_start = PROTOCOL_TRACE; \
  130. (e)->trace_end = 0; \
  131. (e)->protocol = (p); \
  132. } while (0)
  133. // 设置 trace 结束标记
  134. // 用法: SET_TRACE_END(e, PROTOCOL_HTTP)
  135. #define SET_TRACE_END(e, p) do { \
  136. (e)->trace_start = 0; \
  137. (e)->trace_end = PROTOCOL_TRACE; \
  138. (e)->protocol = (p); \
  139. } while (0)
  140. #define SET_TRACE_METHOD(e) do { \
  141. (e)->trace_start = 0; \
  142. (e)->trace_end = 0; \
  143. } while (0)
  144. #endif //EUSPACES_APM_TRACE_H