Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::tracing::span Class Reference

RAII span for distributed tracing. More...

#include <span.h>

Collaboration diagram for kcenon::network::tracing::span:
Collaboration graph

Classes

struct  impl
 Internal implementation structure for span. More...
 

Public Member Functions

 span (std::string_view name, trace_context ctx, span_kind kind=span_kind::internal)
 Construct a new span.
 
 ~span ()
 Destructor - automatically ends the span if not ended.
 
 span (const span &)=delete
 
auto operator= (const span &) -> span &=delete
 
 span (span &&other) noexcept
 
auto operator= (span &&other) noexcept -> span &
 
auto set_attribute (std::string_view key, std::string_view value) -> span &
 Set a string attribute.
 
auto set_attribute (std::string_view key, const char *value) -> span &
 Set a string attribute (const char* overload)
 
auto set_attribute (std::string_view key, int64_t value) -> span &
 Set an integer attribute.
 
auto set_attribute (std::string_view key, double value) -> span &
 Set a double attribute.
 
auto set_attribute (std::string_view key, bool value) -> span &
 Set a boolean attribute.
 
auto add_event (std::string_view name) -> span &
 Add an event to the span.
 
auto add_event (std::string_view name, const std::map< std::string, attribute_value > &attributes) -> span &
 Add an event with attributes.
 
auto set_status (span_status status) -> span &
 Set the span status.
 
auto set_status (span_status status, std::string_view description) -> span &
 Set the span status with description.
 
auto set_error (std::string_view message) -> span &
 Set error status with message.
 
void end ()
 Manually end the span.
 
auto is_ended () const noexcept -> bool
 Check if the span has ended.
 
auto context () const noexcept -> const trace_context &
 Get the trace context for this span.
 
auto name () const noexcept -> const std::string &
 Get the span name.
 
auto kind () const noexcept -> span_kind
 Get the span kind.
 
auto status () const noexcept -> span_status
 Get the span status.
 
auto status_description () const noexcept -> const std::string &
 Get the status description.
 
auto attributes () const noexcept -> const std::map< std::string, attribute_value > &
 Get the span attributes.
 
auto events () const noexcept -> const std::vector< span_event > &
 Get the span events.
 
auto start_time () const noexcept -> std::chrono::steady_clock::time_point
 Get the span start time.
 
auto end_time () const noexcept -> std::chrono::steady_clock::time_point
 Get the span end time.
 
auto duration () const noexcept -> std::chrono::nanoseconds
 Get the span duration.
 

Private Attributes

std::unique_ptr< implimpl_
 

Detailed Description

RAII span for distributed tracing.

A span represents a single operation within a trace. It has a name, start and end time, attributes, and can have events recorded during its lifetime. The span automatically ends when destroyed (RAII).

Spans maintain the current trace context via thread-local storage, enabling automatic parent-child relationship tracking.

Example usage:
void process_request() {
auto span = trace_context::create_span("process_request");
span.set_attribute("request.id", "12345");
// Do work...
span.add_event("processing_started");
if (error) {
span.set_error("Failed to process request");
}
// span automatically ends on destruction
}
RAII span for distributed tracing.
Definition span.h:103
auto set_attribute(std::string_view key, std::string_view value) -> span &
Set a string attribute.
Definition span.cpp:121
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
static auto create_span(std::string_view name) -> span
Create a new root span with a new trace context.
RAII Macro:
NETWORK_TRACE_SPAN("operation.name");
_span.set_attribute("key", "value");
#define NETWORK_TRACE_SPAN(name)
RAII helper macro for creating spans.
Definition span.h:305
See also
trace_context

Definition at line 102 of file span.h.

Constructor & Destructor Documentation

◆ span() [1/3]

kcenon::network::tracing::span::span ( std::string_view name,
trace_context ctx,
span_kind kind = span_kind::internal )
explicit

Construct a new span.

Parameters
nameSpan name (operation name)
ctxTrace context for this span
kindKind of span (default: internal)

Definition at line 84 of file span.cpp.

