xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/xds_interop_server.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2020 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 <iostream>
20 
21 #include "absl/flags/flag.h"
22 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
23 #include "opentelemetry/exporters/prometheus/exporter_options.h"
24 #include "opentelemetry/sdk/metrics/meter_provider.h"
25 
26 #include <grpc/grpc.h>
27 #include <grpcpp/ext/csm_observability.h>
28 #include <grpcpp/health_check_service_interface.h>
29 
30 #include "src/core/lib/iomgr/gethostname.h"
31 #include "test/core/util/test_config.h"
32 #include "test/cpp/interop/xds_interop_server_lib.h"
33 #include "test/cpp/util/test_config.h"
34 
35 ABSL_FLAG(int32_t, port, 8080, "Server port for service.");
36 ABSL_FLAG(int32_t, maintenance_port, 8081,
37           "Server port for maintenance if --security is \"secure\".");
38 ABSL_FLAG(std::string, server_id, "cpp_server",
39           "Server ID to include in responses.");
40 ABSL_FLAG(bool, secure_mode, false,
41           "If true, XdsServerCredentials are used, InsecureServerCredentials "
42           "otherwise");
43 ABSL_FLAG(bool, enable_csm_observability, false,
44           "Whether to enable CSM Observability");
45 
EnableCsmObservability()46 grpc::CsmObservability EnableCsmObservability() {
47   gpr_log(GPR_DEBUG, "Registering Prometheus exporter");
48   opentelemetry::exporter::metrics::PrometheusExporterOptions opts;
49   // default was "localhost:9464" which causes connection issue across GKE
50   // pods
51   opts.url = "0.0.0.0:9464";
52   auto prometheus_exporter =
53       opentelemetry::exporter::metrics::PrometheusExporterFactory::Create(opts);
54   auto meter_provider =
55       std::make_shared<opentelemetry::sdk::metrics::MeterProvider>();
56   meter_provider->AddMetricReader(std::move(prometheus_exporter));
57   auto observability = grpc::CsmObservabilityBuilder()
58                            .SetMeterProvider(std::move(meter_provider))
59                            .BuildAndRegister();
60   assert(observability.ok());
61   return *std::move(observability);
62 }
63 
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65   grpc::testing::TestEnvironment env(&argc, argv);
66   grpc::testing::InitTest(&argc, &argv, true);
67   char* hostname = grpc_gethostname();
68   if (hostname == nullptr) {
69     std::cout << "Failed to get hostname, terminating" << std::endl;
70     return 1;
71   }
72   int port = absl::GetFlag(FLAGS_port);
73   if (port == 0) {
74     std::cout << "Invalid port, terminating" << std::endl;
75     return 1;
76   }
77   int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
78   if (maintenance_port == 0) {
79     std::cout << "Invalid maintenance port, terminating" << std::endl;
80     return 1;
81   }
82   grpc::EnableDefaultHealthCheckService(false);
83   bool enable_csm_observability = absl::GetFlag(FLAGS_enable_csm_observability);
84   grpc::CsmObservability observability;
85   if (enable_csm_observability) {
86     observability = EnableCsmObservability();
87   }
88   grpc::testing::RunServer(absl::GetFlag(FLAGS_secure_mode),
89                            enable_csm_observability, port, maintenance_port,
90                            hostname, absl::GetFlag(FLAGS_server_id),
91                            [](grpc::Server* /* unused */) {});
92 
93   return 0;
94 }
95