Skip to content

Instantly share code, notes, and snippets.

@TonPC64
Created February 24, 2022 14:43
Show Gist options
  • Save TonPC64/e520cee4e297abdd3df30777ccdacb12 to your computer and use it in GitHub Desktop.
Save TonPC64/e520cee4e297abdd3df30777ccdacb12 to your computer and use it in GitHub Desktop.
type server struct {
api.HelloServiceServer
}
func (s *server) SayHello(ctx context.Context, in *api.HelloRequest) (*api.HelloResponse, error) {
tracer := otel.GetTracerProvider().Tracer("webserver-grpc")
_, span := tracer.Start(ctx, "SayHello-span")
defer span.End()
fmt.Println(ctx)
log.Printf("Received: %v\n", in.GetGreeting())
time.Sleep(50 * time.Millisecond)
return &api.HelloResponse{Reply: "Hello " + in.Greeting}, nil
}
func main() {
lis, err := net.Listen("tcp", ":"+os.Getenv("PORT"))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer(
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
)
api.RegisterHelloServiceServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment