diff --git a/CHANGELOG.md b/CHANGELOG.md index 81726cd493..eb5675bed6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,9 @@ Increment the: * [EXPORTER] Support handling retry-able errors for OTLP/HTTP [#3223](https://github.com/open-telemetry/opentelemetry-cpp/pull/3223) +* [EXPORTER] Support handling retry-able errors for OTLP/gRPC + [#3219](https://github.com/open-telemetry/opentelemetry-cpp/pull/3219) + New features: * [SDK] Better control of threads executed by opentelemetry-cpp diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 8d4d95e4b6..0275f820a0 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -347,6 +347,7 @@ elif [[ "$1" == "cmake.exporter.otprotocol.test" ]]; then -DWITH_OTLP_HTTP=ON \ -DWITH_OTLP_FILE=ON \ -DWITH_OTLP_GRPC_SSL_MTLS_PREVIEW=ON \ + -DWITH_OTLP_RETRY_PREVIEW=ON \ "${SRC_DIR}" grpc_cpp_plugin=`which grpc_cpp_plugin` proto_make_file="CMakeFiles/opentelemetry_proto.dir/build.make" diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index cd4e1b62fa..bf0ae1f35b 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -317,7 +317,7 @@ if(BUILD_TESTING) add_executable(otlp_grpc_exporter_test test/otlp_grpc_exporter_test.cc) target_link_libraries( otlp_grpc_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - ${GMOCK_LIB} opentelemetry_exporter_otlp_grpc) + ${GMOCK_LIB} opentelemetry_exporter_otlp_grpc gRPC::grpc++) gtest_add_tests( TARGET otlp_grpc_exporter_test TEST_PREFIX exporter.otlp. diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h index 45bd2e8896..babb4cac54 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h @@ -62,6 +62,18 @@ struct OtlpGrpcClientOptions // Concurrent requests std::size_t max_concurrent_requests; #endif + + /** The maximum number of call attempts, including the original attempt. */ + std::uint32_t retry_policy_max_attempts{}; + + /** The initial backoff delay between retry attempts, random between (0, initial_backoff). */ + std::chrono::duration retry_policy_initial_backoff{}; + + /** The maximum backoff places an upper limit on exponential backoff growth. */ + std::chrono::duration retry_policy_max_backoff{}; + + /** The backoff will be multiplied by this value after each retry attempt. */ + float retry_policy_backoff_multiplier{}; }; } // namespace otlp diff --git a/exporters/otlp/src/otlp_grpc_client.cc b/exporters/otlp/src/otlp_grpc_client.cc index fe38f16e2b..4d2fb6d38e 100644 --- a/exporters/otlp/src/otlp_grpc_client.cc +++ b/exporters/otlp/src/otlp_grpc_client.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include "opentelemetry/common/timestamp.h" #include "opentelemetry/ext/http/common/url_parser.h" #include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/global_log_handler.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -347,6 +349,49 @@ std::shared_ptr OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO grpc_arguments.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP); } +#ifdef ENABLE_OTLP_RETRY_PREVIEW + if (options.retry_policy_max_attempts > 0U && + options.retry_policy_initial_backoff > std::chrono::duration::zero() && + options.retry_policy_max_backoff > std::chrono::duration::zero() && + options.retry_policy_backoff_multiplier > 0.0f) + { + static const auto kServiceConfigJson = opentelemetry::nostd::string_view{R"( + { + "methodConfig": [ + { + "name": [{}], + "retryPolicy": { + "maxAttempts": %0000000000u, + "initialBackoff": "%0000000000.1fs", + "maxBackoff": "%0000000000.1fs", + "backoffMultiplier": %0000000000.1f, + "retryableStatusCodes": [ + "CANCELLED", + "DEADLINE_EXCEEDED", + "ABORTED", + "OUT_OF_RANGE", + "DATA_LOSS", + "UNAVAILABLE" + ] + } + } + ] + })"}; + + // Allocate string with buffer large enough to hold the formatted json config + auto service_config = std::string(kServiceConfigJson.size(), '\0'); + // Prior to C++17, need to explicitly cast away constness from `data()` buffer + std::snprintf( + const_cast(service_config.data()), + service_config.size(), kServiceConfigJson.data(), options.retry_policy_max_attempts, + std::min(std::max(options.retry_policy_initial_backoff.count(), 0.f), 999999999.f), + std::min(std::max(options.retry_policy_max_backoff.count(), 0.f), 999999999.f), + std::min(std::max(options.retry_policy_backoff_multiplier, 0.f), 999999999.f)); + + grpc_arguments.SetServiceConfigJSON(service_config); + } +#endif // ENABLE_OTLP_RETRY_PREVIEW + if (options.use_ssl_credentials) { grpc::SslCredentialsOptions ssl_opts; diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index 53b6d5b8a3..20cc672760 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -105,7 +105,7 @@ sdk::common::ExportResult OtlpGrpcExporter::Export( google::protobuf::ArenaOptions arena_options; // It's easy to allocate datas larger than 1024 when we populate basic resource and attributes arena_options.initial_block_size = 1024; - // When in batch mode, it's easy to export a large number of spans at once, we can alloc a lager + // When in batch mode, it's easy to export a large number of spans at once, we can alloc a larger // block to reduce memory fragments. arena_options.max_block_size = 65536; std::unique_ptr arena{new google::protobuf::Arena{arena_options}}; diff --git a/exporters/otlp/src/otlp_grpc_exporter_options.cc b/exporters/otlp/src/otlp_grpc_exporter_options.cc index 5fcc77fc2f..12a493111e 100644 --- a/exporters/otlp/src/otlp_grpc_exporter_options.cc +++ b/exporters/otlp/src/otlp_grpc_exporter_options.cc @@ -36,6 +36,11 @@ OtlpGrpcExporterOptions::OtlpGrpcExporterOptions() #ifdef ENABLE_ASYNC_EXPORT max_concurrent_requests = 64; #endif + + retry_policy_max_attempts = GetOtlpDefaultTracesRetryMaxAttempts(); + retry_policy_initial_backoff = GetOtlpDefaultTracesRetryInitialBackoff(); + retry_policy_max_backoff = GetOtlpDefaultTracesRetryMaxBackoff(); + retry_policy_backoff_multiplier = GetOtlpDefaultTracesRetryBackoffMultiplier(); } OtlpGrpcExporterOptions::~OtlpGrpcExporterOptions() {} diff --git a/exporters/otlp/src/otlp_grpc_log_record_exporter_options.cc b/exporters/otlp/src/otlp_grpc_log_record_exporter_options.cc index b98f76a922..c789114ac2 100644 --- a/exporters/otlp/src/otlp_grpc_log_record_exporter_options.cc +++ b/exporters/otlp/src/otlp_grpc_log_record_exporter_options.cc @@ -34,6 +34,11 @@ OtlpGrpcLogRecordExporterOptions::OtlpGrpcLogRecordExporterOptions() #ifdef ENABLE_ASYNC_EXPORT max_concurrent_requests = 64; #endif + + retry_policy_max_attempts = GetOtlpDefaultLogsRetryMaxAttempts(); + retry_policy_initial_backoff = GetOtlpDefaultLogsRetryInitialBackoff(); + retry_policy_max_backoff = GetOtlpDefaultLogsRetryMaxBackoff(); + retry_policy_backoff_multiplier = GetOtlpDefaultLogsRetryBackoffMultiplier(); } OtlpGrpcLogRecordExporterOptions::~OtlpGrpcLogRecordExporterOptions() {} diff --git a/exporters/otlp/src/otlp_grpc_metric_exporter_options.cc b/exporters/otlp/src/otlp_grpc_metric_exporter_options.cc index 0d84ec276f..3d9b1963c6 100644 --- a/exporters/otlp/src/otlp_grpc_metric_exporter_options.cc +++ b/exporters/otlp/src/otlp_grpc_metric_exporter_options.cc @@ -35,6 +35,11 @@ OtlpGrpcMetricExporterOptions::OtlpGrpcMetricExporterOptions() #ifdef ENABLE_ASYNC_EXPORT max_concurrent_requests = 64; #endif + + retry_policy_max_attempts = GetOtlpDefaultMetricsRetryMaxAttempts(); + retry_policy_initial_backoff = GetOtlpDefaultMetricsRetryInitialBackoff(); + retry_policy_max_backoff = GetOtlpDefaultMetricsRetryMaxBackoff(); + retry_policy_backoff_multiplier = GetOtlpDefaultMetricsRetryBackoffMultiplier(); } OtlpGrpcMetricExporterOptions::~OtlpGrpcMetricExporterOptions() {} diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index fc4715fc0d..c3ba455c09 100644 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -17,17 +17,23 @@ // That is because `std::result_of` has been removed in C++20. # include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" - +# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" # include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" // Problematic code that pulls in Gmock and breaks with vs2019/c++latest : # include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h" +# include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" + # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +# include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/trace/simple_processor.h" +# include "opentelemetry/sdk/trace/simple_processor_factory.h" # include "opentelemetry/sdk/trace/tracer_provider.h" +# include "opentelemetry/sdk/trace/tracer_provider_factory.h" # include "opentelemetry/trace/provider.h" +# include "opentelemetry/trace/tracer_provider.h" # include # include @@ -285,9 +291,7 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigFromEnv) unsetenv("OTEL_EXPORTER_OTLP_HEADERS"); unsetenv("OTEL_EXPORTER_OTLP_TRACES_HEADERS"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcExporterTestPeer, ConfigHttpsSecureFromEnv) { @@ -303,9 +307,7 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigHttpsSecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_TRACES_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcExporterTestPeer, ConfigHttpInsecureFromEnv) { @@ -321,9 +323,7 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigHttpInsecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_TRACES_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownSecureFromEnv) { @@ -338,9 +338,7 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownSecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_TRACES_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownInsecureFromEnv) { @@ -355,7 +353,206 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownInsecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_TRACES_INSECURE"); } -# endif + +TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryDefaultValues) +{ + std::unique_ptr exporter(new OtlpGrpcExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 5); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1.0); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5.0); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5); +} + +TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_MAX_ATTEMPTS", "123", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_INITIAL_BACKOFF", "4.5", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_MAX_BACKOFF", "6.7", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_BACKOFF_MULTIPLIER", "8.9", 1); + + std::unique_ptr exporter(new OtlpGrpcExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 123); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_TRACES_RETRY_BACKOFF_MULTIPLIER"); +} + +TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryGenericValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1); + + std::unique_ptr exporter(new OtlpGrpcExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 321); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER"); +} +# endif // NO_GETENV + +# ifdef ENABLE_OTLP_RETRY_PREVIEW +struct TestTraceService : public opentelemetry::proto::collector::trace::v1::TraceService::Service +{ + TestTraceService(std::vector status_codes) : status_codes_(status_codes) {} + + inline grpc::Status Export( + grpc::ServerContext *, + const opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest *, + opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse *) override + { + ++request_count_; + return grpc::Status(status_codes_.at(index_++ % status_codes_.size()), "TEST!"); + } + + size_t request_count_ = 0UL; + size_t index_ = 0UL; + std::vector status_codes_; +}; + +using StatusCodeVector = std::vector; + +class OtlpGrpcExporterRetryIntegrationTests + : public ::testing::TestWithParam> +{}; + +INSTANTIATE_TEST_SUITE_P( + StatusCodes, + OtlpGrpcExporterRetryIntegrationTests, + testing::Values( + // With retry policy enabled + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::CANCELLED}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::UNKNOWN}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::INVALID_ARGUMENT}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::DEADLINE_EXCEEDED}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::NOT_FOUND}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::ALREADY_EXISTS}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::PERMISSION_DENIED}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::UNAUTHENTICATED}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::RESOURCE_EXHAUSTED}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::FAILED_PRECONDITION}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::ABORTED}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::OUT_OF_RANGE}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::UNIMPLEMENTED}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::INTERNAL}, 1), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::UNAVAILABLE}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::DATA_LOSS}, 5), + std::make_tuple(true, StatusCodeVector{grpc::StatusCode::OK}, 1), + std::make_tuple(true, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::ABORTED, + grpc::StatusCode::OUT_OF_RANGE, + grpc::StatusCode::DATA_LOSS}, + 5), + std::make_tuple(true, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, + grpc::StatusCode::UNAVAILABLE, + grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::OK}, + 4), + std::make_tuple(true, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::CANCELLED, + grpc::StatusCode::DEADLINE_EXCEEDED, grpc::StatusCode::OK}, + 4), + // With retry policy disabled + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::CANCELLED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::UNKNOWN}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::INVALID_ARGUMENT}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::DEADLINE_EXCEEDED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::NOT_FOUND}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::ALREADY_EXISTS}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::PERMISSION_DENIED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::UNAUTHENTICATED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::RESOURCE_EXHAUSTED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::FAILED_PRECONDITION}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::ABORTED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::OUT_OF_RANGE}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::UNIMPLEMENTED}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::INTERNAL}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::UNAVAILABLE}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::DATA_LOSS}, 1), + std::make_tuple(false, StatusCodeVector{grpc::StatusCode::OK}, 1), + std::make_tuple(false, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::ABORTED, + grpc::StatusCode::OUT_OF_RANGE, + grpc::StatusCode::DATA_LOSS}, + 1), + std::make_tuple(false, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, + grpc::StatusCode::UNAVAILABLE, + grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::OK}, + 1), + std::make_tuple(false, + StatusCodeVector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::CANCELLED, + grpc::StatusCode::DEADLINE_EXCEEDED, grpc::StatusCode::OK}, + 1))); + +TEST_P(OtlpGrpcExporterRetryIntegrationTests, StatusCodes) +{ + namespace otlp = opentelemetry::exporter::otlp; + namespace trace_sdk = opentelemetry::sdk::trace; + + const auto is_retry_enabled = std::get<0>(GetParam()); + const auto status_codes = std::get<1>(GetParam()); + const auto expected_attempts = std::get<2>(GetParam()); + TestTraceService service(status_codes); + std::unique_ptr server; + + std::thread server_thread([&server, &service]() { + std::string address("localhost:4317"); + grpc::ServerBuilder builder; + builder.RegisterService(&service); + builder.AddListeningPort(address, grpc::InsecureServerCredentials()); + server = builder.BuildAndStart(); + server->Wait(); + }); + + otlp::OtlpGrpcExporterOptions opts{}; + + if (is_retry_enabled) + { + opts.retry_policy_max_attempts = 5; + opts.retry_policy_initial_backoff = std::chrono::duration{0.1f}; + opts.retry_policy_max_backoff = std::chrono::duration{5.0f}; + opts.retry_policy_backoff_multiplier = 1.0f; + } + else + { + opts.retry_policy_max_attempts = 0; + opts.retry_policy_initial_backoff = std::chrono::duration::zero(); + opts.retry_policy_max_backoff = std::chrono::duration::zero(); + opts.retry_policy_backoff_multiplier = 0.0f; + } + + auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); + provider->GetTracer("Test tracer")->StartSpan("Test span")->End(); + provider->ForceFlush(); + + ASSERT_TRUE(server); + server->Shutdown(); + + if (server_thread.joinable()) + { + server_thread.join(); + } + + ASSERT_EQ(expected_attempts, service.request_count_); +} +# endif // ENABLE_OTLP_RETRY_PREVIEW } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc index 55dad11512..40b441a612 100644 --- a/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc @@ -466,6 +466,58 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ShareClientTest) trace_provider = opentelemetry::nostd::shared_ptr(); } +#ifndef NO_GETENV +TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryDefaultValues) +{ + std::unique_ptr exporter(new OtlpGrpcLogRecordExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 5); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1.0); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5.0); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5); +} + +TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_MAX_ATTEMPTS", "123", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_INITIAL_BACKOFF", "4.5", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_MAX_BACKOFF", "6.7", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_BACKOFF_MULTIPLIER", "8.9", 1); + + std::unique_ptr exporter(new OtlpGrpcLogRecordExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 123); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_LOGS_RETRY_BACKOFF_MULTIPLIER"); +} + +TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryGenericValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1); + + std::unique_ptr exporter(new OtlpGrpcLogRecordExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 321); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER"); +} +#endif // NO_GETENV + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc index d36def10f5..cf32b1387a 100644 --- a/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_metric_exporter_test.cc @@ -157,9 +157,7 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigFromEnv) unsetenv("OTEL_EXPORTER_OTLP_HEADERS"); unsetenv("OTEL_EXPORTER_OTLP_METRICS_HEADERS"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpsSecureFromEnv) { @@ -175,9 +173,7 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpsSecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpInsecureFromEnv) { @@ -193,9 +189,7 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigHttpInsecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownSecureFromEnv) { @@ -210,9 +204,7 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownSecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); } -# endif -# ifndef NO_GETENV // Test exporter configuration options with use_ssl_credentials TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv) { @@ -227,7 +219,57 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv) unsetenv("OTEL_EXPORTER_OTLP_ENDPOINT"); unsetenv("OTEL_EXPORTER_OTLP_METRICS_INSECURE"); } -# endif + +TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryDefaultValues) +{ + std::unique_ptr exporter(new OtlpGrpcMetricExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 5); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1.0); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5.0); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5); +} + +TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS", "123", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF", "4.5", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF", "6.7", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER", "8.9", 1); + + std::unique_ptr exporter(new OtlpGrpcMetricExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 123); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER"); +} + +TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryGenericValuesFromEnv) +{ + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1); + setenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1); + + std::unique_ptr exporter(new OtlpGrpcMetricExporter()); + const auto options = GetOptions(exporter); + ASSERT_EQ(options.retry_policy_max_attempts, 321); + ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4); + ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6); + ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8); + + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_MAX_BACKOFF"); + unsetenv("OTEL_CPP_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER"); +} +# endif // NO_GETENV TEST_F(OtlpGrpcMetricExporterTestPeer, CheckGetAggregationTemporality) {