xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/otel/greeter_callback_client.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <string>
27 
28 #include "absl/flags/flag.h"
29 #include "absl/flags/parse.h"
30 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
31 #include "opentelemetry/exporters/prometheus/exporter_options.h"
32 #include "opentelemetry/sdk/metrics/meter_provider.h"
33 
34 #include <grpcpp/ext/otel_plugin.h>
35 
36 #ifdef BAZEL_BUILD
37 #include "examples/cpp/otel/util.h"
38 #else
39 #include "util.h"
40 #endif
41 
42 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
43 ABSL_FLAG(std::string, prometheus_endpoint, "localhost:9465",
44           "Prometheus exporter endpoint");
45 
main(int argc,char ** argv)46 int main(int argc, char** argv) {
47   absl::ParseCommandLine(argc, argv);
48   // Register a global gRPC OpenTelemetry plugin configured with a prometheus
49   // exporter.
50   opentelemetry::exporter::metrics::PrometheusExporterOptions opts;
51   opts.url = absl::GetFlag(FLAGS_prometheus_endpoint);
52   auto prometheus_exporter =
53       opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
54   auto meter_provider =
55       std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
56   // The default histogram boundaries are not granular enough for RPCs. Override
57   // the "grpc.client.attempt.duration" view as recommended by
58   // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
59   AddLatencyView(meter_provider.get(), "grpc.client.attempt.duration", "s");
60   meter_provider->AddMetricReader(std::move(prometheus_exporter));
61   auto status = grpc::OpenTelemetryPluginBuilder()
62                     .SetMeterProvider(std::move(meter_provider))
63                     .BuildAndRegisterGlobal();
64   if (!status.ok()) {
65     std::cerr << "Failed to register gRPC OpenTelemetry Plugin: "
66               << status.ToString() << std::endl;
67     return static_cast<int>(status.code());
68   }
69 
70   // Continuously send RPCs every second.
71   RunClient(absl::GetFlag(FLAGS_target));
72 
73   return 0;
74 }
75