30 std::chrono::steady_clock::time_point
end_time;
38 ,
kind(span_kind_value)
64 end_time = std::chrono::steady_clock::now();
85 : impl_(std::make_unique<
impl>(name, std::move(ctx), kind))
100 : impl_(std::move(other.impl_))
112 impl_ = std::move(other.impl_);
123 if (impl_ && !impl_->ended)
125 impl_->attributes[std::string(key)] = std::string(value);
132 return set_attribute(key, std::string_view(value));
137 if (impl_ && !impl_->ended)
139 impl_->attributes[std::string(key)] = value;
146 if (impl_ && !impl_->ended)
148 impl_->attributes[std::string(key)] = value;
155 if (impl_ && !impl_->ended)
157 impl_->attributes[std::string(key)] = value;
164 if (impl_ && !impl_->ended)
167 event.
name = std::string(name);
168 event.timestamp = std::chrono::steady_clock::now();
169 impl_->events.push_back(std::move(event));
175 const std::map<std::string, attribute_value>& attributes) ->
span&
177 if (impl_ && !impl_->ended)
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));
190 if (impl_ && !impl_->ended)
192 impl_->status = status;
199 if (impl_ && !impl_->ended)
201 impl_->status = status;
202 impl_->status_description = std::string(description);
209 if (impl_ && !impl_->ended)
212 impl_->status_description = std::string(
message);
215 std::map<std::string, attribute_value> event_attrs;
216 event_attrs[
"exception.message"] = std::string(
message);
217 add_event(
"exception", event_attrs);
243 static const std::string empty_name;
259 static const std::string empty_description;
266 static const std::map<std::string, attribute_value> empty_attrs;
272 static const std::vector<span_event> empty_events;
290 return std::chrono::nanoseconds::zero();
RAII span for distributed tracing.
auto status() const noexcept -> span_status
Get the span status.
auto is_ended() const noexcept -> bool
Check if the span has ended.
auto start_time() const noexcept -> std::chrono::steady_clock::time_point
Get the span start time.
auto attributes() const noexcept -> const std::map< std::string, attribute_value > &
Get the span attributes.
auto set_attribute(std::string_view key, std::string_view value) -> span &
Set a string attribute.
auto kind() const noexcept -> span_kind
Get the span kind.
span(std::string_view name, trace_context ctx, span_kind kind=span_kind::internal)
Construct a new span.
auto add_event(std::string_view name) -> span &
Add an event to the span.
auto set_error(std::string_view message) -> span &
Set error status with message.
auto status_description() const noexcept -> const std::string &
Get the status description.
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 operator=(const span &) -> span &=delete
~span()
Destructor - automatically ends the span if not ended.
auto events() const noexcept -> const std::vector< span_event > &
Get the span events.
auto set_status(span_status status) -> span &
Set the span status.
auto duration() const noexcept -> std::chrono::nanoseconds
Get the span duration.
void end()
Manually end the span.
std::unique_ptr< impl > impl_
auto end_time() const noexcept -> std::chrono::steady_clock::time_point
Get the span end time.
Immutable trace context for distributed tracing.
static void clear_current()
Clear the current thread-local trace context.
auto is_valid() const noexcept -> bool
Check if this context is valid.
static void set_current(const trace_context &ctx)
Set the current thread-local trace context.
std::variant< std::string, int64_t, double, bool > attribute_value
Attribute value type (supports string, int64, double, bool)
span_kind
Span kind following OpenTelemetry conventions.
@ internal
Default, represents internal operations.
span_status
Span status codes following OpenTelemetry conventions.
@ unset
Default status, span completed without explicit status.
void export_span(const span &s)
Export a completed span.
RAII span implementation for distributed tracing.
Internal implementation structure for span.
std::map< std::string, attribute_value > attributes
std::chrono::steady_clock::time_point end_time
impl(std::string_view span_name, trace_context ctx, span_kind span_kind_value)
std::chrono::steady_clock::time_point start_time
std::string status_description
trace_context previous_context
std::vector< span_event > events
Span event (timestamped annotation)
Configuration structures for OpenTelemetry tracing.