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 <grpc/grpc.h>
20 #include <grpcpp/ext/csm_observability.h>
21 #include <grpcpp/health_check_service_interface.h>
22
23 #include <iostream>
24
25 #include "absl/flags/flag.h"
26 #include "absl/log/log.h"
27 #include "opentelemetry/exporters/prometheus/exporter_factory.h"
28 #include "opentelemetry/exporters/prometheus/exporter_options.h"
29 #include "opentelemetry/sdk/metrics/meter_provider.h"
30 #include "src/core/util/gethostname.h"
31 #include "test/core/test_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 VLOG(2) << "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 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 meter_provider->AddMetricReader(std::move(prometheus_exporter));
58 auto observability = grpc::CsmObservabilityBuilder()
59 .SetMeterProvider(std::move(meter_provider))
60 .BuildAndRegister();
61 assert(observability.ok());
62 return *std::move(observability);
63 }
64
main(int argc,char ** argv)65 int main(int argc, char** argv) {
66 grpc::testing::TestEnvironment env(&argc, argv);
67 grpc::testing::InitTest(&argc, &argv, true);
68 char* hostname = grpc_gethostname();
69 if (hostname == nullptr) {
70 std::cout << "Failed to get hostname, terminating" << std::endl;
71 return 1;
72 }
73 int port = absl::GetFlag(FLAGS_port);
74 if (port == 0) {
75 std::cout << "Invalid port, terminating" << std::endl;
76 return 1;
77 }
78 int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
79 if (maintenance_port == 0) {
80 std::cout << "Invalid maintenance port, terminating" << std::endl;
81 return 1;
82 }
83 grpc::EnableDefaultHealthCheckService(false);
84 bool enable_csm_observability = absl::GetFlag(FLAGS_enable_csm_observability);
85 grpc::CsmObservability observability;
86 if (enable_csm_observability) {
87 observability = EnableCsmObservability();
88 }
89 grpc::testing::RunServer(
90 absl::GetFlag(FLAGS_secure_mode), port, maintenance_port, hostname,
91 absl::GetFlag(FLAGS_server_id), [](grpc::Server* /* unused */) {});
92
93 return 0;
94 }
95