Observability: W3C traceparent/tracestate/baggage propagation (S-OBSERVABILITY-05)
This commit is contained in:
1 parent
404bf4ce2f
commit
cc41e5b1d9
3 files changed
+415
No files matched your search
@@ -0,0 +1,257 @@
|
||||
#include <Void/Observability/Propagation.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Observability
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] char lower(char c) noexcept
|
||||
{
|
||||
return c >= 'A' && c <= 'Z' ? static_cast<char>(c + 32) : c;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool iequals(std::string_view lhs,
|
||||
std::string_view rhs) noexcept
|
||||
{
|
||||
if (lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < lhs.size(); ++i) {
|
||||
if (lower(lhs[i]) != lower(rhs[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<std::string_view>
|
||||
find_header(std::span<const HeaderField> headers,
|
||||
std::string_view name) noexcept
|
||||
{
|
||||
for (const auto& header : headers) {
|
||||
if (iequals(header.name, name)) {
|
||||
return std::string_view{header.value};
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string_view trim(std::string_view text) noexcept
|
||||
{
|
||||
while (!text.empty() && (text.front() == ' ' || text.front() == '\t')) {
|
||||
text.remove_prefix(1);
|
||||
}
|
||||
while (!text.empty() && (text.back() == ' ' || text.back() == '\t')) {
|
||||
text.remove_suffix(1);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool safe_text(std::string_view text) noexcept
|
||||
{
|
||||
for (const char ch : text) {
|
||||
const auto byte = static_cast<unsigned char>(ch);
|
||||
if (byte < 0x20 || byte == 0x7f || ch == ',' || ch == '=') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
validate_tracestate(std::string_view value,
|
||||
const PropagationLimits& limits)
|
||||
{
|
||||
if (value.empty()) {
|
||||
return {};
|
||||
}
|
||||
if (value.size() > limits.max_tracestate_bytes) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate exceeds the byte limit")};
|
||||
}
|
||||
|
||||
std::vector<std::string_view> keys;
|
||||
std::size_t members = 0;
|
||||
std::size_t pos = 0;
|
||||
while (pos <= value.size()) {
|
||||
const auto comma = value.find(',', pos);
|
||||
const auto member = trim(value.substr(
|
||||
pos, comma == std::string_view::npos ? std::string_view::npos
|
||||
: comma - pos));
|
||||
if (member.empty()) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate contains an empty member")};
|
||||
}
|
||||
const auto equals = member.find('=');
|
||||
if (equals == std::string_view::npos || equals == 0 ||
|
||||
equals + 1 >= member.size()) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate member must be key=value")};
|
||||
}
|
||||
const auto key = member.substr(0, equals);
|
||||
const auto entry = member.substr(equals + 1);
|
||||
for (const char ch : key) {
|
||||
const bool ok = (ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= '0' && ch <= '9') || ch == '-' ||
|
||||
ch == '_' || ch == '*' || ch == '/';
|
||||
if (!ok) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate key has invalid characters")};
|
||||
}
|
||||
}
|
||||
for (const char ch : entry) {
|
||||
const auto byte = static_cast<unsigned char>(ch);
|
||||
if (byte < 0x20 || byte == 0x7f) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate value has control characters")};
|
||||
}
|
||||
}
|
||||
if (std::find(keys.begin(), keys.end(), key) != keys.end()) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate contains a duplicate key")};
|
||||
}
|
||||
keys.push_back(key);
|
||||
++members;
|
||||
if (members > limits.max_tracestate_members) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate has too many members")};
|
||||
}
|
||||
if (comma == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = comma + 1;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::vector<BaggageItem>>
|
||||
parse_baggage(std::string_view value)
|
||||
{
|
||||
std::vector<BaggageItem> items;
|
||||
std::size_t pos = 0;
|
||||
while (pos <= value.size()) {
|
||||
const auto comma = value.find(',', pos);
|
||||
const auto member = trim(value.substr(
|
||||
pos, comma == std::string_view::npos ? std::string_view::npos
|
||||
: comma - pos));
|
||||
if (!member.empty()) {
|
||||
const auto equals = member.find('=');
|
||||
if (equals == std::string_view::npos || equals == 0) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"baggage member must be key=value")};
|
||||
}
|
||||
auto key = member.substr(0, equals);
|
||||
auto item = member.substr(equals + 1);
|
||||
// Drop optional ;properties.
|
||||
if (const auto semicolon = item.find(';');
|
||||
semicolon != std::string_view::npos) {
|
||||
item = item.substr(0, semicolon);
|
||||
}
|
||||
if (!safe_text(key) || !safe_text(item)) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"baggage contains invalid characters")};
|
||||
}
|
||||
items.push_back(
|
||||
BaggageItem{std::string{key}, std::string{item}});
|
||||
}
|
||||
if (comma == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
pos = comma + 1;
|
||||
}
|
||||
return items;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<CarriedContext>
|
||||
extract_context(std::span<const HeaderField> headers,
|
||||
ContextLimits context_limits,
|
||||
PropagationLimits propagation_limits)
|
||||
{
|
||||
std::optional<TraceContext> trace;
|
||||
std::string tracestate;
|
||||
std::vector<BaggageItem> baggage;
|
||||
|
||||
if (const auto header = find_header(headers, "traceparent")) {
|
||||
auto parsed = TraceContext::from_traceparent(*header);
|
||||
if (!parsed) {
|
||||
return std::unexpected{std::move(parsed).error()};
|
||||
}
|
||||
trace = *parsed;
|
||||
}
|
||||
if (const auto header = find_header(headers, "tracestate")) {
|
||||
auto valid = validate_tracestate(*header, propagation_limits);
|
||||
if (!valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
tracestate = std::string{*header};
|
||||
}
|
||||
if (const auto header = find_header(headers, "baggage")) {
|
||||
auto parsed = parse_baggage(*header);
|
||||
if (!parsed) {
|
||||
return std::unexpected{std::move(parsed).error()};
|
||||
}
|
||||
baggage = std::move(*parsed);
|
||||
}
|
||||
// A tracestate without a traceparent is meaningless and must not be
|
||||
// propagated alone.
|
||||
if (!tracestate.empty() && !trace.has_value()) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate without traceparent")};
|
||||
}
|
||||
|
||||
auto context = ContextSnapshot::create(
|
||||
trace, baggage, context_limits);
|
||||
if (!context) {
|
||||
return std::unexpected{std::move(context).error()};
|
||||
}
|
||||
return CarriedContext{std::move(*context), std::move(tracestate)};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<HeaderField>>
|
||||
inject_context(const CarriedContext& carried)
|
||||
{
|
||||
std::vector<HeaderField> headers;
|
||||
if (const auto& trace = carried.context.trace()) {
|
||||
headers.push_back(
|
||||
HeaderField{"traceparent", trace->to_traceparent()});
|
||||
}
|
||||
if (!carried.tracestate.empty()) {
|
||||
if (!carried.context.trace().has_value()) {
|
||||
return std::unexpected{make_observability_error(
|
||||
ObservabilityErrorCode::invalid_argument,
|
||||
"tracestate without traceparent")};
|
||||
}
|
||||
headers.push_back(HeaderField{"tracestate", carried.tracestate});
|
||||
}
|
||||
if (!carried.context.baggage().empty()) {
|
||||
std::string value;
|
||||
for (const auto& item : carried.context.baggage()) {
|
||||
if (!value.empty()) {
|
||||
value += ',';
|
||||
}
|
||||
value += item.key;
|
||||
value += '=';
|
||||
value += item.value;
|
||||
}
|
||||
headers.push_back(HeaderField{"baggage", std::move(value)});
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
} // namespace Void::Observability
|
||||
Reference in new issue
Block a user