• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2021 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 // Explicitly define HAVE_ABSEIL to avoid conflict with OTel's Abseil
20 // version. Refer
21 // https://github.com/open-telemetry/opentelemetry-cpp/issues/1042.
22 #ifndef HAVE_ABSEIL
23 #define HAVE_ABSEIL
24 #endif
25 
26 #include <grpcpp/ext/otel_plugin.h>
27 #include <grpcpp/ext/proto_server_reflection_plugin.h>
28 #include <grpcpp/grpcpp.h>
29 #include <grpcpp/health_check_service_interface.h>
30 
31 #include <iostream>
32 #include <memory>
33 #include <string>
34 
35 #include "absl/flags/flag.h"
36 #include "absl/flags/parse.h"
37 #include "absl/strings/str_format.h"
38 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
39 #include "opentelemetry/exporters/prometheus/exporter_options.h"
40 #include "opentelemetry/sdk/metrics/meter_provider.h"
41 
42 #ifdef BAZEL_BUILD
43 #include "examples/cpp/otel/codelab/util.h"
44 #include "examples/protos/helloworld.grpc.pb.h"
45 #else
46 #include "helloworld.grpc.pb.h"
47 #include "util.h"
48 #endif
49 
50 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
51 ABSL_FLAG(std::string, prometheus_endpoint, "localhost:9464",
52           "Prometheus exporter endpoint");
53 
54 namespace {
55 
56 using grpc::CallbackServerContext;
57 using grpc::Server;
58 using grpc::ServerBuilder;
59 using grpc::ServerUnaryReactor;
60 using grpc::Status;
61 using helloworld::Greeter;
62 using helloworld::HelloReply;
63 using helloworld::HelloRequest;
64 
65 // Logic and data behind the server's behavior.
66 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)67   ServerUnaryReactor* SayHello(CallbackServerContext* context,
68                                const HelloRequest* request,
69                                HelloReply* reply) override {
70     std::string prefix("Hello ");
71     reply->set_message(prefix + request->name());
72 
73     // CODELAB HINT: This sleep seems suspicious.
74     std::this_thread::sleep_for(std::chrono::seconds(5));
75 
76     ServerUnaryReactor* reactor = context->DefaultReactor();
77     reactor->Finish(Status::OK);
78     return reactor;
79   }
80 };
81 
RunServer(uint16_t port)82 void RunServer(uint16_t port) {
83   std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
84   GreeterServiceImpl service;
85 
86   grpc::EnableDefaultHealthCheckService(true);
87   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
88   ServerBuilder builder;
89   // Listen on the given address without any authentication mechanism.
90   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
91   // Register "service" as the instance through which we'll communicate with
92   // clients. In this case it corresponds to an *synchronous* service.
93   builder.RegisterService(&service);
94   // Finally assemble the server.
95   std::unique_ptr<Server> server(builder.BuildAndStart());
96   std::cout << "Server listening on " << server_address << std::endl;
97 
98   // Wait for the server to shutdown. Note that some other thread must be
99   // responsible for shutting down the server for this call to ever return.
100   server->Wait();
101 }
102 
103 }  // namespace
104 
main(int argc,char ** argv)105 int main(int argc, char** argv) {
106   absl::ParseCommandLine(argc, argv);
107 
108   // CODELAB HINT : Add code to register gRPC OpenTelemetry plugin here.
109 
110   RunServer(absl::GetFlag(FLAGS_port));
111   return 0;
112 }
113