forked from open-telemetry/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
1268 lines (1225 loc) · 49.5 KB
/
mod.rs
File metadata and controls
1268 lines (1225 loc) · 49.5 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Version 2 of semantic convention schema.
use std::collections::{HashMap, HashSet};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use weaver_semconv::{
deprecated::Deprecated,
group::GroupType,
schema_url::SchemaUrl,
v2::{
attribute_group::AttributeGroupVisibilitySpec, signal_id::SignalId, span::SpanName,
CommonFields,
},
};
use weaver_version::v2::{RegistryChanges, SchemaChanges, SchemaItemChange};
use crate::{
v2::{
attribute::Attribute,
attribute_group::AttributeGroup,
catalog::{AttributeCatalog, Catalog},
entity::Entity,
metric::Metric,
refinements::Refinements,
registry::Registry,
span::{Span, SpanRefinement},
stats::Stats,
},
V2_RESOLVED_FILE_FORMAT,
};
pub mod attribute;
pub mod attribute_group;
pub mod catalog;
pub mod entity;
pub mod event;
pub mod metric;
pub mod refinements;
pub mod registry;
pub mod span;
pub mod stats;
/// A Resolved Telemetry Schema.
/// A Resolved Telemetry Schema is self-contained and doesn't contain any
/// external references to other schemas or semantic conventions.
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ResolvedTelemetrySchema {
/// Version of the file structure.
pub file_format: String,
/// Schema URL that this file is published at.
pub schema_url: SchemaUrl,
/// Catalog of attributes. Note: this will include duplicates for the same key.
pub attribute_catalog: Vec<Attribute>,
/// The registry that this schema belongs to.
pub registry: Registry,
/// Refinements for the registry
pub refinements: Refinements,
}
impl ResolvedTelemetrySchema {
/// Statistics about this schema.
pub fn stats(&self) -> Stats {
Stats {
registry: self.registry.stats(&self.attribute_catalog),
refinements: self.refinements.stats(),
}
}
/// Generate a diff between the current schema (must be the most recent one)
/// and a baseline schema.
#[must_use]
pub fn diff(&self, baseline_schema: &ResolvedTelemetrySchema) -> SchemaChanges {
// TODO - get manifests
SchemaChanges {
registry: self.registry_diff(baseline_schema),
}
}
#[must_use]
fn registry_diff(&self, baseline_schema: &ResolvedTelemetrySchema) -> RegistryChanges {
RegistryChanges {
attribute_changes: self.registry_attribute_diff(baseline_schema),
attribute_group_changes: diff_signals(
&self.registry.attribute_groups,
&baseline_schema.registry.attribute_groups,
),
entity_changes: diff_signals(
&self.registry.entities,
&baseline_schema.registry.entities,
),
event_changes: diff_signals(&self.registry.events, &baseline_schema.registry.events),
metric_changes: diff_signals(&self.registry.metrics, &baseline_schema.registry.metrics),
span_changes: diff_signals(&self.registry.spans, &baseline_schema.registry.spans),
}
}
#[must_use]
fn registry_attribute_diff(
&self,
baseline_schema: &ResolvedTelemetrySchema,
) -> Vec<SchemaItemChange> {
let latest_attributes = self.registry_attribute_map();
let baseline_attributes = baseline_schema.registry_attribute_map();
diff_signals_by_hash(&latest_attributes, &baseline_attributes)
}
/// Get the registry attributes of the resolved telemetry schema in a fast lookup map.
fn registry_attribute_map(&self) -> HashMap<&str, &Attribute> {
self.registry
.attributes
.iter()
.filter_map(|r| self.attribute_catalog.attribute(r))
.map(|a| (a.key.as_str(), a))
.collect()
}
}
/// Easy conversion from v1 to v2.
impl TryFrom<crate::ResolvedTelemetrySchema> for ResolvedTelemetrySchema {
type Error = crate::error::Error;
fn try_from(value: crate::ResolvedTelemetrySchema) -> Result<Self, Self::Error> {
let (attribute_catalog, registry, refinements) =
convert_v1_to_v2(value.catalog, value.registry)?;
let schema_url_str = value.schema_url.clone();
let schema_url: SchemaUrl =
value
.schema_url
.try_into()
.map_err(|e| crate::error::Error::InvalidSchemaUrl {
url: schema_url_str,
error: e,
})?;
Ok(ResolvedTelemetrySchema {
file_format: V2_RESOLVED_FILE_FORMAT.to_owned(),
schema_url,
attribute_catalog,
registry,
refinements,
})
}
}
fn fix_group_id(prefix: &'static str, group_id: &str) -> SignalId {
if group_id.starts_with(prefix) {
group_id.trim_start_matches(prefix).to_owned().into()
} else {
group_id.to_owned().into()
}
}
fn fix_span_group_id(group_id: &str) -> SignalId {
fix_group_id("span.", group_id)
}
/// Converts a V1 registry + catalog to V2.
pub fn convert_v1_to_v2(
c: crate::catalog::Catalog,
r: crate::registry::Registry,
) -> Result<(Vec<Attribute>, Registry, Refinements), crate::error::Error> {
// When pulling attributes, as we collapse things, we need to filter
// to just unique.
let attributes: HashSet<Attribute> = c
.attributes()
.cloned()
.map(|a| Attribute {
key: a.name,
r#type: a.r#type,
examples: a.examples,
common: CommonFields {
brief: a.brief,
note: a.note,
stability: a
.stability
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: a.deprecated,
annotations: a.annotations.unwrap_or_default(),
},
})
.collect();
let v2_catalog = Catalog::from_attributes(attributes.into_iter().collect());
// Create a lookup so we can check inheritance.
let mut group_type_lookup = HashMap::new();
for g in r.groups.iter() {
let _ = group_type_lookup.insert(g.id.clone(), g.r#type.clone());
}
// Pull signals from the registry and create a new span-focused registry.
let mut spans = Vec::new();
let mut span_refinements = Vec::new();
let mut metrics = Vec::new();
let mut metric_refinements = Vec::new();
let mut events = Vec::new();
let mut event_refinements = Vec::new();
let mut entities = Vec::new();
let mut attribute_groups = Vec::new();
for g in r.groups.iter() {
match g.r#type {
GroupType::Span => {
// Check if we extend another span.
let is_refinement = g
.lineage
.as_ref()
.and_then(|l| l.extends_group.as_ref())
.and_then(|parent| group_type_lookup.get(parent))
.map(|t| *t == GroupType::Span)
.unwrap_or(false);
// Pull all the attribute references.
let mut span_attributes = Vec::new();
for attr in g.attributes.iter().filter_map(|a| c.attribute(a)) {
if let Some(a) = v2_catalog.convert_ref(attr) {
span_attributes.push(span::SpanAttributeRef {
base: a,
requirement_level: attr.requirement_level.clone(),
sampling_relevant: attr.sampling_relevant,
});
} else {
// TODO logic error!
log::info!("Logic failure - unable to convert attribute {attr:?}");
}
}
if !is_refinement {
let span = Span {
r#type: fix_span_group_id(&g.id),
kind: g
.span_kind
.clone()
.unwrap_or(weaver_semconv::group::SpanKindSpec::Internal),
// TODO - Pass advanced name controls through V1 groups.
name: SpanName {
note: g.name.clone().unwrap_or_default(),
},
entity_associations: g.entity_associations.clone(),
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
attributes: span_attributes,
};
spans.push(span.clone());
span_refinements.push(SpanRefinement {
id: span.r#type.clone(),
span,
});
} else {
// unwrap should be safe because we verified this is a refinement earlier.
let span_type = g
.lineage
.as_ref()
.and_then(|l| l.extends_group.as_ref())
.map(|id| fix_span_group_id(id))
.expect("Refinement extraction issue - this is a logic bug");
span_refinements.push(SpanRefinement {
id: fix_span_group_id(&g.id),
span: Span {
r#type: span_type,
kind: g
.span_kind
.clone()
.unwrap_or(weaver_semconv::group::SpanKindSpec::Internal),
// TODO - Pass advanced name controls through V1 groups.
name: SpanName {
note: g.name.clone().unwrap_or_default(),
},
entity_associations: g.entity_associations.clone(),
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
attributes: span_attributes,
},
});
}
}
GroupType::Event => {
let is_refinement = g
.lineage
.as_ref()
.and_then(|l| l.extends_group.as_ref())
.and_then(|parent| group_type_lookup.get(parent))
.map(|t| *t == GroupType::Event)
.unwrap_or(false);
let mut event_attributes = Vec::new();
for attr in g.attributes.iter().filter_map(|a| c.attribute(a)) {
if let Some(a) = v2_catalog.convert_ref(attr) {
event_attributes.push(event::EventAttributeRef {
base: a,
requirement_level: attr.requirement_level.clone(),
});
} else {
// TODO logic error!
log::info!("Logic failure - unable to convert attribute {attr:?}");
}
}
// We cannot convert older repositories before event name was required.
if let Some(name) = g.name.clone() {
let event = event::Event {
name: name.into(),
attributes: event_attributes,
entity_associations: g.entity_associations.clone(),
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
};
if !is_refinement {
events.push(event.clone());
event_refinements.push(event::EventRefinement {
id: event.name.clone(),
event,
});
} else {
event_refinements.push(event::EventRefinement {
id: fix_group_id("event.", &g.id),
event,
});
}
} else {
// We have no event name
return Err(crate::error::Error::EventNameNotFound {
group_id: g.id.clone(),
});
}
}
GroupType::Metric => {
// Check if we extend another metric.
let is_refinement = g
.lineage
.as_ref()
.and_then(|l| l.extends_group.as_ref())
.and_then(|parent| group_type_lookup.get(parent))
.map(|t| *t == GroupType::Metric)
.unwrap_or(false);
let mut metric_attributes = Vec::new();
for attr in g.attributes.iter().filter_map(|a| c.attribute(a)) {
if let Some(a) = v2_catalog.convert_ref(attr) {
metric_attributes.push(metric::MetricAttributeRef {
base: a,
requirement_level: attr.requirement_level.clone(),
});
} else {
// TODO logic error!
log::info!("Logic failure - unable to convert attribute {attr:?}");
}
}
// TODO - deal with unwrap errors.
let metric = Metric {
name: g
.metric_name
.clone()
.expect("metric_name must exist on metrics prior to translation to v2")
.into(),
instrument: g
.instrument
.clone()
.expect("instrument must exist on metrics prior to translation to v2"),
unit: g
.unit
.clone()
.expect("unit must exist on metrics prior to translation to v2"),
attributes: metric_attributes,
entity_associations: g.entity_associations.clone(),
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
};
if is_refinement {
metric_refinements.push(metric::MetricRefinement {
id: fix_group_id("metric.", &g.id),
metric,
});
} else {
metrics.push(metric.clone());
metric_refinements.push(metric::MetricRefinement {
id: metric.name.clone(),
metric,
});
}
}
GroupType::Entity => {
let mut id_attrs = Vec::new();
let mut desc_attrs = Vec::new();
for attr in g.attributes.iter().filter_map(|a| c.attribute(a)) {
if let Some(a) = v2_catalog.convert_ref(attr) {
match attr.role {
Some(weaver_semconv::attribute::AttributeRole::Identifying) => {
id_attrs.push(entity::EntityAttributeRef {
base: a,
requirement_level: attr.requirement_level.clone(),
});
}
_ => {
desc_attrs.push(entity::EntityAttributeRef {
base: a,
requirement_level: attr.requirement_level.clone(),
});
}
}
} else {
// TODO logic error!
}
}
entities.push(Entity {
r#type: fix_group_id("entity.", &g.id),
identity: id_attrs,
description: desc_attrs,
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
});
}
GroupType::AttributeGroup => {
if g.visibility
.as_ref()
.is_some_and(|v| AttributeGroupVisibilitySpec::Public == *v)
{
// Now we need to convert the group.
let mut attributes = Vec::new();
// TODO - we need to check lineage and remove parent groups.
for attr in g.attributes.iter().filter_map(|a| c.attribute(a)) {
if let Some(a) = v2_catalog.convert_ref(attr) {
attributes.push(a);
} else {
// TODO logic error!
}
}
attribute_groups.push(AttributeGroup {
id: fix_group_id("attribute_group.", &g.id),
attributes,
common: CommonFields {
brief: g.brief.clone(),
note: g.note.clone(),
stability: g
.stability
.clone()
.unwrap_or(weaver_semconv::stability::Stability::Alpha),
deprecated: g.deprecated.clone(),
annotations: g.annotations.clone().unwrap_or_default(),
},
});
}
}
GroupType::MetricGroup | GroupType::Scope | GroupType::Undefined => {
// Ignored for now, we should probably issue warnings.
}
}
}
// Now we need to hunt for attribute definitions
let mut attributes = Vec::new();
for g in r.groups.iter() {
for a in g.attributes.iter() {
if let Some(attr) = c.attribute(a) {
// Attribute definitions do not have lineage.
let is_def = g
.lineage
.as_ref()
.and_then(|l| l.attribute(&attr.name))
.is_none();
if is_def {
if let Some(v2) = v2_catalog.convert_ref(attr) {
attributes.push(v2);
} else {
// TODO logic error!
}
}
}
}
}
attributes.sort_by(|a, b| a.0.cmp(&b.0));
attributes.dedup();
let v2_registry = Registry {
attributes,
spans,
metrics,
events,
entities,
attribute_groups,
};
let v2_refinements = Refinements {
spans: span_refinements,
metrics: metric_refinements,
events: event_refinements,
};
Ok((v2_catalog.into(), v2_registry, v2_refinements))
}
/// A trait that defines a signal, used for performing "diff"
pub trait Signal {
/// The id of the signal.
fn id(&self) -> &str;
/// The common fields for the signal.
fn common(&self) -> &CommonFields;
}
/// Diffs signal registries.
#[must_use]
fn diff_signals<T: Signal>(latest: &[T], baseline: &[T]) -> Vec<SchemaItemChange> {
let baseline_signals: HashMap<&str, &T> = baseline.iter().map(|s| (s.id(), s)).collect();
let latest_signals: HashMap<&str, &T> = latest.iter().map(|s| (s.id(), s)).collect();
diff_signals_by_hash(&latest_signals, &baseline_signals)
}
/// Finds the difference between two signal registries using a hash into the signal id.
fn diff_signals_by_hash<T: Signal>(
latest: &HashMap<&str, &T>,
baseline: &HashMap<&str, &T>,
) -> Vec<SchemaItemChange> {
let mut changes: Vec<SchemaItemChange> = Vec::new();
for (&signal_id, latest_signal) in latest.iter() {
let baseline_signal = baseline.get(signal_id);
if let Some(baseline_signal) = baseline_signal {
if let Some(deprecated) = latest_signal.common().deprecated.as_ref() {
// is this a change from the baseline?
if let Some(baseline_deprecated) = baseline_signal.common().deprecated.as_ref() {
if deprecated == baseline_deprecated {
continue;
}
}
match deprecated {
Deprecated::Renamed {
renamed_to: rename_to,
note,
} => {
changes.push(SchemaItemChange::Renamed {
old_name: signal_id.to_owned(),
new_name: rename_to.clone(),
note: note.clone(),
});
}
Deprecated::Obsoleted { note } => {
changes.push(SchemaItemChange::Obsoleted {
name: signal_id.to_owned(),
note: note.clone(),
});
}
Deprecated::Unspecified { note } | Deprecated::Uncategorized { note } => {
changes.push(SchemaItemChange::Uncategorized {
name: signal_id.to_owned(),
note: note.clone(),
});
}
}
}
} else {
changes.push(SchemaItemChange::Added {
name: signal_id.to_owned(),
});
}
}
// Any signal in the baseline schema that is not present in the latest schema
// is considered removed.
// Note: This should never occur if the registry evolution process is followed.
// However, detecting this case is useful for identifying a violation of the process.
for (signal_name, _) in baseline.iter() {
if !latest.contains_key(signal_name) {
changes.push(SchemaItemChange::Removed {
name: (*signal_name).to_owned(),
});
}
}
changes
}
#[cfg(test)]
mod tests {
use crate::v2::attribute::{Attribute as AttributeV2, AttributeRef};
use crate::v2::event::Event;
use crate::V1_RESOLVED_FILE_FORMAT;
use crate::{attribute::Attribute, lineage::GroupLineage, registry::Group};
use weaver_semconv::{provenance::Provenance, stability::Stability};
use crate::lineage::AttributeLineage;
use super::*;
#[test]
fn test_convert_span_v1_to_v2() {
let mut builder = crate::catalog::test_utils::CatalogBuilder::default();
let ref0 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Required,
),
sampling_relevant: None,
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
},
None,
);
let ref1 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Recommended,
),
sampling_relevant: Some(true),
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
},
None,
);
let test_refs = [ref0, ref1];
let v1_catalog = builder.build();
let mut refinement_span_lineage = GroupLineage::new(Provenance::new("tmp", "tmp"));
refinement_span_lineage.extends("span.my-span");
refinement_span_lineage
.add_attribute_lineage("test.key".to_owned(), AttributeLineage::new("span.my-span"));
let v1_registry = crate::registry::Registry {
registry_url: "my.schema.url".to_owned(),
groups: vec![
Group {
id: "span.my-span".to_owned(),
r#type: GroupType::Span,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[1]],
span_kind: Some(weaver_semconv::group::SpanKindSpec::Client),
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: Some("my span name".to_owned()),
lineage: None,
display_name: None,
body: None,
annotations: None,
entity_associations: vec![],
visibility: None,
is_v2: false,
},
Group {
id: "span.custom".to_owned(),
r#type: GroupType::Span,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[1]],
span_kind: Some(weaver_semconv::group::SpanKindSpec::Client),
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: Some("my span name".to_owned()),
lineage: Some(refinement_span_lineage),
display_name: None,
body: None,
annotations: None,
entity_associations: vec![],
visibility: None,
is_v2: false,
},
],
};
let (catalog, v2_registry, v2_refinements) =
convert_v1_to_v2(v1_catalog, v1_registry).expect("Failed to convert v1 to v2");
// assert only ONE attribute due to sharing.
assert_eq!(catalog.len(), 1);
// Assert one attribute shows up, due to lineage.
assert_eq!(v2_registry.attributes.len(), 1);
// assert attribute fields not shared show up on ref in span.
assert_eq!(v2_registry.spans.len(), 1);
if let Some(span) = v2_registry.spans.first() {
assert_eq!(span.r#type, "my-span".to_owned().into());
// Make sure attribute ref carries sampling relevant.
}
// Assert we have two refinements (e.g. one real span, one refinement).
assert_eq!(v2_refinements.spans.len(), 2);
let span_ref_ids: Vec<String> = v2_refinements
.spans
.iter()
.map(|s| s.id.to_string())
.collect();
assert_eq!(
span_ref_ids,
vec!["my-span".to_owned(), "custom".to_owned()]
);
}
#[test]
fn test_convert_metric_v1_to_v2() {
let mut builder = crate::catalog::test_utils::CatalogBuilder::default();
let ref0 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Required,
),
sampling_relevant: None,
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
},
None,
);
let ref1 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Recommended,
),
sampling_relevant: Some(true),
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
},
None,
);
let test_refs = [ref0, ref1];
let v1_catalog = builder.build();
let mut refinement_metric_lineage = GroupLineage::new(Provenance::new("tmp", "tmp"));
refinement_metric_lineage.extends("metric.http");
refinement_metric_lineage
.add_attribute_lineage("test.key".to_owned(), AttributeLineage::new("metric.http"));
let v1_registry = crate::registry::Registry {
registry_url: "my.schema.url".to_owned(),
groups: vec![
Group {
id: "metric.http".to_owned(),
r#type: GroupType::Metric,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[0]],
span_kind: None,
events: vec![],
metric_name: Some("http".to_owned()),
instrument: Some(weaver_semconv::group::InstrumentSpec::UpDownCounter),
unit: Some("s".to_owned()),
name: None,
lineage: None,
display_name: None,
body: None,
annotations: None,
entity_associations: vec![],
visibility: None,
is_v2: false,
},
Group {
id: "metric.http.custom".to_owned(),
r#type: GroupType::Metric,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[1]],
span_kind: None,
events: vec![],
metric_name: Some("http".to_owned()),
instrument: Some(weaver_semconv::group::InstrumentSpec::UpDownCounter),
unit: Some("s".to_owned()),
name: None,
lineage: Some(refinement_metric_lineage),
display_name: None,
body: None,
annotations: None,
entity_associations: vec![],
visibility: None,
is_v2: false,
},
],
};
let (_, v2_registry, v2_refinements) =
convert_v1_to_v2(v1_catalog, v1_registry).expect("Failed to convert v1 to v2");
// assert only ONE attribute due to sharing.
assert_eq!(v2_registry.attributes.len(), 1);
// assert attribute fields not shared show up on ref in span.
assert_eq!(v2_registry.metrics.len(), 1);
if let Some(metric) = v2_registry.metrics.first() {
assert_eq!(metric.name, "http".to_owned().into());
// Make sure attribute ref carries sampling relevant.
}
// Assert we have two refinements (e.g. one real span, one refinement).
assert_eq!(v2_refinements.metrics.len(), 2);
let metric_ref_ids: Vec<String> = v2_refinements
.metrics
.iter()
.map(|s| s.id.to_string())
.collect();
assert_eq!(
metric_ref_ids,
vec!["http".to_owned(), "http.custom".to_owned()]
);
}
#[test]
fn test_convert_event_v1_to_v2() {
let mut builder = crate::catalog::test_utils::CatalogBuilder::default();
let ref0 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Required,
),
sampling_relevant: None,
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: None,
},
None,
);
let test_refs = [ref0];
let v1_catalog = builder.build();
let v1_registry = crate::registry::Registry {
registry_url: "my.schema.url".to_owned(),
groups: vec![Group {
id: "event.my-event".to_owned(),
r#type: GroupType::Event,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[0]],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: Some("my-event".to_owned()),
lineage: None,
display_name: None,
body: None,
annotations: None,
entity_associations: vec![],
visibility: None,
is_v2: false,
}],
};
let (_, v2_registry, _) =
convert_v1_to_v2(v1_catalog, v1_registry).expect("Failed to convert v1 to v2");
assert_eq!(v2_registry.events.len(), 1);
if let Some(event) = v2_registry.events.first() {
assert_eq!(event.name, "my-event".to_owned().into());
}
}
#[test]
fn test_convert_entity_v1_to_v2() {
let mut builder = crate::catalog::test_utils::CatalogBuilder::default();
let ref0 = builder.add(
Attribute {
name: "test.key".to_owned(),
r#type: weaver_semconv::attribute::AttributeType::PrimitiveOrArray(
weaver_semconv::attribute::PrimitiveOrArrayTypeSpec::String,
),
brief: "".to_owned(),
examples: None,
tag: None,
requirement_level: weaver_semconv::attribute::RequirementLevel::Basic(
weaver_semconv::attribute::BasicRequirementLevelSpec::Required,
),
sampling_relevant: None,
note: "".to_owned(),
stability: Some(Stability::Stable),
deprecated: None,
prefix: false,
tags: None,
annotations: None,
value: None,
role: Some(weaver_semconv::attribute::AttributeRole::Identifying),
},
None,
);
let test_refs = [ref0];
let v1_catalog = builder.build();
let v1_registry = crate::registry::Registry {
registry_url: "my.schema.url".to_owned(),
groups: vec![Group {
id: "entity.my-entity".to_owned(),
r#type: GroupType::Entity,
brief: "".to_owned(),
note: "".to_owned(),
prefix: "".to_owned(),
extends: None,
stability: Some(Stability::Stable),
deprecated: None,
attributes: vec![test_refs[0]],
span_kind: None,
events: vec![],
metric_name: None,
instrument: None,
unit: None,
name: Some("my-entity".to_owned()),
lineage: None,
display_name: None,
body: None,
annotations: None,