apm_trace.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #define MAX_MQ_TOPIC_SIZE 256 // Max MQ topic size (e.g., Kafka topic)
  33. #define MAX_MQ_KEY_SIZE 256 // Max MQ key size (e.g., Kafka message key)
  34. /***********************************************************
  35. * Trace struct
  36. ***********************************************************/
  37. struct apm_trace_key_t {
  38. __u32 tgid;
  39. __u32 pid;
  40. __u64 goid;
  41. __u64 connectid;
  42. };
  43. struct fd_trace_key_t {
  44. __u32 tgid;
  45. __u32 fd;
  46. };
  47. struct thread_trace_key_t {
  48. __u64 pid_tgid;
  49. };
  50. struct goid_trace_key_t {
  51. __u32 tgid;
  52. __u64 goid;
  53. };
  54. struct fd_trace_poll_times_t {
  55. __u32 start_count;
  56. __u32 end_count;
  57. };
  58. //struct apm_span_context {
  59. // unsigned char TraceID[APM_TRACE_ID_SIZE];
  60. // unsigned char SpanID[APM_SPAN_ID_SIZE];
  61. //};
  62. struct apm_span_context {
  63. unsigned char type_from[APM_TYPE_FROM_SIZE];
  64. unsigned char sample[APM_SAMPLE_SIZE];
  65. unsigned char host_id[APM_HOST_ID_SIZE];
  66. unsigned char app_id[APM_APP_ID_SIZE];
  67. unsigned char instance_id[APM_INSTANCE_ID_SIZE];
  68. unsigned char trace_id[APM_TRACE_ID_SIZE];
  69. unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
  70. unsigned char span_id[APM_SPAN_ID_SIZE];
  71. };
  72. // MQ 信息结构体(用于 Kafka、RabbitMQ 等消息队列)
  73. struct mq_info_t {
  74. char topic[MAX_MQ_TOPIC_SIZE];
  75. char key[MAX_MQ_KEY_SIZE];
  76. };
  77. /*
  78. * Whether traceID is zero ?
  79. * For the client to actively send request, set traceID to zero.
  80. */
  81. // bool is_trace_id_zero;
  82. // __u32 update_time; // 从系统开机开始到创建/更新时的间隔时间单位是秒
  83. // __u32 peer_fd; // 用于socket之间的关联
  84. // __u64 thread_trace_id; // 线程追踪ID
  85. // __u64 socket_id; // Records the socket associated when tracing was created (记录创建追踪时关联的socket)
  86. struct apm_trace_info_t {
  87. __u64 trace_id;
  88. __u8 type;
  89. bool routine_reuse;
  90. struct apm_trace_key_t trace_key;
  91. struct fd_trace_key_t fd_trace_key;
  92. struct thread_trace_key_t thread_trace_key;
  93. struct goid_trace_key_t goid_trace_key;
  94. // struct apm_span_context psc;
  95. // struct apm_span_context sc;
  96. };
  97. // ---------- 业务上下文(thread_ctx) ----------
  98. struct thread_ctx_t {
  99. __u64 token; // 追踪 token / trace_id(入口处写入)
  100. __u64 ts_ns; // 最近一次刷新时间
  101. __u64 exp_ns; // 过期时间(now + TTL)
  102. __u32 root_thread; // 主线程
  103. __u32 tgid; // 所属进程
  104. __u32 parent_tid; // 父线程(用于调试/回溯)
  105. __u8 is_main_thread;// 主线程
  106. __u16 level; // 继承层数
  107. struct apm_trace_info_t *trace_info;
  108. struct apm_trace_key_t trace_key;
  109. };
  110. static __always_inline void cw_copy_byte_arrays(unsigned char *src, unsigned char *dst, __u32 size) {
  111. for (int i = 0; i < size; i++) {
  112. dst[i] = src[i];
  113. }
  114. }
  115. #endif //EUSPACES_APM_TRACE_H