golang beyla采集trace程序原理源码解析

 更新时间:2024年02月16日 10:43:51   作者:a朋  
beyla支持通过ebpf,无侵入的、自动采集应用程序的trace信息,本文以golang的nethttp为例,讲述beyla对trace的采集的实现原理,有需要的朋友可以借鉴参考下,希望能够有所帮助

一. 整体原理

trace采集时,监听了golang应用程序的net/http中的函数:

  • net/http.serverHandler.ServeHTTP;
  • net/http.(*Transport).roundTrip;

监听ServeHTTP时:

  • 若request中没有trace信息,则生成traceparent,存入go_trace_map结构(key=goroutine地址,value=trace信息);
  • 若request中有trace信息,则根据trace信息,重新生成span,存入go_trace_map结构;

监听roundTrip的调用:

  • 首先,根据goroutine地址,读go_trace_map结构,得到trace信息;
  • 然后,将当前连接的trace信息,存入ongoing_http_client_requests结构(key=goroutine地址,value=trace信息);

监听roundTrip的调用返回:

  • 首先,根据goroutine地址,读ongoing_http_client_requests结构,得到trace信息;
  • 然后,将当前调用的trace信息,转换为http_request_trace结构,保存到ringbuf中;

最终,ebpf用户程序,读取ringbuf中的trace信息,采集到trace信息。

二. 监听uprobe/ServeHTTP

处理流程:

  • 首先,提取goroutine和request指针;
  • 然后,通过server_trace_parent()函数,处理trace信息,存入go_trace_map结构;
  • 最后,将数据存入onging_http_server_requests结构;
// beyla/bpf/go_nethttp.c

SEC("uprobe/ServeHTTP")
int uprobe_ServeHTTP(struct pt_regs *ctx) {
    void *goroutine_addr = GOROUTINE_PTR(ctx);
    void *req = GO_PARAM4(ctx);
    http_func_invocation_t invocation = {
        .start_monotime_ns = bpf_ktime_get_ns(),
        .req_ptr = (u64)req,
        .tp = {0}
    };
    if (req) {
        // 处理trace信息,存入go_trace_map
        server_trace_parent(goroutine_addr, &invocation.tp, (void*)(req + req_header_ptr_pos));
    }
    // write event
    if (bpf_map_update_elem(&ongoing_http_server_requests, &goroutine_addr, &invocation, BPF_ANY)) {
        bpf_dbg_printk("can't update map element");
    }
    return 0;
}

重点看一下server_trace_parent()函数:

  • 首先,从req_header读取traceparent:

    • 若读到了,则copy traceId,将parentId=上层的spanId;
    • 否则,则生成trace_id,将parentId=0;
  • 然后,使用urand,生成随机的spanId;
  • 最后,将trace信息存入go_trace_map结构,key=goroutine地址,value=trace信息;
// bpf/go_common.h

static __always_inline void server_trace_parent(void *goroutine_addr, tp_info_t *tp, void *req_header) {
    // May get overriden when decoding existing traceparent, but otherwise we set sample ON
    tp->flags = 1;
    // Get traceparent from the Request.Header
    void *traceparent_ptr = extract_traceparent_from_req_headers(req_header);
    if (traceparent_ptr != NULL) {    // 读到了traceparent
       ....
    } else {     // 未读到traceparent
        bpf_dbg_printk("No traceparent in headers, generating");
        urand_bytes(tp->trace_id, TRACE_ID_SIZE_BYTES);       // 生成随机的trace_id;
        *((u64 *)tp->parent_id) = 0;
    }

    urand_bytes(tp->span_id, SPAN_ID_SIZE_BYTES);
    bpf_map_update_elem(&go_trace_map, &goroutine_addr, tp, BPF_ANY);
}

go_trace_map对象的定义:

struct {
    __uint(type, BPF_MAP_TYPE_LRU_HASH);
    __type(key, void *); // key: pointer to the goroutine
    __type(value, tp_info_t);  // value: traceparent info
    __uint(max_entries, MAX_CONCURRENT_SHARED_REQUESTS);
    __uint(pinning, LIBBPF_PIN_BY_NAME);
} go_trace_map SEC(".maps");

