Skip to content
Snippets Groups Projects
Commit 58c1f450 authored by flycash's avatar flycash
Browse files

Doing tracing filter

parent 7e21b382
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,9 @@ type Context struct {
context.Context
}
func NewContext() *Context {
return &Context{Context: context.Background()}
func NewContext() Context {
return NewContextWith(context.Background())
}
func NewContextWith(ctx context.Context) Context {
return Context{Context: ctx}
}
......@@ -21,7 +21,9 @@ import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/protocol"
......@@ -31,22 +33,38 @@ const (
tracingFilterName = "tracing"
)
// this should be executed before users set their own Tracer
func init() {
extension.SetFilter(tracingFilterName, NewTracingFilter)
opentracing.SetGlobalTracer(opentracing.NoopTracer{})
}
var (
errorKey = "ErrorMsg"
successKey = "Success"
)
type TracingFilter struct {
}
func (tf *TracingFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
// invoker.GetUrl().Context()
operationName := invoker.GetUrl().ServiceKey() + invocation.MethodName()
// withTimeout after we support the timeout between different ends.
invCtx, cancel := context.WithCancel(invocation.Context())
span, spanCtx := opentracing.StartSpanFromContext(invCtx, operationName)
invocation.SetContext(common.NewContextWith(spanCtx))
defer func() {
span.Finish()
cancel()
}()
span, ctx := opentracing.StartSpanFromContext(invocation.Context(), operationName)
defer span.Finish()
result := invoker.Invoke(invocation)
span.SetTag(successKey, result.Error() != nil)
if result.Error() != nil {
span.LogFields(log.String(errorKey, result.Error().Error()))
}
return result
}
func (tf *TracingFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
......
......@@ -32,5 +32,6 @@ type Invocation interface {
Attachments() map[string]string
AttachmentsByKey(string, string) string
Invoker() Invoker
Context() *common.Context
Context() common.Context
SetContext(ctx common.Context)
}
......@@ -41,7 +41,7 @@ type RPCInvocation struct {
attachments map[string]string
invoker protocol.Invoker
lock sync.RWMutex
ctx *common.Context
ctx common.Context
}
func NewRPCInvocation(methodName string, arguments []interface{}, attachments map[string]string) *RPCInvocation {
......@@ -125,11 +125,11 @@ func (r *RPCInvocation) SetCallBack(c interface{}) {
r.callBack = c
}
func (r *RPCInvocation) Context() *common.Context {
func (r *RPCInvocation) Context() common.Context {
return r.ctx
}
func (r *RPCInvocation) SetContext(ctx *common.Context) {
func (r *RPCInvocation) SetContext(ctx common.Context) {
r.ctx = ctx
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment