|
17 | 17 | // That is because `std::result_of` has been removed in C++20. |
18 | 18 |
|
19 | 19 | # include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h" |
20 | | - |
| 20 | +# include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" |
21 | 21 | # include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" |
| 22 | +# include "opentelemetry/nostd/shared_ptr.h" |
22 | 23 |
|
| 24 | +# include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h" |
23 | 25 | // Problematic code that pulls in Gmock and breaks with vs2019/c++latest : |
24 | 26 | # include "opentelemetry/proto/collector/trace/v1/trace_service_mock.grpc.pb.h" |
25 | 27 |
|
26 | 28 | # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" |
27 | 29 |
|
28 | 30 | # include "opentelemetry/sdk/trace/simple_processor.h" |
| 31 | +# include "opentelemetry/sdk/trace/simple_processor_factory.h" |
29 | 32 | # include "opentelemetry/sdk/trace/tracer_provider.h" |
| 33 | +# include "opentelemetry/sdk/trace/tracer_provider_factory.h" |
30 | 34 | # include "opentelemetry/trace/provider.h" |
| 35 | +# include "opentelemetry/trace/tracer_provider.h" |
31 | 36 |
|
32 | 37 | # include <grpcpp/grpcpp.h> |
33 | 38 | # include <gtest/gtest.h> |
@@ -357,6 +362,198 @@ TEST_F(OtlpGrpcExporterTestPeer, ConfigUnknownInsecureFromEnv) |
357 | 362 | } |
358 | 363 | # endif |
359 | 364 |
|
| 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 | + |
360 | 557 | } // namespace otlp |
361 | 558 | } // namespace exporter |
362 | 559 | OPENTELEMETRY_END_NAMESPACE |
|
0 commit comments