Branch data Line data Source code
1 : : #pragma once
2 : :
3 : : #include <hpactor/metrics/metrics_ring_buffer.hpp>
4 : : #include <hpactor/tracing/sampler.hpp>
5 : : #include <hpactor/tracing/span.hpp>
6 : : #include <hpactor/tracing/trace_config.hpp>
7 : : #include <hpactor/tracing/trace_exporter.hpp>
8 : :
9 : : #include <atomic>
10 : : #include <memory>
11 : : #include <thread>
12 : : #include <vector>
13 : :
14 : : namespace hpactor {
15 : : class ActorContext;
16 : : class ActorSystem;
17 : : class TypedMessage;
18 : : } // namespace hpactor
19 : :
20 : : namespace hpactor::tracing {
21 : :
22 : : class TraceManager {
23 : : public:
24 : : TraceManager(TraceConfig config, ActorSystem* system,
25 : : std::unique_ptr<SpanExporter> exporter = nullptr);
26 : : ~TraceManager();
27 : :
28 : : TraceManager(const TraceManager&) = delete;
29 : : TraceManager& operator=(const TraceManager&) = delete;
30 : :
31 : : void start();
32 : : void stop();
33 : : void force_flush();
34 : :
35 : 2 : bool enabled() const noexcept {
36 : 2 : return config_.enabled;
37 : : }
38 : 2 : const TraceConfig& config() const noexcept {
39 : 2 : return config_;
40 : : }
41 : :
42 : : TraceContext create_root_context(std::string_view operation);
43 : : TraceContext child_context(const TraceContext& parent);
44 : :
45 : : SpanHandle start_span(const SpanStart& start);
46 : : void finish_span(SpanHandle& span, SpanStatus status) noexcept;
47 : :
48 : : void inject_message_context(TypedMessage& msg, const ActorContext* ctx,
49 : : bool allow_root);
50 : :
51 : 1 : uint64_t spans_dropped() const noexcept {
52 : 2 : return spans_dropped_.load(std::memory_order_relaxed);
53 : : }
54 : :
55 : : private:
56 : : std::unique_ptr<Sampler> make_sampler() const;
57 : : void drain_once();
58 : : static uint64_t now_ns() noexcept;
59 : :
60 : : TraceConfig config_;
61 : : [[maybe_unused]] ActorSystem* system_{nullptr};
62 : : TraceIdGenerator ids_;
63 : : std::unique_ptr<Sampler> sampler_;
64 : : std::unique_ptr<SpanExporter> exporter_;
65 : : metrics::MpscRingBuffer<SpanRecord> ring_;
66 : : std::atomic<bool> running_{false};
67 : : std::thread drain_thread_;
68 : : std::atomic<uint64_t> spans_dropped_{0};
69 : : };
70 : :
71 : : } // namespace hpactor::tracing
|