• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.gcpobservability;
18 
19 import io.grpc.Grpc;
20 import io.grpc.InsecureServerCredentials;
21 import io.grpc.Server;
22 import io.grpc.examples.helloworld.GreeterGrpc;
23 import io.grpc.examples.helloworld.HelloReply;
24 import io.grpc.examples.helloworld.HelloRequest;
25 import io.grpc.gcp.observability.GcpObservability;
26 import io.grpc.stub.StreamObserver;
27 import java.io.IOException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.logging.Logger;
30 
31 /**
32  * Observability server that manages startup/shutdown of a {@code Greeter} server and generates
33  * logs, metrics and traces based on the configuration.
34  */
35 public class GcpObservabilityServer {
36   private static final Logger logger = Logger.getLogger(GcpObservabilityServer.class.getName());
37 
38   private Server server;
39 
start()40   private void start() throws IOException {
41     int port = 50051;
42     server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
43         .addService(new GreeterImpl())
44         .build()
45         .start();
46     logger.info("Server started, listening on " + port);
47   }
48 
stop()49   private void stop() throws InterruptedException {
50     if (server != null) {
51       server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
52     }
53   }
54 
blockUntilShutdown()55   private void blockUntilShutdown() throws InterruptedException {
56     if (server != null) {
57       server.awaitTermination();
58     }
59   }
60 
61   /**
62    * Main launches the server from the command line.
63    */
main(String[] args)64   public static void main(String[] args) throws IOException, InterruptedException {
65     // Initialize observability
66     GcpObservability observability = GcpObservability.grpcInit();
67     final GcpObservabilityServer server = new GcpObservabilityServer();
68     server.start();
69 
70     Runtime.getRuntime().addShutdownHook(new Thread() {
71       @Override
72       public void run() {
73         System.err.println("*** shutting down gRPC server since JVM is shutting down");
74         try {
75           server.stop();
76         } catch (InterruptedException e) {
77           e.printStackTrace(System.err);
78         }
79         // Shut down observability
80         observability.close();
81         System.err.println("*** server shut down");
82       }
83     });
84 
85     server.blockUntilShutdown();
86   }
87 
88   static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
89 
90     @Override
sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)91     public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
92       HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
93       responseObserver.onNext(reply);
94       responseObserver.onCompleted();
95     }
96   }
97 }
98