| 52 | } |
| 53 | |
| 54 | func main() { |
| 55 | // Turn on global telemetry for the whole binary. If a configuration is |
| 56 | // specified, any created gRPC Client Conn's or Servers will emit telemetry |
| 57 | // data according the configuration. |
| 58 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
| 59 | defer cancel() |
| 60 | err := observability.Start(ctx) |
| 61 | if err != nil { |
| 62 | log.Fatalf("observability.Start() failed: %v", err) |
| 63 | } |
| 64 | defer observability.End() |
| 65 | |
| 66 | flag.Parse() |
| 67 | lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) |
| 68 | if err != nil { |
| 69 | log.Fatalf("failed to listen: %v", err) |
| 70 | } |
| 71 | s := grpc.NewServer() |
| 72 | pb.RegisterGreeterServer(s, &server{}) |
| 73 | log.Printf("server listening at %v", lis.Addr()) |
| 74 | |
| 75 | // This server can potentially be terminated by an external signal from the |
| 76 | // Operating System. The following catches those signals and calls s.Stop(). |
| 77 | // This causes the s.Serve() call to return and run main()'s defers, |
| 78 | // including the observability.End() call that ensures any pending |
| 79 | // observability data is sent to Cloud Operations. |
| 80 | c := make(chan os.Signal, 1) |
| 81 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 82 | go func() { |
| 83 | <-c |
| 84 | s.Stop() |
| 85 | }() |
| 86 | |
| 87 | if err := s.Serve(lis); err != nil { |
| 88 | log.Fatalf("failed to serve: %v", err) |
| 89 | } |
| 90 | } |