有没有一种方法可以将 OpenTracing 上下文传播到 Go 命令行工具?

huangapple go评论76阅读模式
英文:

Is there a way to propagate a opentracing context to a go command line tool?

问题

我正在尝试将一个 opentracing 上下文传播到一个命令行应用程序中。
我看到客户端<->服务器的代码如下:

spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))

我看不到如何初始化一个具有 trace_id 和 span_id 的上下文对象。有什么想法吗?

英文:

I'm trying to propagate a opentracing context to a command line application.
I saw the code for client<->server looks like this:

spanCtx, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header))

I can't see how to initialize a context object having trace_id and span_id. Any ideas?

答案1

得分: 1

遵循追踪器接口

1、使用tracer.StartSpan创建一个Span。

span := tracer.StartSpan("operation1")

> 如何初始化一个具有trace_id和span_id的上下文对象

Span封装了一个包含spanIDtraceID的SpanContext。

详细信息请查看Span定义
这里是Jaeger的SpanContext实现

2、使用tracer.Inject将SpanContext注入到载体中进行传播,例如:

carrier := opentracing.HTTPHeadersCarrier(httpReq.Header)
err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier)

然后将载体发送给其他服务,如RPC或HTTP端点。

3、在另一个服务中,使用tracer.Extract提取SpanContext:

spanCtx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
英文:

Follow Tracer interface.

1、Use tracer.StartSpan to create a Span.

span := tracer.StartSpan(&quot;operation1&quot;)

> how to initialize a context object having trace_id and span_id

Span encapsulates a SpanContext that includes spanID and traceID.

Check Span definition for more detail.
And here is Jaeger's implementation of SpanContext.

2、Use tracer.Inject to inject the SpanContext for propagation within the carrier, like:

carrier := opentracing.HTTPHeadersCarrier(httpReq.Header)
err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier)

Then send the carrier to other services, like RPC or HTTP endpoints.

3、In another service, use tracer.Extract to extract the SpanContext:

spanCtx, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))

huangapple
  • 本文由 发表于 2021年9月2日 00:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/69017155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定