• 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 
28 #include <string>
29 
30 #include "absl/flags/flag.h"
31 #include "absl/flags/parse.h"
32 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
33 #include "opentelemetry/exporters/prometheus/exporter_options.h"
34 #include "opentelemetry/sdk/metrics/meter_provider.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   opts.without_otel_scope = false;
53   auto prometheus_exporter =
54       opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
55   auto meter_provider =
56       std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
57   // The default histogram boundaries are not granular enough for RPCs. Override
58   // the "grpc.client.attempt.duration" view as recommended by
59   // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
60   AddLatencyView(meter_provider.get(), "grpc.client.attempt.duration", "s");
61   meter_provider->AddMetricReader(std::move(prometheus_exporter));
62   auto status = grpc::OpenTelemetryPluginBuilder()
63                     .SetMeterProvider(std::move(meter_provider))
64                     .BuildAndRegisterGlobal();
65   if (!status.ok()) {
66     std::cerr << "Failed to register gRPC OpenTelemetry Plugin: "
67               << status.ToString() << std::endl;
68     return static_cast<int>(status.code());
69   }
70 
71   // Continuously send RPCs every second.
72   RunClient(absl::GetFlag(FLAGS_target));
73 
74   return 0;
75 }
76