|
|
@@ -1,81 +1,20 @@
|
|
|
//
|
|
|
// Created by Carl.Guo on 2024/4/1.
|
|
|
//
|
|
|
-/***********************************************************
|
|
|
- * Trace struct
|
|
|
- ***********************************************************/
|
|
|
-struct apm_trace_key_t {
|
|
|
- __u32 tgid;
|
|
|
- __u32 pid;
|
|
|
- __u64 goid;
|
|
|
-};
|
|
|
-
|
|
|
-struct fd_trace_key_t {
|
|
|
- __u32 tgid;
|
|
|
- __u32 fd;
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-//#define APM_TRACE_ID_SIZE 16
|
|
|
-//#define APM_SPAN_ID_SIZE 8
|
|
|
-#define APM_REMOTE_ADDR_MAX_LEN 256
|
|
|
-
|
|
|
-#define APM_TYPE_FROM_STRING_SIZE 2
|
|
|
-#define APM_SAMPLE_STRING_SIZE 2
|
|
|
-#define APM_HOST_ID_STRING_SIZE 16
|
|
|
-#define APM_APP_ID_STRING_SIZE 16
|
|
|
-#define APM_INSTANCE_ID_STRING_SIZE 16
|
|
|
-#define APM_TRACE_ID_STRING_SIZE 32
|
|
|
-#define APM_ASSUMED_APP_ID_STRING_SIZE 16
|
|
|
-#define APM_SPAN_ID_STRING_SIZE 16
|
|
|
-
|
|
|
-#define APM_TYPE_FROM_SIZE 1
|
|
|
-#define APM_SAMPLE_SIZE 1
|
|
|
-#define APM_HOST_ID_SIZE 8
|
|
|
-#define APM_APP_ID_SIZE 8
|
|
|
-#define APM_INSTANCE_ID_SIZE 8
|
|
|
-#define APM_TRACE_ID_SIZE 16
|
|
|
-#define APM_ASSUMED_APP_ID_SIZE 8
|
|
|
-#define APM_SPAN_ID_SIZE 8
|
|
|
-
|
|
|
-#define CW_HEADER_LENGTH 123
|
|
|
-
|
|
|
-//struct apm_span_context {
|
|
|
-// unsigned char TraceID[APM_TRACE_ID_SIZE];
|
|
|
-// unsigned char SpanID[APM_SPAN_ID_SIZE];
|
|
|
-//};
|
|
|
-
|
|
|
-struct apm_span_context {
|
|
|
- unsigned char type_from[APM_TYPE_FROM_SIZE];
|
|
|
- unsigned char sample[APM_SAMPLE_SIZE];
|
|
|
- unsigned char host_id[APM_HOST_ID_SIZE];
|
|
|
- unsigned char app_id[APM_APP_ID_SIZE];
|
|
|
- unsigned char instance_id[APM_INSTANCE_ID_SIZE];
|
|
|
- unsigned char trace_id[APM_TRACE_ID_SIZE];
|
|
|
- unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
|
|
|
- unsigned char span_id[APM_SPAN_ID_SIZE];
|
|
|
-};
|
|
|
-
|
|
|
-struct apm_trace_info_t {
|
|
|
- /*
|
|
|
- * Whether traceID is zero ?
|
|
|
- * For the client to actively send request, set traceID to zero.
|
|
|
- */
|
|
|
- // bool is_trace_id_zero;
|
|
|
- // __u32 update_time; // 从系统开机开始到创建/更新时的间隔时间单位是秒
|
|
|
- // __u32 peer_fd; // 用于socket之间的关联
|
|
|
- // __u64 thread_trace_id; // 线程追踪ID
|
|
|
- // __u64 socket_id; // Records the socket associated when tracing was created (记录创建追踪时关联的socket)
|
|
|
- __u64 trace_id;
|
|
|
- struct apm_span_context psc;
|
|
|
-};
|
|
|
|
|
|
struct {
|
|
|
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
|
|
__uint(key_size, sizeof(struct apm_trace_key_t));
|
|
|
__uint(value_size, sizeof(struct apm_span_context));
|
|
|
__uint(max_entries, 1);
|
|
|
-} apm_span_context_map SEC(".maps");
|
|
|
+} apm_parent_span_context_map SEC(".maps");
|
|
|
+
|
|
|
+struct {
|
|
|
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
|
|
|
+ __uint(key_size, sizeof(struct apm_trace_key_t));
|
|
|
+ __uint(value_size, sizeof(struct apm_span_context));
|
|
|
+ __uint(max_entries, 1);
|
|
|
+} apm_current_span_context_map SEC(".maps");
|
|
|
|
|
|
struct {
|
|
|
__uint(type, BPF_MAP_TYPE_LRU_HASH);
|
|
|
@@ -153,7 +92,7 @@ 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);
|
|
|
long err = 0;
|
|
|
- err = bpf_map_update_elem(&apm_span_context_map, &trace_key, sc, BPF_ANY);
|
|
|
+ err = bpf_map_update_elem(&apm_parent_span_context_map, &trace_key, sc, BPF_ANY);
|
|
|
if (err != 0) {
|
|
|
bpf_printk("Failed to update tracked_spans map: %ld", err);
|
|
|
return;
|
|
|
@@ -164,11 +103,11 @@ static __inline __attribute__((__always_inline__))
|
|
|
struct apm_span_context *cw_get_parent_tracking_span() {
|
|
|
struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
struct apm_span_context *apm_sc = {0};
|
|
|
- struct apm_span_context *span_contexts = bpf_map_lookup_elem(&apm_span_context_map, &trace_key);
|
|
|
+ struct apm_span_context *span_contexts = bpf_map_lookup_elem(&apm_parent_span_context_map, &trace_key);
|
|
|
bpf_printk("-------");
|
|
|
|
|
|
if (span_contexts) {
|
|
|
- for (int i = 0; i < APM_TYPE_FROM_SIZE; i++) {
|
|
|
+ /*for (int i = 0; i < APM_TYPE_FROM_SIZE; i++) {
|
|
|
bpf_printk("type_from[%d] = %02x", i, span_contexts->type_from[i]);
|
|
|
}
|
|
|
for (int i = 0; i < APM_SAMPLE_SIZE; i++) {
|
|
|
@@ -191,7 +130,7 @@ struct apm_span_context *cw_get_parent_tracking_span() {
|
|
|
}
|
|
|
for (int i = 0; i < APM_SPAN_ID_SIZE; i++) {
|
|
|
bpf_printk("span_id[%d] = %02x", i, span_contexts->span_id[i]);
|
|
|
- }
|
|
|
+ }*/
|
|
|
apm_sc = span_contexts;
|
|
|
}
|
|
|
bpf_printk("-------end");
|
|
|
@@ -200,9 +139,16 @@ struct apm_span_context *cw_get_parent_tracking_span() {
|
|
|
}
|
|
|
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
-__u64 clear_span_context() {
|
|
|
+__u64 clear_parent_span_context() {
|
|
|
+ struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
+ bpf_map_delete_elem(&apm_parent_span_context_map, &trace_key);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+__u64 clear_current_span_context() {
|
|
|
struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
- bpf_map_delete_elem(&apm_span_context_map, &trace_key);
|
|
|
+ bpf_map_delete_elem(&apm_current_span_context_map, &trace_key);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -214,4 +160,56 @@ __u64 clear_trace(__u32 pid, __u32 tid, __u32 fd) {
|
|
|
bpf_map_delete_elem(&trace_info_heap, &trace_key);
|
|
|
bpf_map_delete_elem(&fd_trace_info_heap, &fd_trace_key);
|
|
|
return 0;
|
|
|
+}
|
|
|
+
|
|
|
+// Client当前sc
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+void cw_save_current_tracking_span(struct apm_span_context *sc) {
|
|
|
+ struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
+ long err = 0;
|
|
|
+ err = bpf_map_update_elem(&apm_current_span_context_map, &trace_key, sc, BPF_ANY);
|
|
|
+ if (err != 0) {
|
|
|
+ bpf_printk("Failed to update apm_current_span_context_map map: %ld", err);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+struct apm_span_context *cw_get_current_tracking_span() {
|
|
|
+ struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
+ struct apm_span_context *apm_sc = {0};
|
|
|
+ struct apm_span_context *span_contexts = bpf_map_lookup_elem(&apm_current_span_context_map, &trace_key);
|
|
|
+ bpf_printk("-------");
|
|
|
+
|
|
|
+ if (span_contexts) {
|
|
|
+ /*for (int i = 0; i < APM_TYPE_FROM_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-type_from[%d] = %02x", i, span_contexts->type_from[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_SAMPLE_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-sample[%d] = %02x", i, span_contexts->sample[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_HOST_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-host_id[%d] = %02x", i, span_contexts->host_id[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_APP_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-app_id[%d] = %02x", i, span_contexts->app_id[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_INSTANCE_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-instance_id[%d] = %02x", i, span_contexts->instance_id[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_TRACE_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-trace_id[%d] = %02x", i, span_contexts->trace_id[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_ASSUMED_APP_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-assumed_app_id[%d] = %02x", i, span_contexts->assumed_app_id[i]);
|
|
|
+ }
|
|
|
+ for (int i = 0; i < APM_SPAN_ID_SIZE; i++) {
|
|
|
+ bpf_printk("cw_get_current_tracking_span-span_id[%d] = %02x", i, span_contexts->span_id[i]);
|
|
|
+ }*/
|
|
|
+ apm_sc = span_contexts;
|
|
|
+ }
|
|
|
+ bpf_printk("-------end");
|
|
|
+
|
|
|
+ return apm_sc;
|
|
|
}
|