Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cleanup prematurely #619

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions bpf/go_grpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {

grpc_srv_func_invocation_t *invocation =
bpf_map_lookup_elem(&ongoing_grpc_server_requests, &goroutine_addr);
bpf_map_delete_elem(&ongoing_grpc_server_requests, &goroutine_addr);
if (invocation == NULL) {
bpf_dbg_printk("can't read grpc invocation metadata");
return 0;
goto done;
}

u16 *status = bpf_map_lookup_elem(&ongoing_grpc_request_status, &goroutine_addr);
bpf_map_delete_elem(&ongoing_grpc_request_status, &goroutine_addr);
if (status == NULL) {
bpf_dbg_printk("can't read grpc invocation status");
return 0;
goto done;
}

void *stream_ptr = (void *)invocation->stream;
Expand All @@ -153,7 +151,7 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {
http_request_trace *trace = bpf_ringbuf_reserve(&events, sizeof(http_request_trace), 0);
if (!trace) {
bpf_dbg_printk("can't reserve space in the ringbuffer");
return 0;
goto done;
}
task_pid(&trace->pid);
trace->type = EVENT_GRPC_REQUEST;
Expand All @@ -172,7 +170,7 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {
if (!read_go_str("grpc method", stream_ptr, grpc_stream_method_ptr_pos, &trace->path, sizeof(trace->path))) {
bpf_printk("can't read grpc transport.Stream.Method");
bpf_ringbuf_discard(trace, 0);
return 0;
goto done;
}

void *st_ptr = 0;
Expand Down Expand Up @@ -203,7 +201,7 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {
if (!read_go_byte_arr("grpc peer ptr", peer_ptr, tcp_addr_ip_ptr_pos, &trace->remote_addr, &remote_addr_len, sizeof(trace->remote_addr))) {
bpf_printk("can't read grpc peer ptr");
bpf_ringbuf_discard(trace, 0);
return 0;
goto done;
}
trace->remote_addr_len = remote_addr_len;
}
Expand All @@ -217,7 +215,7 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {
if (!read_go_byte_arr("grpc host ptr", host_ptr, tcp_addr_ip_ptr_pos, &trace->host, &host_len, sizeof(trace->host))) {
bpf_printk("can't read grpc host ptr");
bpf_ringbuf_discard(trace, 0);
return 0;
goto done;
}
trace->host_len = host_len;

Expand All @@ -234,6 +232,10 @@ int uprobe_server_handleStream_return(struct pt_regs *ctx) {
// submit the completed trace via ringbuffer
bpf_ringbuf_submit(trace, get_flags());

done:
bpf_map_delete_elem(&ongoing_grpc_server_requests, &goroutine_addr);
bpf_map_delete_elem(&ongoing_grpc_request_status, &goroutine_addr);

return 0;
}

Expand Down Expand Up @@ -348,17 +350,16 @@ int uprobe_ClientConn_Invoke_return(struct pt_regs *ctx) {

grpc_client_func_invocation_t *invocation =
bpf_map_lookup_elem(&ongoing_grpc_client_requests, &goroutine_addr);
bpf_map_delete_elem(&ongoing_grpc_client_requests, &goroutine_addr);

if (invocation == NULL) {
bpf_dbg_printk("can't read grpc client invocation metadata");
return 0;
goto done;
}

http_request_trace *trace = bpf_ringbuf_reserve(&events, sizeof(http_request_trace), 0);
if (!trace) {
bpf_dbg_printk("can't reserve space in the ringbuffer");
return 0;
goto done;
}

task_pid(&trace->pid);
Expand All @@ -381,14 +382,14 @@ int uprobe_ClientConn_Invoke_return(struct pt_regs *ctx) {
if (!read_go_str_n("method", method_ptr, (u64)method_len, &trace->path, sizeof(trace->path))) {
bpf_printk("can't read grpc client method");
bpf_ringbuf_discard(trace, 0);
return 0;
goto done;
}

// Get the host information of the remote
if (!read_go_str("host", cc_ptr, grpc_client_target_ptr_pos, &trace->host, sizeof(trace->host))) {
bpf_printk("can't read http Request.Host");
bpf_ringbuf_discard(trace, 0);
return 0;
goto done;
}

trace->tp = invocation->tp;
Expand All @@ -398,6 +399,8 @@ int uprobe_ClientConn_Invoke_return(struct pt_regs *ctx) {
// submit the completed trace via ringbuffer
bpf_ringbuf_submit(trace, get_flags());

done:
bpf_map_delete_elem(&ongoing_grpc_client_requests, &goroutine_addr);
return 0;
}

Expand Down Expand Up @@ -550,6 +553,7 @@ int uprobe_hpack_Encoder_WriteField(struct pt_regs *ctx) {

if (val_len <= 0) {
bpf_dbg_printk("Encoded traceparent value too large or empty");
bpf_map_delete_elem(&ongoing_grpc_header_writes, &goroutine_addr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was simply missed cleanup

return 0;
}

Expand Down
Loading
Loading