244 lines
6.6 KiB
C++
244 lines
6.6 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/Observability/Context.hpp>
|
|
#include <Void/Observability/Metrics.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Observability
|
|
{
|
|
|
|
struct TracingLimits {
|
|
std::size_t max_active_spans{1024};
|
|
std::size_t max_active_bytes{8ULL * 1024ULL * 1024ULL};
|
|
std::size_t max_completed_spans{8192};
|
|
std::size_t max_completed_bytes{16ULL * 1024ULL * 1024ULL};
|
|
std::size_t max_attributes_per_span{32};
|
|
std::size_t max_events_per_span{32};
|
|
std::size_t max_bytes_per_span{8ULL * 1024ULL};
|
|
std::size_t max_key_bytes{128};
|
|
std::size_t max_value_bytes{256};
|
|
std::size_t max_export_batch_spans{256};
|
|
std::size_t max_export_batch_bytes{512ULL * 1024ULL};
|
|
std::uint64_t root_sample_denominator{100};
|
|
std::size_t max_export_retries{2};
|
|
std::chrono::nanoseconds export_retry_budget{std::chrono::seconds{5}};
|
|
|
|
[[nodiscard]] Void::Result::Result<void> validate() const;
|
|
};
|
|
|
|
enum class SpanStatus : std::uint8_t {
|
|
unset = 0,
|
|
ok,
|
|
error,
|
|
abandoned
|
|
};
|
|
|
|
struct SpanSpec {
|
|
std::string name{};
|
|
std::vector<std::pair<std::string, std::string>> attributes{};
|
|
std::optional<TraceId> explicit_root_trace_id{};
|
|
};
|
|
|
|
struct SpanEvent {
|
|
std::string name{};
|
|
Void::Time::MonotonicClock::time_point timestamp{};
|
|
};
|
|
|
|
struct CompletedSpan {
|
|
TraceContext context{};
|
|
std::optional<SpanId> parent_span_id{};
|
|
std::string name{};
|
|
Void::Time::MonotonicClock::time_point start_time{};
|
|
Void::Time::MonotonicClock::time_point end_time{};
|
|
Void::Time::CivilClock::time_point civil_start_time{};
|
|
SpanStatus status{SpanStatus::unset};
|
|
std::vector<std::pair<std::string, std::string>> attributes{};
|
|
std::vector<SpanEvent> events{};
|
|
bool truncated{false};
|
|
std::size_t estimated_bytes{0};
|
|
};
|
|
|
|
struct ExportBatch {
|
|
std::vector<CompletedSpan> spans{};
|
|
std::size_t estimated_bytes{0};
|
|
};
|
|
|
|
struct ExportReport {
|
|
std::uint64_t exported_spans{0};
|
|
std::uint64_t failed_spans{0};
|
|
std::uint64_t dropped_spans{0};
|
|
std::uint64_t duplicate_ends{0};
|
|
std::uint64_t retries{0};
|
|
bool timed_out{false};
|
|
bool cancelled{false};
|
|
};
|
|
|
|
struct ExportOptions {
|
|
std::stop_token stop{};
|
|
std::optional<Void::Time::MonotonicClock::time_point> deadline{};
|
|
};
|
|
|
|
struct TracerStats {
|
|
std::size_t active_spans{0};
|
|
std::size_t active_bytes{0};
|
|
std::size_t completed_spans{0};
|
|
std::size_t completed_bytes{0};
|
|
std::size_t in_flight_export_bytes{0};
|
|
std::uint64_t dropped_active_spans{0};
|
|
std::uint64_t dropped_completed_spans{0};
|
|
std::uint64_t duplicate_ends{0};
|
|
std::uint64_t abandoned_spans{0};
|
|
std::uint64_t exported_spans{0};
|
|
std::uint64_t failed_exports{0};
|
|
std::uint64_t reentrant_export_drops{0};
|
|
bool closed{false};
|
|
};
|
|
|
|
class SpanExporter
|
|
{
|
|
public:
|
|
virtual ~SpanExporter() = default;
|
|
|
|
[[nodiscard]] virtual Void::Result::Result<void>
|
|
export_spans(std::span<const CompletedSpan> spans) = 0;
|
|
};
|
|
|
|
class CallbackSpanExporter final : public SpanExporter
|
|
{
|
|
public:
|
|
using Callback =
|
|
std::function<Void::Result::Result<void>(std::span<const CompletedSpan>)>;
|
|
|
|
explicit CallbackSpanExporter(Callback callback);
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
export_spans(std::span<const CompletedSpan> spans) override;
|
|
|
|
[[nodiscard]] std::uint64_t reentrant_drops() const noexcept
|
|
{
|
|
return reentrant_drops_.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
Callback callback_;
|
|
std::atomic<std::uint64_t> reentrant_drops_{0};
|
|
};
|
|
|
|
class TextSnapshotExporter final : public SpanExporter
|
|
{
|
|
public:
|
|
explicit TextSnapshotExporter(
|
|
std::size_t max_output_bytes = 64ULL * 1024ULL);
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
export_spans(std::span<const CompletedSpan> spans) override;
|
|
|
|
[[nodiscard]] static std::string render_metrics(
|
|
const MetricsSnapshot& metrics,
|
|
std::size_t max_output_bytes = 64ULL * 1024ULL);
|
|
|
|
[[nodiscard]] static std::string render_spans(
|
|
std::span<const CompletedSpan> spans,
|
|
std::size_t max_output_bytes = 64ULL * 1024ULL);
|
|
|
|
[[nodiscard]] std::string text() const;
|
|
void clear();
|
|
|
|
private:
|
|
std::size_t max_output_bytes_;
|
|
mutable std::mutex mutex_;
|
|
std::string buffer_;
|
|
};
|
|
|
|
class Tracer;
|
|
|
|
/// Move-only span handle owning child TraceContext (never a raw parent pointer).
|
|
/// Destroying an active unended handle terminates it with SpanStatus::abandoned.
|
|
class Span
|
|
{
|
|
public:
|
|
Span() = default;
|
|
~Span() noexcept;
|
|
|
|
Span(Span&& other) noexcept;
|
|
Span& operator=(Span&& other) noexcept;
|
|
Span(const Span&) = delete;
|
|
Span& operator=(const Span&) = delete;
|
|
|
|
[[nodiscard]] const ContextSnapshot& context() const noexcept
|
|
{
|
|
return context_;
|
|
}
|
|
|
|
[[nodiscard]] bool is_recording() const noexcept
|
|
{
|
|
return state_ != nullptr && !ended_;
|
|
}
|
|
|
|
void set_attribute(std::string_view key, std::string_view value);
|
|
void add_event(std::string_view name);
|
|
void end(SpanStatus status = SpanStatus::ok) noexcept;
|
|
|
|
private:
|
|
friend class Tracer;
|
|
struct State;
|
|
|
|
Span(ContextSnapshot context, std::shared_ptr<State> state) noexcept;
|
|
|
|
ContextSnapshot context_{};
|
|
std::shared_ptr<State> state_{};
|
|
bool ended_{false};
|
|
};
|
|
|
|
/// Head-sampled, byte-bounded tracer with explicit synchronous batch export
|
|
/// and no implicit background worker threads.
|
|
class Tracer
|
|
{
|
|
public:
|
|
[[nodiscard]] static Void::Result::Result<Tracer> create(
|
|
TracingLimits limits = {},
|
|
std::shared_ptr<SpanExporter> exporter = nullptr,
|
|
const Void::Time::MonotonicClock& clock =
|
|
Void::Time::SystemMonotonicClock::instance(),
|
|
const Void::Time::CivilClock& civil_clock =
|
|
Void::Time::SystemCivilClock::instance());
|
|
|
|
Tracer(Tracer&&) noexcept = default;
|
|
Tracer& operator=(Tracer&&) noexcept = default;
|
|
Tracer(const Tracer&) = delete;
|
|
Tracer& operator=(const Tracer&) = delete;
|
|
~Tracer();
|
|
|
|
[[nodiscard]] Span start_span(const ContextSnapshot& parent, SpanSpec spec);
|
|
|
|
[[nodiscard]] ExportReport flush(ExportOptions options = {});
|
|
[[nodiscard]] ExportReport close(ExportOptions options = {});
|
|
[[nodiscard]] TracerStats stats() const;
|
|
|
|
private:
|
|
friend class Span;
|
|
struct Impl;
|
|
|
|
explicit Tracer(std::shared_ptr<Impl> impl) noexcept;
|
|
|
|
std::shared_ptr<Impl> impl_;
|
|
};
|
|
|
|
} // namespace Void::Observability
|