From b0b848231865af4c43f2ed0b8d5967eaf2502d9d Mon Sep 17 00:00:00 2001 From: copy rogers <40619032+rogerogers@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:46:19 +0800 Subject: [PATCH] chore(easy_note): collector as receiver (#56) * chore(easy_note): collector as receiver Signed-off-by: rogerogers * fix: some err Signed-off-by: rogerogers * fix: pass context Signed-off-by: rogerogers --------- Signed-off-by: rogerogers --- easy_note/api_request/api_service/hertz_client.go | 2 +- easy_note/cmd/api/hertz_handler/demoapi/api_service.go | 10 +++++----- easy_note/cmd/api/main.go | 10 ++++++++++ easy_note/cmd/api/rpc/note.go | 9 --------- easy_note/cmd/api/rpc/user.go | 9 --------- easy_note/docker-compose.yaml | 3 ++- easy_note/go.mod | 1 - easy_note/go.sum | 5 ----- 8 files changed, 18 insertions(+), 31 deletions(-) diff --git a/easy_note/api_request/api_service/hertz_client.go b/easy_note/api_request/api_service/hertz_client.go index 483996b3..5c785a79 100644 --- a/easy_note/api_request/api_service/hertz_client.go +++ b/easy_note/api_request/api_service/hertz_client.go @@ -573,7 +573,7 @@ func parseResponseBody(c *cli, res *response) (err error) { } } else { jsonByte, jsonErr := json.Marshal(map[string]interface{}{ - "status_code": res.rawResponse.StatusCode, + "status_code": res.rawResponse.StatusCode(), "body": string(res.bodyByte), }) if jsonErr != nil { diff --git a/easy_note/cmd/api/hertz_handler/demoapi/api_service.go b/easy_note/cmd/api/hertz_handler/demoapi/api_service.go index 67ef038d..b4fccea8 100644 --- a/easy_note/cmd/api/hertz_handler/demoapi/api_service.go +++ b/easy_note/cmd/api/hertz_handler/demoapi/api_service.go @@ -41,7 +41,7 @@ func CreateUser(ctx context.Context, c *app.RequestContext) { SendResponse(c, errno.ConvertErr(err), nil) return } - err = rpc.CreateUser(context.Background(), &demouser.CreateUserRequest{ + err = rpc.CreateUser(ctx, &demouser.CreateUserRequest{ Username: req.Username, Password: req.Password, }) @@ -69,7 +69,7 @@ func CreateNote(ctx context.Context, c *app.RequestContext) { return } v, _ := c.Get(consts.IdentityKey) - err = rpc.CreateNote(context.Background(), &demonote.CreateNoteRequest{ + err = rpc.CreateNote(ctx, &demonote.CreateNoteRequest{ Title: req.Title, Content: req.Content, UserId: v.(*demoapi.User).UserID, @@ -92,7 +92,7 @@ func QueryNote(ctx context.Context, c *app.RequestContext) { return } v, _ := c.Get(consts.IdentityKey) - notes, total, err := rpc.QueryNotes(context.Background(), &demonote.QueryNoteRequest{ + notes, total, err := rpc.QueryNotes(ctx, &demonote.QueryNoteRequest{ UserId: v.(*demoapi.User).UserID, SearchKey: req.SearchKey, Offset: req.Offset, @@ -119,7 +119,7 @@ func UpdateNote(ctx context.Context, c *app.RequestContext) { return } v, _ := c.Get(consts.IdentityKey) - err = rpc.UpdateNote(context.Background(), &demonote.UpdateNoteRequest{ + err = rpc.UpdateNote(ctx, &demonote.UpdateNoteRequest{ NoteId: req.NoteID, UserId: v.(*demoapi.User).UserID, Title: req.Title, @@ -143,7 +143,7 @@ func DeleteNote(ctx context.Context, c *app.RequestContext) { return } v, _ := c.Get(consts.IdentityKey) - err = rpc.DeleteNote(context.Background(), &demonote.DeleteNoteRequest{ + err = rpc.DeleteNote(ctx, &demonote.DeleteNoteRequest{ NoteId: req.NoteID, UserId: v.(*demoapi.User).UserID, }) diff --git a/easy_note/cmd/api/main.go b/easy_note/cmd/api/main.go index 7d32aacb..0b973aa3 100755 --- a/easy_note/cmd/api/main.go +++ b/easy_note/cmd/api/main.go @@ -18,13 +18,16 @@ package main import ( + "context" "github.com/cloudwego/biz-demo/easy_note/cmd/api/mw" "github.com/cloudwego/biz-demo/easy_note/cmd/api/rpc" + "github.com/cloudwego/biz-demo/easy_note/pkg/consts" "github.com/cloudwego/hertz/pkg/app/server" "github.com/cloudwego/hertz/pkg/common/hlog" hertzlogrus "github.com/hertz-contrib/obs-opentelemetry/logging/logrus" "github.com/hertz-contrib/obs-opentelemetry/tracing" "github.com/hertz-contrib/pprof" + "github.com/kitex-contrib/obs-opentelemetry/provider" ) func Init() { @@ -36,6 +39,13 @@ func Init() { } func main() { + p := provider.NewOpenTelemetryProvider( + provider.WithServiceName(consts.ApiServiceName), + provider.WithExportEndpoint(consts.ExportEndpoint), + provider.WithInsecure(), + ) + defer p.Shutdown(context.Background()) // nolint:errcheck + Init() tracer, cfg := tracing.NewServerTracer() h := server.New( diff --git a/easy_note/cmd/api/rpc/note.go b/easy_note/cmd/api/rpc/note.go index f862cc8d..44fa43f3 100644 --- a/easy_note/cmd/api/rpc/note.go +++ b/easy_note/cmd/api/rpc/note.go @@ -25,7 +25,6 @@ import ( "github.com/cloudwego/biz-demo/easy_note/pkg/mw" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/rpcinfo" - "github.com/kitex-contrib/obs-opentelemetry/provider" "github.com/kitex-contrib/obs-opentelemetry/tracing" etcd "github.com/kitex-contrib/registry-etcd" ) @@ -37,14 +36,6 @@ func initNote() { if err != nil { panic(err) } - p := provider.NewOpenTelemetryProvider( - provider.WithServiceName(consts.ApiServiceName), - provider.WithExportEndpoint(consts.ExportEndpoint), - provider.WithInsecure(), - ) - defer func(ctx context.Context, p provider.OtelProvider) { - _ = p.Shutdown(ctx) - }(context.Background(), p) c, err := noteservice.NewClient( consts.NoteServiceName, client.WithResolver(r), diff --git a/easy_note/cmd/api/rpc/user.go b/easy_note/cmd/api/rpc/user.go index 2fc5d725..77106042 100644 --- a/easy_note/cmd/api/rpc/user.go +++ b/easy_note/cmd/api/rpc/user.go @@ -25,7 +25,6 @@ import ( "github.com/cloudwego/biz-demo/easy_note/pkg/mw" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/rpcinfo" - "github.com/kitex-contrib/obs-opentelemetry/provider" "github.com/kitex-contrib/obs-opentelemetry/tracing" etcd "github.com/kitex-contrib/registry-etcd" ) @@ -37,14 +36,6 @@ func initUser() { if err != nil { panic(err) } - p := provider.NewOpenTelemetryProvider( - provider.WithServiceName(consts.ApiServiceName), - provider.WithExportEndpoint(consts.ExportEndpoint), - provider.WithInsecure(), - ) - defer func(ctx context.Context, p provider.OtelProvider) { - _ = p.Shutdown(ctx) - }(context.Background(), p) c, err := userservice.NewClient( consts.UserServiceName, client.WithResolver(r), diff --git a/easy_note/docker-compose.yaml b/easy_note/docker-compose.yaml index c03574a8..599c8109 100644 --- a/easy_note/docker-compose.yaml +++ b/easy_note/docker-compose.yaml @@ -34,6 +34,7 @@ services: - "8888" # Prometheus metrics exposed by the collector - "8889:8889" # Prometheus exporter metrics - "13133:13133" # health_check extension + - "4317:4317" # OTLP gRPC receiver - "55679" # zpages extension depends_on: - jaeger-all-in-one @@ -48,7 +49,7 @@ services: - "14268" - "14250:14250" - "6831:6831" - - "4317:4317" # OTLP gRPC receiver + - "4317" # OTLP gRPC receiver # Victoriametrics victoriametrics: diff --git a/easy_note/go.mod b/easy_note/go.mod index 9ddc126a..7b056d1f 100644 --- a/easy_note/go.mod +++ b/easy_note/go.mod @@ -67,7 +67,6 @@ require ( github.com/nyaruka/phonenumbers v1.1.6 // indirect github.com/oleiade/lane v1.0.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/stretchr/testify v1.8.4 // indirect github.com/tidwall/gjson v1.14.4 // indirect diff --git a/easy_note/go.sum b/easy_note/go.sum index 5e96c484..679bd8de 100644 --- a/easy_note/go.sum +++ b/easy_note/go.sum @@ -378,8 +378,6 @@ github.com/hertz-contrib/obs-opentelemetry/logging/logrus v0.1.1 h1:4KR/JoIc8PSb github.com/hertz-contrib/obs-opentelemetry/logging/logrus v0.1.1/go.mod h1:dLQsSFKUVF4pya7kLlF6l1T69+mOPObotXGJO8ptbho= github.com/hertz-contrib/obs-opentelemetry/tracing v0.2.0 h1:EmcIT5yqkXoytKJikihwFbIYFZJ0g66s8xZEDi1XNYE= github.com/hertz-contrib/obs-opentelemetry/tracing v0.2.0/go.mod h1:vTSOuQfZdcPbe7RMZ1n6eQrevVqmPiWD/bW9kcXt5ME= -github.com/hertz-contrib/pprof v0.1.0 h1:yES6PQ5AJZRKiUQem1cpXU2gskO4cHdPMiXu9qSGS7E= -github.com/hertz-contrib/pprof v0.1.0/go.mod h1:4vgREcobBOExepQdEt/EMD/Nc1u01rpXXaQa2TNjA4A= github.com/hertz-contrib/pprof v0.1.1 h1:x7kOFUtqkveXbDbiISIFE3x02cAtcb6/DBjHgsJmYcM= github.com/hertz-contrib/pprof v0.1.1/go.mod h1:9g23VQrcC4AjW++VA2relquyMXPVfs/s+t5FBIjIIOs= github.com/hertz-contrib/requestid v1.1.0 h1:+y1cuNlNX2KUoEC1SnBJ6M55/TlMTx3M9yxkqi0oTkk= @@ -520,8 +518,6 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d h1:Q+gqLBOPkFGHyCJxXMRqtUgUbTjI8/Ze8vu8GGyNFwo= -github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= @@ -589,7 +585,6 @@ github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6 github.com/v2pro/plz v0.0.0-20221028024117-e5f9aec5b631/go.mod h1:3gacX+hQo+xvl0vtLqCMufzxuNCwt4geAVOMt2LQYfE= github.com/v2pro/quokka v0.0.0-20171201153428-382cb39c6ee6/go.mod h1:0VP5W9AFNVWU8C1QLNeVg8TvzoEkIHWZ4vxtxEVFWUY= github.com/v2pro/wombat v0.0.0-20180402055224-a56dbdcddef2/go.mod h1:wen8nMxrRrUmXnRwH+3wGAW+hyYTHcOrTNhMpxyp/i0= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=