跟踪器发送跨度

Tracer sending a span

我正在尝试在 Go 中使用 Jaeger 工具。

我是 运行 具有 docker 的 Jaeger 后端(如 https://www.jaegertracing.io/docs/1.15/getting-started/ 中所述):

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.15

但是,在 运行 以下 Go 代码之后,我无法在 http://localhost:16686 的 Jaeger UI 中看到跨度,我不确定这段代码有什么问题?我从一段类似的 Python 代码开始,它能够在 Jaeger UI.

上发布 span
package main

import (
    "fmt"
    "io"

    opentracing "github.com/opentracing/opentracing-go"
    jaeger "github.com/uber/jaeger-client-go"
    jaegercfg "github.com/uber/jaeger-client-go/config"
)

func initJaeger(service string) (opentracing.Tracer, io.Closer) {
    agentIP := "localhost"
    agentPort := "5775"
    so := jaeger.SamplerOptionsFactory{}
    logger := jaeger.StdLogger
    logger.Infof("Sending Traces to %s %s", agentIP, agentPort)
    cfg := &jaegercfg.Configuration{
        Sampler: &jaegercfg.SamplerConfig{
            Type:    "const",
            Param:   1,
            Options: []jaeger.SamplerOption{so.Logger(logger)},
        },
        ServiceName: service,
        Reporter: &jaegercfg.ReporterConfig{
            LogSpans:           true,
            LocalAgentHostPort: agentIP + ":" + agentPort,
        },
    }
    tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(logger))
    if err != nil {
        panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
    }

    opentracing.SetGlobalTracer(tracer)
    return opentracing.GlobalTracer(), closer
}

func main() {

    tracer, closer := initJaeger("foo-go-service")

    span := tracer.StartSpan("GoTestSpan")
    var myMap = make(map[string]interface{})
    myMap["foo"] = 42
    myMap["bar"] = "42"
    span.LogKV(myMap)

    childSpanRef := opentracing.ChildOf(span.Context())
    childSpan := tracer.StartSpan("GoChildSpan", childSpanRef)
    var myMap2 = make(map[string]interface{})
    myMap2["foo2"] = 42
    myMap2["bar2"] = "42"
    childSpan.LogKV(myMap2)

    closer.Close()
}

我正在这里挖掘文档 https://godoc.org/github.com/uber/jaeger-client-go and the ones for the open tracing project here https://godoc.org/github.com/opentracing/opentracing-go 但我对行话和库有点困惑 functions/methods。

我能够发布一些跨度,所以我可以在 http://localhost:16686

上看到它们
  • 我们应该 SetOperationName 在 Span 上,这样我们就可以在 UI
  • 上用人类可读的名称来识别它
  • 我们还应该推迟 span.Finish() 调用,这似乎就是我在 UI
  • 上看不到任何内容的原因

这是更新后的主函数:

func main() {

    tracer, closer := initJaeger("foo-go-service")
    defer closer.Close()

    span := tracer.StartSpan("GoTestSpan")
    defer span.Finish()
    span.SetOperationName("opNameGoTestSpan")
    var myMap = make(map[string]interface{})
    myMap["foo"] = 42
    myMap["bar"] = "42"
    span.LogKV(myMap)

    // time.Sleep(2 * time.Second)

    childSpanRef := opentracing.ChildOf(span.Context())
    childSpan := tracer.StartSpan("GoChildSpan", childSpanRef)
    defer childSpan.Finish()
    childSpan.SetOperationName("opNameGoChildSpan")
    var myMap2 = make(map[string]interface{})
    myMap2["foo2"] = 42
    myMap2["bar2"] = "42"
    childSpan.LogKV(myMap2)
}