Skip to content

Commit f06f0b2

Browse files
committed
Test config and retry mechanism
1 parent 5137900 commit f06f0b2

File tree

3 files changed

+302
-1
lines changed

3 files changed

+302
-1
lines changed

exporters/otlp/test/otlp_grpc_exporter_test.cc

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
// That is because `std::result_of` has been removed in C++20.
1818

1919
# include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
20-
20+
# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
2121
# include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
22+
# include "opentelemetry/nostd/shared_ptr.h"
2223

24+
# include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"
2325
// Problematic code that pulls in Gmock and breaks with vs2019/c++latest :
2426
# include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h"
2527

2628
# include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
2729

2830
# include "opentelemetry/sdk/trace/simple_processor.h"
31+
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
2932
# include "opentelemetry/sdk/trace/tracer_provider.h"
33+
# include "opentelemetry/sdk/trace/tracer_provider_factory.h"
3034
# include "opentelemetry/trace/provider.h"
35+
# include "opentelemetry/trace/tracer_provider.h"
3136

3237
# include <grpcpp/grpcpp.h>
3338
# include <gtest/gtest.h>
@@ -357,6 +362,198 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownInsecureFromEnv)
357362
}
358363
# endif
359364

365+
# ifndef NO_GETENV
366+
TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryDefaultValues)
367+
{
368+
std::unique_ptr<OtlpGrpcExporter> exporter(new OtlpGrpcExporter());
369+
const auto options = GetOptions(exporter);
370+
ASSERT_EQ(options.retry_policy_max_attempts, 5);
371+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1);
372+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5);
373+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5);
374+
}
375+
376+
TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryValuesFromEnv)
377+
{
378+
setenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_MAX_ATTEMPTS", "123", 1);
379+
setenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_INITIAL_BACKOFF", "4.5", 1);
380+
setenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_MAX_BACKOFF", "6.7", 1);
381+
setenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_BACKOFF_MULTIPLIER", "8.9", 1);
382+
383+
std::unique_ptr<OtlpGrpcExporter> exporter(new OtlpGrpcExporter());
384+
const auto options = GetOptions(exporter);
385+
ASSERT_EQ(options.retry_policy_max_attempts, 123);
386+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5);
387+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7);
388+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9);
389+
390+
unsetenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_MAX_ATTEMPTS");
391+
unsetenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_INITIAL_BACKOFF");
392+
unsetenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_MAX_BACKOFF");
393+
unsetenv("OTEL_EXPORTER_OTLP_TRACES_RETRY_BACKOFF_MULTIPLIER");
394+
}
395+
396+
TEST_F(OtlpGrpcExporterTestPeer, ConfigRetryGenericValuesFromEnv)
397+
{
398+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1);
399+
setenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1);
400+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1);
401+
setenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1);
402+
403+
std::unique_ptr<OtlpGrpcExporter> exporter(new OtlpGrpcExporter());
404+
const auto options = GetOptions(exporter);
405+
ASSERT_EQ(options.retry_policy_max_attempts, 321);
406+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4);
407+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6);
408+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8);
409+
410+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS");
411+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF");
412+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF");
413+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER");
414+
}
415+
# endif // NO_GETENV
416+
417+
struct TestTraceService : public opentelemetry::proto::collector::trace::v1::TraceService::Service
418+
{
419+
TestTraceService(std::vector<grpc::StatusCode> status_codes) : status_codes_(status_codes) {}
420+
421+
inline grpc::Status Export(
422+
grpc::ServerContext *context,
423+
const opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest *request,
424+
opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse *response) override
425+
{
426+
++request_count_;
427+
return grpc::Status(status_codes_.at(index_++ % status_codes_.size()), "TEST!");
428+
}
429+
430+
size_t request_count_ = 0UL;
431+
size_t index_ = 0UL;
432+
std::vector<grpc::StatusCode> status_codes_;
433+
};
434+
435+
class OtlpGrpcExporterRetryIntegrationTests
436+
: public ::testing::TestWithParam<std::tuple<bool, std::vector<grpc::StatusCode>, std::size_t>>
437+
{};
438+
439+
INSTANTIATE_TEST_SUITE_P(
440+
StatusCodes,
441+
OtlpGrpcExporterRetryIntegrationTests,
442+
testing::Values(
443+
// With retry policy enabled
444+
std::make_tuple(true, std::vector{grpc::StatusCode::CANCELLED}, 5),
445+
std::make_tuple(true, std::vector{grpc::StatusCode::UNKNOWN}, 1),
446+
std::make_tuple(true, std::vector{grpc::StatusCode::INVALID_ARGUMENT}, 1),
447+
std::make_tuple(true, std::vector{grpc::StatusCode::DEADLINE_EXCEEDED}, 5),
448+
std::make_tuple(true, std::vector{grpc::StatusCode::NOT_FOUND}, 1),
449+
std::make_tuple(true, std::vector{grpc::StatusCode::ALREADY_EXISTS}, 1),
450+
std::make_tuple(true, std::vector{grpc::StatusCode::PERMISSION_DENIED}, 1),
451+
std::make_tuple(true, std::vector{grpc::StatusCode::UNAUTHENTICATED}, 1),
452+
std::make_tuple(true, std::vector{grpc::StatusCode::RESOURCE_EXHAUSTED}, 1),
453+
std::make_tuple(true, std::vector{grpc::StatusCode::FAILED_PRECONDITION}, 1),
454+
std::make_tuple(true, std::vector{grpc::StatusCode::ABORTED}, 5),
455+
std::make_tuple(true, std::vector{grpc::StatusCode::OUT_OF_RANGE}, 5),
456+
std::make_tuple(true, std::vector{grpc::StatusCode::UNIMPLEMENTED}, 1),
457+
std::make_tuple(true, std::vector{grpc::StatusCode::INTERNAL}, 1),
458+
std::make_tuple(true, std::vector{grpc::StatusCode::UNAVAILABLE}, 5),
459+
std::make_tuple(true, std::vector{grpc::StatusCode::DATA_LOSS}, 5),
460+
std::make_tuple(true, std::vector{grpc::StatusCode::OK}, 1),
461+
std::make_tuple(true,
462+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::ABORTED,
463+
grpc::StatusCode::OUT_OF_RANGE, grpc::StatusCode::DATA_LOSS},
464+
5),
465+
std::make_tuple(true,
466+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::UNAVAILABLE,
467+
grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::OK},
468+
4),
469+
std::make_tuple(true,
470+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::CANCELLED,
471+
grpc::StatusCode::DEADLINE_EXCEEDED, grpc::StatusCode::OK},
472+
4),
473+
// With retry policy disabled
474+
std::make_tuple(false, std::vector{grpc::StatusCode::CANCELLED}, 1),
475+
std::make_tuple(false, std::vector{grpc::StatusCode::UNKNOWN}, 1),
476+
std::make_tuple(false, std::vector{grpc::StatusCode::INVALID_ARGUMENT}, 1),
477+
std::make_tuple(false, std::vector{grpc::StatusCode::DEADLINE_EXCEEDED}, 1),
478+
std::make_tuple(false, std::vector{grpc::StatusCode::NOT_FOUND}, 1),
479+
std::make_tuple(false, std::vector{grpc::StatusCode::ALREADY_EXISTS}, 1),
480+
std::make_tuple(false, std::vector{grpc::StatusCode::PERMISSION_DENIED}, 1),
481+
std::make_tuple(false, std::vector{grpc::StatusCode::UNAUTHENTICATED}, 1),
482+
std::make_tuple(false, std::vector{grpc::StatusCode::RESOURCE_EXHAUSTED}, 1),
483+
std::make_tuple(false, std::vector{grpc::StatusCode::FAILED_PRECONDITION}, 1),
484+
std::make_tuple(false, std::vector{grpc::StatusCode::ABORTED}, 1),
485+
std::make_tuple(false, std::vector{grpc::StatusCode::OUT_OF_RANGE}, 1),
486+
std::make_tuple(false, std::vector{grpc::StatusCode::UNIMPLEMENTED}, 1),
487+
std::make_tuple(false, std::vector{grpc::StatusCode::INTERNAL}, 1),
488+
std::make_tuple(false, std::vector{grpc::StatusCode::UNAVAILABLE}, 1),
489+
std::make_tuple(false, std::vector{grpc::StatusCode::DATA_LOSS}, 1),
490+
std::make_tuple(false, std::vector{grpc::StatusCode::OK}, 1),
491+
std::make_tuple(false,
492+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::ABORTED,
493+
grpc::StatusCode::OUT_OF_RANGE, grpc::StatusCode::DATA_LOSS},
494+
1),
495+
std::make_tuple(false,
496+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::UNAVAILABLE,
497+
grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::OK},
498+
1),
499+
std::make_tuple(false,
500+
std::vector{grpc::StatusCode::UNAVAILABLE, grpc::StatusCode::CANCELLED,
501+
grpc::StatusCode::DEADLINE_EXCEEDED, grpc::StatusCode::OK},
502+
1)));
503+
504+
TEST_P(OtlpGrpcExporterRetryIntegrationTests, StatusCodes)
505+
{
506+
namespace otlp = opentelemetry::exporter::otlp;
507+
namespace trace_sdk = opentelemetry::sdk::trace;
508+
509+
const auto is_retry_enabled = std::get<0>(GetParam());
510+
const auto status_codes = std::get<1>(GetParam());
511+
const auto expected_attempts = std::get<2>(GetParam());
512+
TestTraceService service(status_codes);
513+
std::unique_ptr<grpc::Server> server;
514+
515+
std::thread server_thread([&server, &service]() {
516+
std::string address("0.0.0.0:4317");
517+
grpc::ServerBuilder builder;
518+
builder.RegisterService(&service);
519+
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
520+
server = builder.BuildAndStart();
521+
server->Wait();
522+
});
523+
524+
otlp::OtlpGrpcExporterOptions opts{};
525+
526+
if (is_retry_enabled)
527+
{
528+
opts.retry_policy_max_attempts = 5;
529+
opts.retry_policy_initial_backoff = SecondsDecimal{0.1};
530+
opts.retry_policy_max_backoff = SecondsDecimal{5};
531+
opts.retry_policy_backoff_multiplier = 1;
532+
}
533+
else
534+
{
535+
opts.retry_policy_max_attempts = 0;
536+
opts.retry_policy_initial_backoff = SecondsDecimal{0};
537+
opts.retry_policy_max_backoff = SecondsDecimal{0};
538+
opts.retry_policy_backoff_multiplier = 0;
539+
}
540+
541+
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
542+
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
543+
auto provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
544+
provider->GetTracer("Test tracer")->StartSpan("Test span")->End();
545+
546+
ASSERT_TRUE(server);
547+
server->Shutdown();
548+
549+
if (server_thread.joinable())
550+
{
551+
server_thread.join();
552+
}
553+
554+
ASSERT_EQ(expected_attempts, service.request_count_);
555+
}
556+
360557
} // namespace otlp
361558
} // namespace exporter
362559
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/test/otlp_grpc_log_record_exporter_test.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,58 @@ TEST_F(OtlpGrpcLogRecordExporterTestPeer, ShareClientTest)
466466
trace_provider = opentelemetry::nostd::shared_ptr<opentelemetry::sdk::trace::TracerProvider>();
467467
}
468468

