Skip to content

Commit

Permalink
examples/features/csm_observability: use helloworld client and server…
Browse files Browse the repository at this point in the history
… instead of echo client and server (grpc#7945)
  • Loading branch information
purnesh42H authored Dec 24, 2024
1 parent e8d5feb commit 724f450
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 7 additions & 4 deletions examples/features/csm_observability/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
Expand All @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions examples/features/csm_observability/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"

"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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 724f450

Please sign in to comment.