Skip to content

Commit

Permalink
Isolate BPF_TRACEPARENT macro
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Dec 13, 2024
1 parent 6643e14 commit 7c6d1f8
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 51 deletions.
66 changes: 33 additions & 33 deletions bpf/protocol_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,46 @@ static __always_inline void http_get_or_create_trace_info(http_connection_metada
//make_tp_string(tp_buf, &tp_p->tp);
//bpf_dbg_printk("tp: %s", tp_buf);

#ifdef BPF_TRACEPARENT
// The below buffer scan can be expensive on high volume of requests. We make it optional
// for customers to enable it. Off by default.
if (!capture_header_buffer) {
if (meta) {
u32 type = trace_type_from_meta(meta);
set_trace_info_for_connection(conn, type, tp_p);
server_or_client_trace(meta->type, conn, tp_p);
if (k_bpf_traceparent_enabled) {
// The below buffer scan can be expensive on high volume of requests. We make it optional
// for customers to enable it. Off by default.
if (!capture_header_buffer) {
if (meta) {
u32 type = trace_type_from_meta(meta);
set_trace_info_for_connection(conn, type, tp_p);
server_or_client_trace(meta->type, conn, tp_p);
}
return;
}
return;
}

unsigned char *buf = tp_char_buf();
if (buf) {
int buf_len = bytes_len;
bpf_clamp_umax(buf_len, TRACE_BUF_SIZE - 1);

bpf_probe_read(buf, buf_len, u_buf);
unsigned char *res = bpf_strstr_tp_loop(buf, buf_len);

if (res) {
bpf_dbg_printk("Found traceparent %s", res);
unsigned char *t_id = extract_trace_id(res);
unsigned char *s_id = extract_span_id(res);
unsigned char *f_id = extract_flags(res);

decode_hex(tp_p->tp.trace_id, t_id, TRACE_ID_CHAR_LEN);
decode_hex((unsigned char *)&tp_p->tp.flags, f_id, FLAGS_CHAR_LEN);
if (meta && meta->type == EVENT_HTTP_CLIENT) {
decode_hex(tp_p->tp.span_id, s_id, SPAN_ID_CHAR_LEN);
unsigned char *buf = tp_char_buf();
if (buf) {
int buf_len = bytes_len;
bpf_clamp_umax(buf_len, TRACE_BUF_SIZE - 1);

bpf_probe_read(buf, buf_len, u_buf);
unsigned char *res = bpf_strstr_tp_loop(buf, buf_len);

if (res) {
bpf_dbg_printk("Found traceparent %s", res);
unsigned char *t_id = extract_trace_id(res);
unsigned char *s_id = extract_span_id(res);
unsigned char *f_id = extract_flags(res);

decode_hex(tp_p->tp.trace_id, t_id, TRACE_ID_CHAR_LEN);
decode_hex((unsigned char *)&tp_p->tp.flags, f_id, FLAGS_CHAR_LEN);
if (meta && meta->type == EVENT_HTTP_CLIENT) {
decode_hex(tp_p->tp.span_id, s_id, SPAN_ID_CHAR_LEN);
} else {
decode_hex(tp_p->tp.parent_id, s_id, SPAN_ID_CHAR_LEN);
}
} else {
decode_hex(tp_p->tp.parent_id, s_id, SPAN_ID_CHAR_LEN);
bpf_dbg_printk("No traceparent, making a new trace_id", res);
}
} else {
bpf_dbg_printk("No traceparent, making a new trace_id", res);
return;
}
} else {
return;
}
#endif

if (meta) {
u32 type = trace_type_from_meta(meta);
Expand Down
16 changes: 14 additions & 2 deletions bpf/trace_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "ringbuf.h"
#include "pin_internal.h"

#ifdef BPF_TRACEPARENT
enum { k_bpf_traceparent_enabled = 1 };
#else
enum { k_bpf_traceparent_enabled = 0 };
#endif

typedef struct trace_key {
pid_key_t p_key; // pid key as seen by the userspace (for example, inside its container)
u64 extra_id; // pids namespace for the process
Expand Down Expand Up @@ -68,8 +74,11 @@ struct callback_ctx {
u32 pos;
};

#ifdef BPF_TRACEPARENT
static int tp_match(u32 index, void *data) {
if (!k_bpf_traceparent_enabled) {
return 0;
}

if (index >= (TRACE_BUF_SIZE - TRACE_PARENT_HEADER_LEN)) {
return 1;
}
Expand All @@ -86,6 +95,10 @@ static int tp_match(u32 index, void *data) {
}

static __always_inline unsigned char *bpf_strstr_tp_loop(unsigned char *buf, int buf_len) {
if (!k_bpf_traceparent_enabled) {
return NULL;
}

struct callback_ctx data = {.buf = buf, .pos = 0};

u32 nr_loops = (u32)buf_len;
Expand All @@ -98,7 +111,6 @@ static __always_inline unsigned char *bpf_strstr_tp_loop(unsigned char *buf, int

return NULL;
}
#endif

static __always_inline tp_info_pid_t *find_parent_trace(pid_connection_info_t *p_conn) {
trace_key_t t_key = {0};
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/generictracer/bpf_arm64_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/generictracer/bpf_debug_arm64_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/generictracer/bpf_debug_x86_bpfel.o
Git LFS file not shown
4 changes: 2 additions & 2 deletions pkg/internal/ebpf/generictracer/bpf_tp_arm64_bpfel.o
Git LFS file not shown
4 changes: 2 additions & 2 deletions pkg/internal/ebpf/generictracer/bpf_tp_debug_arm64_bpfel.o
Git LFS file not shown
4 changes: 2 additions & 2 deletions pkg/internal/ebpf/generictracer/bpf_tp_debug_x86_bpfel.o
Git LFS file not shown
4 changes: 2 additions & 2 deletions pkg/internal/ebpf/generictracer/bpf_tp_x86_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/generictracer/bpf_x86_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/tctracer/bpf_arm64_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/tctracer/bpf_debug_arm64_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/tctracer/bpf_debug_x86_bpfel.o
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/internal/ebpf/tctracer/bpf_x86_bpfel.o
Git LFS file not shown

0 comments on commit 7c6d1f8

Please sign in to comment.