#pragma once // Agent Observability read side. Agents emit OpenTelemetry to Google Cloud // Observability; the data is read back through the Cloud Trace v1 API // (traces.list, traces.get) and the Cloud Logging v2 API (entries.list). // Cloud Trace v2 only writes (batchWrite, createSpan) and is not wrapped: // agents write through their OTel exporter. #include #include #include #include "google/optimize/conversation.h" #include "google/optimize/optimize_service.h" namespace molo::google::optimize { struct Span { std::string span_id; std::string parent_span_id; std::string name; std::string kind; std::string start_time; // RFC 3339 std::string end_time; std::map labels; long duration_ms() const; // 0 when a timestamp is missing or malformed }; struct Trace { std::string project_id; std::string trace_id; std::vector spans; const Span* root() const; // span without a parent present in the trace static Trace from_json(const clients::body::Json& doc); }; struct TraceQuery { std::string view = "COMPLETE"; // MINIMAL | ROOTSPAN | COMPLETE std::string start_time; // RFC 3339, optional std::string end_time; std::string filter; // Trace filter syntax, max 1000 chars std::string order_by; // traceId | name | duration | start [desc] int page_size = 50; // 1..100 std::size_t max_pages = 5; std::size_t max_traces = 200; // <= OptimizeCaps::kMaxTraces }; class TraceReader : public OptimizeService { public: explicit TraceReader(std::shared_ptr client); std::vector list(const std::string& project, const TraceQuery& query = {}) const; Trace get(const std::string& project, const std::string& trace_id) const; static bool valid_trace_id(const std::string& id); // 32 hex characters }; struct LogEntry { std::string log_name; std::string timestamp; std::string severity; std::string trace; // projects/{p}/traces/{id} std::string span_id; std::string text; // textPayload clients::body::Json json; // jsonPayload, null when absent std::map labels; static LogEntry from_json(const clients::body::Json& doc); }; struct LogQuery { std::vector resource_names; // e.g. projects/{p}; 1..100 std::string filter; // Logging query language, max 20000 bool newest_first = true; // orderBy "timestamp desc|asc" int page_size = 50; // 1..100 std::size_t max_entries = 200; // <= OptimizeCaps::kMaxLogEntries // Filter selecting the entries of one trace. static std::string trace_filter(const std::string& project, const std::string& trace_id); }; class LogReader : public OptimizeService { public: explicit LogReader(std::shared_ptr client); std::vector entries(const LogQuery& query) const; // Body of entries:list, exposed for offline checks. Validates the query. static clients::body::Json request_body(const LogQuery& query, const std::string& page_token); }; // Whole conversations out of log entries (local heuristic, see SPEC // Unverified): entries whose jsonPayload holds a role (user | model | // assistant | agent) and a text under content, text or message. class ConversationBuilder { public: static Conversation from_logs(const std::vector& entries, const std::string& id = ""); }; // Numbers for a dashboard page, computed locally from fetched data. struct DashboardData { std::size_t traces = 0; std::size_t spans = 0; long p50_ms = 0; long p95_ms = 0; std::vector> top_spans; // name, count; max 10 std::map log_severity; clients::body::Json to_json() const; }; DashboardData summarize(const std::vector& traces, const std::vector& logs); } // namespace molo::google::optimize