diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h index 66ae76fcfc..6a83245efe 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h @@ -78,10 +78,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter * @return return the status of this operation */ bool Shutdown( - std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override - { - return true; - } + std::chrono::microseconds timeout = std::chrono::microseconds::max()) noexcept override; private: // The configuration options associated with this exporter. @@ -99,6 +96,7 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter * @param stub the service stub to be used for exporting */ OtlpGrpcExporter(std::unique_ptr stub); + bool is_shutdown_ = false; }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index 90d8e4ee9f..06e3355adf 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -94,6 +94,11 @@ std::unique_ptr OtlpGrpcExporter::MakeRecordable() noexc sdk::common::ExportResult OtlpGrpcExporter::Export( const nostd::span> &spans) noexcept { + if (is_shutdown_) + { + OTEL_INTERNAL_LOG_ERROR("[OTLP gRPC] Export failed, exporter is shutdown"); + return sdk::common::ExportResult::kFailure; + } proto::collector::trace::v1::ExportTraceServiceRequest request; OtlpRecordableUtils::PopulateRequest(spans, &request); @@ -120,6 +125,13 @@ sdk::common::ExportResult OtlpGrpcExporter::Export( } return sdk::common::ExportResult::kSuccess; } + +bool OtlpGrpcExporter::Shutdown(std::chrono::microseconds timeout) noexcept +{ + is_shutdown_ = true; + return true; +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/test/otlp_grpc_exporter_test.cc b/exporters/otlp/test/otlp_grpc_exporter_test.cc index 1663888bf5..091cb0d0c4 100755 --- a/exporters/otlp/test/otlp_grpc_exporter_test.cc +++ b/exporters/otlp/test/otlp_grpc_exporter_test.cc @@ -62,6 +62,31 @@ class OtlpGrpcExporterTestPeer : public ::testing::Test } }; +TEST_F(OtlpGrpcExporterTestPeer, ShutdownTest) +{ + auto mock_stub = new proto::collector::trace::v1::MockTraceServiceStub(); + std::unique_ptr stub_interface( + mock_stub); + auto exporter = GetExporter(stub_interface); + + auto recordable_1 = exporter->MakeRecordable(); + recordable_1->SetName("Test span 1"); + auto recordable_2 = exporter->MakeRecordable(); + recordable_2->SetName("Test span 2"); + + // exporter shuold not be shutdown by default + nostd::span> batch_1(&recordable_1, 1); + EXPECT_CALL(*mock_stub, Export(_, _, _)).Times(Exactly(1)).WillOnce(Return(grpc::Status::OK)); + auto result = exporter->Export(batch_1); + EXPECT_EQ(sdk::common::ExportResult::kSuccess, result); + + exporter->Shutdown(); + + nostd::span> batch_2(&recordable_2, 1); + result = exporter->Export(batch_2); + EXPECT_EQ(sdk::common::ExportResult::kFailure, result); +} + // Call Export() directly TEST_F(OtlpGrpcExporterTestPeer, ExportUnitTest) {