85 : impl_(std::make_unique<impl>(name, std::move(ctx), kind))
86{
87 impl_->owner = this;
88}
auto kind() const noexcept -> span_kind
Get the span kind.
Definition span.cpp:247
auto name() const noexcept -> const std::string &
Get the span name.
Definition span.cpp:241
std::unique_ptr< impl > impl_
Definition span.h:286

References impl_, and kcenon::network::tracing::span::impl::owner.

◆ ~span()

kcenon::network::tracing::span::~span ( )

Destructor - automatically ends the span if not ended.

Definition at line 90 of file span.cpp.

91{
92 // End span before impl_ destruction to ensure owner->impl_ is still valid
93 if (impl_ && !impl_->ended)
94 {
95 impl_->do_end();
96 }
97}

References kcenon::network::tracing::span::impl::do_end(), kcenon::network::tracing::span::impl::ended, and impl_.

Here is the call graph for this function:

◆ span() [2/3]

kcenon::network::tracing::span::span ( const span & )
delete

◆ span() [3/3]

kcenon::network::tracing::span::span ( span && other)
noexcept

Definition at line 99 of file span.cpp.

100 : impl_(std::move(other.impl_))
101{
102 if (impl_)
103 {
104 impl_->owner = this;
105 }
106}

Member Function Documentation

◆ add_event() [1/2]

auto kcenon::network::tracing::span::add_event ( std::string_view name) -> span&

Add an event to the span.

Parameters
nameEvent name
Returns
Reference to this span for chaining

Definition at line 162 of file span.cpp.

163{
164 if (impl_ && !impl_->ended)
165 {
166 span_event event;
167 event.name = std::string(name);
168 event.timestamp = std::chrono::steady_clock::now();
169 impl_->events.push_back(std::move(event));
170 }
171 return *this;
172}
std::vector< span_event > events
Definition span.cpp:28

References kcenon::network::tracing::span_event::name.

◆ add_event() [2/2]

auto kcenon::network::tracing::span::add_event ( std::string_view name,
const std::map< std::string, attribute_value > & attributes ) -> span&

Add an event with attributes.

Parameters
nameEvent name
attributesEvent attributes
Returns
Reference to this span for chaining

Definition at line 174 of file span.cpp.

176{
177 if (impl_ && !impl_->ended)
178 {
179 span_event event;
180 event.name = std::string(name);
181 event.timestamp = std::chrono::steady_clock::now();
182 event.attributes = attributes;
183 impl_->events.push_back(std::move(event));
184 }
185 return *this;
186}
auto attributes() const noexcept -> const std::map< std::string, attribute_value > &
Get the span attributes.
Definition span.cpp:263

References kcenon::network::tracing::span_event::name.

◆ attributes()

auto kcenon::network::tracing::span::attributes ( ) const -> const std::map<std::string, attribute_value>&
nodiscardnoexcept

Get the span attributes.

Returns
Map of attributes

Definition at line 263 of file span.cpp.

265{
266 static const std::map<std::string, attribute_value> empty_attrs;
267 return impl_ ? impl_->attributes : empty_attrs;
268}
std::map< std::string, attribute_value > attributes
Definition span.cpp:27

References kcenon::network::tracing::span::impl::attributes, and impl_.

◆ context()

auto kcenon::network::tracing::span::context ( ) const -> const trace_context&
nodiscardnoexcept

Get the trace context for this span.

Returns
Trace context

Definition at line 235 of file span.cpp.

236{
237 static const trace_context empty_context;
238 return impl_ ? impl_->context : empty_context;
239}

References kcenon::network::tracing::span::impl::context, and impl_.

◆ duration()

auto kcenon::network::tracing::span::duration ( ) const -> std::chrono::nanoseconds
nodiscardnoexcept

Get the span duration.

Returns
Duration in nanoseconds

Definition at line 286 of file span.cpp.

287{
288 if (!impl_)
289 {
290 return std::chrono::nanoseconds::zero();
291 }
292
293 auto end = impl_->ended ? impl_->end_time : std::chrono::steady_clock::now();
294 return std::chrono::duration_cast<std::chrono::nanoseconds>(end - impl_->start_time);
295}
void end()
Manually end the span.
Definition span.cpp:222
std::chrono::steady_clock::time_point end_time
Definition span.cpp:30
std::chrono::steady_clock::time_point start_time
Definition span.cpp:29

