Skip to content

Commit

Permalink
otlp_grpc_exporter export after shutdown should fail (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo authored Nov 3, 2021
1 parent 52f6198 commit 8e8c50a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<proto::collector::trace::v1::TraceService::StubInterface> stub);
bool is_shutdown_ = false;
};
} // namespace otlp
} // namespace exporter
Expand Down
12 changes: 12 additions & 0 deletions exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ std::unique_ptr<sdk::trace::Recordable> OtlpGrpcExporter::MakeRecordable() noexc
sdk::common::ExportResult OtlpGrpcExporter::Export(
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &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);

Expand All @@ -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
25 changes: 25 additions & 0 deletions exporters/otlp/test/otlp_grpc_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<proto::collector::trace::v1::TraceService::StubInterface> 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<std::unique_ptr<sdk::trace::Recordable>> 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<std::unique_ptr<sdk::trace::Recordable>> batch_2(&recordable_2, 1);
result = exporter->Export(batch_2);
EXPECT_EQ(sdk::common::ExportResult::kFailure, result);
}

// Call Export() directly
TEST_F(OtlpGrpcExporterTestPeer, ExportUnitTest)
{
Expand Down

0 comments on commit 8e8c50a

Please sign in to comment.