mirror of https://github.com/grpc/grpc-go.git
examples/features/csm_observability: use helloworld client and server instead of echo client and server (#7945)
This commit is contained in:
parent
e8d5feb181
commit
724f450f77
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue