|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use serde::Serialize; |
| 4 | +use serde_json::Value; |
| 5 | +use tracing_core::{field, span, Event, Subscriber}; |
| 6 | +use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer}; |
| 7 | + |
| 8 | +pub struct AntithesisLayer; |
| 9 | + |
| 10 | +struct ScopeValues(BTreeMap<&'static str, Value>); |
| 11 | + |
| 12 | +impl<S> Layer<S> for AntithesisLayer |
| 13 | +where S: Subscriber + for<'a> LookupSpan<'a> { |
| 14 | + fn on_new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) { |
| 15 | + let span = ctx.span(id).expect("Span not found"); |
| 16 | + let mut values = BTreeMap::new(); |
| 17 | + attrs.record(&mut JsonVisitor::with(&mut values)); |
| 18 | + span.extensions_mut().insert(ScopeValues(values)); |
| 19 | + } |
| 20 | + |
| 21 | + fn on_record(&self, span: &span::Id, record: &span::Record<'_>, ctx: Context<'_, S>) { |
| 22 | + let span = ctx.span(span).expect("Span not found"); |
| 23 | + let mut extensions = span.extensions_mut(); |
| 24 | + if let Some(ScopeValues(values)) = extensions.get_mut() { |
| 25 | + record.record(&mut JsonVisitor::with(values)); |
| 26 | + } else { |
| 27 | + let mut values = BTreeMap::new(); |
| 28 | + record.record(&mut JsonVisitor::with(&mut values)); |
| 29 | + extensions.insert(ScopeValues(values)); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { |
| 34 | + let mut values = BTreeMap::new(); |
| 35 | + event.record(&mut JsonVisitor::with(&mut values)); |
| 36 | + let spans = ctx.event_scope(event).map(|scope| { |
| 37 | + scope.from_root().filter_map(|span| { |
| 38 | + let extensions = span.extensions(); |
| 39 | + let ScopeValues(values) = extensions.get()?; |
| 40 | + Some(values.clone()) |
| 41 | + }).collect::<Vec<_>>() |
| 42 | + }); |
| 43 | + |
| 44 | + #[derive(Serialize)] |
| 45 | + struct Event { |
| 46 | + spans: Option<Vec<BTreeMap<&'static str, Value>>>, |
| 47 | + #[serde(flatten)] |
| 48 | + values: BTreeMap<&'static str, Value>, |
| 49 | + } |
| 50 | + crate::internal::dispatch_output(&Event { values, spans }); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +struct JsonVisitor<'a> { |
| 55 | + values: &'a mut BTreeMap<&'static str, Value>, |
| 56 | +} |
| 57 | + |
| 58 | +impl<'a> JsonVisitor<'a> { |
| 59 | + fn with(values: &'a mut BTreeMap<&'static str, Value>) -> Self { |
| 60 | + Self { values } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<'a> field::Visit for JsonVisitor<'a> { |
| 65 | + fn record_f64(&mut self, field: &field::Field, value: f64) { |
| 66 | + self.values.insert(field.name(), Value::from(value)); |
| 67 | + } |
| 68 | + |
| 69 | + fn record_i64(&mut self, field: &field::Field, value: i64) { |
| 70 | + self.values.insert(field.name(), Value::from(value)); |
| 71 | + } |
| 72 | + |
| 73 | + fn record_u64(&mut self, field: &field::Field, value: u64) { |
| 74 | + self.values.insert(field.name(), Value::from(value)); |
| 75 | + } |
| 76 | + |
| 77 | + fn record_bool(&mut self, field: &field::Field, value: bool) { |
| 78 | + self.values.insert(field.name(), Value::from(value)); |
| 79 | + } |
| 80 | + |
| 81 | + fn record_str(&mut self, field: &field::Field, value: &str) { |
| 82 | + self.values.insert(field.name(), Value::from(value)); |
| 83 | + } |
| 84 | + |
| 85 | + fn record_debug(&mut self, field: &field::Field, value: &dyn std::fmt::Debug) { |
| 86 | + self.values.insert(field.name(), Value::from(format!("{:?}", value))); |
| 87 | + } |
| 88 | +} |
0 commit comments