New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support ignoring alias fields from trace registry #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -243,7 +243,7 @@ where | |
Option(format) => format!("Option<{}>", Self::quote_type(format, known_sizes)), | ||
Seq(format) => format!("Vec<{}>", Self::quote_type(format, None)), | ||
Map { key, value } => format!( | ||
"Map<{}, {}>", | ||
"std::collections::BTreeMap<{}, {}>", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will changing to BTreeMap change any behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully, its deterministic. |
||
Self::quote_type(key, None), | ||
Self::quote_type(value, None) | ||
), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,10 @@ use crate::{ | |
}; | ||
use once_cell::sync::Lazy; | ||
use serde::{de::DeserializeSeed, Deserialize, Serialize}; | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
use std::{ | ||
collections::{BTreeMap, BTreeSet}, | ||
ops::Deref, | ||
}; | ||
|
||
/// A map of container formats. | ||
pub type Registry = BTreeMap<String, ContainerFormat>; | ||
|
@@ -285,6 +288,60 @@ impl Tracer { | |
self.record_container(samples, name, format, value, false) | ||
} | ||
|
||
/// Removes aliases for a field in a container from the registry. | ||
/// | ||
/// The serde doesn't differentiate between alias names from field names so | ||
/// this is a way to inform the registry of aliases of a particular field in a | ||
/// container. | ||
pub fn ignore_aliases( | ||
&mut self, | ||
container_name: &'static str, | ||
aliases: &[&'static str], | ||
) -> Result<()> { | ||
let container_format = match self.registry.get_mut(container_name) { | ||
None => { | ||
return Err(Error::Custom(format!( | ||
"cannot alias. container not found: {}", | ||
container_name | ||
))) | ||
} | ||
Some(container_format) => container_format, | ||
}; | ||
match container_format { | ||
ContainerFormat::Struct(named_formats) => { | ||
let (non_alias_formats, alias_formats): (Vec<_>, Vec<_>) = named_formats | ||
.iter() | ||
.cloned() | ||
.partition(|format| !aliases.contains(&format.name.deref())); | ||
|
||
if alias_formats.len() != aliases.len() { | ||
return Err(Error::Custom(format!( | ||
"cannot ignore alias. not all aliases are found in container: {:?}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if the user says "ignore alias x" but x isn't found, this will throw an error, is that right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this was intentional. |
||
aliases | ||
))); | ||
} | ||
|
||
for alias_format in alias_formats { | ||
if !alias_format.value.is_unknown() { | ||
return Err(Error::Custom(format!( | ||
"cannot ignore alias. tracer registered value: {}", | ||
alias_format.name | ||
))); | ||
} | ||
} | ||
|
||
*named_formats = non_alias_formats; | ||
} | ||
_ => { | ||
return Err(Error::Custom(format!( | ||
"cannot record alias for non-struct container: {}", | ||
container_name | ||
))) | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn get_sample<'de, 'a>( | ||
&'a self, | ||
samples: &'de Samples, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -457,3 +457,35 @@ fn test_repeated_tracing() { | |
)))))) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_trace_deserialization_with_alias_types() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, looks good. |
||
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)] | ||
struct Foo { | ||
#[serde(rename = "b", alias = "c")] | ||
pub a: Vec<u8>, | ||
} | ||
|
||
let samples = Samples::new(); | ||
let mut tracer = Tracer::new(TracerConfig::default()); | ||
|
||
tracer.trace_type::<Foo>(&samples).unwrap(); | ||
assert_eq!( | ||
tracer.registry().unwrap_err(), | ||
Error::UnknownFormatInContainer("Foo".into()), | ||
); | ||
|
||
let mut tracer = Tracer::new(TracerConfig::default()); | ||
|
||
tracer.trace_type::<Foo>(&samples).unwrap(); | ||
tracer.ignore_aliases("Foo", &["c"]).unwrap(); | ||
|
||
let registry = tracer.registry().unwrap(); | ||
assert_eq!( | ||
*registry.get("Foo").unwrap(), | ||
ContainerFormat::Struct(vec![Named { | ||
name: "b".into(), | ||
value: Format::Seq(Box::new(Format::U8)) | ||
}]) | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was this Map type, I can't see any type imported with that name. I suppose it's a serde specific thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure either. It was failing a unit test, so I fixed it. It was failing earlier too, but I guess nobody noticed.