Skip to content

Commit

Permalink
Add attribute registry as a parameter
Browse files Browse the repository at this point in the history
When updating markdown, allow attribute registry links to be specified via command line argument.
  • Loading branch information
jsuereth committed Apr 1, 2024
1 parent 154e52f commit ff76f9c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
39 changes: 22 additions & 17 deletions crates/weaver_semconv_gen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,28 @@ impl<'a> AttributeView<'a> {
}
}

fn write_registry_link<T: Write>(&self, out: &mut T) -> Result<(), Error> {
fn write_registry_link<T: Write>(&self, out: &mut T, prefix: &str) -> Result<(), Error> {
let reg_name = self.attribute.name.split('.').next().unwrap_or("");
// TODO - the existing build-tools semconv will look at currently
// generating markdown location to see if it's the same structure
// as where the attribute originated from.
//
// Going forward, link vs. not link should be an option in generation.
// OR we should move this to a template-render scenario.
Ok(write!(out, "../attributes-registry/{reg_name}.md")?)
// TODO - We should try to link to the name itself, instead
// of just the correct group.
Ok(write!(out, "{prefix}/{reg_name}.md")?)
}

fn write_name_with_optional_link<Out: Write>(&self, out: &mut Out) -> Result<(), Error> {
write!(out, "[`")?;
self.write_name(out)?;
write!(out, "`](")?;
self.write_registry_link(out)?;
write!(out, ")")?;
fn write_name_with_optional_link<Out: Write>(
&self,
out: &mut Out,
attribute_registry_base_url: Option<&str>,
) -> Result<(), Error> {
match attribute_registry_base_url {
Some(prefix) => {
write!(out, "[`")?;
self.write_name(out)?;
write!(out, "`](")?;
self.write_registry_link(out, prefix)?;
write!(out, ")")?;
}
None => self.write_name(out)?,
}
Ok(())
}

Expand Down Expand Up @@ -337,6 +342,7 @@ impl<'a> AttributeTableView<'a> {
out: &mut Out,
args: &GenerateMarkdownArgs,
ctx: &mut GenerateMarkdownContext,
attribute_registry_base_url: Option<&str>,
) -> Result<(), Error> {
if self.group.r#type == GroupType::Event {
write!(out, "The event name MUST be `{}`\n\n", self.event_name())?;
Expand Down Expand Up @@ -366,7 +372,7 @@ impl<'a> AttributeTableView<'a> {

for attr in &attributes {
write!(out, "| ")?;
attr.write_name_with_optional_link(out)?;
attr.write_name_with_optional_link(out, attribute_registry_base_url)?;
write!(out, " | ")?;
attr.write_type_string(out)?;
write!(out, " | ")?;
Expand Down Expand Up @@ -401,9 +407,8 @@ impl<'a> AttributeTableView<'a> {
"and SHOULD be provided **at span creation time** (if provided at all):\n\n"
)?;
for a in sampling_relevant {
// TODO - existing output uses registry-link-name.
write!(out, "* ")?;
a.write_name_with_optional_link(out)?;
a.write_name_with_optional_link(out, attribute_registry_base_url)?;
writeln!(out)?;
}
}
Expand Down
16 changes: 11 additions & 5 deletions crates/weaver_semconv_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl GenerateMarkdownArgs {
fn generate_markdown_snippet(
lookup: &ResolvedSemconvRegistry,
args: GenerateMarkdownArgs,
attribute_registry_base_url: Option<&str>,
) -> Result<String, Error> {
let mut ctx = GenerateMarkdownContext::default();
let mut result = String::new();
Expand All @@ -135,7 +136,7 @@ fn generate_markdown_snippet(
view.generate_markdown(&mut result, &mut ctx)?;
} else {
let other = AttributeTableView::try_new(args.id.as_str(), lookup)?;
other.generate_markdown(&mut result, &args, &mut ctx)?;
other.generate_markdown(&mut result, &args, &mut ctx, attribute_registry_base_url)?;
}
Ok(result)
}
Expand All @@ -144,6 +145,7 @@ fn generate_markdown_snippet(
fn update_markdown_contents(
contents: &str,
lookup: &ResolvedSemconvRegistry,
attribute_registry_base_url: Option<&str>,
) -> Result<String, Error> {
let mut result = String::new();
let mut handling_snippet = false;
Expand All @@ -165,7 +167,7 @@ fn update_markdown_contents(
if parser::is_markdown_snippet_directive(line) {
handling_snippet = true;
let arg = parser::parse_markdown_snippet_directive(line)?;
let snippet = generate_markdown_snippet(lookup, arg)?;
let snippet = generate_markdown_snippet(lookup, arg, attribute_registry_base_url)?;
result.push_str(&snippet);
}
}
Expand All @@ -178,9 +180,11 @@ pub fn update_markdown(
file: &str,
lookup: &ResolvedSemconvRegistry,
dry_run: bool,
attribute_registry_base_url: Option<&str>,
) -> Result<(), Error> {
let original_markdown = fs::read_to_string(file)?;
let updated_markdown = update_markdown_contents(&original_markdown, lookup)?;
let updated_markdown =
update_markdown_contents(&original_markdown, lookup, attribute_registry_base_url)?;
if !dry_run {
fs::write(file, updated_markdown)?;
Ok(())
Expand Down Expand Up @@ -273,17 +277,19 @@ mod tests {
fn test_http_semconv() -> Result<(), Error> {
let logger = TestLogger::default();
let lookup = ResolvedSemconvRegistry::try_from_path("data/**/*.yaml", logger.clone())?;

let attribute_registry_url = "../attributes-registry";
// Check our test files.
force_print_error(update_markdown(
"data/http-span-full-attribute-table.md",
&lookup,
true,
Some(attribute_registry_url),
));
force_print_error(update_markdown(
"data/http-metric-semconv.md",
&lookup,
true,
Some(attribute_registry_url),
));
Ok(())
}
Expand All @@ -309,6 +315,6 @@ mod tests {
let lookup = ResolvedSemconvRegistry::try_from_path(&semconv_path, logger.clone())?;
let test_path = path.join("test.md").display().to_string();
// Attempts to update the test - will fail if there is any difference in the generated markdown.
update_markdown(&test_path, &lookup, true)
update_markdown(&test_path, &lookup, true, None)
}
}
14 changes: 11 additions & 3 deletions src/registry/update_markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub struct RegistryUpdateMarkdownArgs {
/// Whether or not to run updates in dry-run mode.
#[arg(long, default_value = "false")]
pub dry_run: bool,

/// Optional path to the attribute registry.
/// If provided, all attributes will be linked here.
#[arg(long)]
pub attribute_regsitry_base_url: Option<String>,

Check warning on line 37 in src/registry/update_markdown.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"regsitry" should be "registry".
}

/// Update markdown files.
Expand Down Expand Up @@ -67,9 +72,12 @@ pub(crate) fn command(
})
{
log.info(&format!("{}: ${}", operation, entry.path().display()));
if let Err(error) =
update_markdown(&entry.path().display().to_string(), &registry, args.dry_run)
{
if let Err(error) = update_markdown(
&entry.path().display().to_string(),
&registry,
args.dry_run,
args.attribute_regsitry_base_url.as_deref(),

Check warning on line 79 in src/registry/update_markdown.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"regsitry" should be "registry".
) {
has_error = true;
log.error(&format!("{error}"));
}
Expand Down

0 comments on commit ff76f9c

Please sign in to comment.