469+
#ifndef NO_GETENV
470+
TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryDefaultValues)
471+
{
472+
std::unique_ptr<OtlpGrpcLogRecordExporter> exporter(new OtlpGrpcLogRecordExporter());
473+
const auto options = GetOptions(exporter);
474+
ASSERT_EQ(options.retry_policy_max_attempts, 5);
475+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1);
476+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5);
477+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5);
478+
}
479+
480+
TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryValuesFromEnv)
481+
{
482+
setenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_MAX_ATTEMPTS", "123", 1);
483+
setenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_INITIAL_BACKOFF", "4.5", 1);
484+
setenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_MAX_BACKOFF", "6.7", 1);
485+
setenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_BACKOFF_MULTIPLIER", "8.9", 1);
486+
487+
std::unique_ptr<OtlpGrpcLogRecordExporter> exporter(new OtlpGrpcLogRecordExporter());
488+
const auto options = GetOptions(exporter);
489+
ASSERT_EQ(options.retry_policy_max_attempts, 123);
490+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5);
491+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7);
492+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9);
493+
494+
unsetenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_MAX_ATTEMPTS");
495+
unsetenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_INITIAL_BACKOFF");
496+
unsetenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_MAX_BACKOFF");
497+
unsetenv("OTEL_EXPORTER_OTLP_LOGS_RETRY_BACKOFF_MULTIPLIER");
498+
}
499+
500+
TEST_F(OtlpGrpcLogRecordExporterTestPeer, ConfigRetryGenericValuesFromEnv)
501+
{
502+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1);
503+
setenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1);
504+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1);
505+
setenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1);
506+
507+
std::unique_ptr<OtlpGrpcLogRecordExporter> exporter(new OtlpGrpcLogRecordExporter());
508+
const auto options = GetOptions(exporter);
509+
ASSERT_EQ(options.retry_policy_max_attempts, 321);
510+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4);
511+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6);
512+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8);
513+
514+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS");
515+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF");
516+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF");
517+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER");
518+
}
519+
#endif // NO_GETENV
520+
469521
} // namespace otlp
470522
} // namespace exporter
471523
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/test/otlp_grpc_metric_exporter_test.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,58 @@ TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigUnknownInsecureFromEnv)
204204
}
205205
# endif
206206

