use std::collections::{BTreeMap, HashMap};
use std::future::Future;
use std::sync::Arc;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use tracing::span::{Attributes, Id, Record};
use tracing_subscriber::layer::{Context as LayerContext, Layer, SubscriberExt};
use tracing_subscriber::registry::LookupSpan;
use super::d6::{self, D6Fields};
use super::log_digest::{LogDigest, LogRecord, build_log_digest};
pub const PRESENT: &str = "PRESENT";
pub(crate) const NON_DETERMINISTIC_ATTRIBUTES: &[&str] = &[
"gcp.vertex.agent.event_id",
"gen_ai.tool.call.id",
"gen_ai.conversation.id",
"gcp.vertex.agent.invocation_id",
];
pub(crate) const JSON_STRING_ATTRIBUTES: &[&str] = &[
"gen_ai.input.messages",
"gen_ai.output.messages",
"gen_ai.system_instructions",
"gcp.vertex.agent.tool_call_args",
"gcp.vertex.agent.tool_response",
];
pub(crate) const JSON_STRING_KEY: &str = "JSON_STRING";
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SpanStub {
pub name: String,
pub span_id: u64,
pub parent_span_id: Option<u64>,
pub start_time: i64,
pub attributes: Vec<(String, Value)>,
pub status: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Digest {
pub root_span: SpanDigest,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SpanDigest {
pub name: String,
pub attributes: BTreeMap<String, Value>,
pub status: String,
pub children: Vec<SpanDigest>,
pub logs: Vec<LogDigest>,
}
pub fn build_digest(spans: &[SpanStub], logs: &[LogRecord]) -> Result<Digest, String> {
let mut digests: Vec<SpanDigest> = spans
.iter()
.map(|s| SpanDigest {
name: s.name.clone(),
attributes: normalize_span_attributes(&s.attributes),
status: s.status.clone(),
children: Vec::new(),
logs: Vec::new(),
})
.collect();
let by_span_id: HashMap<u64, usize> = spans
.iter()
.enumerate()
.map(|(i, s)| (s.span_id, i))
.collect();
for r in logs {
let Some(&i) = r.span_id.and_then(|id| by_span_id.get(&id)) else {
return Err(format!(
"log record {} was emitted outside any collected span",
gadk_platform::go_quote(&r.event_name)
));
};
digests[i].logs.push(build_log_digest(r));
}
let mut roots = link_and_sort(spans, digests, &by_span_id);
if roots.len() != 1 {
return Err(format!("expected exactly 1 root span, got {}", roots.len()));
}
Ok(Digest {
root_span: roots.remove(0),
})
}
fn link_and_sort(
spans: &[SpanStub],
digests: Vec<SpanDigest>,
by_span_id: &HashMap<u64, usize>,
) -> Vec<SpanDigest> {
let mut order: Vec<usize> = (0..spans.len()).collect();
order.sort_by(|&a, &b| {
spans[a]
.start_time
.cmp(&spans[b].start_time)
.then_with(|| spans[a].name.cmp(&spans[b].name))
});
let parent_of = |i: usize| -> Option<usize> {
spans[i]
.parent_span_id
.and_then(|p| by_span_id.get(&p).copied())
};
let mut children: HashMap<usize, Vec<usize>> = HashMap::new();
let mut roots = Vec::new();
for &i in &order {
match parent_of(i) {
Some(p) => children.entry(p).or_default().push(i),
None => roots.push(i),
}
}
let mut slots: Vec<Option<SpanDigest>> = digests.into_iter().map(Some).collect();
fn build(
i: usize,
slots: &mut [Option<SpanDigest>],
children: &HashMap<usize, Vec<usize>>,
) -> Option<SpanDigest> {
let mut d = slots.get_mut(i)?.take()?;
for &c in children.get(&i).map(Vec::as_slice).unwrap_or_default() {
if let Some(child) = build(c, slots, children) {
d.children.push(child);
}
}
Some(d)
}
roots
.into_iter()
.filter_map(|r| build(r, &mut slots, &children))
.collect()
}
pub async fn capture_spans<F: Future>(fut: F) -> (F::Output, Vec<SpanStub>) {
let (out, spans, _logs) = capture(fut).await;
(out, spans)
}
pub(crate) async fn capture<F: Future>(fut: F) -> (F::Output, Vec<SpanStub>, Vec<LogRecord>) {
pin_tracing_interest();
let store: Arc<Mutex<Store>> = Arc::default();
let sub = tracing_subscriber::registry().with(CaptureLayer(store.clone()));
let out = {
let _g = tracing::subscriber::set_default(sub);
fut.await
};
let store = std::mem::take(&mut *store.lock());
(out, store.spans, store.logs)
}
pub(crate) fn pin_tracing_interest() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
let _ = tracing::subscriber::set_global_default(tracing_subscriber::registry());
});
}
#[derive(Default)]
struct Store {
spans: Vec<SpanStub>,
logs: Vec<LogRecord>,
}
struct CaptureLayer(Arc<Mutex<Store>>);
struct Idx(usize);
fn apply(stub: &mut SpanStub, fields: D6Fields) {
if let Some(name) = fields.name {
stub.name = name;
}
if let Some(code) = fields.status_code {
stub.status = code.as_str().to_owned();
}
for (key, value) in fields.attributes {
let value = value.to_json();
match stub.attributes.iter_mut().find(|(k, _)| *k == key) {
Some(slot) => slot.1 = value,
None => stub.attributes.push((key, value)),
}
}
}
impl CaptureLayer {
fn index_of<S>(id: &Id, ctx: &LayerContext<'_, S>) -> Option<usize>
where
S: tracing::Subscriber + for<'a> LookupSpan<'a>,
{
ctx.span(id)?.extensions().get::<Idx>().map(|i| i.0)
}
}
impl<S> Layer<S> for CaptureLayer
where
S: tracing::Subscriber + for<'a> LookupSpan<'a>,
{
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: LayerContext<'_, S>) {
let parent = ctx
.span(id)
.and_then(|s| s.parent())
.and_then(|p| p.extensions().get::<Idx>().map(|i| i.0 as u64 + 1));
let mut store = self.0.lock();
let i = store.spans.len();
let mut stub = SpanStub {
name: attrs.metadata().name().to_owned(),
span_id: i as u64 + 1,
parent_span_id: parent,
start_time: i as i64,
attributes: Vec::new(),
status: "UNSET".to_owned(),
};
let mut fields = D6Fields::default();
attrs.record(&mut fields);
apply(&mut stub, fields);
store.spans.push(stub);
drop(store);
if let Some(span) = ctx.span(id) {
span.extensions_mut().insert(Idx(i));
}
}
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: LayerContext<'_, S>) {
let Some(i) = Self::index_of(id, &ctx) else {
return;
};
let mut fields = D6Fields::default();
values.record(&mut fields);
if let Some(stub) = self.0.lock().spans.get_mut(i) {
apply(stub, fields);
}
}
fn on_event(&self, event: &tracing::Event<'_>, ctx: LayerContext<'_, S>) {
let Some(log) = d6::decode_log(event) else {
return;
};
let span_id = ctx
.event_span(event)
.and_then(|s| s.extensions().get::<Idx>().map(|i| i.0 as u64 + 1));
self.0.lock().logs.push(LogRecord {
event_name: log.event_name,
span_id,
body: log.body.unwrap_or(Value::Null),
attributes: log
.attributes
.map(|m| m.into_iter().collect())
.unwrap_or_default(),
});
}
}
pub(crate) fn normalize_span_attributes(attrs: &[(String, Value)]) -> BTreeMap<String, Value> {
attrs
.iter()
.map(|(key, value)| {
let mut value = value.clone();
if let Value::String(s) = &value
&& JSON_STRING_ATTRIBUTES.contains(&key.as_str())
&& let Ok(parsed) = serde_json::from_str::<Value>(s)
{
value = Value::Object(Map::from_iter([(JSON_STRING_KEY.to_owned(), parsed)]));
}
(key.clone(), normalize_attribute(key, value))
})
.collect()
}
pub(crate) fn normalize_attribute(key: &str, value: Value) -> Value {
if NON_DETERMINISTIC_ATTRIBUTES.contains(&key) {
return Value::String(PRESENT.to_owned());
}
value
}