forked from open-telemetry/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.rs
More file actions
371 lines (351 loc) · 14.6 KB
/
registry.rs
File metadata and controls
371 lines (351 loc) · 14.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: Apache-2.0
//! Registry used during the evaluation of the templates. References to the
//! catalog are resolved to the actual catalog entries to ease the template
//! evaluation.
use crate::error::Error;
use itertools::Itertools;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use weaver_resolved_schema::attribute::Attribute;
use weaver_resolved_schema::catalog::Catalog;
use weaver_resolved_schema::lineage::GroupLineage;
use weaver_resolved_schema::registry::{Group, Registry};
use weaver_semconv::any_value::AnyValueSpec;
use weaver_semconv::deprecated::Deprecated;
use weaver_semconv::group::{GroupType, InstrumentSpec, SpanKindSpec};
use weaver_semconv::stability::Stability;
use weaver_semconv::YamlValue;
/// A resolved semantic convention registry used in the context of the template and policy
/// engines.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ResolvedRegistry {
/// The semantic convention registry url.
#[serde(skip_serializing_if = "String::is_empty")]
pub registry_url: String,
/// A list of semantic convention groups.
pub groups: Vec<ResolvedGroup>,
}
/// Resolved group specification used in the context of the template engine.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct ResolvedGroup {
/// The id that uniquely identifies the semantic convention.
pub id: String,
/// The type of the group including the specific fields for each type.
pub r#type: GroupType,
/// A brief description of the semantic convention.
#[serde(skip_serializing_if = "String::is_empty")]
pub brief: String,
/// A more elaborate description of the semantic convention.
/// It defaults to an empty string.
#[serde(default)]
#[serde(skip_serializing_if = "String::is_empty")]
pub note: String,
/// Prefix for the attributes for this semantic convention.
/// It defaults to an empty string.
#[serde(default)]
#[serde(skip_serializing_if = "String::is_empty")]
pub prefix: String,
/// Reference another semantic convention id. It inherits
/// all attributes defined in the specified semantic
/// convention.
#[serde(skip_serializing_if = "Option::is_none")]
pub extends: Option<String>,
/// Specifies the stability of the semantic convention.
/// Note that, if stability is missing but deprecated is present, it will
/// automatically set the stability to deprecated. If deprecated is
/// present and stability differs from deprecated, this will result in an
/// error.
#[serde(skip_serializing_if = "Option::is_none")]
pub stability: Option<Stability>,
/// Specifies if the semantic convention is deprecated. The string
/// provided as `description` MUST specify why it's deprecated and/or what
/// to use instead. See also stability.
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecated: Option<Deprecated>,
/// List of attributes that belong to the semantic convention.
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub attributes: Vec<Attribute>,
/// Specifies the kind of the span.
/// Note: only valid if type is span
#[serde(skip_serializing_if = "Option::is_none")]
pub span_kind: Option<SpanKindSpec>,
/// List of strings that specify the ids of event semantic conventions
/// associated with this span semantic convention.
/// Note: only valid if type is span
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub events: Vec<String>,
/// The metric name as described by the [OpenTelemetry Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#timeseries-model).
/// Note: This field is required if type is metric.
#[serde(skip_serializing_if = "Option::is_none")]
pub metric_name: Option<String>,
/// The instrument type that should be used to record the metric. Note that
/// the semantic conventions must be written using the names of the
/// synchronous instrument types (counter, gauge, updowncounter and
/// histogram).
/// For more details: [Metrics semantic conventions - Instrument types](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/metrics/semantic_conventions#instrument-types).
/// Note: This field is required if type is metric.
#[serde(skip_serializing_if = "Option::is_none")]
pub instrument: Option<InstrumentSpec>,
/// The unit in which the metric is measured, which should adhere to the
/// [guidelines](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/metrics/semantic_conventions#instrument-units).
/// Note: This field is required if type is metric.
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>,
/// The name of the event. If not specified, the prefix is used.
/// If prefix is empty (or unspecified), name is required.
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// The lineage of the group.
#[serde(skip_serializing_if = "Option::is_none")]
pub lineage: Option<GroupLineage>,
/// The readable name for attribute groups used when generating registry tables.
#[serde(skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
/// The body specification used for event semantic conventions.
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<AnyValueSpec>,
/// The associated entities of this group.
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub entity_associations: Vec<String>,
/// Annotations for the group.
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<BTreeMap<String, YamlValue>>,
}
impl ResolvedGroup {
/// Constructs a Template-friendly groups structure from resolved registry structures.
pub fn try_from_resolved(group: &Group, catalog: &Catalog) -> Result<Self, Error> {
let mut errors = Vec::new();
let id = group.id.clone();
let group_type = group.r#type.clone();
let brief = group.brief.clone();
let note = group.note.clone();
let prefix = group.prefix.clone();
let extends = group.extends.clone();
let stability = group.stability.clone();
let deprecated = group.deprecated.clone();
let attributes = group
.attributes
.iter()
.filter_map(|attr_ref| {
let attr = catalog.attribute(attr_ref).cloned();
if attr.is_none() {
errors.push(Error::AttributeNotFound {
group_id: id.clone(),
attr_ref: *attr_ref,
});
}
attr
})
.collect();
let lineage = group.lineage.clone();
if !errors.is_empty() {
return Err(Error::CompoundError(errors));
}
Ok(ResolvedGroup {
id,
r#type: group_type,
brief,
note,
prefix,
extends,
stability,
deprecated,
attributes,
span_kind: group.span_kind.clone(),
events: group.events.clone(),
metric_name: group.metric_name.clone(),
instrument: group.instrument.clone(),
unit: group.unit.clone(),
name: group.name.clone(),
lineage,
display_name: group.display_name.clone(),
body: group.body.clone(),
entity_associations: group.entity_associations.clone(),
annotations: group.annotations.clone(),
})
}
}
impl ResolvedRegistry {
/// Create a new template registry from a resolved registry.
pub fn try_from_resolved_registry(
registry: &Registry,
catalog: &Catalog,
) -> Result<Self, Error> {
let mut errors = Vec::new();
let groups = registry
.groups
.iter()
.map(|group| {
let id = group.id.clone();
let group_type = group.r#type.clone();
let brief = group.brief.clone();
let note = group.note.clone();
let prefix = group.prefix.clone();
let extends = group.extends.clone();
let stability = group.stability.clone();
let deprecated = group.deprecated.clone();
let attributes = group
.attributes
.iter()
.filter_map(|attr_ref| {
let attr = catalog.attribute(attr_ref).cloned();
if attr.is_none() {
errors.push(Error::AttributeNotFound {
group_id: id.clone(),
attr_ref: *attr_ref,
});
}
attr
})
.collect();
let lineage = group.lineage.clone();
ResolvedGroup {
id,
r#type: group_type,
brief,
note,
prefix,
extends,
stability,
deprecated,
attributes,
span_kind: group.span_kind.clone(),
events: group.events.clone(),
metric_name: group.metric_name.clone(),
instrument: group.instrument.clone(),
unit: group.unit.clone(),
name: group.name.clone(),
lineage,
display_name: group.display_name.clone(),
body: group.body.clone(),
entity_associations: group.entity_associations.clone(),
annotations: group.annotations.clone(),
}
})
.sorted_by(|a, b| a.id.cmp(&b.id))
.collect();
if !errors.is_empty() {
return Err(Error::CompoundError(errors));
}
Ok(Self {
registry_url: registry.registry_url.clone(),
groups,
})
}
}
#[cfg(test)]
mod tests {
use crate::ResolvedRegistry;
use schemars::schema_for;
use serde_json::to_string_pretty;
use weaver_resolved_schema::catalog::Catalog;
use weaver_resolved_schema::registry::{Group, Registry};
use weaver_semconv::group::GroupType;
#[test]
fn test_json_schema_gen() {
// Ensure the JSON schema can be generated for the TemplateRegistry
let schema = schema_for!(ResolvedRegistry);
// Ensure the schema can be serialized to a string
assert!(to_string_pretty(&schema).is_ok());
}
#[test]
fn test_groups_sorted_deterministically() {
// Create a registry with groups in non-alphabetical order
let registry = Registry {
registry_url: "test".to_owned(),
groups: vec![
Group {
id: "zebra.group".to_owned(),
r#type: GroupType::AttributeGroup,
brief: "Zebra group".to_owned(),
note: String::new(),
prefix: String::new(),
extends: None,
stability: None,
deprecated: None,
attributes: vec![],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: None,
lineage: None,
display_name: None,
body: None,
entity_associations: vec![],
annotations: None,
visibility: None,
is_v2: false,
},
Group {
id: "apple.group".to_owned(),
r#type: GroupType::AttributeGroup,
brief: "Apple group".to_owned(),
note: String::new(),
prefix: String::new(),
extends: None,
stability: None,
deprecated: None,
attributes: vec![],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: None,
lineage: None,
display_name: None,
body: None,
entity_associations: vec![],
annotations: None,
visibility: None,
is_v2: false,
},
Group {
id: "middle.group".to_owned(),
r#type: GroupType::AttributeGroup,
brief: "Middle group".to_owned(),
note: String::new(),
prefix: String::new(),
extends: None,
stability: None,
deprecated: None,
attributes: vec![],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: None,
lineage: None,
display_name: None,
body: None,
entity_associations: vec![],
annotations: None,
visibility: None,
is_v2: false,
},
],
};
let catalog = Catalog::default();
// Convert to resolved registry
let resolved = ResolvedRegistry::try_from_resolved_registry(®istry, &catalog)
.expect("Failed to create resolved registry");
// Verify groups are sorted alphabetically by id
assert_eq!(resolved.groups.len(), 3);
assert_eq!(resolved.groups[0].id, "apple.group");
assert_eq!(resolved.groups[1].id, "middle.group");
assert_eq!(resolved.groups[2].id, "zebra.group");
// Verify the sorting is stable across multiple conversions
let resolved2 = ResolvedRegistry::try_from_resolved_registry(®istry, &catalog)
.expect("Failed to create resolved registry");
assert_eq!(resolved.groups, resolved2.groups);
}
}