Skip to content

Commit

Permalink
Merge branch 'main' into fossa
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuereth authored Feb 18, 2025
2 parents e67333b + 2a44418 commit 094dcef
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
86 changes: 86 additions & 0 deletions crates/weaver_semconv/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! A group specification.
use schemars::JsonSchema;
use std::collections::HashSet;
use std::fmt::{Display, Formatter};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -119,6 +120,9 @@ impl GroupSpec {
});
}

// Groups should only reference attributes once.
validate_duplicate_attribute_ref(&mut errors, &self.attributes, &self.id, path_or_url);

// All types, except metric and event, must have extends or attributes or both.
// For backward compatibility, if constraints are set, extends or attributes are not required.
if self.r#type != GroupType::Metric
Expand Down Expand Up @@ -348,6 +352,26 @@ impl GroupSpec {
}
}

fn validate_duplicate_attribute_ref(
errors: &mut Vec<Error>,
attributes: &[AttributeSpec],
group_id: &str,
path_or_url: &str,
) {
let mut seen = HashSet::new();
for a in attributes.iter() {
if let AttributeSpec::Ref { r#ref, .. } = a {
if !seen.insert(r#ref.to_owned()) {
errors.push(Error::InvalidGroupDuplicateAttributeRef {
path_or_url: path_or_url.to_owned(),
group_id: group_id.to_owned(),
attribute_ref: r#ref.to_owned(),
});
}
}
}
}

fn validate_any_value_examples(
errors: &mut Vec<Error>,
any_value: Option<&AnyValueSpec>,
Expand Down Expand Up @@ -1535,6 +1559,68 @@ mod tests {
.is_ok());
}

#[test]
fn test_validate_duplicate_attribute_ref() {
let bad_attributes = vec![
AttributeSpec::Ref {
r#ref: "attribute".to_owned(),
brief: None,
examples: None,
tag: None,
requirement_level: None,
sampling_relevant: None,
note: None,
stability: None,
deprecated: None,
prefix: false,
},
AttributeSpec::Ref {
r#ref: "attribute".to_owned(),
brief: None,
examples: None,
tag: None,
requirement_level: None,
sampling_relevant: None,
note: None,
stability: None,
deprecated: None,
prefix: false,
},
];
let mut group = GroupSpec {
id: "test".to_owned(),
r#type: GroupType::AttributeGroup,
brief: "test".to_owned(),
note: "test".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![],
constraints: vec![],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: None,
display_name: None,
body: None,
};

// Check group with duplicate attributes.
group.attributes = bad_attributes.clone();
let result = group.validate("<test>");
assert_eq!(
Err(Error::InvalidGroupDuplicateAttributeRef {
path_or_url: "<test>".to_owned(),
group_id: "test".to_owned(),
attribute_ref: "attribute".to_owned(),
}),
result.into_result_failing_non_fatal()
);
}

#[test]
fn test_instrumentation_spec() {
assert_eq!(Counter.to_string(), "counter");
Expand Down
12 changes: 12 additions & 0 deletions crates/weaver_semconv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ pub enum Error {
error: String,
},

/// The semantic convention spec contains a group with duplicate attribute references.
#[error("Duplicate attribute refs for '{attribute_ref}' found on group '{group_id}' detected while resolving '{path_or_url:?}'.")]
#[diagnostic(severity(Warning))]
InvalidGroupDuplicateAttributeRef {
/// The path or URL of the semantic convention asset.
path_or_url: String,
/// That path or URL of the semantic convention asset.
group_id: String,
/// The attribute being referenced twice.
attribute_ref: String,
},

/// The semantic convention spec contains an invalid group stability definition.
#[error("Invalid stability on group '{group_id}' detected while resolving '{path_or_url:?}'. {error}")]
#[diagnostic(severity(Warning))]
Expand Down

0 comments on commit 094dcef

Please sign in to comment.