A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
trace_context.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_TRACE_CONTEXT_H_
18#define A11_OBS_TRACE_CONTEXT_H_
19
20#include <cstdint>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <vector>
25
26#include <absl/status/status.h>
27#include <absl/status/statusor.h>
28
29#include "a11/data/types.h"
30
31namespace a11::obs {
32
34inline constexpr std::string_view kTraceparentHeader = "x-otel-traceparent";
36inline constexpr std::string_view kTracestateHeader = "x-otel-tracestate";
38inline constexpr std::string_view kBaggageHeader = "x-otel-baggage";
39
42 std::string key;
43 std::string value;
44 std::string properties;
45
46 friend bool operator==(const BaggageEntry&, const BaggageEntry&) = default;
47};
48
51 std::string trace_id;
52 std::string span_id;
53 std::uint8_t trace_flags = 0;
54 std::string tracestate;
55 std::vector<BaggageEntry> baggage;
56
58 [[nodiscard]] bool sampled() const { return (trace_flags & 0x01U) != 0; }
59
60 friend bool operator==(const TraceContext&, const TraceContext&) = default;
61};
62
66absl::StatusOr<std::optional<TraceContext>> ExtractTraceContext(
67 const data::ByteMap& headers);
68
70absl::Status InjectTraceContext(const TraceContext& context,
71 data::ByteMap& headers);
72
73} // namespace a11::obs
74
75#endif // A11_OBS_TRACE_CONTEXT_H_
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:58
Definition provider.cc:58
constexpr std::string_view kTracestateHeader
Reserved action header carrying the W3C tracestate value.
Definition trace_context.h:36
constexpr std::string_view kBaggageHeader
Reserved action header carrying request-scoped W3C baggage.
Definition trace_context.h:38
absl::Status InjectTraceContext(const TraceContext &context, data::ByteMap &headers)
Serialize context into reserved headers, replacing existing values.
Definition trace_context.cc:177
constexpr std::string_view kTraceparentHeader
Reserved action header carrying the W3C traceparent value.
Definition trace_context.h:34
absl::StatusOr< std::optional< TraceContext > > ExtractTraceContext(const data::ByteMap &headers)
Extract a remote parent; nullopt means no tracing headers were supplied.
Definition trace_context.cc:148
One decoded W3C baggage member propagated through nested and remote actions.
Definition trace_context.h:41
std::string properties
Trailing semicolon-separated properties.
Definition trace_context.h:44
std::string value
Percent-decoded member value.
Definition trace_context.h:43
std::string key
Trimmed member key; percent escapes stay encoded.
Definition trace_context.h:42
friend bool operator==(const BaggageEntry &, const BaggageEntry &)=default
SDK-independent W3C parent context recovered from reserved action headers.
Definition trace_context.h:50
std::string span_id
16 lowercase hex characters.
Definition trace_context.h:52
std::vector< BaggageEntry > baggage
Request-scoped propagated values.
Definition trace_context.h:55
bool sampled() const
Whether the W3C sampled flag is set.
Definition trace_context.h:58
std::string trace_id
32 lowercase hex characters.
Definition trace_context.h:51
std::uint8_t trace_flags
W3C trace flags byte.
Definition trace_context.h:53
friend bool operator==(const TraceContext &, const TraceContext &)=default
std::string tracestate
Raw tracestate header; may be empty.
Definition trace_context.h:54
A11's core wire value types: chunks, node fragments and messages.