open-telemetry / opentelemetry-go-instrumentation

OpenTelemetry Auto Instrumentation using eBPF
https://opentelemetry.io
Apache License 2.0
530 stars 81 forks source link

Regarding the issue of Traceparent #792

Open guolifu opened 6 months ago

guolifu commented 6 months ago

Describe the bug

I have created a service using an HTTP server, and in the GET method, I called another API. I added a header, Traceparent, at the entry point of the request. However, strangely, the traceID did not propagate smoothly, but instead, a new traceID was generated. Through debugging, I found an exception message: bpf_trace_printk: context c0000a6010 not found in context map. In the uprobe_Transport_roundTrip method, struct span_context *parent_span_ctx = get_parent_span_context(context_ptr_val);returns NULL. I don't know where the problem lies. It seems that the probe startup is working fine.

Environment

To Reproduce

Steps to reproduce the behavior:

  1. http.HandleFunc("/get2", func(writer http.ResponseWriter, request *http.Request) {
        fmt.Println("curl request:", request)
        resp, err := http.Get("http://localhost:82/user")
        if err != nil {
            fmt.Println("error:", err)
            return
        }
        defer resp.Body.Close()
    
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            fmt.Println("error:", err)
            return
        }
        _, _ = io.WriteString(writer, "81 resp")
        fmt.Println("resp:", string(body))
    })
    
    fmt.Println("HTTP start")
    
    if err := http.ListenAndServe("0.0.0.0:81", nil); err != nil {
        fmt.Println("error:", err)
        return
    }
  2. build it

  3. OTEL_GO_AUTO_TARGET_EXE=/opt/github/opentelemetry-go-instrumentation/demo/ebpf/ebpfdemo OTEL_SERVICE_NAME=my_service OTEL_EXPORTER_OTLP_ENDPOINT=http://0.0.0.0:4317 ./otel-go-instrumentation

  4. curl -H "Traceparent: 00-95b5ec2b81e2374a4a27ce36ab71d349-18f65a5a3ab22213-01" http://10.211.55.27:81/get2

  5. image

Expected behavior

TraceId should remain consistent.

Additional context

Add any other context about the problem here.

RonFed commented 6 months ago

Hey, I think that passing the context object from the incoming request to the outgoing one will resolve this. (i.e Using NewRequestWithContext to perform the get request)

guolifu commented 6 months ago

Hey, I think that passing the context object from the incoming request to the outgoing one will resolve this. (i.e Using NewRequestWithContext to perform the get request)

Thank you very much for your assistance. 👍🏻 Following your advice, it did indeed run successfully as expected. However, for some of the code already written and deployed, we have extensively utilized the http.Get(Post) method. Are there any effective adaptation methods available to us?