|
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"; |
@@ -998,7 +1004,81 @@ void GrpcAgent::check_exit_on_profile() { |
998 | 1004 | } |
999 | 1005 | } |
1000 | 1006 |
|
1001 | | -int GrpcAgent::config(const json& config) { |
| 1007 | +void GrpcAgent::configure_grpc_exporters(const std::string& endpoint, |
| 1008 | + bool insecure) { |
| 1009 | + Debug("GrpcAgent configured. Endpoint: %s. Insecure: %d\n", |
| 1010 | + endpoint.c_str(), static_cast<unsigned>(insecure)); |
| 1011 | + |
| 1012 | + OtlpGrpcClientOptions opts; |
| 1013 | + opts.compression = "gzip"; |
| 1014 | + opts.endpoint = endpoint; |
| 1015 | + opts.metadata = {{"nsolid-agent-id", agent_id_}, |
| 1016 | + {"nsolid-saas", saas()}}; |
| 1017 | + // Make sure the client is initialized. We set it to the same |
| 1018 | + // default value as max_concurrent_requests as the exporters. |
| 1019 | + opts.max_concurrent_requests = 64; |
| 1020 | + opts.use_ssl_credentials = !insecure; |
| 1021 | + if (!insecure) { |
| 1022 | + if (!custom_certs_.empty()) { |
| 1023 | + opts.ssl_credentials_cacert_as_string = custom_certs_; |
| 1024 | + } else { |
| 1025 | + opts.ssl_credentials_cacert_as_string = cacert_; |
| 1026 | + } |
| 1027 | + } |
| 1028 | + |
| 1029 | + opts.retry_policy_max_attempts = retry_max_attempts; |
| 1030 | + opts.retry_policy_initial_backoff = retry_initial_backoff; |
| 1031 | + opts.retry_policy_max_backoff = retry_max_backoff; |
| 1032 | + opts.retry_policy_backoff_multiplier = retry_backoff_multiplier; |
| 1033 | + |
| 1034 | + nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(opts); |
| 1035 | + // CommandStream needs to be created before the OTLP client to avoid |
| 1036 | + // a race condition with abseil mutexes. |
| 1037 | + reset_command_stream(); |
| 1038 | + |
| 1039 | + std::shared_ptr<OtlpGrpcClient> client = |
| 1040 | + OtlpGrpcClientFactory::Create(opts); |
| 1041 | + |
| 1042 | + auto set_common_options = [&](auto& options) { |
| 1043 | + options.compression = "gzip"; |
| 1044 | + options.endpoint = endpoint; |
| 1045 | + options.metadata = {{"nsolid-agent-id", agent_id_}, |
| 1046 | + {"nsolid-saas", saas()}}; |
| 1047 | + if (!insecure) { |
| 1048 | + options.use_ssl_credentials = true; |
| 1049 | + if (!custom_certs_.empty()) { |
| 1050 | + options.ssl_credentials_cacert_as_string = custom_certs_; |
| 1051 | + } else { |
| 1052 | + options.ssl_credentials_cacert_as_string = cacert_; |
| 1053 | + } |
| 1054 | + } |
| 1055 | + |
| 1056 | + options.retry_policy_max_attempts = retry_max_attempts; |
| 1057 | + options.retry_policy_initial_backoff = retry_initial_backoff; |
| 1058 | + options.retry_policy_max_backoff = retry_max_backoff; |
| 1059 | + options.retry_policy_backoff_multiplier = retry_backoff_multiplier; |
| 1060 | + }; |
| 1061 | + |
| 1062 | + { |
| 1063 | + OtlpGrpcExporterOptions options; |
| 1064 | + set_common_options(options); |
| 1065 | + trace_exporter_ = std::make_unique<OtlpGrpcExporter>(options, client); |
| 1066 | + } |
| 1067 | + { |
| 1068 | + OtlpGrpcMetricExporterOptions options; |
| 1069 | + set_common_options(options); |
| 1070 | + metrics_exporter_ = |
| 1071 | + std::make_unique<OtlpGrpcMetricExporter>(options, client); |
| 1072 | + } |
| 1073 | + { |
| 1074 | + OtlpGrpcLogRecordExporterOptions options; |
| 1075 | + set_common_options(options); |
| 1076 | + log_exporter_ = |
| 1077 | + std::make_unique<OtlpGrpcLogRecordExporter>(options, client); |
| 1078 | + } |
| 1079 | +} |
| 1080 | + |
| 1081 | +int GrpcAgent::config(const nlohmann::json& config) { |
1002 | 1082 | int ret = 0; |
1003 | 1083 | json old_config = config_; |
1004 | 1084 | config_ = config; |
@@ -1026,93 +1106,13 @@ int GrpcAgent::config(const json& config) { |
1026 | 1106 | // Only parse the insecure flag in non SaaS mode. |
1027 | 1107 | if (insecure_str.has_value() && (!saas_ || saas_->testing)) { |
1028 | 1108 | // insecure = std::stoull(insecure_str.value()); |
1029 | | - insecure = std::stoi(insecure_str.value()); |
| 1109 | + insecure = utils::parse_env_var_int(kNSOLID_GRPC_INSECURE, 0, 0, 1); |
1030 | 1110 | } |
1031 | 1111 |
|
1032 | 1112 | const std::string& endpoint = !saas_ ? |
1033 | 1113 | it->get<std::string>() : |
1034 | 1114 | saas_->endpoint; |
1035 | | - Debug("GrpcAgent configured. Endpoint: %s. Insecure: %d\n", |
1036 | | - endpoint.c_str(), static_cast<unsigned>(insecure)); |
1037 | | - |
1038 | | - OtlpGrpcClientOptions opts; |
1039 | | - opts.compression = "gzip"; |
1040 | | - opts.endpoint = endpoint; |
1041 | | - opts.metadata = {{"nsolid-agent-id", agent_id_}, |
1042 | | - {"nsolid-saas", saas()}}; |
1043 | | - // Make sure the client is initialized. We set it to the same |
1044 | | - // default value as ax_concurrent_requests as the exporters. |
1045 | | - opts.max_concurrent_requests = 64; |
1046 | | - opts.use_ssl_credentials = !insecure; |
1047 | | - if (!insecure) { |
1048 | | - if (!custom_certs_.empty()) { |
1049 | | - opts.ssl_credentials_cacert_as_string = custom_certs_; |
1050 | | - } else { |
1051 | | - opts.ssl_credentials_cacert_as_string = cacert_; |
1052 | | - } |
1053 | | - } |
1054 | | - |
1055 | | - nsolid_service_stub_ = GrpcClient::MakeNSolidServiceStub(opts); |
1056 | | - // CommandStream needs to be created before the OTLP client to avoid |
1057 | | - // a race condition with abseil mutexes. |
1058 | | - reset_command_stream(); |
1059 | | - |
1060 | | - std::shared_ptr<OtlpGrpcClient> client = |
1061 | | - OtlpGrpcClientFactory::Create(opts); |
1062 | | - |
1063 | | - { |
1064 | | - OtlpGrpcExporterOptions options; |
1065 | | - options.compression = "gzip"; |
1066 | | - options.endpoint = endpoint; |
1067 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1068 | | - {"nsolid-saas", saas()}}; |
1069 | | - if (!insecure) { |
1070 | | - options.use_ssl_credentials = true; |
1071 | | - if (!custom_certs_.empty()) { |
1072 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1073 | | - } else { |
1074 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1075 | | - } |
1076 | | - } |
1077 | | - |
1078 | | - trace_exporter_ = std::make_unique<OtlpGrpcExporter>(options, client); |
1079 | | - } |
1080 | | - { |
1081 | | - OtlpGrpcMetricExporterOptions options; |
1082 | | - options.compression = "gzip"; |
1083 | | - options.endpoint = endpoint; |
1084 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1085 | | - {"nsolid-saas", saas()}}; |
1086 | | - if (!insecure) { |
1087 | | - options.use_ssl_credentials = true; |
1088 | | - if (!custom_certs_.empty()) { |
1089 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1090 | | - } else { |
1091 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1092 | | - } |
1093 | | - } |
1094 | | - |
1095 | | - metrics_exporter_ = |
1096 | | - std::make_unique<OtlpGrpcMetricExporter>(options, client); |
1097 | | - } |
1098 | | - { |
1099 | | - OtlpGrpcLogRecordExporterOptions options; |
1100 | | - options.compression = "gzip"; |
1101 | | - options.endpoint = endpoint; |
1102 | | - options.metadata = {{"nsolid-agent-id", agent_id_}, |
1103 | | - {"nsolid-saas", saas()}}; |
1104 | | - if (!insecure) { |
1105 | | - options.use_ssl_credentials = true; |
1106 | | - if (!custom_certs_.empty()) { |
1107 | | - options.ssl_credentials_cacert_as_string = custom_certs_; |
1108 | | - } else { |
1109 | | - options.ssl_credentials_cacert_as_string = cacert_; |
1110 | | - } |
1111 | | - } |
1112 | | - |
1113 | | - log_exporter_ = |
1114 | | - std::make_unique<OtlpGrpcLogRecordExporter>(options, client); |
1115 | | - } |
| 1115 | + configure_grpc_exporters(endpoint, insecure); |
1116 | 1116 | } |
1117 | 1117 | } |
1118 | 1118 |
|
@@ -1821,7 +1821,8 @@ void GrpcAgent::send_exit() { |
1821 | 1821 | exit_body->set_profile(cpu_profile_state.last_main_profile); |
1822 | 1822 | } |
1823 | 1823 |
|
1824 | | - auto context = GrpcClient::MakeClientContext(agent_id_, saas()); |
| 1824 | + // 1 second for fast exit |
| 1825 | + auto context = GrpcClient::MakeClientContext(agent_id_, saas(), 1); |
1825 | 1826 | uv_cond_t cond; |
1826 | 1827 | uv_mutex_t lock; |
1827 | 1828 | bool signaled = false; |
|
0 commit comments