rpcx 调用[通过传递 string(tranid,spanid,parentSpanId)]
最后更新于:2022-04-02 02:57:54
[TOC]
## rpcx 调用[通过传递 string(tranid,spanid,parentSpanId)]
[rpcx_jaeger [tracing_string] ](https://github.com/idcpj/rpcx_jaeger/tree/tracing_string)
核心代码
```
/**
改函数只适用于 jaeger
通过传递 tracekey string 来追踪
@tracekey 格式 : 6f8b8a1101b0124f:6f8b8a1101b0124f:0000000000000000:1
首个 span
span, carrier, _ := lib.GenerateSpanWithContext( "first2","")
defer span.Finish()
之后 通过传递 carrier 来实现追踪
span, _, _ := lib.GenerateSpanWithContext( "first2",carrier)
defer span.Finish()
*/
func GenerateSpanWithContext(operationName string, traceKey string) (opentracing.Span, string, error) {
var span opentracing.Span
tracer := opentracing.GlobalTracer()
if traceKey != "" {
spanContext, err := jaeger.ContextFromString(traceKey) //通过 traceKey 获取 spanContext
if err != nil {
log.Printf("metadata error %s\n", err)
return nil, "", err
}
span = tracer.StartSpan(operationName, ext.RPCServerOption(spanContext))
} else {
span = opentracing.StartSpan(operationName)
}
metadata := opentracing.TextMapCarrier(make(map[string]string))
err := tracer.Inject(span.Context(), opentracing.TextMap, metadata)
if err != nil {
return nil, "", err
}
return span, metadata[jaeger.TraceContextHeaderName], nil
}
```
';