From 6024e57681cf752c72fae3a18ea0b74a66a43879 Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Wed, 21 Feb 2024 22:48:47 +0700 Subject: [PATCH 1/2] fix(productcatalogservice): fix graceful shutdown issues --- src/productcatalogservice/go.mod | 2 +- src/productcatalogservice/go.sum | 4 ++-- src/productcatalogservice/main.go | 32 +++++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/productcatalogservice/go.mod b/src/productcatalogservice/go.mod index 200346cd38..a0d7d4bcdf 100644 --- a/src/productcatalogservice/go.mod +++ b/src/productcatalogservice/go.mod @@ -12,7 +12,7 @@ require ( go.opentelemetry.io/otel/sdk v1.23.1 go.opentelemetry.io/otel/sdk/metric v1.23.1 go.opentelemetry.io/otel/trace v1.23.1 - google.golang.org/grpc v1.61.0 + google.golang.org/grpc v1.61.1 google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 google.golang.org/protobuf v1.32.0 ) diff --git a/src/productcatalogservice/go.sum b/src/productcatalogservice/go.sum index c624fb587d..664b7d666d 100644 --- a/src/productcatalogservice/go.sum +++ b/src/productcatalogservice/go.sum @@ -74,8 +74,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe h1: google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:4jWUdICTdgc3Ibxmr8nAJiiLHwQBY0UI0XZcEMaFKaA= google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe h1:bQnxqljG/wqi4NTXu2+DJ3n7APcEA882QZ1JvhQAq9o= google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s= -google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= -google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= +google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY= +google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 h1:rNBFJjBCOgVr9pWD7rs/knKL4FRTKgpZmsRfV214zcA= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0/go.mod h1:Dk1tviKTvMCz5tvh7t+fh94dhmQVHuCt2OzJB3CTW9Y= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= diff --git a/src/productcatalogservice/main.go b/src/productcatalogservice/main.go index 2f3d0d7f2d..fdcfcca5a2 100644 --- a/src/productcatalogservice/main.go +++ b/src/productcatalogservice/main.go @@ -10,37 +10,38 @@ import ( "context" "fmt" "io/fs" - "net" "os" + "os/signal" "strings" "sync" + "syscall" "time" - pb "github.com/opentelemetry/opentelemetry-demo/src/productcatalogservice/genproto/oteldemo" "github.com/sirupsen/logrus" + "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" "go.opentelemetry.io/contrib/instrumentation/runtime" - sdkmetric "go.opentelemetry.io/otel/sdk/metric" - healthpb "google.golang.org/grpc/health/grpc_health_v1" - "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" otelcodes "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" "go.opentelemetry.io/otel/propagation" + sdkmetric "go.opentelemetry.io/otel/sdk/metric" sdkresource "go.opentelemetry.io/otel/sdk/resource" sdktrace "go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/trace" - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials/insecure" + healthpb "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/reflection" "google.golang.org/grpc/status" + "google.golang.org/protobuf/encoding/protojson" + + pb "github.com/opentelemetry/opentelemetry-demo/src/productcatalogservice/genproto/oteldemo" ) var ( @@ -115,6 +116,7 @@ func main() { if err := tp.Shutdown(context.Background()); err != nil { log.Fatalf("Tracer Provider Shutdown: %v", err) } + log.Println("Shutdown tracer provider") }() mp := initMeterProvider() @@ -122,6 +124,7 @@ func main() { if err := mp.Shutdown(context.Background()); err != nil { log.Fatalf("Error shutting down meter provider: %v", err) } + log.Println("Shutdown meter provider") }() err := runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second)) @@ -149,7 +152,20 @@ func main() { pb.RegisterProductCatalogServiceServer(srv, svc) healthpb.RegisterHealthServer(srv, svc) - srv.Serve(ln) + + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGKILL) + defer cancel() + + go func() { + if err := srv.Serve(ln); err != nil { + log.Fatalf("Failed to serve gRPC server, err: %v", err) + } + }() + + <-ctx.Done() + + srv.GracefulStop() + log.Println("ProductCatalogService gRPC server stopped") } type productCatalog struct { From b5154bd4776ea2f710344ba6e16e7e065ce9c03a Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Fri, 23 Feb 2024 14:37:39 +0700 Subject: [PATCH 2/2] fix(productcatalogservice): update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 087da9fcc0..534717b298 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ the release. ([#1398](https://github.com/open-telemetry/opentelemetry-demo/pull/1398)) * [chore] remove unused integration test ([#1406](https://github.com/open-telemetry/opentelemetry-demo/pull/1406)) +* [productcatalogservice] fix graceful shutdown issues + ([#1402](https://github.com/open-telemetry/opentelemetry-demo/pull/1402)) ## 1.8.0