xref: /aosp_15_r20/external/grpc-grpc/examples/cpp/otel/ostream/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/ostream/metric_exporter.h"
31 #include "opentelemetry/exporters/ostream/metric_exporter_factory.h"
32 #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
33 #include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h"
34 #include "opentelemetry/sdk/metrics/meter_provider.h"
35 
36 #include <grpcpp/ext/otel_plugin.h>
37 
38 #ifdef BAZEL_BUILD
39 #include "examples/cpp/otel/util.h"
40 #else
41 #include "../util.h"
42 #endif
43 
44 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
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 an ostream
49   // exporter.
50   auto ostream_exporter =
51       opentelemetry::exporter::metrics::OStreamMetricExporterFactory::Create();
52   opentelemetry::sdk::metrics::PeriodicExportingMetricReaderOptions
53       reader_options;
54   reader_options.export_interval_millis = std::chrono::milliseconds(1000);
55   reader_options.export_timeout_millis = std::chrono::milliseconds(500);
56   auto reader =
57       opentelemetry::sdk::metrics::PeriodicExportingMetricReaderFactory::Create(
58           std::move(ostream_exporter), reader_options);
59   auto meter_provider =
60       std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
61   // The default histogram boundaries are not granular enough for RPCs. Override
62   // the "grpc.client.attempt.duration" view as recommended by
63   // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
64   AddLatencyView(meter_provider.get(), "grpc.client.attempt.duration", "s");
65   meter_provider->AddMetricReader(std::move(reader));
66   auto status = grpc::OpenTelemetryPluginBuilder()
67                     .SetMeterProvider(std::move(meter_provider))
68                     .BuildAndRegisterGlobal();
69   if (!status.ok()) {
70     std::cerr << "Failed to register gRPC OpenTelemetry Plugin: "
71               << status.ToString() << std::endl;
72     return static_cast<int>(status.code());
73   }
74 
75   // Continuously send RPCs every second.
76   RunClient(absl::GetFlag(FLAGS_target));
77 
78   return 0;
79 }
80