Skip to content

Commit

Permalink
Merge pull request #28 from FantasyRL/not_main
Browse files Browse the repository at this point in the history
feat:RPC tracing(jaeger)
  • Loading branch information
FantasyRL authored May 27, 2024
2 parents e99acde + 69eba46 commit eee8ff3
Show file tree
Hide file tree
Showing 29 changed files with 255 additions and 20 deletions.
42 changes: 42 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
*.o
*.a
*.so
_obj
_test
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.exe~
*.test
*.prof
*.rar
*.zip
*.gz
*.psd
*.bmd
*.cfg
*.pptx
*.log
*nohup.out
*settings.pyc
*.sublime-project
*.sublime-workspace
!.gitkeep
.DS_Store
/.idea
/.vscode
#/output
*.local.yml
dumped_hertz_remote_config.json

/data
hz_update.sh
kitex_update.sh
/codeql
analz.sarif
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _testmain.go
/output
*.local.yml
dumped_hertz_remote_config.json

/cmd/*/output
/data
hz_update.sh
kitex_update.sh
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:latest AS builder
FROM golang:1.22 AS builder
LABEL authors="fanr"

ENV TZ Asia/Shanghai
Expand All @@ -12,5 +12,6 @@ WORKDIR /app

ADD . /app
RUN go mod tidy
#CMD ["go run","example"]
RUN make build-all

14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ API_PATH= $(DIR)/cmd/api
SHELL=/bin/bash
KITEX_GEN_PATH=$(DIR)/kitex_gen
MODULE= bibi
OUTPUT=$(DIR)/output

.PHONY: init
init:
Expand All @@ -28,10 +29,19 @@ $(SERVICES):



.PHONY: build-all
build-all:
.PHONY: start-all
start-all:
sh start.sh

SERVICES := api user video interaction follow chat
.PHONY: build-all
build-all:
@for service in $(SERVICES); do \
cd ${RPC};cd $$service; \
echo "build $$service ..." && sh build.sh; \
cd ${RPC}/$$service/output/bin/ && cp -r . ${OUTPUT}/$$service; \
echo "done"; \
done \

KSERVICES := user video interaction follow chat
.PHONY: kgen
Expand Down
3 changes: 0 additions & 3 deletions causal.md

This file was deleted.

1 change: 1 addition & 0 deletions cmd/api/biz/handler/api/chat_handler.go

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

8 changes: 6 additions & 2 deletions cmd/api/biz/handler/api/user_handler.go

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

6 changes: 4 additions & 2 deletions cmd/api/biz/mw/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bibi/cmd/api/biz/model/api"
"bibi/cmd/api/biz/rpc"
"bibi/config"
"bibi/kitex_gen/base"
"bibi/kitex_gen/user"
"bibi/pkg/errno"
"bibi/pkg/pack"

"bibi/kitex_gen/base"
"bibi/kitex_gen/user"

"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/hlog"
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/biz/rpc/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"bibi/kitex_gen/chat"
"bibi/kitex_gen/chat/chathandler"
"bibi/pkg/constants"
opentracing "github.com/kitex-contrib/tracer-opentracing"

"context"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/loadbalance"
Expand All @@ -26,6 +28,7 @@ func InitChatRPC() {
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(opentracing.NewDefaultClientSuite()),
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/api/biz/rpc/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/loadbalance"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
opentracing "github.com/kitex-contrib/tracer-opentracing"
)

func InitFollowRPC() {
Expand All @@ -26,6 +27,7 @@ func InitFollowRPC() {
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(opentracing.NewDefaultClientSuite()),
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
1 change: 0 additions & 1 deletion cmd/api/biz/rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ func Init() {
InitFollowRPC()
InitInteractionRPC()
InitChatRPC()

}
2 changes: 2 additions & 0 deletions cmd/api/biz/rpc/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bibi/kitex_gen/interaction"
"bibi/kitex_gen/interaction/interactionhandler"
"context"
opentracing "github.com/kitex-contrib/tracer-opentracing"

"bibi/pkg/constants"
"github.com/cloudwego/kitex/client"
Expand All @@ -27,6 +28,7 @@ func InitInteractionRPC() {
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(opentracing.NewDefaultClientSuite()),
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
10 changes: 10 additions & 0 deletions cmd/api/biz/rpc/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/loadbalance"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
kopentracing "github.com/kitex-contrib/tracer-opentracing"
)

func InitUserRPC() {
Expand All @@ -19,13 +20,22 @@ func InitUserRPC() {
panic(err)
}

//kTracer, kCloser := tracer.InitJaegerTracer("kitex-client")
//defer kCloser.Close()
//tracer.InitJaegerTracer("kitex-client")

c, err := userhandler.NewClient(
constants.UserServiceName,
client.WithMuxConnection(constants.MuxConnection),
client.WithRPCTimeout(constants.RPCTimeout),
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(kopentracing.NewDefaultClientSuite()),
//client.WithSuite(kopentracing.NewClientSuite(kTracer, func(c context.Context) string {
// endpoint := rpcinfo.GetRPCInfo(c).From()
// return endpoint.ServiceName() + "::" + endpoint.Method()
//})), //jaeger
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
4 changes: 4 additions & 0 deletions cmd/api/biz/rpc/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"bibi/kitex_gen/video"
"bibi/kitex_gen/video/videohandler"
"bibi/pkg/constants"
"bibi/pkg/tracer"
"context"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/loadbalance"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
opentracing "github.com/kitex-contrib/tracer-opentracing"
)

func InitVideoRPC() {
Expand All @@ -19,13 +21,15 @@ func InitVideoRPC() {
panic(err)
}

tracer.InitJaegerTracer(constants.VideoServiceName)
c, err := videohandler.NewClient(
constants.VideoServiceName,
client.WithMuxConnection(constants.MuxConnection),
client.WithRPCTimeout(constants.RPCTimeout),
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(opentracing.NewDefaultClientSuite()),
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
10 changes: 10 additions & 0 deletions cmd/api/main.go

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

5 changes: 4 additions & 1 deletion cmd/interaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bibi/config"
interaction "bibi/kitex_gen/interaction/interactionhandler"
"bibi/pkg/constants"
"bibi/pkg/tracer"
"bibi/pkg/utils"
"bibi/pkg/utils/eslogrus"
"crypto/tls"
Expand All @@ -18,6 +19,7 @@ import (
elastic "github.com/elastic/go-elasticsearch/v8"
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
etcd "github.com/kitex-contrib/registry-etcd"
opentracing "github.com/kitex-contrib/tracer-opentracing"
"github.com/sirupsen/logrus"
"net"
"net/http"
Expand All @@ -36,7 +38,7 @@ func Init() {
InitEs()
klog.SetLevel(klog.LevelDebug)
klog.SetLogger(kitexlogrus.NewLogger(kitexlogrus.WithHook(EsHookLog())))

tracer.InitJaegerTracer(constants.UserServiceName)
rpc.InitVideoRPC()
}

Expand Down Expand Up @@ -68,6 +70,7 @@ func main() {

svr := interaction.NewServer(interactionHandlerImpl, // 指定 Registry 与服务基本信息
server.WithRegistry(r),
server.WithSuite(opentracing.NewDefaultServerSuite()),
server.WithServiceAddr(serviceAddr),
server.WithServerBasicInfo(
&rpcinfo.EndpointBasicInfo{
Expand Down
2 changes: 2 additions & 0 deletions cmd/interaction/rpc/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/loadbalance"
"github.com/cloudwego/kitex/pkg/retry"
etcd "github.com/kitex-contrib/registry-etcd"
opentracing "github.com/kitex-contrib/tracer-opentracing"
)

var videoClient videohandler.Client
Expand All @@ -28,6 +29,7 @@ func InitVideoRPC() {
client.WithConnectTimeout(constants.ConnectTimeout),
client.WithFailureRetry(retry.NewFailurePolicy()),
client.WithResolver(r),
client.WithSuite(opentracing.NewDefaultClientSuite()),
client.WithLoadBalancer(loadbalance.NewWeightedRoundRobinBalancer()),
)

Expand Down
7 changes: 5 additions & 2 deletions cmd/user/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ func (s *UserHandlerImpl) Register(ctx context.Context, req *user.RegisterReques
// Login implements the UserHandlerImpl interface.
func (s *UserHandlerImpl) Login(ctx context.Context, req *user.LoginRequest) (resp *user.LoginResponse, err error) {
resp = new(user.LoginResponse)

//stTracer, closer := tracer.InitJaegerTracer("login")
//defer closer.Close()
//parentSpan := stTracer.StartSpan("handler")
//defer parentSpan.Finish()
//userResp, err := service.NewUserService(ctx).Login(req, stTracer, parentSpan)
userResp, err := service.NewUserService(ctx).Login(req)

resp.Base = pack.BuildBaseResp(err)
if err != nil {
return resp, nil
Expand Down
Loading

0 comments on commit eee8ff3

Please sign in to comment.