typedef struct tp_info {
    unsigned char trace_id[TRACE_ID_SIZE_BYTES];
    unsigned char span_id[SPAN_ID_SIZE_BYTES];
    unsigned char parent_id[SPAN_ID_SIZE_BYTES];
    u64 ts;
    u8  flags;
} tp_info_t;

三. 监听uprobe/roundTrip

roundTrip函数,在使用http client发起请求时,被调用。

处理流程:

  • 首先,提取goroutine地址和request地址;
  • 然后,根据goroutine_addr和request,查找trace信息;
  • 最后,将trace信息写入ongoing_http_client_requests对象;
// beyla/bpf/go_nethttp.c
SEC("uprobe/roundTrip")
int uprobe_roundTrip(struct pt_regs *ctx) {
    roundTripStartHelper(ctx);
    return 0;
}
static __always_inline void roundTripStartHelper(struct pt_regs *ctx) {
    void *goroutine_addr = GOROUTINE_PTR(ctx);
    void *req = GO_PARAM2(ctx);
    http_func_invocation_t invocation = {
        .start_monotime_ns = bpf_ktime_get_ns(),
        .req_ptr = (u64)req,
        .tp = {0}
    };
    // 根据request和goroutine_addr,查找trace信息
    __attribute__((__unused__)) u8 existing_tp = client_trace_parent(goroutine_addr, &invocation.tp, (void*)(req + req_header_ptr_pos));
    // 将trace信息写入ongoing_http_client_requests
    if (bpf_map_update_elem(&ongoing_http_client_requests, &goroutine_addr, &invocation, BPF_ANY)) {
        bpf_dbg_printk("can't update http client map element");
    }
}

重点看一下查找trace信息的client_trace_parent()函数:

  • 首先,尝试从request的header中提取traceparent:

    • 若找到了,则copy traceId,设置当前span.parentId=上游span的spanId;
  • 然后,再使用goroutine及其parent_goroutine,去go_trace_map中找:

    • 若找到了,则copy traceId,设置当前span.parentId=上游span的spanId;
// beyla/go_common.h
static __always_inline u8 client_trace_parent(void *goroutine_addr, tp_info_t *tp_i, void *req_header) {
    u8 found_trace_id = 0;
    u8 trace_id_exists = 0;    
    // May get overriden when decoding existing traceparent or finding a server span, but otherwise we set sample ON
    tp_i->flags = 1;
    // 首先尝试从request的header中提取traceparent
    if (req_header) {
        ...
    }
    // 然后再使用goroutine去go_trace_map中找
    if (!found_trace_id) {
        tp_info_t *tp = 0;
        u64 parent_id = find_parent_goroutine(goroutine_addr);
        if (parent_id) {// we found a parent request
            tp = (tp_info_t *)bpf_map_lookup_elem(&go_trace_map, &parent_id);
        }
        if (tp) {   // 找到了,copy traceId,当前span.parentId=上流span.spanId
            *((u64 *)tp_i->trace_id) = *((u64 *)tp->trace_id);
            *((u64 *)(tp_i->trace_id + 8)) = *((u64 *)(tp->trace_id + 8));
            *((u64 *)tp_i->parent_id) = *((u64 *)tp->span_id);
            tp_i->flags = tp->flags;
        } 
        ...
        // 生成当前span.spanId
        urand_bytes(tp_i->span_id, SPAN_ID_SIZE_BYTES);
    }
    return trace_id_exists;
}

这里有个隐形的假设条件:

  • 一个goroutine及其child goroutine仅处理一个http请求;
  • nethttp的框架在设计时,就由一个goroutine去处理一个http请求,是符合这个假设的;

四. 监听uprobe/roundTrip_return

处理流程:

  • 首先,使用goroutine_addr,从ongoing_http_client_requests中找trace信息;
  • 然后,初始化http_request_trace:

    • 从request中找method/host/url/content_length,赋值给http_request_trace;
    • 将trace信息赋值到http_request_trace;
    • 从response中找status,赋值给http_request_trace;
  • 最后,将http_request_trace提交到ringbuf;
