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

Fix:1676 Segfault when short export period is used for metrics #1682

Merged
merged 10 commits into from
Oct 15, 2022
25 changes: 14 additions & 11 deletions sdk/src/metrics/meter_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void MeterContext::AddMeter(std::shared_ptr<Meter> meter)
bool MeterContext::Shutdown() noexcept
{
bool result = true;
// Shutdown only once.
if (!shutdown_latch_.test_and_set(std::memory_order_acquire))
{

Expand All @@ -80,25 +81,27 @@ bool MeterContext::Shutdown() noexcept
OTEL_INTERNAL_LOG_WARN("[MeterContext::Shutdown] Unable to shutdown all metric readers");
}
}
else
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::Shutdown] Shutdown can be invoked only once.");
}
return result;
}

bool MeterContext::ForceFlush(std::chrono::microseconds timeout) noexcept
{
// TODO - Implement timeout logic.
bool result = true;
if (!shutdown_latch_.test_and_set(std::memory_order_acquire))
// Simultaneous flush not allowed.
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(forceflush_lock_);
for (auto &collector : collectors_)
{

for (auto &collector : collectors_)
{
bool status = std::static_pointer_cast<MetricCollector>(collector)->ForceFlush(timeout);
result = result && status;
}
if (!result)
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::ForceFlush] Unable to ForceFlush all metric readers");
}
bool status = std::static_pointer_cast<MetricCollector>(collector)->ForceFlush(timeout);
result = result && status;
}
if (!result)
{
OTEL_INTERNAL_LOG_WARN("[MeterContext::ForceFlush] Unable to ForceFlush all metric readers");
}
return result;
}
Expand Down
6 changes: 5 additions & 1 deletion sdk/test/metrics/meter_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ TEST(MeterProvider, GetMeter)
ASSERT_EQ(m4, m5);
ASSERT_NE(m3, m6);

// Should be an sdk::trace::Tracer with the processor attached.
// Should be an sdk::metrics::Meter
# ifdef OPENTELEMETRY_RTTI_ENABLED
auto sdkMeter1 = dynamic_cast<Meter *>(m1.get());
# else
Expand All @@ -98,5 +98,9 @@ TEST(MeterProvider, GetMeter)
std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("name1", "version1", "schema1")};

mp1.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));

// cleanup properly without crash
mp1.ForceFlush();
mp1.Shutdown();
}
#endif