Skip to content
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

feat: Add support for Maps map[] to the definition of an AnyValue #396

Merged
merged 4 commits into from
Oct 4, 2024
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [Next] - YYYY-MM-DD

What's changed

* Add support for Maps `map[]` to the definition of an `AnyValue`. ([#396](https://github.com/open-telemetry/weaver/pull/396) by @MSNev).

## [0.10.0] - 2024-09-23

What's changed
Expand Down
2 changes: 1 addition & 1 deletion crates/weaver_semconv/data/expected/any_value.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"body":{"type":"map","id":"id","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"enum","id":"id_enum","brief":"brief","note":"note","requirement_level":"optional","members":[{"id":"id","value":42,"brief":"brief","note":"note","stability":null,"deprecated":null}]},{"type":"map","id":"id_map","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"int","id":"id_int","brief":"brief","note":"note","requirement_level":"required"},{"type":"bytes","id":"id_bytes","brief":"brief","note":"note","requirement_level":"required"},{"type":"string","id":"id_string","brief":"brief","note":"note","requirement_level":"optional"},{"type":"boolean","id":"id_bool","brief":"brief","note":"note","requirement_level":"optional"},{"type":"map","id":"id_nested_map","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"int[]","id":"id_nested_int","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double[]","id":"id_nested_bytes","brief":"brief","note":"note","requirement_level":"optional"},{"type":"string[]","id":"id_nested_string","brief":"brief","note":"note","requirement_level":"optional"},{"type":"boolean[]","id":"id_nested_bool","brief":"brief","note":"note","requirement_level":"optional"}]}]},{"type":"int","id":"id_int","brief":"brief","note":"note","requirement_level":"optional"},{"type":"bytes","id":"id_bytes","brief":"brief","note":"note","requirement_level":"optional"},{"type":"string","id":"id_string","brief":"brief","note":"note","requirement_level":"recommended"},{"type":"boolean","id":"id_bool","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double","id":"id_double","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double[]","id":"id_doubles","brief":"brief","note":"note","requirement_level":"optional"}]}}
{"body":{"type":"map","id":"id","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"enum","id":"id_enum","brief":"brief","note":"note","requirement_level":"optional","members":[{"id":"id","value":42,"brief":"brief","note":"note","stability":null,"deprecated":null}]},{"type":"map","id":"id_map","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"int","id":"id_int","brief":"brief","note":"note","requirement_level":"required"},{"type":"bytes","id":"id_bytes","brief":"brief","note":"note","requirement_level":"required"},{"type":"string","id":"id_string","brief":"brief","note":"note","requirement_level":"optional"},{"type":"boolean","id":"id_bool","brief":"brief","note":"note","requirement_level":"optional"},{"type":"map","id":"id_nested_map","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"int[]","id":"id_nested_int","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double[]","id":"id_nested_bytes","brief":"brief","note":"note","requirement_level":"optional"},{"type":"string[]","id":"id_nested_string","brief":"brief","note":"note","requirement_level":"optional"},{"type":"boolean[]","id":"id_nested_bool","brief":"brief","note":"note","requirement_level":"optional"}]},{"type":"map[]","id":"id_nested_map_array","brief":"brief","note":"note","requirement_level":"optional","fields":[{"type":"int[]","id":"id_nested_int","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double[]","id":"id_nested_bytes","brief":"brief","note":"note","requirement_level":"optional"},{"type":"string[]","id":"id_nested_string","brief":"brief","note":"note","requirement_level":"optional"},{"type":"boolean[]","id":"id_nested_bool","brief":"brief","note":"note","requirement_level":"optional"}]}]},{"type":"int","id":"id_int","brief":"brief","note":"note","requirement_level":"optional"},{"type":"bytes","id":"id_bytes","brief":"brief","note":"note","requirement_level":"optional"},{"type":"string","id":"id_string","brief":"brief","note":"note","requirement_level":"recommended"},{"type":"boolean","id":"id_bool","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double","id":"id_double","brief":"brief","note":"note","requirement_level":"optional"},{"type":"double[]","id":"id_doubles","brief":"brief","note":"note","requirement_level":"optional"}]}}
26 changes: 26 additions & 0 deletions crates/weaver_semconv/data/expected/any_value.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ body:
brief: brief
note: note
requirement_level: optional
- type: map[]
id: id_nested_map_array
brief: brief
note: note
requirement_level: optional
fields:
- type: int[]
id: id_nested_int
brief: brief
note: note
requirement_level: optional
- type: double[]
id: id_nested_bytes
brief: brief
note: note
requirement_level: optional
- type: string[]
id: id_nested_string
brief: brief
note: note
requirement_level: optional
- type: boolean[]
id: id_nested_bool
brief: brief
note: note
requirement_level: optional
- type: int
id: id_int
brief: brief
Expand Down
93 changes: 93 additions & 0 deletions crates/weaver_semconv/src/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ pub enum AnyValueSpec {
fields: Vec<AnyValueSpec>,
},

/// The value type is a map of key, value pairs
#[serde(rename = "map[]")]
Maps {
/// The common value specification
#[serde(flatten)]
common: AnyValueCommonSpec,
/// The collection of key, values where the value is an `AnyValueSpec`
fields: Vec<AnyValueSpec>,
},

/// The value type will just be a bytes.
Bytes {
/// The common value specification
Expand Down Expand Up @@ -161,6 +171,7 @@ impl Display for AnyValueSpec {
AnyValueSpec::Ints { .. } => write!(f, "int[]"),
AnyValueSpec::Doubles { .. } => write!(f, "double[]"),
AnyValueSpec::Booleans { .. } => write!(f, "boolean[]"),
AnyValueSpec::Maps { .. } => write!(f, "map[]"),
AnyValueSpec::Bytes { .. } => write!(f, "byte[]"),
AnyValueSpec::Undefined { .. } => write!(f, "undefined"),
AnyValueSpec::Enum { members, .. } => {
Expand Down Expand Up @@ -189,6 +200,7 @@ impl AnyValueSpec {
AnyValueSpec::Doubles { common, .. } => common,
AnyValueSpec::Booleans { common, .. } => common,
AnyValueSpec::Map { common, .. } => common,
AnyValueSpec::Maps { common, .. } => common,
AnyValueSpec::Bytes { common, .. } => common,
AnyValueSpec::Undefined { common, .. } => common,
AnyValueSpec::Enum { common, .. } => common,
Expand Down Expand Up @@ -396,6 +408,68 @@ mod tests {
},
],
},
AnyValueSpec::Maps {
common: AnyValueCommonSpec {
id: "id_nested_map_array".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional,
),
},
fields: vec![
AnyValueSpec::Ints {
common: AnyValueCommonSpec {
id: "id_nested_int".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional,
),
},
},
AnyValueSpec::Doubles {
common: AnyValueCommonSpec {
id: "id_nested_bytes".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional,
),
},
},
AnyValueSpec::Strings {
common: AnyValueCommonSpec {
id: "id_nested_string".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional,
),
},
},
AnyValueSpec::Booleans {
common: AnyValueCommonSpec {
id: "id_nested_bool".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional,
),
},
},
],
},
],
},
AnyValueSpec::Int {
Expand Down Expand Up @@ -659,6 +733,25 @@ mod tests {
),
"byte[]"
);
assert_eq!(
format!(
"{}",
AnyValueSpec::Maps {
common: AnyValueCommonSpec {
id: "id".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(
BasicRequirementLevelSpec::Optional
),
},
fields: vec![]
}
),
"map[]"
);
assert_eq!(
format!(
"{}",
Expand Down
5 changes: 4 additions & 1 deletion crates/weaver_semconv/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,19 @@ impl Examples {
| (Examples::Double(_), AnyValueSpec::Double { .. })
| (Examples::String(_), AnyValueSpec::String { .. })
| (Examples::String(_), AnyValueSpec::Map { .. })
| (Examples::String(_), AnyValueSpec::Maps { .. })
| (Examples::Ints(_), AnyValueSpec::Int { .. })
| (Examples::Doubles(_), AnyValueSpec::Double { .. })
| (Examples::Bools(_), AnyValueSpec::Boolean { .. })
| (Examples::Strings(_), AnyValueSpec::String { .. })
| (Examples::Strings(_), AnyValueSpec::Map { .. })
| (Examples::Strings(_), AnyValueSpec::Maps { .. })
| (Examples::ListOfInts(_), AnyValueSpec::Ints { .. })
| (Examples::ListOfDoubles(_), AnyValueSpec::Doubles { .. })
| (Examples::ListOfBools(_), AnyValueSpec::Booleans { .. })
| (Examples::ListOfStrings(_), AnyValueSpec::Strings { .. })
| (Examples::ListOfStrings(_), AnyValueSpec::Map { .. }) => WResult::Ok(()),
(Examples::ListOfStrings(_), AnyValueSpec::Maps { .. }) => WResult::Ok(()),
(_, AnyValueSpec::Enum { .. })
| (_, AnyValueSpec::Bytes { .. })
| (_, AnyValueSpec::Undefined { .. }) => {
Expand All @@ -535,7 +538,7 @@ impl Examples {
error: format!("All examples SHOULD be of type `{}`", any_value),
}],
),
(_, AnyValueSpec::Map { .. }) => WResult::OkWithNFEs(
(_, AnyValueSpec::Maps { .. }) | (_, AnyValueSpec::Map { .. }) => WResult::OkWithNFEs(
(),
vec![Error::InvalidAnyValueExampleError {
path_or_url: path_or_url.to_owned(),
Expand Down
29 changes: 28 additions & 1 deletion crates/weaver_semconv/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ mod tests {
result
);

// Examples are not required for Maps.
// Examples are not required for Map.
group.body = Some(AnyValueSpec::Map {
common: AnyValueCommonSpec {
id: "map_id".to_owned(),
Expand All @@ -699,6 +699,33 @@ mod tests {
}],
});

assert!(group
.validate("<test>")
.into_result_failing_non_fatal()
.is_ok());

// Examples are not required for Map[].
group.body = Some(AnyValueSpec::Maps {
common: AnyValueCommonSpec {
id: "map_id".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: None,
requirement_level: RequirementLevel::Basic(BasicRequirementLevelSpec::Optional),
},
fields: vec![AnyValueSpec::String {
common: AnyValueCommonSpec {
id: "string_id".to_owned(),
brief: "brief".to_owned(),
note: "note".to_owned(),
stability: None,
examples: Some(Examples::String("test".to_owned())),
requirement_level: RequirementLevel::Basic(BasicRequirementLevelSpec::Optional),
},
}],
});

assert!(group
.validate("<test>")
.into_result_failing_non_fatal()
Expand Down
Loading