// beyla/bpf/go_nethttp.c
SEC("uprobe/roundTrip_return")
int uprobe_roundTripReturn(struct pt_regs *ctx) {
    void *goroutine_addr = GOROUTINE_PTR(ctx);
    // 使用goroutine_addr找ongoing_http_client_requests
    http_func_invocation_t *invocation =
        bpf_map_lookup_elem(&ongoing_http_client_requests, &goroutine_addr);
    bpf_map_delete_elem(&ongoing_http_client_requests, &goroutine_addr);
    http_request_trace *trace = bpf_ringbuf_reserve(&events, sizeof(http_request_trace), 0);
    // 初始化http_request_trace
    task_pid(&trace->pid);
    trace->type = EVENT_HTTP_CLIENT;
    trace->start_monotime_ns = invocation->start_monotime_ns;
    trace->go_start_monotime_ns = invocation->start_monotime_ns;
    trace->end_monotime_ns = bpf_ktime_get_ns();
    void *req_ptr = (void *)invocation->req_ptr;
    void *resp_ptr = (void *)GO_PARAM1(ctx);
    // 从request中找method,赋值给trace->method
    if (!read_go_str("method", req_ptr, method_ptr_pos, &trace->method, sizeof(trace->method))) {
        ...
    }
    // 从request中找host,赋值给trace->host
    if (!read_go_str("host", req_ptr, host_ptr_pos, &trace->host, sizeof(trace->host))) {
        ...
    }
    // 从request中找url,赋值给trace->path
    void *url_ptr = 0;
    bpf_probe_read(&url_ptr, sizeof(url_ptr), (void *)(req_ptr + url_ptr_pos));
    if (!url_ptr || !read_go_str("path", url_ptr, path_ptr_pos, &trace->path, sizeof(trace->path))) {
        ...
    }
    // 赋值trace信息
    trace->tp = invocation->tp;
    // 从request中找content_length,赋值给trace->content_length
    bpf_probe_read(&trace->content_length, sizeof(trace->content_length), (void *)(req_ptr + content_length_ptr_pos));
    // 从resp中找status,赋值给trace->status
    bpf_probe_read(&trace->status, sizeof(trace->status), (void *)(resp_ptr + status_code_ptr_pos));
    // 提交trace到ringbuf
    bpf_ringbuf_submit(trace, get_flags());
    return 0;
}

参考:

1.https://github.com/grafana/beyla/issues/521

2.https://github.com/grafana/beyla/blob/main/docs/sources/distributed-traces.md

以上就是golang beyla采集trace程序原理源码解析的详细内容,更多关于golang beyla采集trace的资料请关注脚本之家其它相关文章!

相关文章

  • golang 如何删除二进制文件中的源码路径信息

    golang 如何删除二进制文件中的源码路径信息

    这篇文章主要介绍了golang 如何删除二进制文件中的源码路径信息,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Go 实现一次性打包各个平台的可执行程序

    Go 实现一次性打包各个平台的可执行程序

    这篇文章主要介绍了Go 实现一次性打包各个平台的可执行程序,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Go语言编译原理之源码调试

    Go语言编译原理之源码调试

    这篇文章主要为大家介绍了Go语言编译原理之源码调试示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • golang对etcd存取和数值监测的实现

    golang对etcd存取和数值监测的实现

    这篇文章主要介绍了golang对etcd存取和数值监测的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Go 语言 json解析框架与 gjson 详解

    Go 语言 json解析框架与 gjson 详解

    这篇文章主要介绍了Go语言json解析框架与gjson,JSON 解析是我们不可避免的常见问题,在Go语言中,我们可以借助gjson库来方便的进行json属性的提取与解析,需要的朋友可以参考一下
    2022-07-07
  • Golang比较两个slice是否相等的问题

    Golang比较两个slice是否相等的问题

    本文主要介绍了Golang比较两个slice是否相等的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 深入Go goroutine理解

    深入Go goroutine理解

    这篇文章主要介绍了深入Go goroutine理解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Go语言字符串常见操作的使用汇总

    Go语言字符串常见操作的使用汇总

    这篇文章主要为大家总结了Go语言中常见的几种字符串操作,例如:位置索引、替换、统计次数等,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-04-04
  • go install/build生成的文件命名和路径操作

    go install/build生成的文件命名和路径操作

    这篇文章主要介绍了go install/build生成的文件命名和路径操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • Golang泛型实现类型转换的方法实例

    Golang泛型实现类型转换的方法实例

    将一个值从一种类型转换到另一种类型,便发生了类型转换,下面这篇文章主要给大家介绍了关于Golang泛型实现类型转换的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12

最新评论