|
1 | 1 | #include "grpc_agent.h" |
| 2 | +#include "../../src/nsolid/nsolid_util.h" |
2 | 3 |
|
3 | 4 | #include "asserts-cpp/asserts.h" |
4 | 5 | #include "nsolid/nsolid_api.h" |
@@ -62,6 +63,11 @@ using ThreadMetricsMap = std::map<uint64_t, ThreadMetrics::MetricsStor>; |
62 | 63 |
|
63 | 64 | constexpr uint64_t span_timer_interval = 1000; |
64 | 65 | constexpr size_t span_msg_q_min_size = 1000; |
| 66 | +// At class or file scope |
| 67 | +constexpr size_t retry_max_attempts = 5; |
| 68 | +constexpr auto retry_initial_backoff = std::chrono::milliseconds(500); |
| 69 | +constexpr auto retry_max_backoff = std::chrono::seconds(5); |
| 70 | +constexpr float retry_backoff_multiplier = 2.0f; |
65 | 71 |
|
66 | 72 | const char* const kNSOLID_GRPC_INSECURE = "NSOLID_GRPC_INSECURE"; |
67 | 73 | const char* const kNSOLID_GRPC_CERTS = "NSOLID_GRPC_CERTS"; |
@@ -992,7 +998,81 @@ void GrpcAgent::check_exit_on_profile() { |
992 | 998 | } |
993 | 999 | } |
994 | 1000 |
|
995 | | -int GrpcAgent::config(const json& config) { |
| 1001 | +void GrpcAgent::configure_grpc_exporters(const std::string& endpoint, |
| 1002 | + bool insecure) { |
| 1003 | + Debug("GrpcAgent configured. Endpoint: %s. Insecure: %d\n", |
| 1004 | + endpoint.c_str(), static_cast<unsigned>(insecure)); |
| 1005 | + |
| 1006 | + OtlpGrpcClientOptions opts; |
| 1007 | + opts.compression = "gzip"; |
| 1008 | + opts.endpoint = endpoint; |
| 1009 | + opts.metadata = {{"nsolid-agent-id", agent_id_}, |
| 1010 | + {"nsolid-saas", saas()}}; |
| 1011 | + // Make sure the client is initialized. We set it to the same |
| 1012 | + // default value as max_concurrent_requests as the exporters. |
| 1013 | + opts.max_concurrent_requests = 64; |
| 1014 | + opts.use_ssl_credentials = !insecure; |
| 1015 | + if (!insecure) { |
| 1016 | + if (!custom_certs_.empty()) { |
| 1017 | + opts.ssl_credentials_cacert_as_string = custom_certs_; |
| 1018 | + } else { |
| 1019 | + opts.ssl_credentials_cacert_as_string = cacert_; |
| 1020 | + } |
| 1021 | + } |
| 1022 | + |
| 1023 | + opts.retry_policy_max_attempts = retry_max_attempts; |
| 1024 | + opts.retry_policy_initial_backoff = retry_initial_backoff; |
| 1025 | + opts.retry_policy_max_backoff = retry_max_backoff; |
| 1026 | + opts.retry_policy_backoff_multiplier = retry_backoff_multiplier; |
| 1027 | + |
| 1028 | + nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(opts); |
| 1029 | + // CommandStream needs to be created before the OTLP client to avoid |
| 1030 | + // a race condition with abseil mutexes. |
| 1031 | + reset_command_stream(); |
| 1032 | + |
| 1033 | + std::shared_ptr<OtlpGrpcClient> client = |
| 1034 | + OtlpGrpcClientFactory::Create(opts); |
| 1035 | + |
| 1036 | + auto set_common_options = [&](auto& options) { |
| 1037 | + options.compression = "gzip"; |
| 1038 | + options.endpoint = endpoint; |
| 1039 | + options.metadata = {{"nsolid-agent-id", agent_id_}, |
| 1040 | + {"nsolid-saas", saas()}}; |
| 1041 | + if (!insecure) { |
| 1042 | + options.use_ssl_credentials = true; |
| 1043 | + if (!custom_certs_.empty()) { |
| 1044 | + options.ssl_credentials_cacert_as_string = custom_certs_; |
| 1045 | + } else { |
| 1046 | + options.ssl_credentials_cacert_as_string = cacert_; |
| 1047 | + } |
| 1048 | + } |
| 1049 | + |
| 1050 | + options.retry_policy_max_attempts = retry_max_attempts; |
| 1051 | + options.retry_policy_initial_backoff = retry_initial_backoff; |
| 1052 | + options.retry_policy_max_backoff = retry_max_backoff; |
| 1053 | + options.retry_policy_backoff_multiplier = retry_backoff_multiplier; |
| 1054 | + }; |
| 1055 | + |
| 1056 | + { |
| 1057 | + OtlpGrpcExporterOptions options; |
| 1058 | + set_common_options(options); |
| 1059 | + trace_exporter_ = std::make_unique<OtlpGrpcExporter>(options, client); |
| 1060 | + } |
| 1061 | + { |
| 1062 | + OtlpGrpcMetricExporterOptions options; |
| 1063 | + set_common_options(options); |
| 1064 | + metrics_exporter_ = |
| 1065 | + std::make_unique<OtlpGrpcMetricExporter>(options, client); |
| 1066 | + } |
| 1067 | + { |
| 1068 | + OtlpGrpcLogRecordExporterOptions options; |
| 1069 | + set_common_options(options); |
| 1070 | + log_exporter_ = |
| 1071 | + std::make_unique<OtlpGrpcLogRecordExporter>(options, client); |
| 1072 | + } |
| 1073 | +} |
| 1074 | + |
| 1075 | +int GrpcAgent::config(const nlohmann::json& config) { |
996 | 1076 | int ret = 0; |
997 | 1077 | json old_config = config_; |
998 | 1078 | config_ = config; |
@@ -1020,93 +1100,13 @@ int GrpcAgent::config(const json& config) { |
1020 | 1100 | // Only parse the insecure flag in non SaaS mode. |
1021 | 1101 | if (insecure_str.has_value() && (!saas_ || saas_->testing)) { |
1022 | 1102 | // insecure = std::stoull(insecure_str.value()); |
1023 | | - insecure = std::stoi(insecure_str.value()); |
| 1103 | + insecure = utils::parse_env_var_int(kNSOLID_GRPC_INSECURE, 0, 0, 1); |
1024 | 1104 | } |
1025 | 1105 |
|
1026 | 1106 | const std::string& endpoint = !saas_ ? |
1027 | 1107 | it->get<std::string>() : |
1028 | 1108 | saas_->endpoint; |
1029 | | - Debug("GrpcAgent configured. Endpoint: %s. Insecure: %d\n", |
1030 | | - endpoint.c_str(), static_cast<unsigned>(insecure)); |
1031 | | - |
1032 | | - OtlpGrpcClientOptions opts; |
1033 | | - opts.compression = "gzip"; |
1034 | | - opts.endpoint = endpoint; |
1035 | | - opts.metadata = {{"nsolid-agent-id", agent_id_}, |
1036 | | - {"nsolid-saas", saas()}}; |
1037 | | - // Make sure the client is initialized. We set it to the same |
1038 | | - // default value as ax_concurrent_requests as the exporters. |
1039 | | - opts.max_concurrent_requests = 64; |
1040 | | - opts.use_ssl_credentials = !insecure; |
1041 | | - if (!insecure) { |
1042 | | - if (!custom_certs_.empty()) { |
1043 | | - opts.ssl_credentials_cacert_as_string = custom_certs_; |
1044 | | - } else { |
1045 | | - opts.ssl_credentials_cacert_as_string = cacert_; |
1046 | | - } |
1047 | | - } |
1048 | | - |
1049 | | - nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(opts); |
1050 | | - // CommandStream needs to be created before the OTLP client to avoid |
1051 | | - // a race condition with abseil mutexes. |
1052 | | - reset_command_stream(); |
1053 | | - |
1054 | | - std::shared_ptr<OtlpGrpcClient> client = |
1055 | | - OtlpGrpcClientFactory::Create(opts); |
1056 | | - |
1057 | | - { |
1058 | | - OtlpGrpcExporterOptions options; |
1059 | | - options.compression = "gzip"; |
1060 | | - options.endpoint = endpoint; |
1061 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1062 | | - {"nsolid-saas", saas()}}; |
1063 | | - if (!insecure) { |
1064 | | - options.use_ssl_credentials = true; |
1065 | | - if (!custom_certs_.empty()) { |
1066 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1067 | | - } else { |
1068 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1069 | | - } |
1070 | | - } |
1071 | | - |
1072 | | - trace_exporter_ = std::make_unique<OtlpGrpcExporter>(options, client); |
1073 | | - } |
1074 | | - { |
1075 | | - OtlpGrpcMetricExporterOptions options; |
1076 | | - options.compression = "gzip"; |
1077 | | - options.endpoint = endpoint; |
1078 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1079 | | - {"nsolid-saas", saas()}}; |
1080 | | - if (!insecure) { |
1081 | | - options.use_ssl_credentials = true; |
1082 | | - if (!custom_certs_.empty()) { |
1083 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1084 | | - } else { |
1085 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1086 | | - } |
1087 | | - } |
1088 | | - |
1089 | | - metrics_exporter_ = |
1090 | | - std::make_unique<OtlpGrpcMetricExporter>(options, client); |
1091 | | - } |
1092 | | - { |
1093 | | - OtlpGrpcLogRecordExporterOptions options; |
1094 | | - options.compression = "gzip"; |
1095 | | - options.endpoint = endpoint; |
1096 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1097 | | - {"nsolid-saas", saas()}}; |
1098 | | - if (!insecure) { |
1099 | | - options.use_ssl_credentials = true; |
1100 | | - if (!custom_certs_.empty()) { |
1101 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1102 | | - } else { |
1103 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1104 | | - } |
1105 | | - } |
1106 | | - |
1107 | | - log_exporter_ = |
1108 | | - std::make_unique<OtlpGrpcLogRecordExporter>(options, client); |
1109 | | - } |
| 1109 | + configure_grpc_exporters(endpoint, insecure); |
1110 | 1110 | } |
1111 | 1111 | } |
1112 | 1112 |
|
@@ -1804,7 +1804,8 @@ void GrpcAgent::send_exit() { |
1804 | 1804 | exit_body->set_profile(cpu_profile_state.last_main_profile); |
1805 | 1805 | } |
1806 | 1806 |
|
1807 | | - auto context = GrpcClient::MakeClientContext(agent_id_, saas()); |
| 1807 | + // 1 second for fast exit |
| 1808 | + auto context = GrpcClient::MakeClientContext(agent_id_, saas(), 1); |
1808 | 1809 | uv_cond_t cond; |
1809 | 1810 | uv_mutex_t lock; |
1810 | 1811 | bool signaled = false; |
|
0 commit comments