References end(), kcenon::network::tracing::span::impl::end_time, kcenon::network::tracing::span::impl::ended, impl_, and kcenon::network::tracing::span::impl::start_time.

Here is the call graph for this function:

◆ end()

void kcenon::network::tracing::span::end ( )

Manually end the span.

After calling end(), the span will not be ended again in destructor.

Definition at line 222 of file span.cpp.

223{
224 if (impl_)
225 {
226 impl_->do_end();
227 }
228}

References kcenon::network::tracing::span::impl::do_end(), and impl_.

Referenced by duration().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ end_time()

auto kcenon::network::tracing::span::end_time ( ) const -> std::chrono::steady_clock::time_point
nodiscardnoexcept

Get the span end time.

Returns
End timestamp (start_time if not ended)

Definition at line 281 of file span.cpp.

282{
283 return impl_ ? impl_->end_time : std::chrono::steady_clock::time_point{};
284}

References kcenon::network::tracing::span::impl::end_time, and impl_.

◆ events()

auto kcenon::network::tracing::span::events ( ) const -> const std::vector<span_event>&
nodiscardnoexcept

Get the span events.

Returns
Vector of events

Definition at line 270 of file span.cpp.

271{
272 static const std::vector<span_event> empty_events;
273 return impl_ ? impl_->events : empty_events;
274}

References kcenon::network::tracing::span::impl::events, and impl_.

◆ is_ended()

auto kcenon::network::tracing::span::is_ended ( ) const -> bool
nodiscardnoexcept

Check if the span has ended.

Returns
true if the span has ended

Definition at line 230 of file span.cpp.

231{
232 return impl_ ? impl_->ended : true;
233}

References kcenon::network::tracing::span::impl::ended, and impl_.

◆ kind()

auto kcenon::network::tracing::span::kind ( ) const -> span_kind
nodiscardnoexcept

Get the span kind.

Returns
Span kind

Definition at line 247 of file span.cpp.

248{
250}
@ internal
Default, represents internal operations.

References impl_, kcenon::network::tracing::internal, and kcenon::network::tracing::span::impl::kind.

◆ name()

auto kcenon::network::tracing::span::name ( ) const -> const std::string&
nodiscardnoexcept

Get the span name.

Returns
Span name

Definition at line 241 of file span.cpp.

242{
243 static const std::string empty_name;
244 return impl_ ? impl_->name : empty_name;
245}

References impl_, and kcenon::network::tracing::span::impl::name.

◆ operator=() [1/2]

auto kcenon::network::tracing::span::operator= ( const span & ) -> span &=delete
delete

◆ operator=() [2/2]

auto kcenon::network::tracing::span::operator= ( span && other) -> span&
noexcept

Definition at line 108 of file span.cpp.

109{
110 if (this != &other)
111 {
112 impl_ = std::move(other.impl_);
113 if (impl_)
114 {
115 impl_->owner = this;
116 }
117 }
118 return *this;
119}

◆ set_attribute() [1/5]

auto kcenon::network::tracing::span::set_attribute ( std::string_view key,
bool value ) -> span&

Set a boolean attribute.

Parameters
keyAttribute key
valueAttribute value
Returns
Reference to this span for chaining

Definition at line 153 of file span.cpp.

154{
155 if (impl_ && !impl_->ended)
156 {
157 impl_->attributes[std::string(key)] = value;
158 }
159 return *this;
160}

◆ set_attribute() [2/5]

auto kcenon::network::tracing::span::set_attribute ( std::string_view key,
const char * value ) -> span&

Set a string attribute (const char* overload)

Parameters
keyAttribute key
valueAttribute value
Returns
Reference to this span for chaining

This overload prevents implicit conversion of const char* to bool.

Definition at line 130 of file span.cpp.

131{
132 return set_attribute(key, std::string_view(value));
133}

◆ set_attribute() [3/5]

