Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions relay-conventions/build/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pub enum DeprecationStatus {
#[derive(Debug, Clone, Deserialize)]
pub struct Deprecation {
/// The attribute's new name.
replacement: Option<String>,
pub replacement: Option<String>,
/// How to handle the attribute's deprecation.
#[serde(rename = "_status")]
status: Option<DeprecationStatus>,
pub status: Option<DeprecationStatus>,
}

/// An attribute, according to the `sentry-conventions` schema.
Expand All @@ -64,6 +64,8 @@ pub struct Deprecation {
pub struct Attribute {
/// The attribute's name.
pub key: String,
#[serde(rename = "type")]
pub ty: String,
/// Short description of the attribute.
pub brief: String,
/// Whether the attribute should be scrubbed.
Expand All @@ -87,6 +89,7 @@ pub struct Attribute {
pub fn check_attribute(attribute: &Attribute) {
let Attribute {
key,
ty: _,
brief: _,
apply_scrubbing: _,
deprecation,
Expand Down Expand Up @@ -140,6 +143,7 @@ fn format_write_behavior(deprecation: Option<&Deprecation>) -> String {
pub fn format_attribute_info(attr: &Attribute) -> String {
let Attribute {
key: _,
ty: _,
brief: _,
apply_scrubbing,
deprecation,
Expand All @@ -161,6 +165,7 @@ pub fn format_attribute_info(attr: &Attribute) -> String {
pub fn format_constant(attr: &Attribute) -> String {
let Attribute {
key,
ty: _,
brief,
apply_scrubbing,
deprecation,
Expand Down Expand Up @@ -220,6 +225,7 @@ pub fn format_constant(attr: &Attribute) -> String {
pub fn format_interpolating_fn(attribute: &Attribute) -> Option<String> {
let Attribute {
key,
ty: _,
brief: _,
apply_scrubbing: _,
deprecation,
Expand Down Expand Up @@ -306,6 +312,7 @@ pub fn parse_segments(key: &str) -> impl Iterator<Item = &str> {
pub fn constant_pair(attribute: &Attribute) -> Option<(String, String)> {
let Attribute {
key,
ty: _,
brief: _,
apply_scrubbing: _,
deprecation,
Expand Down
41 changes: 41 additions & 0 deletions relay-conventions/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod attributes;
mod description;
mod measurements;
mod name;
mod read_names;
mod template;

use std::collections::BTreeMap;
Expand Down Expand Up @@ -46,6 +47,7 @@ fn write_attribute_rs(crate_dir: &Path) {
let mut attribute_replacement_map = BTreeMap::new();

let mut root = RawNode::default();
let mut all_attributes = BTreeMap::new();

for file in WalkDir::new(crate_dir.join(ATTRIBUTE_DIR)) {
let file = file.unwrap();
Expand Down Expand Up @@ -86,9 +88,48 @@ fn write_attribute_rs(crate_dir: &Path) {
break;
}
}
drop(parts);
all_attributes.insert(attr.key.clone(), attr);
}
}

let read_attributes = all_attributes
.iter()
.map(|(key, attribute)| {
let replacement = attribute.deprecation.as_ref().and_then(|deprecation| {
if matches!(deprecation.status, Some(DeprecationStatus::Transform)) {
None
} else {
deprecation.replacement.as_deref()
}
});
(
key.as_str(),
read_names::ReadAttribute {
ty: &attribute.ty,
replacement,
},
)
})
.collect();
let mut read_root = RawNode::default();
for (key, names) in read_names::read_names(&read_attributes) {
let mut node = &mut read_root;
for part in parse_segments(key) {
node = node.children.entry(part.to_owned()).or_default();
}
node.info = Some(format!("&{names:?}"));
}
let read_names_path = Path::new(&env::var("OUT_DIR").unwrap()).join("attribute_read_names.rs");
let mut read_names_file = BufWriter::new(File::create(read_names_path).unwrap());
write!(
&mut read_names_file,
"static ATTRIBUTE_READ_NAMES: Node<&[&str]> = "
)
.unwrap();
read_root.write(&mut read_names_file).unwrap();
writeln!(&mut read_names_file, ";").unwrap();

let canonical_fn_path = Path::new(&env::var("OUT_DIR").unwrap()).join("canonical_fn.rs");
let mut canonical_fn_file = BufWriter::new(File::create(&canonical_fn_path).unwrap());

Expand Down
194 changes: 194 additions & 0 deletions relay-conventions/build/read_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
use std::collections::{BTreeMap, BTreeSet};

pub struct ReadAttribute<'a> {
pub ty: &'a str,
pub replacement: Option<&'a str>,
}

pub fn read_names<'a>(
attributes: &BTreeMap<&'a str, ReadAttribute<'a>>,
) -> BTreeMap<&'a str, Vec<&'a str>> {
let mut replacements = BTreeMap::new();
let mut predecessors: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();

for (&key, attribute) in attributes {
let Some(replacement) = attribute.replacement else {
continue;
};
let target = attributes
.get(replacement)
.unwrap_or_else(|| panic!("Unknown replacement {replacement} for {key}"));
if attribute.ty != target.ty || key.contains("<key>") != replacement.contains("<key>") {
continue;
}
replacements.insert(key, replacement);
predecessors.entry(replacement).or_default().insert(key);
}

let mut result = BTreeMap::new();
for &key in attributes.keys() {
let mut head = key;
let mut visited = BTreeSet::from([head]);
while let Some(&replacement) = replacements.get(head) {
assert!(
visited.insert(replacement),
"Deprecation cycle involving {replacement}"
);
head = replacement;
}

let mut names = Vec::new();
let mut level = BTreeSet::from([head]);
while !level.is_empty() {
let mut next = BTreeSet::new();
for name in level {
names.push(name);
if let Some(older_names) = predecessors.get(name) {
next.extend(older_names);
}
}
level = next;
}
result.insert(key, names);
}
result
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn orders_by_distance_then_name_for_every_member() {
let attributes = BTreeMap::from([
(
"current",
ReadAttribute {
ty: "string",
replacement: None,
},
),
(
"middle_b",
ReadAttribute {
ty: "string",
replacement: Some("current"),
},
),
(
"middle_a",
ReadAttribute {
ty: "string",
replacement: Some("current"),
},
),
(
"oldest_a",
ReadAttribute {
ty: "string",
replacement: Some("middle_b"),
},
),
(
"oldest_b",
ReadAttribute {
ty: "string",
replacement: Some("middle_a"),
},
),
]);
for names in read_names(&attributes).values() {
assert_eq!(
names,
&["current", "middle_a", "middle_b", "oldest_a", "oldest_b"]
);
}
}

#[test]
fn excludes_incompatible_types_and_key_shapes() {
let attributes = BTreeMap::from([
(
"current.<key>",
ReadAttribute {
ty: "string",
replacement: None,
},
),
(
"old.<key>",
ReadAttribute {
ty: "string",
replacement: Some("current.<key>"),
},
),
(
"array.<key>",
ReadAttribute {
ty: "string[]",
replacement: Some("current.<key>"),
},
),
(
"scalar",
ReadAttribute {
ty: "string",
replacement: Some("current.<key>"),
},
),
(
"query.<key>",
ReadAttribute {
ty: "string",
replacement: Some("query"),
},
),
(
"query",
ReadAttribute {
ty: "string",
replacement: None,
},
),
]);
let names = read_names(&attributes);
assert_eq!(names["current.<key>"], ["current.<key>", "old.<key>"]);
assert_eq!(names["old.<key>"], names["current.<key>"]);
for key in ["array.<key>", "scalar", "query.<key>", "query"] {
assert_eq!(names[key], [key]);
}
}

#[test]
#[should_panic(expected = "Deprecation cycle")]
fn rejects_cycles() {
read_names(&BTreeMap::from([
(
"a",
ReadAttribute {
ty: "string",
replacement: Some("b"),
},
),
(
"b",
ReadAttribute {
ty: "string",
replacement: Some("a"),
},
),
]));
}

#[test]
#[should_panic(expected = "Unknown replacement")]
fn rejects_unknown_replacements() {
read_names(&BTreeMap::from([(
"a",
ReadAttribute {
ty: "string",
replacement: Some("unknown"),
},
)]));
}
}
Loading
Loading