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 ServerUnaryReactor* reactor = context->DefaultReactor();
74 reactor->Finish(Status::OK);
75 return reactor;
76 }
77 };
78
RunServer(uint16_t port)79 void RunServer(uint16_t port) {
80 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
81 GreeterServiceImpl service;
82
83 grpc::EnableDefaultHealthCheckService(true);
84 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
85 ServerBuilder builder;
86 // Listen on the given address without any authentication mechanism.
87 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
88 // Register "service" as the instance through which we'll communicate with
89 // clients. In this case it corresponds to an *synchronous* service.
90 builder.RegisterService(&service);
91 // Finally assemble the server.
92 std::unique_ptr<Server> server(builder.BuildAndStart());
93 std::cout << "Server listening on " << server_address << std::endl;
94
95 // Wait for the server to shutdown. Note that some other thread must be
96 // responsible for shutting down the server for this call to ever return.
97 server->Wait();
98 }
99
100 } // namespace
101
main(int argc,char ** argv)102 int main(int argc, char** argv) {
103 absl::ParseCommandLine(argc, argv);
104 // Register a global gRPC OpenTelemetry plugin configured with a prometheus
105 // exporter.
106 opentelemetry::exporter::metrics::PrometheusExporterOptions opts;
107 opts.url = absl::GetFlag(FLAGS_prometheus_endpoint);
108 auto prometheus_exporter =
109 opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
110 auto meter_provider =
111 std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
112 // The default histogram boundaries are not granular enough for RPCs. Override
113 // the "grpc.server.call.duration" view as recommended by
114 // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
115 AddLatencyView(meter_provider.get(), "grpc.server.call.duration", "s");
116 meter_provider->AddMetricReader(std::move(prometheus_exporter));
117 auto status = grpc::OpenTelemetryPluginBuilder()
118 .SetMeterProvider(std::move(meter_provider))
119 .BuildAndRegisterGlobal();
120 if (!status.ok()) {
121 std::cerr << "Failed to register gRPC OpenTelemetry Plugin: "
122 << status.ToString() << std::endl;
123 return static_cast<int>(status.code());
124 }
125 RunServer(absl::GetFlag(FLAGS_port));
126 return 0;
127 }
128