• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <grpcpp/ext/gcp_observability.h>
20 #include <grpcpp/ext/proto_server_reflection_plugin.h>
21 #include <grpcpp/grpcpp.h>
22 #include <grpcpp/health_check_service_interface.h>
23 
24 #include <chrono>
25 #include <csignal>
26 #include <iostream>
27 #include <memory>
28 #include <string>
29 #include <thread>
30 
31 #include "absl/flags/flag.h"
32 #include "absl/flags/parse.h"
33 #include "absl/strings/str_format.h"
34 
35 #ifdef BAZEL_BUILD
36 #include "examples/protos/helloworld.grpc.pb.h"
37 #else
38 #include "helloworld.grpc.pb.h"
39 #endif
40 
41 using grpc::Server;
42 using grpc::ServerBuilder;
43 using grpc::ServerContext;
44 using grpc::Status;
45 using helloworld::Greeter;
46 using helloworld::HelloReply;
47 using helloworld::HelloRequest;
48 
49 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
50 
51 namespace {
52 
53 volatile std::sig_atomic_t g_shutdown_flag = 0;
54 
signal_handler(int signal)55 void signal_handler(int signal) {
56   g_shutdown_flag = 1;
57   std::signal(signal, SIG_DFL);
58 }
59 
60 // Logic and data behind the server's behavior.W
61 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)62   Status SayHello(ServerContext* context, const HelloRequest* request,
63                   HelloReply* reply) override {
64     std::string prefix("Hello ");
65     reply->set_message(prefix + request->name());
66     return Status::OK;
67   }
68 };
69 
RunServer(uint16_t port)70 void RunServer(uint16_t port) {
71   std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
72   GreeterServiceImpl service;
73   grpc::EnableDefaultHealthCheckService(true);
74   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
75   ServerBuilder builder;
76   // Listen on the given address without any authentication mechanism.
77   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
78   // Register "service" as the instance through which we'll communicate with
79   // clients. In this case it corresponds to an *synchronous* service.
80   builder.RegisterService(&service);
81   // Finally assemble the server.
82   std::unique_ptr<Server> server(builder.BuildAndStart());
83   std::cout << "Server listening on " << server_address << std::endl;
84   // Instead of server->Wait(), we are waiting on a shutdown notification from
85   // SIGINT.
86   while (!g_shutdown_flag) {
87     std::this_thread::sleep_for(std::chrono::milliseconds(100));
88   }
89   server->Shutdown();
90 }
91 
92 }  // namespace
93 
main(int argc,char ** argv)94 int main(int argc, char** argv) {
95   absl::ParseCommandLine(argc, argv);
96   // Install a signal handler for an indication to shut down server and flush
97   // out observability data;
98   std::signal(SIGINT, signal_handler);
99   // Turn on GCP Observability for the whole binary. Based on the configuration,
100   // this will emit observability data (stats, tracing and logging) to GCP
101   // backends. Note that this should be done before any other gRPC operation.
102   auto observability = grpc::GcpObservability::Init();
103   if (!observability.ok()) {
104     std::cerr << "GcpObservability::Init() failed: "
105               << observability.status().ToString() << std::endl;
106     return static_cast<int>(observability.status().code());
107   }
108   std::cout << "Initialized GCP Observability" << std::endl;
109   RunServer(absl::GetFlag(FLAGS_port));
110   // 'observability' object going out of scope will flush observability data.
111   std::cout << "Closing and flushing GCP Observability data" << std::endl;
112   return 0;
113 }
114