From 0722918589539f5c1c72e44c602720f2ce40a338 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 7 Nov 2023 15:53:40 +0900 Subject: [PATCH] Support OpenTelemetry --- .../federation/federation_grpc_federation.pb.go | 16 ++++++++++++++++ _examples/12_simple_validation/go.mod | 6 +++++- _examples/12_simple_validation/go.sum | 14 +++++++++++++- generator/testdata/expected_validation.go | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/_examples/12_simple_validation/federation/federation_grpc_federation.pb.go b/_examples/12_simple_validation/federation/federation_grpc_federation.pb.go index d6e36a71..8c6d10df 100644 --- a/_examples/12_simple_validation/federation/federation_grpc_federation.pb.go +++ b/_examples/12_simple_validation/federation/federation_grpc_federation.pb.go @@ -13,6 +13,8 @@ import ( "github.com/google/cel-go/cel" celtypes "github.com/google/cel-go/common/types" grpcfed "github.com/mercari/grpc-federation/grpc/federation" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" "golang.org/x/sync/errgroup" "golang.org/x/sync/singleflight" ) @@ -74,6 +76,7 @@ type FederationService struct { logger *slog.Logger errorHandler grpcfed.ErrorHandler env *cel.Env + tracer trace.Tracer client *FederationServiceDependentClientSet } @@ -106,12 +109,16 @@ func NewFederationService(cfg FederationServiceConfig) (*FederationService, erro logger: logger, errorHandler: errorHandler, env: env, + tracer: otel.Tracer("org.federation.FederationService"), client: &FederationServiceDependentClientSet{}, }, nil } // GetPost implements "org.federation.FederationService/GetPost" method. func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (res *GetPostResponse, e error) { + ctx, span := s.tracer.Start(ctx, "org.federation.FederationService/GetPost") + defer span.End() + ctx = grpcfed.WithLogger(ctx, s.logger) defer func() { if r := recover(); r != nil { @@ -124,6 +131,7 @@ func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (r Id: req.Id, }) if err != nil { + grpcfed.RecordErrorToSpan(ctx, err) grpcfed.OutputErrorLog(ctx, s.logger, err) return nil, err } @@ -132,6 +140,9 @@ func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (r // resolve_Org_Federation_GetPostResponse resolve "org.federation.GetPostResponse" message. func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.Context, req *Org_Federation_GetPostResponseArgument[*FederationServiceDependentClientSet]) (*GetPostResponse, error) { + ctx, span := s.tracer.Start(ctx, "org.federation.GetPostResponse") + defer span.End() + s.logger.DebugContext(ctx, "resolve org.federation.GetPostResponse", slog.Any("message_args", s.logvalue_Org_Federation_GetPostResponseArgument(req))) var ( sg singleflight.Group @@ -276,6 +287,7 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C }) if err := eg.Wait(); err != nil { + grpcfed.RecordErrorToSpan(ctx, err) return nil, err } @@ -290,6 +302,7 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C { _value, err := grpcfed.EvalCEL(s.env, "post", envOpts, evalValues, reflect.TypeOf((*Post)(nil))) if err != nil { + grpcfed.RecordErrorToSpan(ctx, err) return nil, err } ret.Post = _value.(*Post) @@ -301,6 +314,9 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C // resolve_Org_Federation_Post resolve "org.federation.Post" message. func (s *FederationService) resolve_Org_Federation_Post(ctx context.Context, req *Org_Federation_PostArgument[*FederationServiceDependentClientSet]) (*Post, error) { + ctx, span := s.tracer.Start(ctx, "org.federation.Post") + defer span.End() + s.logger.DebugContext(ctx, "resolve org.federation.Post", slog.Any("message_args", s.logvalue_Org_Federation_PostArgument(req))) // create a message value to be returned. diff --git a/_examples/12_simple_validation/go.mod b/_examples/12_simple_validation/go.mod index 5cb76bbd..3941b472 100644 --- a/_examples/12_simple_validation/go.mod +++ b/_examples/12_simple_validation/go.mod @@ -8,6 +8,8 @@ require ( github.com/google/cel-go v0.18.1 github.com/google/go-cmp v0.6.0 github.com/mercari/grpc-federation v0.0.0-00010101000000-000000000000 + go.opentelemetry.io/otel v1.19.0 + go.opentelemetry.io/otel/trace v1.19.0 golang.org/x/sync v0.4.0 google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a google.golang.org/grpc v1.58.3 @@ -17,12 +19,14 @@ require ( require ( github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect + github.com/go-logr/logr v1.2.4 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect + go.opentelemetry.io/otel/metric v1.19.0 // indirect golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/_examples/12_simple_validation/go.sum b/_examples/12_simple_validation/go.sum index 3a8ec9f5..5f850e96 100644 --- a/_examples/12_simple_validation/go.sum +++ b/_examples/12_simple_validation/go.sum @@ -2,8 +2,14 @@ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -20,6 +26,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs= +go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY= +go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE= +go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8= +go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg= +go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= diff --git a/generator/testdata/expected_validation.go b/generator/testdata/expected_validation.go index 097d3a13..b6dcce0b 100644 --- a/generator/testdata/expected_validation.go +++ b/generator/testdata/expected_validation.go @@ -13,6 +13,8 @@ import ( "github.com/google/cel-go/cel" celtypes "github.com/google/cel-go/common/types" grpcfed "github.com/mercari/grpc-federation/grpc/federation" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/trace" "golang.org/x/sync/errgroup" "golang.org/x/sync/singleflight" ) @@ -74,6 +76,7 @@ type FederationService struct { logger *slog.Logger errorHandler grpcfed.ErrorHandler env *cel.Env + tracer trace.Tracer client *FederationServiceDependentClientSet } @@ -106,12 +109,16 @@ func NewFederationService(cfg FederationServiceConfig) (*FederationService, erro logger: logger, errorHandler: errorHandler, env: env, + tracer: otel.Tracer("org.federation.FederationService"), client: &FederationServiceDependentClientSet{}, }, nil } // GetPost implements "org.federation.FederationService/GetPost" method. func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (res *GetPostResponse, e error) { + ctx, span := s.tracer.Start(ctx, "org.federation.FederationService/GetPost") + defer span.End() + ctx = grpcfed.WithLogger(ctx, s.logger) defer func() { if r := recover(); r != nil { @@ -124,6 +131,7 @@ func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (r Id: req.Id, }) if err != nil { + grpcfed.RecordErrorToSpan(ctx, err) grpcfed.OutputErrorLog(ctx, s.logger, err) return nil, err } @@ -132,6 +140,9 @@ func (s *FederationService) GetPost(ctx context.Context, req *GetPostRequest) (r // resolve_Org_Federation_GetPostResponse resolve "org.federation.GetPostResponse" message. func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.Context, req *Org_Federation_GetPostResponseArgument[*FederationServiceDependentClientSet]) (*GetPostResponse, error) { + ctx, span := s.tracer.Start(ctx, "org.federation.GetPostResponse") + defer span.End() + s.logger.DebugContext(ctx, "resolve org.federation.GetPostResponse", slog.Any("message_args", s.logvalue_Org_Federation_GetPostResponseArgument(req))) var ( sg singleflight.Group @@ -250,6 +261,7 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C }) if err := eg.Wait(); err != nil { + grpcfed.RecordErrorToSpan(ctx, err) return nil, err } @@ -264,6 +276,7 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C { _value, err := grpcfed.EvalCEL(s.env, "post", envOpts, evalValues, reflect.TypeOf((*Post)(nil))) if err != nil { + grpcfed.RecordErrorToSpan(ctx, err) return nil, err } ret.Post = _value.(*Post) @@ -275,6 +288,9 @@ func (s *FederationService) resolve_Org_Federation_GetPostResponse(ctx context.C // resolve_Org_Federation_Post resolve "org.federation.Post" message. func (s *FederationService) resolve_Org_Federation_Post(ctx context.Context, req *Org_Federation_PostArgument[*FederationServiceDependentClientSet]) (*Post, error) { + ctx, span := s.tracer.Start(ctx, "org.federation.Post") + defer span.End() + s.logger.DebugContext(ctx, "resolve org.federation.Post", slog.Any("message_args", s.logvalue_Org_Federation_PostArgument(req))) // create a message value to be returned.