分布式跟踪不工作jaeger+ opentelemetry

发布于 2025-01-24 07:33:12 字数 4342 浏览 0 评论 0原文

我正在尝试使用Basic Go Client-Server应用程序实现分布式跟踪。使用默认的Jaeger Docker-Compose多合一。
为修复而做的事情无济于事:

  1. 更改collector to agent代理 to collector
  2. 检查日志,对“ client”在那里
  3. 尝试注入标头(传播)
  4. 而不将标题注入标头(传播)

客户端代码:

import(
...
    tracesdk "go.opentelemetry.io/otel/sdk/trace"
...
)

func main() {
.....
    exporter, err := jaeger.New(
        jaeger.WithAgentEndpoint(
            jaeger.WithAgentHost("localhost"),
            jaeger.WithAgentPort("6831"),
        ),
    )
    if err != nil {
        return err
    }

    tp := tracesdk.NewTracerProvider(
        tracesdk.WithBatcher(exporter),
        tracesdk.WithResource(resource.NewWithAttributes(
            semconv.SchemaURL,
            semconv.ServiceNameKey.String("client"),
            semconv.ServiceVersionKey.String("1.0.0"),
            semconv.DeploymentEnvironmentKey.String("local"),
        )),
    )

    defer func() {
        if err := tp.Shutdown(context.Background()); err != nil {
            log.Fatal(err)
        }
    }()

    otel.SetTracerProvider(tp)
    otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
        propagation.TraceContext{},
        propagation.Baggage{},
        propagators.Jaeger{},
    ))
    .......
}
....
func fetcherSuccess() error {
    tr := otel.Tracer("clientHTTP")
    ctx, span := tr.Start(context.Background(), "client.fetcherSuccess")
    defer span.End()

    req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/success", nil)
    if err != nil {
        AddSpanError(span, err)
        FailSpan(span, "request error")
        return err
    }
    // Try to inject headers to the context using this otel.GetTextMapPropagator().Inject(ctx, h)
    headers := InjectHTTPHeaders(ctx)
    for k, v := range headers {
        req.Header.Add(k, v)
    }

    res, _ := http.DefaultClient.Do(req)

    return nil
}

服务器代码:

import(
...
    tracesdk "go.opentelemetry.io/otel/sdk/trace"
...
)

func main() {
.....
    exporter, err := jaeger.New(
        jaeger.WithAgentEndpoint(
            jaeger.WithAgentHost("localhost"),
            jaeger.WithAgentPort("6831"),
        ),
    )
    if err != nil {
        return err
    }

    tp := tracesdk.NewTracerProvider(
        tracesdk.WithBatcher(exporter),
        tracesdk.WithResource(resource.NewWithAttributes(
            semconv.SchemaURL,
            semconv.ServiceNameKey.String("server"),
            semconv.ServiceVersionKey.String("1.0.0"),
            semconv.DeploymentEnvironmentKey.String("local"),
        )),
    )

    defer func() {
        if err := tp.Shutdown(context.Background()); err != nil {
            log.Fatal(err)
        }
    }()

    otel.SetTracerProvider(tp)
    otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
        propagation.TraceContext{},
        propagation.Baggage{},
        propagators.Jaeger{},
    ))

    if err := handleRequests(); err != nil {
        panic("unable to create handler")
    }
    .......
}

func handleRequests() error {
    router := http.NewServeMux()

    router.HandleFunc("/success", handleSuccess)
    router.HandleFunc("/error", handleError)
    fmt.Println("Server is listening on port: 8080")

    if err := http.ListenAndServe(":8080", router); err != nil {
        return err
    }

    return nil
}
....

func handleSuccess(w http.ResponseWriter, r *http.Request) {
    tr := otel.Tracer("serverHTTP")
    //Extract headers using otel.GetTextMapPropagator().Extract()
    ctx := ExtractHTTPHeaders(r.Context(), r.Header)
    ctx, span := tr.Start(ctx, "server.handleSuccess")

    defer span.End()

    //Add some tags here to help debug.
    AddSpanTags(span, map[string]string{"name": "Ivan"})

    //Add some event.
    AddSpanEvents(span, "testEvent", map[string]string{"eventInfo": "some info"})

    initCall(ctx, false)

    w.WriteHeader(http.StatusOK)
    w.Write([]byte("done"))
}

jaeger ui中,我仅看到“ server”跨度,而不是“ client”。因此,我了解的是,出于某些原因,从“客户端”无法触及agent/collector(我两者都尝试过)。我的代码有什么问题吗? Jaeger init似乎是同等的,为什么一个应用程序不发送任何内容对我来说尚不清楚。