auto kcenon::network::tracing::span::set_attribute ( std::string_view key,
double value ) -> span&

Set a double attribute.

Parameters
keyAttribute key
valueAttribute value
Returns
Reference to this span for chaining

Definition at line 144 of file span.cpp.

145{
146 if (impl_ && !impl_->ended)
147 {
148 impl_->attributes[std::string(key)] = value;
149 }
150 return *this;
151}

◆ set_attribute() [4/5]

auto kcenon::network::tracing::span::set_attribute ( std::string_view key,
int64_t value ) -> span&

Set an integer attribute.

Parameters
keyAttribute key
valueAttribute value
Returns
Reference to this span for chaining

Definition at line 135 of file span.cpp.

136{
137 if (impl_ && !impl_->ended)
138 {
139 impl_->attributes[std::string(key)] = value;
140 }
141 return *this;
142}

◆ set_attribute() [5/5]

auto kcenon::network::tracing::span::set_attribute ( std::string_view key,
std::string_view value ) -> span&

Set a string attribute.

Parameters
keyAttribute key
valueAttribute value
Returns
Reference to this span for chaining

Definition at line 121 of file span.cpp.

122{
123 if (impl_ && !impl_->ended)
124 {
125 impl_->attributes[std::string(key)] = std::string(value);
126 }
127 return *this;
128}

◆ set_error()

auto kcenon::network::tracing::span::set_error ( std::string_view message) -> span&

Set error status with message.

Parameters
messageError message
Returns
Reference to this span for chaining

Definition at line 207 of file span.cpp.

208{
209 if (impl_ && !impl_->ended)
210 {
212 impl_->status_description = std::string(message);
213
214 // Also add an exception event following OpenTelemetry conventions
215 std::map<std::string, attribute_value> event_attrs;
216 event_attrs["exception.message"] = std::string(message);
217 add_event("exception", event_attrs);
218 }
219 return *this;
220}

References kcenon::network::tracing::error, and kcenon::network::message.

◆ set_status() [1/2]

auto kcenon::network::tracing::span::set_status ( span_status status) -> span&

Set the span status.

Parameters
statusStatus code
Returns
Reference to this span for chaining

Definition at line 188 of file span.cpp.

189{
190 if (impl_ && !impl_->ended)
191 {
193 }
194 return *this;
195}
auto status() const noexcept -> span_status
Get the span status.
Definition span.cpp:252

◆ set_status() [2/2]

auto kcenon::network::tracing::span::set_status ( span_status status,
std::string_view description ) -> span&

Set the span status with description.

Parameters
statusStatus code
descriptionStatus description
Returns
Reference to this span for chaining

Definition at line 197 of file span.cpp.

198{
199 if (impl_ && !impl_->ended)
200 {
202 impl_->status_description = std::string(description);
203 }
204 return *this;
205}

◆ start_time()

auto kcenon::network::tracing::span::start_time ( ) const -> std::chrono::steady_clock::time_point
nodiscardnoexcept

Get the span start time.

Returns
Start timestamp

Definition at line 276 of file span.cpp.

277{
278 return impl_ ? impl_->start_time : std::chrono::steady_clock::time_point{};
279}

References impl_, and kcenon::network::tracing::span::impl::start_time.

◆ status()

auto kcenon::network::tracing::span::status ( ) const -> span_status
nodiscardnoexcept

Get the span status.

Returns
Span status

Definition at line 252 of file span.cpp.

253{
255}
@ unset
Default status, span completed without explicit status.

References impl_, kcenon::network::tracing::span::impl::status, and kcenon::network::tracing::unset.

◆ status_description()

auto kcenon::network::tracing::span::status_description ( ) const -> const std::string&
nodiscardnoexcept

Get the status description.

Returns
Status description (empty if not set)

Definition at line 257 of file span.cpp.

258{
259 static const std::string empty_description;
260 return impl_ ? impl_->status_description : empty_description;
261}

References impl_, and kcenon::network::tracing::span::impl::status_description.

Member Data Documentation

◆ impl_

std::unique_ptr<impl> kcenon::network::tracing::span::impl_
private

The documentation for this class was generated from the following files: