Skip to content

Commit be2c493

Browse files
Add AntithesisLayer to integrate with tracing.
1 parent 76e6d33 commit be2c493

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed

Cargo.lock

+118
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Rust SDK for the Antithesis autonomous software testing platform.
1818
serde = { version = "1.0.113", features = ["derive"] }
1919
serde_json = "1.0.25"
2020
rand = "0.8"
21+
tracing-core = "0.1"
22+
tracing-subscriber = "0.3"
2123

2224
# needed only if full feature is set
2325
rustc_version_runtime = {version = "0.3", optional = true}

lib/src/lifecycle.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ pub fn send_event(name: &str, details: &Value) {
7171
internal::dispatch_output(&json_event)
7272
}
7373

74+
mod tracing;
75+
7476
#[cfg(test)]
7577
mod tests {
7678
use super::*;

lib/src/lifecycle/tracing.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)