当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
opencensus.go 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2018-07-17 16:16 . fix opencensus breaking changes
// Package opencensus provides wrappers for OpenCensus tracing.
package opencensus
import (
"context"
"encoding/base64"
"fmt"
"github.com/micro/go-log"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)
const (
// TracePropagationField is the key for the tracing context
// that will be injected in go-micro's metadata.
TracePropagationField = "X-Trace-Context"
)
// clientWrapper wraps an RPC client and adds tracing.
type clientWrapper struct {
client.Client
}
func injectTraceIntoCtx(ctx context.Context, span *trace.Span) context.Context {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}
spanCtx := propagation.Binary(span.SpanContext())
md[TracePropagationField] = base64.RawStdEncoding.EncodeToString(spanCtx)
return metadata.NewContext(ctx, md)
}
// Call implements client.Client.Call.
func (w *clientWrapper) Call(
ctx context.Context,
req client.Request,
rsp interface{},
opts ...client.CallOption) (err error) {
t := newRequestTracker(req, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Call(ctx, req, rsp, opts...)
return
}
// Publish implements client.Client.Publish.
func (w *clientWrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) (err error) {
t := newPublicationTracker(p, ClientProfile)
ctx = t.start(ctx, true)
defer func() { t.end(ctx, err) }()
ctx = injectTraceIntoCtx(ctx, t.span)
err = w.Client.Publish(ctx, p, opts...)
return
}
// NewClientWrapper returns a client.Wrapper
// that adds monitoring to outgoing requests.
func NewClientWrapper() client.Wrapper {
return func(c client.Client) client.Client {
return &clientWrapper{c}
}
}
func getTraceFromCtx(ctx context.Context) *trace.SpanContext {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}
encodedTraceCtx, ok := md[TracePropagationField]
if !ok {
return nil
}
traceCtxBytes, err := base64.RawStdEncoding.DecodeString(encodedTraceCtx)
if err != nil {
log.Logf("Could not decode trace context: %s", err.Error())
return nil
}
spanCtx, ok := propagation.FromBinary(traceCtxBytes)
if !ok {
log.Log("Could not decode trace context from binary")
return nil
}
return &spanCtx
}
// NewHandlerWrapper returns a server.HandlerWrapper
// that adds tracing to incoming requests.
func NewHandlerWrapper() server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
t := newRequestTracker(req, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Method()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/%s/%s", ServerProfile.Role, req.Service(), req.Method()),
)
}
err = fn(ctx, req, rsp)
return
}
}
}
// NewSubscriberWrapper returns a server.SubscriberWrapper
// that adds tracing to subscription requests.
func NewSubscriberWrapper() server.SubscriberWrapper {
return func(fn server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, p server.Message) (err error) {
t := newPublicationTracker(p, ServerProfile)
ctx = t.start(ctx, false)
defer func() { t.end(ctx, err) }()
spanCtx := getTraceFromCtx(ctx)
if spanCtx != nil {
ctx, t.span = trace.StartSpanWithRemoteParent(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
*spanCtx,
)
} else {
ctx, t.span = trace.StartSpan(
ctx,
fmt.Sprintf("rpc/%s/pubsub/%s", ServerProfile.Role, p.Topic()),
)
}
err = fn(ctx, p)
return
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.12.1

搜索帮助