• 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/grpcpp.h>
21 
22 #include <chrono>
23 #include <iostream>
24 #include <memory>
25 #include <string>
26 #include <thread>
27 
28 #include "absl/flags/flag.h"
29 #include "absl/flags/parse.h"
30 
31 #ifdef BAZEL_BUILD
32 #include "examples/protos/helloworld.grpc.pb.h"
33 #else
34 #include "helloworld.grpc.pb.h"
35 #endif
36 
37 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
38 
39 using grpc::Channel;
40 using grpc::ClientContext;
41 using grpc::Status;
42 using helloworld::Greeter;
43 using helloworld::HelloReply;
44 using helloworld::HelloRequest;
45 
46 class GreeterClient {
47  public:
GreeterClient(std::shared_ptr<Channel> channel)48   GreeterClient(std::shared_ptr<Channel> channel)
49       : stub_(Greeter::NewStub(channel)) {}
50 
51   // Assembles the client's payload, sends it and presents the response back
52   // from the server.
SayHello(const std::string & user)53   std::string SayHello(const std::string& user) {
54     // Data we are sending to the server.
55     HelloRequest request;
56     request.set_name(user);
57 
58     // Container for the data we expect from the server.
59     HelloReply reply;
60 
61     // Context for the client. It could be used to convey extra information to
62     // the server and/or tweak certain RPC behaviors.
63     ClientContext context;
64 
65     // The actual RPC.
66     Status status = stub_->SayHello(&context, request, &reply);
67 
68     // Act upon its status.
69     if (status.ok()) {
70       return reply.message();
71     } else {
72       std::cout << status.error_code() << ": " << status.error_message()
73                 << std::endl;
74       return "RPC failed";
75     }
76   }
77 
78  private:
79   std::unique_ptr<Greeter::Stub> stub_;
80 };
81 
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83   absl::ParseCommandLine(argc, argv);
84   // Instantiate the client. It requires a channel, out of which the actual RPCs
85   // are created. This channel models a connection to an endpoint specified by
86   // the argument "--target=" which is the only expected argument.
87   std::string target_str = absl::GetFlag(FLAGS_target);
88   // Turn on GCP Observability for the whole binary. Based on the configuration,
89   // this will emit observability data (stats, tracing and logging) to GCP
90   // backends. Note that this should be done before any other gRPC operation.
91   auto observability = grpc::GcpObservability::Init();
92   if (!observability.ok()) {
93     std::cerr << "GcpObservability::Init() failed: "
94               << observability.status().ToString() << std::endl;
95     return static_cast<int>(observability.status().code());
96   }
97   std::cout << "Initialized GCP Observability" << std::endl;
98   // We indicate that the channel isn't authenticated (use of
99   // InsecureChannelCredentials()).
100   GreeterClient greeter(
101       grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
102   std::string user("world");
103   std::string reply = greeter.SayHello(user);
104   std::cout << "Greeter received: " << reply << std::endl;
105   // 'observability' object going out of scope will flush observability data.
106   std::cout << "Closing and flushing GCP Observability data" << std::endl;
107   return 0;
108 }
109