forked from open-telemetry/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatalog.rs
More file actions
177 lines (162 loc) · 6.21 KB
/
catalog.rs
File metadata and controls
177 lines (162 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Catalog of attributes and other.
use std::collections::BTreeMap;
use crate::v2::attribute::{Attribute, AttributeRef};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// A catalog of indexed attributes shared across semconv groups, or signals.
/// Attribute references are used to refer to attributes in the catalog.
///
/// Note: This is meant to be a temporary datastructure used for creating
/// the registry.
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, Default)]
#[serde(deny_unknown_fields)]
#[must_use]
pub(crate) struct Catalog {
/// Catalog of attributes used in the schema.
#[serde(skip_serializing_if = "Vec::is_empty")]
attributes: Vec<Attribute>,
/// Lookup map to more efficiently find attributes.
lookup: BTreeMap<String, Vec<usize>>,
}
/// Collapses this catalog into the attribute list, preserving order.
impl From<Catalog> for Vec<Attribute> {
fn from(val: Catalog) -> Self {
val.attributes
}
}
impl Catalog {
/// Creates a catalog from a list of attributes.
pub(crate) fn from_attributes(mut attributes: Vec<Attribute>) -> Self {
attributes.sort_by(|a, b| a.key.cmp(&b.key));
let mut lookup: BTreeMap<String, Vec<usize>> = BTreeMap::new();
for (idx, attr) in attributes.iter().enumerate() {
lookup.entry(attr.key.clone()).or_default().push(idx);
}
Self { attributes, lookup }
}
/// Converts an attribute from V1 into an AttributeRef
/// on the current list of attributes in the order of this catalog.
#[must_use]
pub(crate) fn convert_ref(
&self,
attribute: &crate::attribute::Attribute,
) -> Option<AttributeRef> {
// Note - we do a fast lookup to contentious attributes,
// then linear scan of attributes with same key but different
// other aspects.
self.lookup.get(&attribute.name)?.iter().find_map(|idx| {
self.attributes
.get(*idx)
.filter(|a| {
a.key == attribute.name
&& a.r#type == attribute.r#type
&& a.examples == attribute.examples
&& a.common.brief == attribute.brief
&& a.common.note == attribute.note
&& a.common.deprecated == attribute.deprecated
&& attribute
.stability
.as_ref()
.map(|s| a.common.stability == *s)
.unwrap_or(false)
&& attribute
.annotations
.as_ref()
.map(|ans| a.common.annotations == *ans)
.unwrap_or(a.common.annotations.is_empty())
})
.map(|_| AttributeRef(*idx as u32))
})
}
}
/// Provides methods which can resolve an `AttributeRef` into an `Attribute`.
pub trait AttributeCatalog {
/// Returns the attribute from an attribute ref if it exists.
#[must_use]
fn attribute(&self, attribute_ref: &AttributeRef) -> Option<&Attribute>;
/// Returns the attribute name from an attribute ref if it exists
/// in the catalog or None if it does not exist.
#[must_use]
fn attribute_key(&self, attribute_ref: &AttributeRef) -> Option<&str> {
self.attribute(attribute_ref).map(|a| a.key.as_str())
}
}
impl AttributeCatalog for [Attribute] {
fn attribute(&self, attribute_ref: &AttributeRef) -> Option<&Attribute> {
self.get(attribute_ref.0 as usize)
}
}
impl AttributeCatalog for Vec<Attribute> {
fn attribute(&self, attribute_ref: &AttributeRef) -> Option<&Attribute> {
self.get(attribute_ref.0 as usize)
}
}
#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use weaver_semconv::attribute::{BasicRequirementLevelSpec, RequirementLevel};
use weaver_semconv::{attribute::AttributeType, stability::Stability};
use super::Catalog;
use crate::v2::attribute::Attribute;
use crate::v2::CommonFields;
#[test]
fn test_lookup_works() {
let key = "test.key".to_owned();
let atype = AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
);
let brief = "brief".to_owned();
let note = "note".to_owned();
let stability = Stability::Stable;
let annotations = BTreeMap::new();
let catalog = Catalog::from_attributes(vec![Attribute {
key: key.clone(),
r#type: atype.clone(),
examples: None,
common: CommonFields {
brief: brief.clone(),
note: note.clone(),
stability: stability.clone(),
deprecated: None,
annotations: annotations.clone(),
},
}]);
let result = catalog.convert_ref(&crate::attribute::Attribute {
name: key.clone(),
r#type: atype.clone(),
brief: brief.clone(),
examples: None,
tag: None,
requirement_level: RequirementLevel::Basic(BasicRequirementLevelSpec::Required),
sampling_relevant: Some(true),
note: note.clone(),
stability: Some(stability.clone()),
deprecated: None,
prefix: false,
tags: None,
annotations: Some(annotations.clone()),
value: None,
role: None,
});
assert!(result.is_some());
// Make sure "none" annotations is the same as empty annotations.
let result2 = catalog.convert_ref(&crate::attribute::Attribute {
name: key.clone(),
r#type: atype.clone(),
brief: brief.clone(),
examples: None,
tag: None,
requirement_level: RequirementLevel::Basic(BasicRequirementLevelSpec::Required),
sampling_relevant: Some(true),
note: note.clone(),
stability: Some(stability.clone()),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
});
assert!(result2.is_some());
}
}