A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
3#ifndef A11_OBS_SPAN_H_
4#define A11_OBS_SPAN_H_
5
6#include <cstddef>
7#include <cstdint>
8#include <new>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14#include <absl/status/status.h>
15
16#include "a11/data/types.h"
17
18namespace a11::obs {
19
22
24enum class SpanStatus { kUnset, kOk, kError };
25
37class Span {
38 public:
40 ~Span();
41
48
51
52 // This span's identifiers as lowercase hex (32 chars / 16 chars), or empty
53 // strings for an inactive span or an invalid context.
55 [[nodiscard]] std::string TraceIdHex() const;
57 [[nodiscard]] std::string SpanIdHex() const;
58
60 void SetAttribute(std::string_view key, std::string_view value);
62 void SetAttribute(std::string_view key, const char* value);
64 void SetAttribute(std::string_view key, std::int64_t value);
66 void SetAttribute(std::string_view key, bool value);
68 void SetAttribute(std::string_view key, double value);
69
71 void AddEvent(std::string_view name);
73 void AddEvent(
74 std::string_view name,
75 const std::vector<std::pair<std::string, std::string>>& attributes);
76
78 void SetStatus(const absl::Status& status);
79
81 void SetStatus(SpanStatus status, std::string_view description = {});
82
83 // Renames the span (OTel UpdateName). The exported name is whatever it is at
84 // End(). No-op for an inactive span.
86 void UpdateName(std::string_view name);
87
89 void End() noexcept;
90
91 // Serializes this span's context (and any inherited baggage) into the
92 // reserved headers so nested or remote actions continue the same trace.
93 // A no-op that returns OkStatus for an inactive span.
95 absl::Status InjectContext(data::ByteMap& headers) const;
96
97 private:
99
100 struct Impl;
101 // Sized/aligned to hold Impl (a shared span handle plus a small string).
102 // Verified with static_assert in tracer.cc.
103 static constexpr std::size_t kStorageSize = 64;
104 static constexpr std::size_t kStorageAlign = alignof(std::max_align_t);
105
106 // Defined in tracer.cc, where Impl is a complete type (std::launder and
107 // Impl's members require completeness).
108 [[nodiscard]] Impl* impl() noexcept;
109 [[nodiscard]] const Impl* impl() const noexcept;
110 // Default-constructs an Impl in the inline storage, marks the span engaged,
111 // and returns it for the tracer to populate.
112 Impl* Engage() noexcept;
113 void Reset() noexcept;
114
115 alignas(kStorageAlign) unsigned char storage_[kStorageSize];
116 bool engaged_ = false;
117};
118
119} // namespace a11::obs
120
121#endif // A11_OBS_SPAN_H_
Move-only RAII handle to one action, session, or transport span.
Definition span.h:37
std::string SpanIdHex() const
Return the 16-character span id, or empty when inactive.
Definition tracer.cc:320
Span() noexcept=default
std::string TraceIdHex() const
Return the 32-character trace id, or empty when inactive.
Definition tracer.cc:312
bool IsRecording() const noexcept
Whether this handle currently records attributes and events.
Definition tracer.cc:205
void UpdateName(std::string_view name)
Replace the name that will be exported when the span ends.
Definition tracer.cc:298
void SetStatus(const absl::Status &status)
Map an A11 operation status onto the span's OTel outcome.
Definition tracer.cc:269
absl::Status InjectContext(data::ByteMap &headers) const
Inject trace context and inherited baggage into reserved A11 headers.
Definition tracer.cc:328
void End() noexcept
Finish the span and release its implementation; safe to call repeatedly.
Definition tracer.cc:305
void SetAttribute(std::string_view key, std::string_view value)
Set or replace a string attribute on the live span.
Definition tracer.cc:209
void AddEvent(std::string_view name)
Record a named point-in-time event.
Definition tracer.cc:244
Factory that makes parentage explicit for A11's migrating fibers.
Definition tracer.h:21
Definition provider.cc:47
SpanStatus
OpenTelemetry outcome states, mirrored to keep OTel out of this API.
Definition span.h:24
SpanKind
OpenTelemetry span relationships, mirrored to keep OTel out of this API.
Definition span.h:21
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
Definition tracer.cc:43
A11's core wire value types: chunks, node fragments and messages.