-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ftr/Transfer tracing context for dubbo protocol #344
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ const ( | |
|
||
const ( | ||
DUBBOGO_CTX_KEY = "dubbogo-ctx" | ||
CONTEXT_KEY = "context" | ||
) | ||
|
||
const ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import ( | |
import ( | ||
"github.com/apache/dubbo-go-hessian2" | ||
"github.com/dubbogo/getty" | ||
"github.com/opentracing/opentracing-go" | ||
perrors "github.com/pkg/errors" | ||
) | ||
|
||
|
@@ -63,9 +64,9 @@ func (s *rpcSession) GetReqNum() int32 { | |
return atomic.LoadInt32(&s.reqNum) | ||
} | ||
|
||
//////////////////////////////////////////// | ||
// ////////////////////////////////////////// | ||
// RpcClientHandler | ||
//////////////////////////////////////////// | ||
// ////////////////////////////////////////// | ||
|
||
// RpcClientHandler ... | ||
type RpcClientHandler struct { | ||
|
@@ -157,9 +158,9 @@ func (h *RpcClientHandler) OnCron(session getty.Session) { | |
h.conn.pool.rpcClient.heartbeat(session) | ||
} | ||
|
||
//////////////////////////////////////////// | ||
// ////////////////////////////////////////// | ||
// RpcServerHandler | ||
//////////////////////////////////////////// | ||
// ////////////////////////////////////////// | ||
|
||
// RpcServerHandler ... | ||
type RpcServerHandler struct { | ||
|
@@ -284,7 +285,10 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) { | |
|
||
args := p.Body.(map[string]interface{})["args"].([]interface{}) | ||
inv := invocation.NewRPCInvocation(p.Service.Method, args, attachments) | ||
result := invoker.Invoke(context.Background(), inv) | ||
|
||
ctx := rebuildCtx(inv) | ||
|
||
result := invoker.Invoke(ctx, inv) | ||
if err := result.Error(); err != nil { | ||
p.Header.ResponseStatus = hessian.Response_OK | ||
p.Body = hessian.NewResponse(nil, err, result.Attachments()) | ||
|
@@ -327,6 +331,22 @@ func (h *RpcServerHandler) OnCron(session getty.Session) { | |
} | ||
} | ||
|
||
// rebuild the context by attachment. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as go lint said, the func name should be the first word of its comment. |
||
// Once we decided to transfer more context's key-value, we should change this. | ||
// now we only support rebuild the tracing context | ||
func rebuildCtx(inv *invocation.RPCInvocation) context.Context { | ||
ctx := context.Background() | ||
|
||
// actually, if user do not use any opentracing framework, it will always be error. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will always be error. --> the error will always be not nil. |
||
spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.TextMap, | ||
opentracing.TextMapCarrier(inv.Attachments())) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this blank line. |
||
if err == nil { | ||
ctx = context.WithValue(ctx, constant.TRACING_REMOTE_SPAN_CTX, spanCtx) | ||
} | ||
return ctx | ||
} | ||
|
||
func reply(session getty.Session, req *DubboPackage, tp hessian.PackageType) { | ||
resp := &DubboPackage{ | ||
Header: hessian.DubboHeader{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dubbo | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
import ( | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/mocktracer" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common/constant" | ||
"github.com/apache/dubbo-go/protocol/invocation" | ||
) | ||
|
||
// test rebuild the ctx | ||
func TestRebuildCtx(t *testing.T) { | ||
opentracing.SetGlobalTracer(mocktracer.New()) | ||
attach := make(map[string]string, 10) | ||
attach[constant.VERSION_KEY] = "1.0" | ||
attach[constant.GROUP_KEY] = "MyGroup" | ||
inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach) | ||
|
||
// attachment doesn't contains any tracing key-value pair, | ||
ctx := rebuildCtx(inv) | ||
assert.NotNil(t, ctx) | ||
assert.Nil(t, ctx.Value(constant.TRACING_REMOTE_SPAN_CTX)) | ||
|
||
span, ctx := opentracing.StartSpanFromContext(ctx, "Test-Client") | ||
|
||
opentracing.GlobalTracer().Inject(span.Context(), opentracing.TextMap, | ||
opentracing.TextMapCarrier(inv.Attachments())) | ||
// rebuild the context success | ||
inv = invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach) | ||
ctx = rebuildCtx(inv) | ||
span.Finish() | ||
assert.NotNil(t, ctx) | ||
assert.NotNil(t, ctx.Value(constant.TRACING_REMOTE_SPAN_CTX)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is that key use for ? it should add some comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not used, so I remove it.