Skip to content

Commit 83c9eee

Browse files
author
myxy99
committed
grpc
1 parent 62aa90d commit 83c9eee

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

xgrpc/client/interceptor.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ package clientinterceptors
77
import (
88
"context"
99
"errors"
10-
"fmt"
1110
"github.com/afex/hystrix-go/hystrix"
1211
"github.com/coder2z/g-saber/xcast"
1312
"github.com/coder2z/g-saber/xlog"
13+
"github.com/coder2z/g-saber/xstring"
1414
"github.com/coder2z/g-server/xapp"
1515
"github.com/coder2z/g-server/xcode"
1616
"github.com/coder2z/g-server/xmonitor"
@@ -153,15 +153,24 @@ func XLoggerUnaryClientInterceptor(name string) grpc.UnaryClientInterceptor {
153153
}
154154
}
155155

156+
type XAid struct {
157+
AppName string `json:"app_name"`
158+
HostName string `json:"host_name"`
159+
AppId string `json:"app_id"`
160+
HostIP string `json:"host_ip"`
161+
}
162+
156163
func XAidUnaryClientInterceptor() grpc.UnaryClientInterceptor {
157164
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
158165
md, ok := metadata.FromOutgoingContext(ctx)
166+
var info = XAid{
167+
AppName: xapp.Name(),
168+
HostName: xapp.HostName(),
169+
AppId: xapp.AppId(),
170+
HostIP: xapp.HostIP(),
171+
}
159172
clientAidMD := metadata.Pairs(
160-
"info", fmt.Sprintf("%s/%s/%s",
161-
xapp.Name(),
162-
xapp.HostName(),
163-
xapp.AppId(),
164-
),
173+
"info", xstring.Json(info),
165174
"ip", xapp.HostIP(),
166175
"app_id", xapp.AppId(),
167176
"app_name", xapp.Name(),

xgrpc/xgrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func WithUnaryClientInterceptors(interceptors ...grpc.UnaryClientInterceptor) gr
2424
}
2525

2626
func ExtractFromCtx(ctx context.Context, key string) string {
27-
if md, ok := metadata.FromOutgoingContext(ctx); ok {
27+
if md, ok := metadata.FromIncomingContext(ctx); ok {
2828
return strings.Join(md.Get(key), ",")
2929
}
3030
return ""

0 commit comments

Comments
 (0)