跟踪器发送跨度

时间:2019-11-22 17:44:10

标签: go instrumentation opentracing jaeger

我正在尝试在Jaeger的Go中使用检测。

我正在像这样用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上发布跨度。

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处挖掘文档,在https://godoc.org/github.com/opentracing/opentracing-go处挖掘开放跟踪项目的文档,但是我对行话和库函数/方法感到有些困惑。

  • 我想将“一般”事件发布到Jaeger后端,并在Jaeger UI中查看这些事件。
  • 我还想使用某种针对请求结构的自动包装程序来发布“ HTTP请求”事件,但即使是简单/通用事件现在也都很好。

1 个答案:

答案 0 :(得分:0)

我能够发布一些跨度,以便可以在http://localhost:16686上看到它们

  • 我们应该在跨度上SetOperationName,以便我们可以在用户界面上以易于理解的名称对其进行识别
  • 我们还应该推迟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)
}