Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Fix crash in PeriodicExportingMetricReader. #2983

Merged
merged 16 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sdk/include/opentelemetry/sdk/metrics/metric_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <atomic>
#include <chrono>
#include <memory>
owent marked this conversation as resolved.
Show resolved Hide resolved

#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
Expand All @@ -19,7 +20,7 @@ namespace metrics
/**
* MetricReader defines the interface to collect metrics from SDK
*/
class MetricReader
class MetricReader : public std::enable_shared_from_this<MetricReader>
owent marked this conversation as resolved.
Show resolved Hide resolved
{
public:
MetricReader();
Expand Down
35 changes: 21 additions & 14 deletions sdk/src/metrics/export/periodic_exporting_metric_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,26 @@ void PeriodicExportingMetricReader::DoBackgroundWork()

bool PeriodicExportingMetricReader::CollectAndExportOnce()
{
std::atomic<bool> cancel_export_for_timeout{false};
auto future_receive = std::async(std::launch::async, [this, &cancel_export_for_timeout] {
Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) {
if (cancel_export_for_timeout)
{
OTEL_INTERNAL_LOG_ERROR(
"[Periodic Exporting Metric Reader] Collect took longer configured time: "
<< export_timeout_millis_.count() << " ms, and timed out");
return false;
}
this->exporter_->Export(metric_data);
return true;
});
std::shared_ptr<std::atomic<bool>> cancel_export_for_timeout =
std::make_shared<std::atomic<bool>>(false);
auto keep_lifetime = shared_from_this();
owent marked this conversation as resolved.
Show resolved Hide resolved

auto future_receive = std::async(std::launch::async, [keep_lifetime, cancel_export_for_timeout] {
keep_lifetime->Collect(
[keep_lifetime, cancel_export_for_timeout](ResourceMetrics &metric_data) {
if (cancel_export_for_timeout->load(std::memory_order_acquire))
{
OTEL_INTERNAL_LOG_ERROR(
"[Periodic Exporting Metric Reader] Collect took longer configured time: "
<< static_cast<PeriodicExportingMetricReader *>(keep_lifetime.get())
->export_timeout_millis_.count()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - same as earlier "do we need the typecast ?"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep_lifetime is std::shared_ptr<MetricReader> but we need PeriodicExportingMetricReader* here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead make PeriodicExportingMetricReader inherit from std::enable_shared_from_this<PeriodicExportingMetricReader>, and remove for MetricReader, if this allows avoiding typecast.

class PeriodicExportingMetricReader : public MetricReader, public std::enable_shared_from_this<PeriodicExportingMetricReader> {
    // ... rest of the class ...
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it's a better solution for just a temporary fixes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so to clarify expectations:

Is the public std::enable_shared_from_this<PeriodicExportingMetricReader> meant to be temporary, until a better solution is found ?

I do agree it fixes the immediate crash, so the code no longer keep a pointer to a local variable in dead memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, assuming this is a temporary fix.

<< " ms, and timed out");
return false;
}
static_cast<PeriodicExportingMetricReader *>(keep_lifetime.get())
->exporter_->Export(metric_data);
return true;
});
});

std::future_status status;
Expand All @@ -111,7 +118,7 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
status = future_receive.wait_for(std::chrono::milliseconds(export_timeout_millis_));
if (status == std::future_status::timeout)
{
cancel_export_for_timeout = true;
cancel_export_for_timeout->store(true, std::memory_order_release);
marcalff marked this conversation as resolved.
Show resolved Hide resolved
break;
}
} while (status != std::future_status::ready);
Expand Down
38 changes: 33 additions & 5 deletions sdk/test/metrics/periodic_exporting_metric_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@

#include <gtest/gtest.h>

#include <chrono>
#include <memory>
#include <thread>

using namespace opentelemetry;
using namespace opentelemetry::sdk::instrumentationscope;
using namespace opentelemetry::sdk::metrics;

class MockPushMetricExporter : public PushMetricExporter
{
public:
MockPushMetricExporter(std::chrono::milliseconds wait) : wait_(wait) {}

opentelemetry::sdk::common::ExportResult Export(const ResourceMetrics &record) noexcept override
{
if (wait_ > std::chrono::milliseconds::zero())
{
std::this_thread::sleep_for(wait_);
}
records_.push_back(record);
return opentelemetry::sdk::common::ExportResult::kSuccess;
}
Expand All @@ -34,6 +44,7 @@ class MockPushMetricExporter : public PushMetricExporter

private:
std::vector<ResourceMetrics> records_;
std::chrono::milliseconds wait_;
};

class MockMetricProducer : public MetricProducer
Expand Down Expand Up @@ -61,17 +72,34 @@ class MockMetricProducer : public MetricProducer

TEST(PeriodicExporingMetricReader, BasicTests)
{
std::unique_ptr<PushMetricExporter> exporter(new MockPushMetricExporter());
std::unique_ptr<PushMetricExporter> exporter(
new MockPushMetricExporter(std::chrono::milliseconds{0}));
PeriodicExportingMetricReaderOptions options;
options.export_timeout_millis = std::chrono::milliseconds(200);
options.export_interval_millis = std::chrono::milliseconds(500);
auto exporter_ptr = exporter.get();
PeriodicExportingMetricReader reader(std::move(exporter), options);
std::shared_ptr<PeriodicExportingMetricReader> reader =
std::make_shared<PeriodicExportingMetricReader>(std::move(exporter), options);
MockMetricProducer producer;
reader.SetMetricProducer(&producer);
reader->SetMetricProducer(&producer);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
EXPECT_NO_THROW(reader.ForceFlush());
reader.Shutdown();
EXPECT_NO_THROW(reader->ForceFlush());
reader->Shutdown();
EXPECT_EQ(static_cast<MockPushMetricExporter *>(exporter_ptr)->GetDataCount(),
static_cast<MockMetricProducer *>(&producer)->GetDataCount());
}

TEST(PeriodicExporingMetricReader, Timeout)
{
std::unique_ptr<PushMetricExporter> exporter(
new MockPushMetricExporter(std::chrono::milliseconds{2000}));
PeriodicExportingMetricReaderOptions options;
options.export_timeout_millis = std::chrono::milliseconds(200);
options.export_interval_millis = std::chrono::milliseconds(500);
std::shared_ptr<PeriodicExportingMetricReader> reader =
std::make_shared<PeriodicExportingMetricReader>(std::move(exporter), options);
MockMetricProducer producer;
reader->SetMetricProducer(&producer);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
reader->Shutdown();
}