Fetch the repository succeeded.
// 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)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。