439 lines
12 KiB
C++
439 lines
12 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <span>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Observability
|
|
{
|
|
|
|
enum class MetricKind : std::uint8_t {
|
|
counter = 0,
|
|
gauge,
|
|
histogram
|
|
};
|
|
|
|
struct AttributeView {
|
|
std::string_view key{};
|
|
std::string_view value{};
|
|
};
|
|
|
|
struct MetricPoint {
|
|
std::string_view name{};
|
|
MetricKind kind{MetricKind::counter};
|
|
double value{0.0};
|
|
std::uint64_t integer_delta{0};
|
|
std::span<const AttributeView> attributes{};
|
|
};
|
|
|
|
struct DiagnosticEvent {
|
|
std::string_view component{};
|
|
std::string_view message{};
|
|
bool is_error{false};
|
|
};
|
|
|
|
/// Zero-dependency synchronous observation callback table.
|
|
/// Null hooks perform no allocation or backend initialization.
|
|
struct ObservationHooks {
|
|
void* state{nullptr};
|
|
void (*record)(void* state, const MetricPoint& point) noexcept{nullptr};
|
|
void (*diagnostic)(void* state, const DiagnosticEvent& event) noexcept{nullptr};
|
|
|
|
[[nodiscard]] constexpr bool active() const noexcept
|
|
{
|
|
return record != nullptr || diagnostic != nullptr;
|
|
}
|
|
|
|
void emit_metric(const MetricPoint& point) const noexcept
|
|
{
|
|
if (record != nullptr) {
|
|
record(state, point);
|
|
}
|
|
}
|
|
|
|
void emit_diagnostic(const DiagnosticEvent& event) const noexcept
|
|
{
|
|
if (diagnostic != nullptr) {
|
|
diagnostic(state, event);
|
|
}
|
|
}
|
|
};
|
|
|
|
inline constexpr ObservationHooks kNullObservationHooks{};
|
|
|
|
enum class DrainStatus : std::uint8_t {
|
|
drained = 0,
|
|
not_found,
|
|
would_deadlock
|
|
};
|
|
|
|
using HookRegistrationId = std::uint64_t;
|
|
|
|
namespace Detail
|
|
{
|
|
|
|
struct HookControl {
|
|
HookRegistrationId id{0};
|
|
ObservationHooks raw_hooks{};
|
|
std::function<void(const MetricPoint&)> guarded_record{};
|
|
std::function<void(const DiagnosticEvent&)> guarded_diagnostic{};
|
|
bool is_guarded{false};
|
|
std::shared_ptr<const void> owner{};
|
|
std::atomic<bool> detached{false};
|
|
std::atomic<bool> quarantined{false};
|
|
std::atomic<std::size_t> active_calls{0};
|
|
std::atomic<std::uint64_t> failed_calls{0};
|
|
std::mutex drain_mutex;
|
|
std::condition_variable drain_cv;
|
|
};
|
|
|
|
// Per-thread stack of hook controls currently executing. A function-local
|
|
// thread_local keeps a single definition across TUs; a namespace-scope
|
|
// `inline thread_local` makes MinGW emit one TLS init function per TU and
|
|
// fails to link with duplicate symbols.
|
|
[[nodiscard]] inline std::vector<const HookControl*>&
|
|
active_hook_controls() noexcept
|
|
{
|
|
static thread_local std::vector<const HookControl*> controls;
|
|
return controls;
|
|
}
|
|
|
|
struct HookCallScope {
|
|
const HookControl* control{nullptr};
|
|
|
|
explicit HookCallScope(const HookControl* ctrl) : control(ctrl)
|
|
{
|
|
if (control != nullptr) {
|
|
active_hook_controls().push_back(control);
|
|
}
|
|
}
|
|
|
|
~HookCallScope() noexcept
|
|
{
|
|
auto& controls = active_hook_controls();
|
|
if (control != nullptr && !controls.empty()) {
|
|
controls.pop_back();
|
|
}
|
|
}
|
|
|
|
HookCallScope(const HookCallScope&) = delete;
|
|
HookCallScope& operator=(const HookCallScope&) = delete;
|
|
|
|
[[nodiscard]] static bool is_active_on_thread(const HookControl* ctrl) noexcept
|
|
{
|
|
for (const auto* entry : active_hook_controls()) {
|
|
if (entry == ctrl) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
|
|
} // namespace Detail
|
|
|
|
/// Move-only RAII lease retaining hook callback/owner state across an invocation
|
|
/// outside any internal configuration mutex.
|
|
class HookLease
|
|
{
|
|
public:
|
|
HookLease() noexcept = default;
|
|
|
|
~HookLease() noexcept { reset(); }
|
|
|
|
HookLease(HookLease&& other) noexcept
|
|
: control_(std::exchange(other.control_, nullptr))
|
|
{
|
|
}
|
|
|
|
HookLease& operator=(HookLease&& other) noexcept
|
|
{
|
|
if (this != &other) {
|
|
reset();
|
|
control_ = std::exchange(other.control_, nullptr);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
HookLease(const HookLease&) = delete;
|
|
HookLease& operator=(const HookLease&) = delete;
|
|
|
|
[[nodiscard]] explicit operator bool() const noexcept
|
|
{
|
|
return control_ != nullptr;
|
|
}
|
|
|
|
[[nodiscard]] HookRegistrationId registration_id() const noexcept
|
|
{
|
|
return control_ != nullptr ? control_->id : 0;
|
|
}
|
|
|
|
[[nodiscard]] bool is_quarantined() const noexcept
|
|
{
|
|
return control_ != nullptr &&
|
|
control_->quarantined.load(std::memory_order_acquire);
|
|
}
|
|
|
|
void reset() noexcept
|
|
{
|
|
auto ctrl = std::exchange(control_, nullptr);
|
|
if (!ctrl) {
|
|
return;
|
|
}
|
|
if (ctrl->active_calls.fetch_sub(1, std::memory_order_acq_rel) == 1) {
|
|
std::lock_guard lock{ctrl->drain_mutex};
|
|
ctrl->drain_cv.notify_all();
|
|
}
|
|
}
|
|
|
|
private:
|
|
friend class HookSlot;
|
|
|
|
explicit HookLease(std::shared_ptr<Detail::HookControl> ctrl) noexcept
|
|
: control_(std::move(ctrl))
|
|
{
|
|
}
|
|
|
|
std::shared_ptr<Detail::HookControl> control_{};
|
|
};
|
|
|
|
/// Thread-safe hook registration owner supporting safe replacement, explicit
|
|
/// detach-and-drain synchronization, recursion suppression, and exception quarantine.
|
|
class HookSlot
|
|
{
|
|
public:
|
|
HookSlot() = default;
|
|
|
|
~HookSlot() noexcept { (void)detach_and_drain(0); }
|
|
|
|
HookSlot(const HookSlot&) = delete;
|
|
HookSlot& operator=(const HookSlot&) = delete;
|
|
HookSlot(HookSlot&&) = delete;
|
|
HookSlot& operator=(HookSlot&&) = delete;
|
|
|
|
HookRegistrationId install(ObservationHooks hooks,
|
|
std::shared_ptr<const void> owner = {})
|
|
{
|
|
if (!hooks.active() && !owner) {
|
|
std::shared_ptr<Detail::HookControl> old;
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
old = std::exchange(active_, nullptr);
|
|
if (old) {
|
|
old->detached.store(true, std::memory_order_release);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
auto next = std::make_shared<Detail::HookControl>();
|
|
next->raw_hooks = hooks;
|
|
next->owner = std::move(owner);
|
|
next->is_guarded = false;
|
|
return swap_active(std::move(next));
|
|
}
|
|
|
|
HookRegistrationId install_guarded(
|
|
std::function<void(const MetricPoint&)> record_fn,
|
|
std::function<void(const DiagnosticEvent&)> diagnostic_fn = {},
|
|
std::shared_ptr<const void> owner = {})
|
|
{
|
|
auto next = std::make_shared<Detail::HookControl>();
|
|
next->guarded_record = std::move(record_fn);
|
|
next->guarded_diagnostic = std::move(diagnostic_fn);
|
|
next->owner = std::move(owner);
|
|
next->is_guarded = true;
|
|
return swap_active(std::move(next));
|
|
}
|
|
|
|
[[nodiscard]] HookLease acquire_lease() const noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
if (!active_ || active_->detached.load(std::memory_order_acquire)) {
|
|
return {};
|
|
}
|
|
active_->active_calls.fetch_add(1, std::memory_order_acq_rel);
|
|
return HookLease{active_};
|
|
}
|
|
|
|
DrainStatus detach_and_drain(HookRegistrationId id = 0) noexcept
|
|
{
|
|
std::shared_ptr<Detail::HookControl> target;
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
prune_retired_locked();
|
|
if (id == 0) {
|
|
target = active_;
|
|
} else if (active_ && active_->id == id) {
|
|
target = active_;
|
|
} else if (const auto it = known_.find(id); it != known_.end()) {
|
|
target = it->second.lock();
|
|
}
|
|
|
|
if (!target) {
|
|
return DrainStatus::not_found;
|
|
}
|
|
if (Detail::HookCallScope::is_active_on_thread(target.get())) {
|
|
return DrainStatus::would_deadlock;
|
|
}
|
|
|
|
target->detached.store(true, std::memory_order_release);
|
|
if (target == active_) {
|
|
active_.reset();
|
|
}
|
|
known_.erase(target->id);
|
|
}
|
|
|
|
{
|
|
std::unique_lock drain_lock{target->drain_mutex};
|
|
target->drain_cv.wait(drain_lock, [&target] {
|
|
return target->active_calls.load(std::memory_order_acquire) == 0;
|
|
});
|
|
}
|
|
target.reset();
|
|
return DrainStatus::drained;
|
|
}
|
|
|
|
void invoke_metric(const MetricPoint& point) noexcept
|
|
{
|
|
auto lease = acquire_lease();
|
|
if (!lease) {
|
|
return;
|
|
}
|
|
auto* ctrl = lease.control_.get();
|
|
if (ctrl->quarantined.load(std::memory_order_acquire)) {
|
|
return;
|
|
}
|
|
if (Detail::HookCallScope::is_active_on_thread(ctrl)) {
|
|
reentrant_calls_.fetch_add(1, std::memory_order_relaxed);
|
|
return;
|
|
}
|
|
Detail::HookCallScope scope{ctrl};
|
|
if (!ctrl->is_guarded) {
|
|
ctrl->raw_hooks.emit_metric(point);
|
|
return;
|
|
}
|
|
if (!ctrl->guarded_record) {
|
|
return;
|
|
}
|
|
try {
|
|
ctrl->guarded_record(point);
|
|
} catch (...) {
|
|
ctrl->failed_calls.fetch_add(1, std::memory_order_relaxed);
|
|
failed_calls_.fetch_add(1, std::memory_order_relaxed);
|
|
ctrl->quarantined.store(true, std::memory_order_release);
|
|
}
|
|
}
|
|
|
|
void invoke_diagnostic(const DiagnosticEvent& event) noexcept
|
|
{
|
|
auto lease = acquire_lease();
|
|
if (!lease) {
|
|
return;
|
|
}
|
|
auto* ctrl = lease.control_.get();
|
|
if (ctrl->quarantined.load(std::memory_order_acquire)) {
|
|
return;
|
|
}
|
|
if (Detail::HookCallScope::is_active_on_thread(ctrl)) {
|
|
reentrant_calls_.fetch_add(1, std::memory_order_relaxed);
|
|
return;
|
|
}
|
|
Detail::HookCallScope scope{ctrl};
|
|
if (!ctrl->is_guarded) {
|
|
ctrl->raw_hooks.emit_diagnostic(event);
|
|
return;
|
|
}
|
|
if (!ctrl->guarded_diagnostic) {
|
|
return;
|
|
}
|
|
try {
|
|
ctrl->guarded_diagnostic(event);
|
|
} catch (...) {
|
|
ctrl->failed_calls.fetch_add(1, std::memory_order_relaxed);
|
|
failed_calls_.fetch_add(1, std::memory_order_relaxed);
|
|
ctrl->quarantined.store(true, std::memory_order_release);
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] bool active() const noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
return active_ != nullptr && !active_->detached.load(std::memory_order_acquire);
|
|
}
|
|
|
|
[[nodiscard]] bool is_quarantined() const noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
return active_ != nullptr &&
|
|
active_->quarantined.load(std::memory_order_acquire);
|
|
}
|
|
|
|
void reset_quarantine() noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
if (active_) {
|
|
active_->quarantined.store(false, std::memory_order_release);
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] std::uint64_t failed_calls() const noexcept
|
|
{
|
|
return failed_calls_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
[[nodiscard]] std::uint64_t reentrant_calls() const noexcept
|
|
{
|
|
return reentrant_calls_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
HookRegistrationId swap_active(std::shared_ptr<Detail::HookControl> next)
|
|
{
|
|
std::shared_ptr<Detail::HookControl> old;
|
|
HookRegistrationId id = 0;
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
prune_retired_locked();
|
|
id = ++next_id_;
|
|
next->id = id;
|
|
known_.emplace(id, next);
|
|
old = std::exchange(active_, std::move(next));
|
|
if (old) {
|
|
old->detached.store(true, std::memory_order_release);
|
|
}
|
|
}
|
|
old.reset();
|
|
return id;
|
|
}
|
|
|
|
void prune_retired_locked()
|
|
{
|
|
for (auto it = known_.begin(); it != known_.end();) {
|
|
if (it->second.expired()) {
|
|
it = known_.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
mutable std::mutex mutex_;
|
|
HookRegistrationId next_id_{0};
|
|
std::shared_ptr<Detail::HookControl> active_{};
|
|
std::unordered_map<HookRegistrationId, std::weak_ptr<Detail::HookControl>> known_;
|
|
std::atomic<std::uint64_t> failed_calls_{0};
|
|
std::atomic<std::uint64_t> reentrant_calls_{0};
|
|
};
|
|
|
|
} // namespace Void::Observability
|