The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
Paused

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
opentracing.go 2.78 KB
Copy Edit Raw Blame History
Asim Aslam authored 2018-03-03 12:28 . switch to stdlib context
// Package opentracing provides wrappers for OpenTracing
package opentracing
import (
"fmt"
"context"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/server"
"github.com/opentracing/opentracing-go"
)
type otWrapper struct {
ot opentracing.Tracer
client.Client
}
func traceIntoContext(ctx context.Context, tracer opentracing.Tracer, name string) (context.Context, opentracing.Span, error) {
md, ok := metadata.FromContext(ctx)
if !ok {
md = make(map[string]string)
}
var sp opentracing.Span
wireContext, err := tracer.Extract(opentracing.TextMap, opentracing.TextMapCarrier(md))
if err != nil {
sp = tracer.StartSpan(name)
} else {
sp = tracer.StartSpan(name, opentracing.ChildOf(wireContext))
}
if err := sp.Tracer().Inject(sp.Context(), opentracing.TextMap, opentracing.TextMapCarrier(md)); err != nil {
return nil, nil, err
}
ctx = opentracing.ContextWithSpan(ctx, sp)
ctx = metadata.NewContext(ctx, md)
return ctx, sp, nil
}
func (o *otWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
name := fmt.Sprintf("%s.%s", req.Service(), req.Method())
ctx, span, err := traceIntoContext(ctx, o.ot, name)
if err != nil {
return err
}
defer span.Finish()
return o.Client.Call(ctx, req, rsp, opts...)
}
func (o *otWrapper) Publish(ctx context.Context, p client.Publication, opts ...client.PublishOption) error {
name := fmt.Sprintf("Pub to %s", p.Topic())
ctx, span, err := traceIntoContext(ctx, o.ot, name)
if err != nil {
return err
}
defer span.Finish()
return o.Client.Publish(ctx, p, opts...)
}
// NewClientWrapper accepts an open tracing Trace and returns a Client Wrapper
func NewClientWrapper(ot opentracing.Tracer) client.Wrapper {
return func(c client.Client) client.Client {
return &otWrapper{ot, c}
}
}
// NewHandlerWrapper accepts an opentracing Tracer and returns a Handler Wrapper
func NewHandlerWrapper(ot opentracing.Tracer) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
name := fmt.Sprintf("%s.%s", req.Service(), req.Method())
ctx, span, err := traceIntoContext(ctx, ot, name)
if err != nil {
return err
}
defer span.Finish()
return h(ctx, req, rsp)
}
}
}
// NewSubscriberWrapper accepts an opentracing Tracer and returns a Subscriber Wrapper
func NewSubscriberWrapper(ot opentracing.Tracer) server.SubscriberWrapper {
return func(next server.SubscriberFunc) server.SubscriberFunc {
return func(ctx context.Context, msg server.Publication) error {
name := "Pub to " + msg.Topic()
ctx, span, err := traceIntoContext(ctx, ot, name)
if err != nil {
return err
}
defer span.Finish()
return next(ctx, msg)
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.9.3

Search

0d507c66 1850385 C8b1a773 1850385