英文:
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封装了一个包含spanID和traceID的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("operation1")
> 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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论