Kaynağa Gözat

Feature #TASK_QT-18250 提交代码

rock 7 ay önce
ebeveyn
işleme
44f3e02082

+ 90 - 0
ebpftracer/ebpf/utrace/go/net/grpc.server.probe.bpf.c

@@ -249,6 +249,77 @@ static __always_inline int writeStatus(struct pt_regs *ctx, void *status_ptr) {
     }
     req_ptr->has_status = true;
 
+
+    __u64 id = bpf_get_current_pid_tgid();
+	__u32 zero = 0;
+    __u32 fd = 0;
+	__u32 pid, tid;
+	__u32 http_status = 200;
+
+	pid = id >> 32;
+	tid =  (__u32)id;
+    bpf_printk("enter uprobe_server_handleStream2_Returns\n");
+
+    struct l7_request_key k = {};
+    k.pid = pid;
+    k.fd = fd;
+    k.is_tls = 0;
+    k.stream_id = -1;
+    struct apm_trace_info_t * start_trace_info = get_trace_info_by_fd(pid, fd);
+	if (!start_trace_info) {
+		return -1;
+	}
+    __u64 trace_id = start_trace_info->trace_id;
+	__u32 event_count = cw_get_event_count(trace_id);
+    cw_bpf_debug("[uprobeThread/pidpidpidpid][Trace End in l7][HTTP]pid:[%d]--[%lld]", tid, bpf_ktime_get_ns());
+	cw_bpf_debug("[Trace End in l7][Response][HTTP] event_count:%d", event_count);
+	cw_bpf_debug("[Trace End in l7][Response][HTTP] pid:%d,fd:%d,trace_id:%llu", tid, fd, trace_id);
+
+    // 发送事件到用户空间 start
+    struct l7_event *e = bpf_map_lookup_elem(&l7_event_heap, &zero);
+    if (!e) {
+	    cw_clear_trace(pid, tid, fd);
+        return -1;
+    }
+	// parent sc
+	struct apm_span_context *cw_psc = cw_get_parent_tracking_span_by_trace_key(start_trace_info->trace_key);
+	if(cw_psc){
+		cw_copy_byte_arrays(cw_psc->trace_id, e->trace_id_from, APM_TRACE_ID_SIZE);
+		cw_copy_byte_arrays(cw_psc->assumed_app_id, e->called_id, APM_ASSUMED_APP_ID_SIZE);
+		cw_copy_byte_arrays(cw_psc->instance_id, e->instance_id_from, APM_INSTANCE_ID_SIZE);
+		cw_copy_byte_arrays(cw_psc->app_id, e->app_id_from, APM_APP_ID_SIZE);
+		cw_copy_byte_arrays(cw_psc->span_id, e->span_id_from, APM_SPAN_ID_SIZE);
+        cw_copy_byte_arrays(cw_psc->type_from, e->type_from, APM_TYPE_FROM_SIZE);
+	}
+    struct l7_request *req = bpf_map_lookup_elem(&active_l7_requests, &k);
+    if (!req)
+    {
+	    cw_clear_trace(pid, tid, fd);
+        return 0;
+    }
+	e->start_at = req->ns;
+	cw_bpf_debug("req->ns:%llu",req->ns);
+	e->end_at = bpf_ktime_get_ns();
+    e->duration = e->end_at - e->start_at;
+    e->protocol = PROTOCOL_TRACE;
+    e->status = http_status;
+    e->pid = k.pid;
+    e->fd = k.fd;
+    // e->connection_timestamp = get_connection_timestamp(k.pid, k.fd);
+    e->trace_start = 0;
+    e->trace_end = 1;
+    e->trace_id = trace_id;
+    e->payload_size = 0;
+    e->event_count = event_count;
+    // COPY_PAYLOAD(e->payload, size, payload);
+    bpf_map_delete_elem(&active_l7_requests, &k);
+	// 清除事件计数
+	bpf_map_delete_elem(&trace_event_count_heap, &trace_id);
+	// 清除业务层trace信息
+	clear_parent_span_context_by_trace_key(start_trace_info->trace_key);
+	// 清除trace信息
+	cw_clear_trace(pid, tid, fd);
+
     return 0;
 }
 
@@ -447,6 +518,7 @@ lookup:
 // func (t *http2Server) operateHeaders(ctx context.Context, frame *http2.MetaHeadersFrame, handle func(*Stream)) error
 SEC("uprobe/http2Server_operateHeader")
 int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
+    s32 find_w3c = 0;
     void *arg4 = get_argument(ctx, 4);
     void *arg2 = get_argument(ctx, 2);
     void *frame_ptr = is_new_frame_pos ? arg4 : arg2;
@@ -455,6 +527,13 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
     char key[W3C_KEY_LENGTH] = "traceparent";
     bpf_printk("enter the uprobe_http2Server_operateHeader\n");
     
+    __u32 zero = 0;
+    struct apm_span_context *cw_parent_span_context = bpf_map_lookup_elem(&apm_span_context_heap3, &zero);
+    if (cw_parent_span_context == NULL) {
+        return -1;
+    }
+    __builtin_memset(cw_parent_span_context, 0, sizeof(struct apm_span_context));
+    
     for (s32 i = 0; i < MAX_HEADERS; i++) {
         if (i >= header_fields.len) {
             break;
@@ -465,9 +544,14 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
         if (hf.name.len == W3C_KEY_LENGTH && hf.value.len == W3C_VAL_LENGTH) {
             char current_key[W3C_KEY_LENGTH];
             bpf_probe_read(current_key, sizeof(current_key), hf.name.str);
+            
             if (bpf_memcmp(key, current_key, sizeof(key))) {
                 // char val[W3C_VAL_LENGTH];
                 // bpf_probe_read(val, W3C_VAL_LENGTH, hf.value.str);
+            
+                // cw_string_to_span_context(hf.value.str, cw_parent_span_context);
+                // cw_save_parent_tracking_span(cw_parent_span_context);
+                // find_w3c = 1;
 
                 // // Get stream id
                 // void *headers_frame = NULL;
@@ -491,6 +575,12 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
             }
         }
     }
+    // if (find_w3c == 0)
+    // {
+    //     generate_random_bytes(cw_parent_span_context->trace_id, TRACE_ID_SIZE);
+    //     bpf_printk("enter uprobe_http2Server_operateHeader, generate the traceid\n");
+    // }
+    // cw_save_parent_tracking_span(cw_parent_span_context);
 
     return 0;
 }

+ 1 - 1
ebpftracer/tls.go

@@ -33,7 +33,7 @@ const (
 	goTransport           = "net/http.(*Transport).roundTrip"
 	goGrpcServerHandleStream = "google.golang.org/grpc.(*Server).handleStream"
 	goGrpcHttp2OperateHeader = "google.golang.org/grpc/internal/transport.(*http2Server).operateHeaders"
-	goGrpcServerWritestatus = "google.golang.org/grpc/internal/transport.(*http2Server).writeStatus"
+	goGrpcServerWritestatus = "google.golang.org/grpc/internal/transport.(*http2Server).WriteStatus"
 )
 
 var (