A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A11_OBS_SPAN_H_
18#define A11_OBS_SPAN_H_
19
20#include <cstddef>
21#include <cstdint>
22#include <new>
23#include <string>
24#include <string_view>
25#include <utility>
26#include <vector>
27
28#include <absl/status/status.h>
29
30#include "a11/data/types.h"
31
32namespace a11::obs {
33
36
38enum class SpanStatus { kUnset, kOk, kError };
39
51class Span {
52 public:
53 Span() noexcept = default;
54 ~Span();
55
57 Span(Span&& other) noexcept;
59 Span& operator=(Span&& other) noexcept;
60 Span(const Span&) = delete;
61 Span& operator=(const Span&) = delete;
62
64 [[nodiscard]] bool IsRecording() const noexcept;
65
66 // This span's identifiers as lowercase hex (32 chars / 16 chars), or empty
67 // strings for an inactive span or an invalid context.
69 [[nodiscard]] std::string TraceIdHex() const;
71 [[nodiscard]] std::string SpanIdHex() const;
72
74 void SetAttribute(std::string_view key, std::string_view value);
76 void SetAttribute(std::string_view key, const char* value);
78 void SetAttribute(std::string_view key, std::int64_t value);
80 void SetAttribute(std::string_view key, bool value);
82 void SetAttribute(std::string_view key, double value);
83
85 void AddEvent(std::string_view name);
87 void AddEvent(
88 std::string_view name,
89 const std::vector<std::pair<std::string, std::string>>& attributes);
90
92 void SetStatus(const absl::Status& status);
93
95 void SetStatus(SpanStatus status, std::string_view description = {});
96
97 // Renames the span (OTel UpdateName). The exported name is whatever it is at
98 // End(). No-op for an inactive span.
100 void UpdateName(std::string_view name);
101
103 void End() noexcept;
104
105 // Serializes this span's context (and any inherited baggage) into the
106 // reserved headers so nested or remote actions continue the same trace.
108 absl::Status InjectContext(data::ByteMap& headers) const;
109
110 private:
111 friend class Tracer;
112
113 struct Impl;
114 // Sized/aligned to hold Impl (a shared span handle plus a small string).
115 // Verified with static_assert in tracer.cc.
116 static constexpr std::size_t kStorageSize = 64;
117 static constexpr std::size_t kStorageAlign = alignof(std::max_align_t);
118
119 // Defined in tracer.cc, where Impl is a complete type (std::launder and
120 // Impl's members require completeness).
121 [[nodiscard]] Impl* impl() noexcept;
122 [[nodiscard]] const Impl* impl() const noexcept;
123 // Default-constructs an Impl in the inline storage, marks the span engaged,
124 // and returns it for the tracer to populate.
125 Impl* Engage() noexcept;
126 void Reset() noexcept;
127
128 alignas(kStorageAlign) unsigned char storage_[kStorageSize];
129 bool engaged_ = false;
130};
131
132} // namespace a11::obs
133
134#endif // A11_OBS_SPAN_H_
Move-only RAII handle to one action, session, or transport span.
Definition span.h:51
std::string SpanIdHex() const
Return the 16-character span id, or empty when inactive.
Definition tracer.cc:327
Span() noexcept=default
std::string TraceIdHex() const
Return the 32-character trace id, or empty when inactive.
Definition tracer.cc:319
bool IsRecording() const noexcept
Whether this handle currently records attributes and events.
Definition tracer.cc:212
void UpdateName(std::string_view name)
Replace the name that will be exported when the span ends.
Definition tracer.cc:305
void SetStatus(const absl::Status &status)
Map an A11 operation status onto the span's OTel outcome.
Definition tracer.cc:276
absl::Status InjectContext(data::ByteMap &headers) const
Inject trace context and inherited baggage into reserved A11 headers.
Definition tracer.cc:335
void End() noexcept
Finish the span and release its implementation; safe to call repeatedly.
Definition tracer.cc:312
void SetAttribute(std::string_view key, std::string_view value)
Set or replace a string attribute on the live span.
Definition tracer.cc:216
void AddEvent(std::string_view name)
Record a named point-in-time event.
Definition tracer.cc:251
Factory that makes parentage explicit for A11's migrating fibers.
Definition tracer.h:35
std::string value
Definition discover.cc:114
std::string key
Definition discover.cc:782
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
std::string description
The description, quoted exactly as it was written.
Definition format.cc:53
Definition provider.cc:58
SpanStatus
OpenTelemetry outcome states, mirrored to keep OTel out of this API.
Definition span.h:38
SpanKind
OpenTelemetry span relationships, mirrored to keep OTel out of this API.
Definition span.h:35
Definition tracer.cc:54
A11's core wire value types: chunks, node fragments and messages.