链接来自不同服务的跨度的一种方法是使用uber-trace-id
来自父跨度。如果你有LogSpans
set to true
在你的ReporterConfig
, uber-trace-id
是打印出来的内容("Reporting span xxx-xxx-xxx"
).
代码如下:
//API Gateway
carrier := opentracing.TextMapCarrier{} //you can use any type of carrier or even create your own
ctx, _ := opentracing.GlobalTracer().Extract(opentracing.TextMap, carrier)
span := apitracer.tracer.StartSpan(name, ext.RPCServerOption(ctx))
_ := span.Tracer().Inject(span.Context(), opentracing.TextMap, carrier)
uberTraceID := carrier["uber-trace-id"]
您现在可以通过uberTraceID
代替validateEmailSpan.Context()
您的其他服务。
您可以在其他服务中使用此功能:
//Email service
func NewChildSpanThatFollows(name, uberTraceID string) opentracing.Span {
carrier := opentracing.TextMapCarrier{}
carrier.Set("uber-trace-id", uberTraceID)
ctx, _ := opentracing.GlobalTracer().Extract(opentracing.TextMap, carrier)
span := opentracing.StartSpan(name, opentracing.FollowsFrom(ctx))
_ := span.Tracer().Inject(span.Context(), opentracing.TextMap, carrier)
return span
}
如果我需要查看以父子方式链接在一起的服务之间的跨度,这对我有用。如果还需要传递其他信息,我建议将其作为 JSON 对象中的常规数据传递,然后创建我自己的Carrier
或者如果需要对传递的数据进行搜索,则使用标签。
span.SetTag("request_id", requestID)
EDIT:
Here https://github.com/yurishkuro/opentracing-tutorial/tree/master/go您可以找到有关使用 opentracing 的精彩教程。它用HTTPHeadersCarrier
,它有一步一步的演练,但基本上与上面的过程相同。