Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
16#pragma once
17
18#include "trace_context.h"
19
20#include <chrono>
21#include <map>
22#include <memory>
23#include <string>
24#include <string_view>
25#include <variant>
26#include <vector>
27
29
33enum class span_status
34{
35 unset = 0,
36 ok = 1,
37 error = 2
38};
39
43enum class span_kind
44{
45 internal = 0,
46 server = 1,
47 client = 2,
48 producer = 3,
49 consumer = 4
50};
51
55using attribute_value = std::variant<std::string, int64_t, double, bool>;
56
61{
62 std::string name;
63 std::chrono::steady_clock::time_point timestamp;
64 std::map<std::string, attribute_value> attributes;
65};
66
102class span
103{
104public:
111 explicit span(std::string_view name, trace_context ctx,
113
117 ~span();
118
119 // Non-copyable
120 span(const span&) = delete;
121 auto operator=(const span&) -> span& = delete;
122
123 // Movable
124 span(span&& other) noexcept;
125 auto operator=(span&& other) noexcept -> span&;
126
133 auto set_attribute(std::string_view key, std::string_view value) -> span&;
134
143 auto set_attribute(std::string_view key, const char* value) -> span&;
144
151 auto set_attribute(std::string_view key, int64_t value) -> span&;
152
159 auto set_attribute(std::string_view key, double value) -> span&;
160
167 auto set_attribute(std::string_view key, bool value) -> span&;
168
174 auto add_event(std::string_view name) -> span&;
175
182 auto add_event(std::string_view name,
183 const std::map<std::string, attribute_value>& attributes)
184 -> span&;
185
192
199 auto set_status(span_status status, std::string_view description) -> span&;
200
206 auto set_error(std::string_view message) -> span&;
207
213 void end();
214
219 [[nodiscard]] auto is_ended() const noexcept -> bool;
220
225 [[nodiscard]] auto context() const noexcept -> const trace_context&;
226
231 [[nodiscard]] auto name() const noexcept -> const std::string&;
232
237 [[nodiscard]] auto kind() const noexcept -> span_kind;
238
243 [[nodiscard]] auto status() const noexcept -> span_status;
244
249 [[nodiscard]] auto status_description() const noexcept -> const std::string&;
250
255 [[nodiscard]] auto attributes() const noexcept
256 -> const std::map<std::string, attribute_value>&;
257
262 [[nodiscard]] auto events() const noexcept -> const std::vector<span_event>&;
263
268 [[nodiscard]] auto start_time() const noexcept
269 -> std::chrono::steady_clock::time_point;
270
275 [[nodiscard]] auto end_time() const noexcept
276 -> std::chrono::steady_clock::time_point;
277
282 [[nodiscard]] auto duration() const noexcept -> std::chrono::nanoseconds;
283
284private:
285 struct impl;
286 std::unique_ptr<impl> impl_;
287};
288
305#define NETWORK_TRACE_SPAN(name) \
306 auto _span = ::kcenon::network::tracing::trace_context::create_span(name)
307
311#define NETWORK_TRACE_CLIENT_SPAN(name) \
312 auto _span = ::kcenon::network::tracing::span( \
313 name, ::kcenon::network::tracing::trace_context::current().is_valid() \
314 ? ::kcenon::network::tracing::trace_context::current() \
315 .create_child_span(name) \
316 .context() \
317 : ::kcenon::network::tracing::trace_context(), \
318 ::kcenon::network::tracing::span_kind::client)
319
323#define NETWORK_TRACE_SERVER_SPAN(name) \
324 auto _span = ::kcenon::network::tracing::span( \
325 name, ::kcenon::network::tracing::trace_context::current().is_valid() \
326 ? ::kcenon::network::tracing::trace_context::current() \
327 .create_child_span(name) \
328 .context() \
329 : ::kcenon::network::tracing::trace_context(), \
330 ::kcenon::network::tracing::span_kind::server)
331
332} // namespace kcenon::network::tracing
RAII span for distributed tracing.
Definition span.h:103
auto status() const noexcept -> span_status
Get the span status.
Definition span.cpp:252
span(const span &)=delete
auto is_ended() const noexcept -> bool
Check if the span has ended.
Definition span.cpp:230
auto start_time() const noexcept -> std::chrono::steady_clock::time_point
Get the span start time.
Definition span.cpp:276
auto attributes() const noexcept -> const std::map< std::string, attribute_value > &
Get the span attributes.
Definition span.cpp:263
auto set_attribute(std::string_view key, std::string_view value) -> span &
Set a string attribute.
Definition span.cpp:121
auto kind() const noexcept -> span_kind
Get the span kind.
Definition span.cpp:247
span(std::string_view name, trace_context ctx, span_kind kind=span_kind::internal)
Construct a new span.
Definition span.cpp:84
auto add_event(std::string_view name) -> span &
Add an event to the span.
Definition span.cpp:162
auto set_error(std::string_view message) -> span &
Set error status with message.
Definition span.cpp:207
auto status_description() const noexcept -> const std::string &
Get the status description.
Definition span.cpp:257
auto context() const noexcept -> const trace_context &
Get the trace context for this span.
Definition span.cpp:235
auto name() const noexcept -> const std::string &
Get the span name.
Definition span.cpp:241
auto operator=(const span &) -> span &=delete
~span()
Destructor - automatically ends the span if not ended.
Definition span.cpp:90
auto events() const noexcept -> const std::vector< span_event > &
Get the span events.
Definition span.cpp:270
auto set_status(span_status status) -> span &
Set the span status.
Definition span.cpp:188
auto duration() const noexcept -> std::chrono::nanoseconds
Get the span duration.
Definition span.cpp:286
void end()
Manually end the span.
Definition span.cpp:222
std::unique_ptr< impl > impl_
Definition span.h:286
auto end_time() const noexcept -> std::chrono::steady_clock::time_point
Get the span end time.
Definition span.cpp:281
Immutable trace context for distributed tracing.
std::variant< std::string, int64_t, double, bool > attribute_value
Attribute value type (supports string, int64, double, bool)
Definition span.h:55
span_kind
Span kind following OpenTelemetry conventions.
Definition span.h:44
@ consumer
Message consumer (e.g., queue subscriber)
@ server
Server-side handling of a request.
@ internal
Default, represents internal operations.
@ producer
Message producer (e.g., queue publisher)
span_status
Span status codes following OpenTelemetry conventions.
Definition span.h:34
@ unset
Default status, span completed without explicit status.
Internal implementation structure for span.
Definition span.cpp:21
Span event (timestamped annotation)
Definition span.h:61
std::chrono::steady_clock::time_point timestamp
Definition span.h:63
std::map< std::string, attribute_value > attributes
Definition span.h:64
Distributed tracing context for OpenTelemetry-compatible tracing.