Browse Source

Fixed #JiraBug28447 Euspace-resin应用抓取到的trace_id均为0-在http请求入口生成横向串联的trace_id

Tom 1 year ago
parent
commit
91aeee6514
1 changed files with 36 additions and 0 deletions
  1. 36 0
      ebpftracer/ebpf/l7/l7.c

+ 36 - 0
ebpftracer/ebpf/l7/l7.c

@@ -243,6 +243,19 @@ struct l7_user_msghdr {
     __u32 msg_flags;
 };
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+	__type(key, int);
+	__type(value, struct apm_span_context);
+	__uint(max_entries, 1);
+} apm_span_context_heap3 SEC(".maps");
+
+#define TRACE_ID_SIZE 16
+static __inline __u32 has_cw_header(const char *data);
+static __always_inline void cw_string_to_span_context(char *str, struct apm_span_context *ctx);
+static __always_inline void generate_random_bytes(unsigned char *buff, __u32 size);
+static __inline __attribute__((__always_inline__)) void cw_save_parent_tracking_span(struct apm_span_context *sc);
+
 static inline __attribute__((__always_inline__))
 void send_event(void *ctx, struct l7_event *e, struct connection_id cid, struct connection *conn) {
     e->connection_timestamp = conn->timestamp;
@@ -827,6 +840,29 @@ int trace_exit_read(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long int ret)
         // 入口方法缓存
 //        bpf_map_update_elem(&trace_info_heap, &trace_key, &trace_info, BPF_NOEXIST);
 //	    bpf_map_update_elem(&fd_trace_info_heap, &fd_trace_key, &trace_info, BPF_NOEXIST);
+
+        // ---------- 在http请求入口生成 横向串联的trace_id start ----------
+
+        __u32 key = 0;
+        __u32 offset = has_cw_header(req->payload);
+//        cw_bpf_debug("pid:[%d],offset:[%d]\n",tid,offset);
+        struct apm_span_context *cw_parent_span_context = bpf_map_lookup_elem(&apm_span_context_heap3, &key);
+         if (cw_parent_span_context == NULL) {
+             return -1;
+         }
+         __builtin_memset(cw_parent_span_context, 0, sizeof(struct apm_span_context));
+
+         if (offset >0) {
+             cw_string_to_span_context(&payload[offset], cw_parent_span_context);
+         } else {
+             generate_random_bytes(cw_parent_span_context->trace_id, TRACE_ID_SIZE);
+         }
+        // 保存 trace_id 到psc
+        cw_save_parent_tracking_span(cw_parent_span_context);
+        cw_bpf_debug("[Trace Start in l7][HTTP]pid:[%d] trace_id:[%llu]\n", tid, cw_parent_span_context->trace_id);
+
+        // ---------- 在http请求入口生成 横向串联的trace_id end ----------
+
         bpf_perf_event_output(ctx, &l7_events, BPF_F_CURRENT_CPU, e, sizeof(*e));
         cw_bpf_debug("[Receive][HTTP] to user space");