1 /*
2 *
3 * Copyright 2023 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 #include <grpcpp/ext/admin_services.h>
20 #include <grpcpp/ext/csm_observability.h>
21 #include <grpcpp/ext/proto_server_reflection_plugin.h>
22 #include <grpcpp/grpcpp.h>
23 #include <grpcpp/health_check_service_interface.h>
24 #include <grpcpp/xds_server_builder.h>
25
26 #include <iostream>
27 #include <memory>
28 #include <string>
29
30 #include "absl/flags/flag.h"
31 #include "absl/flags/parse.h"
32 #include "absl/log/globals.h"
33 #include "absl/log/initialize.h"
34 #include "absl/log/log.h"
35 #include "absl/strings/str_cat.h"
36 #include "examples/cpp/otel/util.h"
37 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
38 #include "opentelemetry/exporters/prometheus/exporter_options.h"
39 #include "opentelemetry/sdk/metrics/meter_provider.h"
40
41 ABSL_FLAG(int32_t, port, 50051, "Server port for service.");
42 ABSL_FLAG(std::string, prometheus_endpoint, "localhost:9464",
43 "Prometheus exporter endpoint");
44
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46 absl::ParseCommandLine(argc, argv);
47 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
48 absl::SetGlobalVLogLevel(2);
49 absl::InitializeLog();
50 opentelemetry::exporter::metrics::PrometheusExporterOptions opts;
51 // default was "localhost:9464" which causes connection issue across GKE pods
52 opts.url = "0.0.0.0:9464";
53 opts.without_otel_scope = false;
54 auto prometheus_exporter =
55 opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
56 auto meter_provider =
57 std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
58 // The default histogram boundaries are not granular enough for RPCs. Override
59 // the "grpc.server.call.duration" view as recommended by
60 // https://github.com/grpc/proposal/blob/master/A66-otel-stats.md.
61 AddLatencyView(meter_provider.get(), "grpc.server.call.duration", "s");
62 meter_provider->AddMetricReader(std::move(prometheus_exporter));
63 auto observability = grpc::CsmObservabilityBuilder()
64 .SetMeterProvider(std::move(meter_provider))
65 .BuildAndRegister();
66 if (!observability.ok()) {
67 std::cerr << "CsmObservability::Init() failed: "
68 << observability.status().ToString() << std::endl;
69 return static_cast<int>(observability.status().code());
70 }
71 RunXdsEnabledServer(absl::GetFlag(FLAGS_port));
72 return 0;
73 }
74