gpt4 book ai didi

go - 在一个 docker 容器中打开 go 代码,在另一个容器中打开 jaegertracing

转载 作者:数据小太阳 更新时间:2023-10-29 03:19:35 26 4
gpt4 key购买 nike

我有一个 go 项目,它使用以下命令在一个 docker 容器中发送 OpenTracing 跨度,并在它自己的容器中运行 jaegertracing:

docker run -p 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest

当我运行以下 go 测试代码时,我可以在 jaegerui 中看到它们:

 import (
"testing"
//"fmt"
"io"

opentracing "github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
config "github.com/uber/jaeger-client-go/config"
log2 "github.com/sirupsen/logrus"
// olog"github.com/opentracing/opentracing-go/log"
jaegerlog "github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-lib/metrics"
)

func TestSum(t *testing.T) {
log2.Info("start opentracing")
// helloTo := "sonam"
tracer, closer := initJaeger("opentracing_test")
defer closer.Close()

helloStr := "Sonam"
//tracer := opentracing.GlobalTracer()

span := tracer.StartSpan("TestSum")
println(helloStr)
span.Finish()
}


// initJaeger returns an instance of Jaeger Tracer that samples 100% of traces and logs all spans to stdout.
func initJaeger(service string) (opentracing.Tracer, io.Closer) {
cfg := config.Configuration{
ServiceName: service,
Sampler: &config.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &config.ReporterConfig{
LogSpans: true,
},
}

//add
jLogger := jaegerlog.StdLogger
jMetricsFactory := metrics.NullFactory

// Initialize tracer with a logger and a metrics factory
tracer, closer, _ := cfg.NewTracer(
config.Logger(jLogger),
config.Metrics(jMetricsFactory),
)
// Set the singleton opentracing.Tracer with the Jaeger tracer.
opentracing.SetGlobalTracer(tracer)
return tracer, closer
}

但当代码在单独的 docker 容器中执行时则不然。

我需要在我的 go app docker 容器中传递什么?

最佳答案

我的问题出在配置上。使用上述配置未读取环境变量。我必须使用以下内容来读取开放跟踪中的环境变量:

cfg, err := jconfig.FromEnv()
if err != nil {
log.Error("cannot parse Jaeger env vars")
}

cfg.ServiceName = service
cfg.Sampler.Type = jaeger.SamplerTypeConst
cfg.Sampler.Param = 1
cfg.Reporter.LogSpans = true

关于go - 在一个 docker 容器中打开 go 代码,在另一个容器中打开 jaegertracing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com