2 Star 1 Fork 2

trackertrader/rpcx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
opencensus.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
wulengbing@163.com 提交于 2022-06-07 11:30 . modify
package client
import (
"context"
"gitee.com/trackertrader/rpcx/share"
"go.opencensus.io/trace"
)
type OpenCensusPlugin struct{}
func (p *OpenCensusPlugin) PreCall(ctx context.Context, servicePath, serviceMethod string, args interface{}) error {
var span1 *trace.Span
// if it is called in rpc service in case that a service calls antoher service,
// we uses the span in the service context as the parent span.
parentSpan := ctx.Value(share.OpencensusSpanServerKey)
if parentSpan != nil {
_, span1 = trace.StartSpanWithRemoteParent(ctx, "rpcx.client."+servicePath+"."+serviceMethod,
parentSpan.(*trace.Span).SpanContext())
} else {
parentContext, err := share.GetOpencensusSpanContextFromContext(ctx)
if err == nil && parentContext != nil { //try to parse span from request
_, span1 = trace.StartSpanWithRemoteParent(ctx, "rpcx.client."+servicePath+"."+serviceMethod,
*parentContext)
} else { // parse span from context or create root context
_, span1 = trace.StartSpan(ctx, "rpcx.client."+servicePath+"."+serviceMethod)
}
}
if rpcxContext, ok := ctx.(*share.Context); ok {
rpcxContext.SetValue(share.OpencensusSpanClientKey, span1)
}
return nil
}
func (p *OpenCensusPlugin) PostCall(ctx context.Context, servicePath, serviceMethod string, args interface{}, reply interface{}, err error) error {
if rpcxContext, ok := ctx.(*share.Context); ok {
span1 := rpcxContext.Value(share.OpencensusSpanClientKey)
if span1 != nil {
span1.(*trace.Span).End()
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/trackertrader/rpcx.git
git@gitee.com:trackertrader/rpcx.git
trackertrader
rpcx
rpcx
v1.2.1

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385