Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
http_tracecontext.h
1#ifndef SOURCEMETA_CORE_HTTP_TRACECONTEXT_H_
2#define SOURCEMETA_CORE_HTTP_TRACECONTEXT_H_
3
4#ifndef SOURCEMETA_CORE_HTTP_EXPORT
5#include <sourcemeta/core/http_export.h>
6#endif
7
8#include <sourcemeta/core/http_syntax.h>
9#include <sourcemeta/core/text.h>
10
11#include <array> // std::array
12#include <concepts> // std::invocable
13#include <cstdint> // std::uint8_t
14#include <optional> // std::optional
15#include <string_view> // std::string_view
16
17namespace sourcemeta::core {
18
31 std::uint8_t version{0};
34 std::string_view trace_id;
37 std::string_view parent_id;
39 std::uint8_t flags{0};
40};
41
60constexpr auto http_traceparent_sampled(const std::uint8_t flags) noexcept
61 -> bool {
62 return (flags & 0x01U) != 0;
63}
64
93SOURCEMETA_CORE_HTTP_EXPORT
94auto http_is_traceparent(const std::string_view value) noexcept -> bool;
95
114SOURCEMETA_CORE_HTTP_EXPORT
115auto http_parse_traceparent(const std::string_view value) noexcept
116 -> std::optional<HTTPTraceParent>;
117
146SOURCEMETA_CORE_HTTP_EXPORT
147auto http_make_traceparent(const bool sampled) -> std::array<char, 55>;
148
183SOURCEMETA_CORE_HTTP_EXPORT
184auto http_continue_traceparent(const std::string_view value, const bool sampled)
185 -> std::optional<std::array<char, 55>>;
186
218SOURCEMETA_CORE_HTTP_EXPORT
219auto http_is_tracestate(const std::string_view value) noexcept -> bool;
220
243template <typename Callback>
244 requires std::invocable<Callback, std::string_view, std::string_view>
245inline auto http_parse_tracestate(const std::string_view value,
246 Callback callback) -> bool {
247 if (!http_is_tracestate(value)) {
248 return false;
249 }
250 auto rest{value};
251 while (true) {
252 const auto comma{rest.find(',')};
253 const auto entry{trim(rest.substr(0, comma), http_is_ows)};
254 const auto equal{entry.find('=')};
255 if (equal != std::string_view::npos) {
256 callback(entry.substr(0, equal), entry.substr(equal + 1));
257 }
258 if (comma == std::string_view::npos) {
259 return true;
260 }
261 rest.remove_prefix(comma + 1);
262 }
263}
264
284template <typename Container>
285 requires requires(Container container, std::string_view entry) {
286 container.emplace_back(entry, entry);
287 }
288inline auto http_parse_tracestate(const std::string_view value,
289 Container &members) -> bool {
291 value,
292 [&members](const std::string_view key, const std::string_view entry)
293 -> void { members.emplace_back(key, entry); });
294}
295
296} // namespace sourcemeta::core
297
298#endif
std::string_view parent_id
Definition http_tracecontext.h:37
std::string_view trace_id
Definition http_tracecontext.h:34
std::uint8_t flags
An eight bit field of which one bit is currently defined.
Definition http_tracecontext.h:39
std::uint8_t version
Definition http_tracecontext.h:31
auto http_is_ows(const char character) noexcept -> bool
Definition http_syntax.h:28
SOURCEMETA_CORE_HTTP_EXPORT auto http_continue_traceparent(const std::string_view value, const bool sampled) -> std::optional< std::array< char, 55 > >
SOURCEMETA_CORE_HTTP_EXPORT auto http_parse_traceparent(const std::string_view value) noexcept -> std::optional< HTTPTraceParent >
SOURCEMETA_CORE_HTTP_EXPORT auto http_is_traceparent(const std::string_view value) noexcept -> bool
SOURCEMETA_CORE_HTTP_EXPORT auto http_is_tracestate(const std::string_view value) noexcept -> bool
auto http_parse_tracestate(const std::string_view value, Callback callback) -> bool
Definition http_tracecontext.h:245
constexpr auto http_traceparent_sampled(const std::uint8_t flags) noexcept -> bool
Definition http_tracecontext.h:60
SOURCEMETA_CORE_HTTP_EXPORT auto http_make_traceparent(const bool sampled) -> std::array< char, 55 >
Definition http_tracecontext.h:24
SOURCEMETA_CORE_TEXT_EXPORT auto trim(const std::string_view input) noexcept -> std::string_view