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

JsonRPC tracing chain

parent 70a74366
No related branches found
No related tags found
No related merge requests found
......@@ -139,5 +139,5 @@ const (
)
const (
TRACING_CURRENT_SPAN_CTX = "tracing.current.span"
TRACING_REMOTE_SPAN_CTX = "tracing.remote.span.ctx"
)
......@@ -76,7 +76,7 @@ type ProxyInvoker struct {
protocol.BaseInvoker
}
func (pi *ProxyInvoker) Invoke(context context.Context, invocation protocol.Invocation) protocol.Result {
func (pi *ProxyInvoker) Invoke(ctx context.Context, invocation protocol.Invocation) protocol.Result {
result := &protocol.RPCResult{}
result.SetAttachments(invocation.Attachments())
......@@ -105,7 +105,7 @@ func (pi *ProxyInvoker) Invoke(context context.Context, invocation protocol.Invo
in := []reflect.Value{svc.Rcvr()}
if method.CtxType() != nil {
in = append(in, method.SuiteContext(nil)) // todo: ctx will be used later.
in = append(in, method.SuiteContext(ctx)) // todo: ctx will be used later.
}
// prepare argv
......
......@@ -19,9 +19,6 @@ package filter_impl
import (
"context"
"time"
"github.com/apache/dubbo-go/common/constant"
)
import (
......@@ -30,6 +27,7 @@ import (
)
import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/protocol"
......@@ -50,28 +48,40 @@ var (
successKey = "Success"
)
// if you wish to using opentracing, please add the this filter into your filter attribute in your configure file.
// notice that this could be used in both client-side and server-side.
type tracingFilter struct {
}
func (tf *tracingFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
var (
spanCtx context.Context
span opentracing.Span
)
operationName := invoker.GetUrl().ServiceKey() + "#" + invocation.MethodName()
wiredCtx := ctx.Value(constant.TRACING_CURRENT_SPAN_CTX)
var span opentracing.Span
var spanCtx = ctx
if wiredCtx == nil {
span, spanCtx = opentracing.StartSpanFromContext(ctx, operationName)
} else {
// it means that the client passed the SpanContext in their request
wiredCtx := ctx.Value(constant.TRACING_REMOTE_SPAN_CTX)
preSpan := opentracing.SpanFromContext(ctx)
if preSpan != nil {
// it means that someone already create a span to trace, so we use the span to be the parent span
span = opentracing.StartSpan(operationName, opentracing.ChildOf(preSpan.Context()))
spanCtx = opentracing.ContextWithSpan(ctx, span)
} else if wiredCtx != nil {
// it means that there has a remote span, usually from client side. so we use this as the parent
span = opentracing.StartSpan(operationName, opentracing.ChildOf(wiredCtx.(opentracing.SpanContext)))
spanCtx = opentracing.ContextWithSpan(ctx, span)
} else {
// it means that there is not any span, so we create a span as the root span.
span, spanCtx = opentracing.StartSpanFromContext(ctx, operationName)
}
defer func() {
span.Finish()
}()
time.Sleep(100 * time.Millisecond)
result := invoker.Invoke(spanCtx, invocation)
span.SetTag(successKey, result.Error() != nil)
if result.Error() != nil {
......
......@@ -22,6 +22,14 @@ import (
"testing"
)
import (
"github.com/opentracing/opentracing-go"
)
import (
"github.com/apache/dubbo-go/common/constant"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
......@@ -39,7 +47,20 @@ func TestTracingFilter_Invoke(t *testing.T) {
attach := make(map[string]string, 10)
inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
ctx := context.Background()
tf := newTracingFilter()
tf.Invoke(context.TODO(), invoker, inv)
// do not has any span
tf.Invoke(ctx, invoker, inv)
span, ctx := opentracing.StartSpanFromContext(ctx, "Test-Operation")
defer span.Finish()
// has previous span
tf.Invoke(ctx, invoker, inv)
ctx = context.Background()
// has remote ctx
ctx = context.WithValue(context.Background(), constant.TRACING_REMOTE_SPAN_CTX, span.Context())
tf.Invoke(ctx, invoker, inv)
}
......@@ -25,6 +25,7 @@ import (
)
import (
"github.com/opentracing/opentracing-go"
perrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
......@@ -74,6 +75,7 @@ func TestHTTPClient_Call(t *testing.T) {
"X-Services": url.Path,
"X-Method": "GetUser",
})
req := client.NewRequest(url, "GetUser", []interface{}{"1", "username"})
reply := &User{}
err = client.Call(ctx, url, req, reply)
......@@ -147,6 +149,10 @@ func TestHTTPClient_Call(t *testing.T) {
"X-Services": url.Path,
"X-Method": "GetUser4",
})
span := opentracing.StartSpan("Test-Inject-Tracing-ID")
ctx = opentracing.ContextWithSpan(ctx, span)
req = client.NewRequest(url, "GetUser4", []interface{}{1})
reply = &User{}
err = client.Call(ctx, url, req, reply)
......
......@@ -50,7 +50,7 @@ func (ji *JsonrpcInvoker) Invoke(ctx context.Context, invocation protocol.Invoca
inv := invocation.(*invocation_impl.RPCInvocation)
url := ji.GetUrl()
req := ji.client.NewRequest(url, inv.MethodName(), inv.Arguments())
ctxNew := context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
ctxNew := context.WithValue(ctx, constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": inv.MethodName(),
......
......@@ -29,11 +29,10 @@ import (
"runtime/debug"
"sync"
"time"
"github.com/opentracing/opentracing-go"
)
import (
"github.com/opentracing/opentracing-go"
perrors "github.com/pkg/errors"
)
......@@ -155,7 +154,7 @@ func (s *Server) handlePkg(conn net.Conn) {
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(r.Header))
if err == nil {
ctx = context.WithValue(ctx, constant.TRACING_CURRENT_SPAN_CTX, spanCtx)
ctx = context.WithValue(ctx, constant.TRACING_REMOTE_SPAN_CTX, spanCtx)
}
if len(reqHeader["Timeout"]) > 0 {
......
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