Go 如何使用json传递opentracing数据
我的API网关启动一个跟踪程序和一个验证电子邮件的范围。然后将其传递给用户服务进行验证 我想将这个span详细信息作为一个json对象传递给用户服务,并作为一个json对象启动另一个span tracer.start_span'Validate Email',child\u of=API\u gateway\u span 为此,我使用了以下结构:Go 如何使用json传递opentracing数据,go,microservices,opentracing,distributed-tracing,Go,Microservices,Opentracing,Distributed Tracing,我的API网关启动一个跟踪程序和一个验证电子邮件的范围。然后将其传递给用户服务进行验证 我想将这个span详细信息作为一个json对象传递给用户服务,并作为一个json对象启动另一个span tracer.start_span'Validate Email',child\u of=API\u gateway\u span 为此,我使用了以下结构: type checkEmail struct { GatewayTracerSpan opentracing.SpanContext `jso
type checkEmail struct {
GatewayTracerSpan opentracing.SpanContext `json: gatewayTracerSpan`
Email string `json: email`
Uuid string `json: uuid`
}
在功能上
但GatewayTracerSpan始终是空值。
我刚刚开始分布式跟踪。在这里,我选择在本机http头上使用json,因为它便于升级任何协议更改
这可能吗?如果是,我做得对吗?或者我犯了什么错误?链接不同服务范围的一种方法是使用父范围的uber跟踪id。如果您在ReporterConfig中将LogSpans设置为true,则uber跟踪id是打印出来的报告范围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对象中的常规数据传递,然后创建自己的载体或使用标记(如果需要)对传递的数据进行搜索
span.SetTag("request_id", requestID)
编辑:
您可以找到有关使用opentracing的优秀教程。它使用HTTPHeadersCarrier,它有一个逐步的演练,但基本上与上面的过程相同。您需要从validateEmailSpan.Context中获得什么?您只是想查看在不同服务中创建的跨区,它们链接在一起,还是需要validateEmailSpan.Context中的一些特定信息?@Eminala我只是想将跨区数据传递到我的下一个服务,并启动一个新的跨区,将API网关跨区保留为父级。我按照您的建议更改了代码,但是TraceID仍然是空的。@Sachith我在上面的答案中添加了一个有用的链接。除此之外,如果不看一下您实现的代码,我真的帮不上什么忙。
span.SetTag("request_id", requestID)