From 5046be4b5a11cddb9ce5a6177d1d41bc5573f934 Mon Sep 17 00:00:00 2001 From: Purnesh Dixit Date: Tue, 24 Dec 2024 18:11:16 +0530 Subject: [PATCH] examples/features/csm_observability: use helloworld client and server instead of echo client and server (#7945) --- .../features/csm_observability/client/main.go | 11 +++++++---- .../features/csm_observability/server/main.go | 15 ++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/examples/features/csm_observability/client/main.go b/examples/features/csm_observability/client/main.go index cf33e6cb641d..c5b81ff0beb7 100644 --- a/examples/features/csm_observability/client/main.go +++ b/examples/features/csm_observability/client/main.go @@ -30,7 +30,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" xdscreds "google.golang.org/grpc/credentials/xds" - "google.golang.org/grpc/examples/features/proto/echo" + pb "google.golang.org/grpc/examples/helloworld/helloworld" "google.golang.org/grpc/stats/opentelemetry" "google.golang.org/grpc/stats/opentelemetry/csm" _ "google.golang.org/grpc/xds" // To install the xds resolvers and balancers. @@ -40,9 +40,12 @@ import ( "go.opentelemetry.io/otel/sdk/metric" ) +const defaultName = "world" + var ( target = flag.String("target", "xds:///helloworld:50051", "the server address to connect to") prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint") + name = flag.String("name", defaultName, "Name to greet") ) func main() { @@ -68,15 +71,15 @@ func main() { log.Fatalf("Failed to start NewClient: %v", err) } defer cc.Close() - c := echo.NewEchoClient(cc) + c := pb.NewGreeterClient(cc) // Make an RPC every second. This should trigger telemetry to be emitted from // the client and the server. for { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) - r, err := c.UnaryEcho(ctx, &echo.EchoRequest{Message: "this is examples/opentelemetry"}) + r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name}) if err != nil { - log.Printf("UnaryEcho failed: %v", err) + log.Fatalf("Could not greet: %v", err) } fmt.Println(r) time.Sleep(time.Second) diff --git a/examples/features/csm_observability/server/main.go b/examples/features/csm_observability/server/main.go index 7a87517cbde6..3c4c7a328e0c 100644 --- a/examples/features/csm_observability/server/main.go +++ b/examples/features/csm_observability/server/main.go @@ -22,7 +22,6 @@ package main import ( "context" "flag" - "fmt" "log" "net" "net/http" @@ -30,7 +29,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" xdscreds "google.golang.org/grpc/credentials/xds" - pb "google.golang.org/grpc/examples/features/proto/echo" + pb "google.golang.org/grpc/examples/helloworld/helloworld" "google.golang.org/grpc/stats/opentelemetry" "google.golang.org/grpc/stats/opentelemetry/csm" "google.golang.org/grpc/xds" @@ -45,13 +44,15 @@ var ( prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint") ) -type echoServer struct { - pb.UnimplementedEchoServer +// server is used to implement helloworld.GreeterServer. +type server struct { + pb.UnimplementedGreeterServer addr string } -func (s *echoServer) UnaryEcho(_ context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) { - return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil +// SayHello implements helloworld.GreeterServer +func (s *server) SayHello(_ context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { + return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil } func main() { @@ -80,7 +81,7 @@ func main() { if err != nil { log.Fatalf("Failed to start xDS Server: %v", err) } - pb.RegisterEchoServer(s, &echoServer{addr: ":" + *port}) + pb.RegisterGreeterServer(s, &server{addr: ":" + *port}) log.Printf("Serving on %s\n", *port)