207+
# ifndef NO_GETENV
208+
TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryDefaultValues)
209+
{
210+
std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter());
211+
const auto options = GetOptions(exporter);
212+
ASSERT_EQ(options.retry_policy_max_attempts, 5);
213+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 1);
214+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 5);
215+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 1.5);
216+
}
217+
218+
TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryValuesFromEnv)
219+
{
220+
setenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS", "123", 1);
221+
setenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF", "4.5", 1);
222+
setenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF", "6.7", 1);
223+
setenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER", "8.9", 1);
224+
225+
std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter());
226+
const auto options = GetOptions(exporter);
227+
ASSERT_EQ(options.retry_policy_max_attempts, 123);
228+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 4.5);
229+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 6.7);
230+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 8.9);
231+
232+
unsetenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_MAX_ATTEMPTS");
233+
unsetenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_INITIAL_BACKOFF");
234+
unsetenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_MAX_BACKOFF");
235+
unsetenv("OTEL_EXPORTER_OTLP_METRICS_RETRY_BACKOFF_MULTIPLIER");
236+
}
237+
238+
TEST_F(OtlpGrpcMetricExporterTestPeer, ConfigRetryGenericValuesFromEnv)
239+
{
240+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS", "321", 1);
241+
setenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF", "5.4", 1);
242+
setenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF", "7.6", 1);
243+
setenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER", "9.8", 1);
244+
245+
std::unique_ptr<OtlpGrpcMetricExporter> exporter(new OtlpGrpcMetricExporter());
246+
const auto options = GetOptions(exporter);
247+
ASSERT_EQ(options.retry_policy_max_attempts, 321);
248+
ASSERT_FLOAT_EQ(options.retry_policy_initial_backoff.count(), 5.4);
249+
ASSERT_FLOAT_EQ(options.retry_policy_max_backoff.count(), 7.6);
250+
ASSERT_FLOAT_EQ(options.retry_policy_backoff_multiplier, 9.8);
251+
252+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_ATTEMPTS");
253+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_INITIAL_BACKOFF");
254+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_MAX_BACKOFF");
255+
unsetenv("OTEL_EXPORTER_OTLP_RETRY_BACKOFF_MULTIPLIER");
256+
}
257+
# endif // NO_GETENV
258+
207259
} // namespace otlp
208260
} // namespace exporter
209261
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)