如何使用json传递opentracing数据 [英] How to pass opentracing data using json
问题描述
我的API网关启动一个跟踪器和一个跨度以验证电子邮件.然后将其传递给user-service
进行验证.
我想将此span
详细信息作为json对象传递给user-service
,并以
validateEmailSpan := apitracer.tracer.StartSpan("Validate Email")
emailJson := checkEmail{
GatewayTracerSpan: validateEmailSpan.Context(),
Email: email,
Uuid: uuid,
}
启动另一个span
tracer.start_span('Validate Email', child_of=API_gateway_span)
为此,我使用了以下结构:
type checkEmail struct {
GatewayTracerSpan opentracing.SpanContext `json: gatewayTracerSpan`
Email string `json: email`
Uuid string `json: uuid`
}
在function()
validateEmailSpan := apitracer.tracer.StartSpan("Validate Email")
emailJson := checkEmail{
GatewayTracerSpan: validateEmailSpan.Context(),
Email: email,
Uuid: uuid,
}
但GatewayTracerSpan
始终为空值.
我刚刚开始分布式跟踪.在这里,我选择在本机http-headers
上使用json,因为它可以轻松升级任何协议更改.
这可能吗?如果是这样,我做对了吗?还是我犯了什么错误?
一种链接来自不同服务的跨度的方法是使用父跨度中的uber-trace-id
.如果在ReporterConfig
中将LogSpans
设置为true
,则将打印出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)
此处,您可以找到有关使用opentracing的出色教程.它使用HTTPHeadersCarrier
,具有逐步的逐步介绍,但基本上与上述过程相同.
My API-gateway starts a tracer and a span for validate email. Then its passed to user-service
for validation.
I want to pass this span
details to user-service
as a json object and start another span
as a
tracer.start_span('Validate Email', child_of=API_gateway_span)
To do it, I have used following struct:
type checkEmail struct {
GatewayTracerSpan opentracing.SpanContext `json: gatewayTracerSpan`
Email string `json: email`
Uuid string `json: uuid`
}
In function()
validateEmailSpan := apitracer.tracer.StartSpan("Validate Email")
emailJson := checkEmail{
GatewayTracerSpan: validateEmailSpan.Context(),
Email: email,
Uuid: uuid,
}
But always GatewayTracerSpan
is empty value.
I have just started distributed-tracing. Here I selected to use json over native http-headers
as its easy for upgrade any protocol change.
Is this possible? If so, am I doing it right? Or what mistakes did I make?
One way to link spans from different services is to use uber-trace-id
from the parent span. If you have LogSpans
set to true
in your ReporterConfig
, uber-trace-id
is what gets printed out ("Reporting span xxx-xxx-xxx"
).
Here is how it might look like in the code:
//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"]
You can now pass uberTraceID
instead of validateEmailSpan.Context()
to your other services.
You can use this function in your other services:
//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
}
This works for me if I need to see spans between services linked together in a parent-child manner. If other information needs to be passed as well, I would suggest passing it as regular data in the JSON object, then either create my own Carrier
or use tags if needed to do a search with that passed data.
span.SetTag("request_id", requestID)
EDIT:
Here you can find a great tutorial on using opentracing. It uses HTTPHeadersCarrier
, it has a step by step walkthrough, but it's basically the same process as above.
这篇关于如何使用json传递opentracing数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!