I am trying to implement distributed tracing with basic GO client-server app. Using default Jaeger docker-compose all-in-one.
What was done to fix and doesn't help:

  1. Changed collector to agent and agent to collector.
  2. Checked logs, nothing about "client" there
  3. Tried to inject headers (propagation)
  4. Tried without injecting to headers (propagation)

CLIENT CODE:

import(
...
    tracesdk "go.opentelemetry.io/otel/sdk/trace"
...
)

func main() {
.....
    exporter, err := jaeger.New(
        jaeger.WithAgentEndpoint(
            jaeger.WithAgentHost("localhost"),
            jaeger.WithAgentPort("6831"),
        ),
    )
    if err != nil {
        return err
    }

    tp := tracesdk.NewTracerProvider(
        tracesdk.WithBatcher(exporter),
        tracesdk.WithResource(resource.NewWithAttributes(
            semconv.SchemaURL,
            semconv.ServiceNameKey.String("client"),
            semconv.ServiceVersionKey.String("1.0.0"),
            semconv.DeploymentEnvironmentKey.String("local"),
        )),
    )

    defer func() {
        if err := tp.Shutdown(context.Background()); err != nil {
            log.Fatal(err)
        }
    }()

    otel.SetTracerProvider(tp)
    otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
        propagation.TraceContext{},
        propagation.Baggage{},
        propagators.Jaeger{},
    ))
    .......
}
....
func fetcherSuccess() error {
    tr := otel.Tracer("clientHTTP")
    ctx, span := tr.Start(context.Background(), "client.fetcherSuccess")
    defer span.End()

    req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:8080/success", nil)
    if err != nil {
        AddSpanError(span, err)
        FailSpan(span, "request error")
        return err
    }
    // Try to inject headers to the context using this otel.GetTextMapPropagator().Inject(ctx, h)
    headers := InjectHTTPHeaders(ctx)
    for k, v := range headers {
        req.Header.Add(k, v)
    }

    res, _ := http.DefaultClient.Do(req)

    return nil
}

SERVER CODE:

import(
...
    tracesdk "go.opentelemetry.io/otel/sdk/trace"
...
)

func main() {
.....
    exporter, err := jaeger.New(
        jaeger.WithAgentEndpoint(
            jaeger.WithAgentHost("localhost"),
            jaeger.WithAgentPort("6831"),
        ),
    )
    if err != nil {
        return err
    }

    tp := tracesdk.NewTracerProvider(
        tracesdk.WithBatcher(exporter),
        tracesdk.WithResource(resource.NewWithAttributes(
            semconv.SchemaURL,
            semconv.ServiceNameKey.String("server"),
            semconv.ServiceVersionKey.String("1.0.0"),
            semconv.DeploymentEnvironmentKey.String("local"),
        )),
    )

    defer func() {
        if err := tp.Shutdown(context.Background()); err != nil {
            log.Fatal(err)
        }
    }()

    otel.SetTracerProvider(tp)
    otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
        propagation.TraceContext{},
        propagation.Baggage{},
        propagators.Jaeger{},
    ))

    if err := handleRequests(); err != nil {
        panic("unable to create handler")
    }
    .......
}

func handleRequests() error {
    router := http.NewServeMux()

    router.HandleFunc("/success", handleSuccess)
    router.HandleFunc("/error", handleError)
    fmt.Println("Server is listening on port: 8080")

    if err := http.ListenAndServe(":8080", router); err != nil {
        return err
    }

    return nil
}
....

func handleSuccess(w http.ResponseWriter, r *http.Request) {
    tr := otel.Tracer("serverHTTP")
    //Extract headers using otel.GetTextMapPropagator().Extract()
    ctx := ExtractHTTPHeaders(r.Context(), r.Header)
    ctx, span := tr.Start(ctx, "server.handleSuccess")

    defer span.End()

    //Add some tags here to help debug.
    AddSpanTags(span, map[string]string{"name": "Ivan"})

    //Add some event.
    AddSpanEvents(span, "testEvent", map[string]string{"eventInfo": "some info"})

    initCall(ctx, false)

    w.WriteHeader(http.StatusOK)
    w.Write([]byte("done"))
}

In Jaeger UI I see only "server" spans but not "client". So what I understand that for some reasons trace from "client" unable to reach the agent/collector(I tried both). Is there any problems with my code? Jaeger init seems equal why one app doesn't send anything is not clear for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文