#pragma once #include #include namespace borla { // Correlation ids shared with server and backend (see docs/0019 §10.1). // // session connection scope · refreshed on every realtime session // trace turn scope · refreshed when the device starts an exchange // request call scope · refreshed per HTTP/OTA call // // Every id is a fixed-capacity C string with no allocation, so it is safe to // read from a log formatter that runs on any task. Values are written only by // the owning subsystem and read everywhere; readers may observe a value that // is one refresh stale, which is acceptable for correlation. class TraceContext final { public: static constexpr size_t kIdCapacity = 64; // Never null; "-" when nothing has been set yet. static const char* TraceId(); static const char* SessionId(); static const char* RequestId(); // Seeds the device-side portion of every generated id. Called once with // the device MAC so ids are globally attributable without a UUID library. static void Initialize(const char* device_id); // Generates "--" and installs it. static void RefreshTrace(const char* prefix); // Returns the freshly generated id so the caller can put it in a header. static const char* NewRequestId(); // Mints a session id for the connection that is about to be opened, and // returns it for the handshake header. // // Required because the session is connection-scoped. Without it the id is // still "-" on the first connect (which the server would adopt verbatim, // since "-" is a non-empty string) and is the *previous* connection's id on // every reconnect, merging two unrelated sessions under one id. The server // overwrites this with its own id in ServerHello, so this is a proposal, // not a decision. static const char* RefreshSession(); // Adopts an id handed down by the server (ServerHello session id). static void SetSession(const char* session_id); static void SetTrace(const char* trace_id); // Pure formatter, exposed for tests: "--", truncated // to fit and always NUL-terminated. Returns the written length. static size_t FormatId( const char* prefix, const char* device_id, uint64_t monotonic_ms, char* output, size_t capacity); private: static void Assign( const char* value, char* output, size_t capacity); }; } // namespace borla