فهرست منبع

Feature #TASK_QT-18250 grpc

Carl 7 ماه پیش
والد
کامیت
9f78d11eb5

+ 2 - 2
ebpftracer/ebpf/l7/apm_trace.c

@@ -282,8 +282,8 @@ struct apm_trace_info_t *get_trace_info_by_fd(__u32 pid, __u32 fd) {
 static __inline __attribute__((__always_inline__))
 void cw_save_parent_tracking_span(struct apm_span_context *sc) {
 	struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
-//	__u64 pid_tgid = bpf_get_current_pid_tgid();
-//	cw_bpf_debug("[pid:%d][goid:%d] save psc header", (__u32) (pid_tgid >> 32), trace_key.goid);
+	__u64 pid_tgid = bpf_get_current_pid_tgid();
+	bpf_printk("save [pid:%d][goid:%d] save psc header", (__u32) (pid_tgid >> 32), trace_key.goid);
 //	for (int i = 0; i < APM_APP_ID_SIZE; i++) {
 //		cw_bpf_debug("trace_enter_write - span_id_from = %02x", sc->app_id[i]);
 //	}

+ 19 - 0
ebpftracer/ebpf/utrace/go/include/go_context.h

@@ -205,4 +205,23 @@ static __always_inline void *get_Go_context(void *ctx, int context_pos, const vo
     return ctx_val;
 }
 
+static __always_inline void get_Go_context2(void *ctx,
+                                           int context_pos,
+                                           const volatile u64 context_offset,
+                                           bool passed_as_arg,
+                                           struct go_iface *contextContext) {
+	// Read the argument which is either the context.Context type pointer or pointer to a struct containing the context.Context
+	void *ctx_type_or_struct = get_argument(ctx, context_pos);
+	if (passed_as_arg) {
+		contextContext->type = ctx_type_or_struct;
+		contextContext->data = get_argument(ctx, context_pos + 1);
+	} else {
+		void *context_struct_ptr = (void *)(ctx_type_or_struct + context_offset);
+		bpf_probe_read(&contextContext->type, sizeof(contextContext->type), context_struct_ptr);
+		bpf_probe_read(&contextContext->data,
+		               sizeof(contextContext->data),
+		               get_go_interface_instance(context_struct_ptr));
+	}
+}
+
 #endif

+ 40 - 14
ebpftracer/ebpf/utrace/go/net/grpc.client.probe.bpf.c

@@ -90,6 +90,12 @@ volatile const bool write_status_supported;
 // func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error
 SEC("uprobe/ClientConn_Invoke")
 int uprobe_ClientConn_Invoke(struct pt_regs *ctx) {
+	struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
+//	bpf_printk("get1 %d",trace_key.goid);
+//	void *key1 = (void *)GOROUTINE(ctx);
+//	bpf_printk("key1 %llu",key1);
+
+	return 0;
     // bpf_printk("enter the uprobe_ClientConn_Invoke \n");
     // positions
     u64 clientconn_pos = 1;
@@ -160,6 +166,8 @@ int uprobe_ClientConn_Invoke(struct pt_regs *ctx) {
 // func (cc *ClientConn) Invoke(ctx context.Context, method string, args, reply interface{}, opts ...CallOption) error
 SEC("uprobe/ClientConn_Invoke")
 int uprobe_ClientConn_Invoke_Returns(struct pt_regs *ctx) {
+	return 0;
+
     // bpf_printk("enter the uprobe_ClientConn_Invoke_Returns \n");
     void *key = (void *)GOROUTINE(ctx);
     struct grpc_client_request_t *grpc_span = bpf_map_lookup_elem(&grpc_client_events, &key);
@@ -293,6 +301,21 @@ cw_append_item_to_slice(void *new_item, u32 item_size, void *slice_user_ptr) {
 
 SEC("uprobe/loopyWriter_headerHandler")
 int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx) {
+//	struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
+//	bpf_printk("get2 %d",trace_key.goid);
+
+//	void *key1 = (void *)GOROUTINE(ctx);
+//	bpf_printk("LoopyWriter_HeaderHandler %llu",key1);
+
+	void *headerFrame_ptr2 = get_argument(ctx, 2);
+	u32 stream_id = 0;
+	bpf_probe_read(
+			&stream_id, sizeof(stream_id), (void *)(headerFrame_ptr2 + (headerFrame_streamid_pos)));
+	bpf_printk("LoopyWriter_HeaderHandler stream_id -> %d",stream_id);
+
+//	cw_get_parent_tracking_span();
+	return 0;
+
     void *headerFrame_ptr = get_argument(ctx, 2);
     bpf_printk("enter the get header handler storage\n");
 
@@ -399,19 +422,22 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx) {
 SEC("uprobe/http2Client_NewStream")
 // func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (*Stream, error)
 int uprobe_http2Client_NewStream(struct pt_regs *ctx) {
-    // bpf_printk("enter the uprobe_http2Client_NewStream \n");
-    // struct go_iface go_context = {0};
-    // get_Go_context(ctx, 2, 0, true);
-    // void *httpclient_ptr = get_argument(ctx, 1);
-    // u32 nextid = 0;
-    // bpf_probe_read(&nextid, sizeof(nextid), (void *)(httpclient_ptr + (httpclient_nextid_pos)));
-    // // Get the span context from go context. The mapping is created in the Invoke probe,
-    // // the context here is derived from the Invoke context.
-    // struct span_context *current_span_context = get_parent_span_context(&go_context);
-    // if (current_span_context != NULL) {
-    //     bpf_printk("uprobe_http2Client_NewStream streamid is %d\n", nextid);
-    //     bpf_map_update_elem(&streamid_to_span_contexts, &nextid, current_span_context, 0);
-    // }
+	struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
+//	bpf_printk("http2Client_NewStream goid %d",trace_key.goid);
+	struct go_iface go_context = {0};
+	get_Go_context2(ctx, 2, 0, true, &go_context);
+	void *httpclient_ptr = get_argument(ctx, 1);
+	u32 nextid = 0;
+	bpf_probe_read(&nextid, sizeof(nextid), (void *)(httpclient_ptr + (httpclient_nextid_pos)));
+	// Get the span context from go context. The mapping is created in the Invoke probe,
+	// the context here is derived from the Invoke context.
+	bpf_printk("http2Client_NewStream goid:%d -> stream_id:%d",trace_key.goid,nextid);
+
+	struct span_context *current_span_context = get_parent_span_context(&go_context);
+	if (current_span_context != NULL) {
+		bpf_map_update_elem(&streamid_to_span_contexts, &nextid, current_span_context, 0);
+		bpf_printk("http2Client_NewStream nextid %d",nextid);
+	}
 
-    return 0;
+	return 0;
 }

+ 25 - 35
ebpftracer/ebpf/utrace/go/net/grpc.server.probe.bpf.c

@@ -582,7 +582,7 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
     void *frame_ptr = is_new_frame_pos ? arg4 : arg2;
     struct go_slice header_fields = {};
     bpf_probe_read(&header_fields, sizeof(header_fields), (void *)(frame_ptr + frame_fields_pos));
-    char key[W3C_KEY_LENGTH] = "traceparent";
+    char key[CW_HEADER_KEY_LENGTH] = "cwtrace";
     bpf_printk("enter the uprobe_http2Server_operateHeader\n");
     
     __u32 zero = 0;
@@ -599,46 +599,36 @@ int uprobe_http2Server_operateHeader(struct pt_regs *ctx) {
         struct hpack_header_field hf = {};
         long res =
             bpf_probe_read(&hf, sizeof(hf), (void *)(header_fields.ptr + (i * sizeof(hf))));
-        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;
-                // bpf_probe_read(&headers_frame, sizeof(headers_frame), frame_ptr);
-                // u32 stream_id = 0;
-                // bpf_probe_read(
-                //     &stream_id, sizeof(stream_id), (void *)(headers_frame + frame_stream_id_pod));
-                
-                // 使用 per-cpu array map 存储大变量,避免栈空间超限
-                // u32 zero = 0;
-                // struct grpc_request_t *grpcReq = bpf_map_lookup_elem(&operate_header_storage, &zero);
-                // if (grpcReq == NULL) {
-                //     bpf_printk("grpc:server:operateHeader: failed to get storage");
-                //     return -1;
-                // }
-                
-                // 清零并初始化
-                // __builtin_memset(grpcReq, 0, sizeof(struct grpc_request_t));
-                // w3c_string_to_span_context(val, &grpcReq->psc);
-                // bpf_map_update_elem(&streamid_to_grpc_events, &stream_id, grpcReq, 0);
-            }
-        }
+//        if (hf.name.len == CW_HEADER_KEY_LENGTH && hf.value.len == CW_HEADER_VAL_LENGTH) {
+//            char current_key[CW_HEADER_KEY_LENGTH];
+//            bpf_probe_read(current_key, sizeof(current_key), hf.name.str);
+//
+//	        if (bpf_memcmp(key, current_key, sizeof(key))) {
+//		        find_w3c =1;
+//		        char val[CW_HEADER_VAL_LENGTH];
+//		        bpf_probe_read(val, CW_HEADER_VAL_LENGTH, hf.value.str);
+//
+//		        // Get stream id
+//		        void *headers_frame = NULL;
+//		        bpf_probe_read(&headers_frame, sizeof(headers_frame), frame_ptr);
+//		        u32 stream_id = 0;
+//		        bpf_probe_read(
+//				        &stream_id, sizeof(stream_id), (void *)(headers_frame + frame_stream_id_pod));
+//		        bpf_printk("has header %s",val);
+//		        struct grpc_request_t grpcReq = {};
+////		        w3c_string_to_span_context(val, &grpcReq.psc);
+//		        cw_string_to_span_context(val, cw_parent_span_context);
+//
+//		        bpf_map_update_elem(&streamid_to_grpc_events, &stream_id, &grpcReq, 0);
+//	        }
+//		}
     }
     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);
+     cw_save_parent_tracking_span(cw_parent_span_context);
     return 0;
 }