Skip to content

Commit

Permalink
add trace support
Browse files Browse the repository at this point in the history
  • Loading branch information
xiemalin committed Aug 6, 2021
1 parent 3522459 commit 1a42e49
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 65 deletions.
24 changes: 16 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,18 @@ func (u *URL) SetPort(port *int) *URL {

// RpcInvocation define rpc invocation
type RpcInvocation struct {
ServiceName *string
MethodName *string
ParameterIn *proto.Message
Attachment []byte
LogId *int64
CompressType *int32
AuthenticateData []byte
ChunkSize uint32
ServiceName *string
MethodName *string
ParameterIn *proto.Message
Attachment []byte
LogId *int64
CompressType *int32
AuthenticateData []byte
ChunkSize uint32
TraceId int64
SpanId int64
ParentSpanId int64
RpcRequestMetaExt map[string]string
}

// NewRpcCient new rpc client
Expand Down Expand Up @@ -145,6 +149,10 @@ func (r *RpcInvocation) GetRequestRpcDataPackage() (*RpcDataPackage, error) {
rpcDataPackage.MagicCode(MAGIC_CODE)
rpcDataPackage.AuthenticationData(r.AuthenticateData)
rpcDataPackage.chunkSize = r.ChunkSize
rpcDataPackage.TraceId(r.TraceId)
rpcDataPackage.SpanId(r.SpanId)
rpcDataPackage.ParentSpanId(r.ParentSpanId)
rpcDataPackage.RpcRequestMetaExt(r.RpcRequestMetaExt)
if r.CompressType != nil {
rpcDataPackage.CompressType(*r.CompressType)
}
Expand Down
20 changes: 20 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func (as *StringMatchAuthService) Authenticate(service, name string, authToken [
return strings.Compare(AUTH_TOKEN, string(authToken)) == 0
}

type AddOneTraceService struct {
}

// Trace
func (as *AddOneTraceService) Trace(service, name string, traceInfo *baidurpc.TraceInfo) *baidurpc.TraceInfo {
*traceInfo.SpanId++
*traceInfo.TraceId++
*traceInfo.ParentSpanId++
return traceInfo
}

// TestSingleTcpConnectionClient
func TestSingleTcpConnectionClient(t *testing.T) {
Convey("TestSingleTcpConnectionClient", t, func() {
Expand Down Expand Up @@ -230,6 +241,7 @@ func startRpcServer(chunksize uint32) *baidurpc.TcpServer {
}
rpcServer.RegisterNameWithMethodMapping("EchoService", echoservice, methodMapping)

rpcServer.SetTraceService(new(AddOneTraceService))
rpcServer.Start()

return rpcServer
Expand Down Expand Up @@ -291,6 +303,10 @@ func doSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.RpcClient, ser
rpcInvocation.SetParameterIn(&dm)
rpcInvocation.LogId = proto.Int64(1)
rpcInvocation.ChunkSize = chunkSize
rpcInvocation.TraceId = 10
rpcInvocation.SpanId = 11
rpcInvocation.ParentSpanId = 12
rpcInvocation.RpcRequestMetaExt = map[string]string{"key1": "value1"}

if withAttachement {
rpcInvocation.Attachment = []byte("This is attachment data")
Expand Down Expand Up @@ -333,6 +349,10 @@ func doSimpleRPCInvokeWithSignatureWithConvey(rpcClient *baidurpc.RpcClient, ser
So(string(response.Attachment), ShouldEqual, "I am a attachementThis is attachment data")
}

So(*response.GetTraceId(), ShouldEqual, rpcInvocation.TraceId+1)
So(*response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1)
So(*response.GetParentSpanId(), ShouldEqual, rpcInvocation.ParentSpanId+1)
So(response.GetRpcRequestMetaExt()["key1"], ShouldEqual, "value1")
})

}
101 changes: 44 additions & 57 deletions data.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions docs/Demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,58 @@ baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它

至此RPC已经开发完成,运行上面代码,就可以发布完成.

#### 开发启验证功能

实现 AuthService 接口

```go
type StringMatchAuthService struct {
}
// Authenticate
func (as *StringMatchAuthService) Authenticate(service, name string, authToken []byte) bool {
if authToken == nil {
return false
}
return strings.Compare(AUTH_TOKEN, string(authToken)) == 0
}
```
设置到service对象
```go
// ...
rpcServer := baidurpc.NewTpcServer(&serverMeta)
rpcServer.SetAuthService(new(StringMatchAuthService))
```

#### 设置trace功能

实现 TraceService 接口

```go
type AddOneTraceService struct {
}
// Trace
func (as *AddOneTraceService) Trace(service, name string, traceInfo *baidurpc.TraceInfo) *baidurpc.TraceInfo {
*traceInfo.SpanId++
*traceInfo.TraceId++
*traceInfo.ParentSpanId++
return traceInfo
}
```

设置到service对象
```go
// ...
rpcServer := baidurpc.NewTpcServer(&serverMeta)
rpcServer.SetTraceService(new(AddOneTraceService))
```

### 开发RPC客户端

Expand Down Expand Up @@ -190,6 +242,29 @@ baidurpc是一种基于TCP协议的二进制高性能RPC通信协议实现。它
```

### 设置Trace功能
```go
// 调用RPC
serviceName := "echoService"
methodName := "echo"
rpcInvocation := baidurpc.NewRpcInvocation(&serviceName, &methodName)
// 设置trace信息
rpcInvocation.TraceId = 10
rpcInvocation.SpanId = 11
rpcInvocation.ParentSpanId = 12
rpcInvocation.RpcRequestMetaExt = map[string]string{"key1": "value1"}
// 调用时,设置超时功能
response, err := rpcClient.SendRpcRequestWithTimeout(100*time.Millisecond, rpcInvocation, &parameterOut)
// 如果发生超时, 返回的错误码为 62
// 获取服务端返回的trace信息
response.GetTraceId()
response.GetParentSpanId()
response.GetParentSpanId()
response.GetRpcRequestMetaExt()
```


### 开发Ha RPC客户端

Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions rpcpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,60 @@ func (r *RpcDataPackage) GetLogId() int64 {
return r.Meta.Request.GetLogId()
}

func (r *RpcDataPackage) TraceId(traceId int64) *RpcDataPackage {
initRequest(r)
r.Meta.Request.TraceId = &traceId
return r
}

func (r *RpcDataPackage) GetTraceId() *int64 {
initRequest(r)
return r.Meta.Request.TraceId
}

func (r *RpcDataPackage) SpanId(spanId int64) *RpcDataPackage {
initRequest(r)
r.Meta.Request.SpanId = &spanId
return r
}

func (r *RpcDataPackage) GetSpanId() *int64 {
initRequest(r)
return r.Meta.Request.SpanId
}

func (r *RpcDataPackage) ParentSpanId(parentSpanId int64) *RpcDataPackage {
initRequest(r)
r.Meta.Request.ParentSpanId = &parentSpanId
return r
}

func (r *RpcDataPackage) GetParentSpanId() *int64 {
initRequest(r)
return r.Meta.Request.ParentSpanId
}

func (r *RpcDataPackage) RpcRequestMetaExt(ext map[string]string) *RpcDataPackage {
initRequest(r)
extMap := make([]*RpcRequestMetaExtField, 0)
for key, value := range ext {
extfield := &RpcRequestMetaExtField{Key: key, Value: value}
extMap = append(extMap, extfield)
}
r.Meta.Request.RpcRequestMetaExt = extMap
return r
}

func (r *RpcDataPackage) GetRpcRequestMetaExt() map[string]string {
initRequest(r)
ret := make(map[string]string)
for _, rr := range r.Meta.Request.RpcRequestMetaExt {
ret[rr.Key] = rr.Value
}

return ret
}

func (r *RpcDataPackage) ErrorCode(errorCode int32) *RpcDataPackage {
initResponse(r)

Expand Down
Loading

0 comments on commit 1a42e49

Please sign in to comment.