-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathlogs_asserter.rs
121 lines (102 loc) · 4.1 KB
/
logs_asserter.rs
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
use anyhow::Result;
#[cfg(feature = "experimental_metadata_attributes")]
use opentelemetry_proto::tonic::common::v1::KeyValue;
use opentelemetry_proto::tonic::logs::v1::{LogRecord, LogsData, ResourceLogs};
use std::fs::File;
// Given two ResourceLogs, assert that they are equal except for the timestamps
pub struct LogsAsserter {
results: Vec<ResourceLogs>,
expected: Vec<ResourceLogs>,
}
impl LogsAsserter {
// Create a new LogsAsserter
pub fn new(results: Vec<ResourceLogs>, expected: Vec<ResourceLogs>) -> Self {
LogsAsserter { results, expected }
}
pub fn assert(self) {
self.assert_resource_logs_eq(&self.results, &self.expected);
}
fn assert_resource_logs_eq(&self, results: &[ResourceLogs], expected: &[ResourceLogs]) {
let mut results_logs = Vec::new();
let mut expected_logs = Vec::new();
assert_eq!(results.len(), expected.len());
for i in 0..results.len() {
let result_resource_logs = &results[i];
let expected_resource_logs = &expected[i];
assert_eq!(
result_resource_logs.resource,
expected_resource_logs.resource
);
assert_eq!(
result_resource_logs.schema_url,
expected_resource_logs.schema_url
);
assert_eq!(
result_resource_logs.scope_logs.len(),
expected_resource_logs.scope_logs.len()
);
for i in 0..result_resource_logs.scope_logs.len() {
let result_scope_logs = &result_resource_logs.scope_logs[i];
let expected_scope_logs = &expected_resource_logs.scope_logs[i];
assert_eq!(result_scope_logs.scope, expected_scope_logs.scope);
results_logs.extend(result_scope_logs.log_records.clone());
expected_logs.extend(expected_scope_logs.log_records.clone());
}
}
for (result_log, expected_log) in results_logs.iter().zip(expected_logs.iter()) {
assert_eq!(
LogRecordWrapper(result_log.clone()),
LogRecordWrapper(expected_log.clone())
);
}
}
}
pub struct LogRecordWrapper(pub LogRecord);
impl PartialEq for LogRecordWrapper {
fn eq(&self, other: &Self) -> bool {
let LogRecordWrapper(ref a) = *self;
let LogRecordWrapper(ref b) = *other;
assert_eq!(
a.severity_number, b.severity_number,
"severity_number does not match"
);
assert_eq!(
a.severity_text, b.severity_text,
"severity_text does not match"
);
let a_attrs = a.attributes.clone();
#[cfg(feature = "experimental_metadata_attributes")]
let a_attrs: Vec<_> = a_attrs
.into_iter()
.filter(|KeyValue { key, .. }| !key.as_str().starts_with("code."))
.collect();
let b_attrs = b.attributes.clone();
#[cfg(feature = "experimental_metadata_attributes")]
let b_attrs: Vec<_> = b_attrs
.into_iter()
.filter(|KeyValue { key, .. }| !key.as_str().starts_with("code."))
.collect();
assert_eq!(a.body, b.body, "body does not match");
assert_eq!(a_attrs, b_attrs, "attributes do not match");
assert_eq!(
a.dropped_attributes_count, b.dropped_attributes_count,
"dropped_attributes_count does not match"
);
assert_eq!(a.flags, b.flags, "flags do not match");
assert_eq!(a.trace_id, b.trace_id, "trace_id does not match");
assert_eq!(a.span_id, b.span_id, "span_id does not match");
true
}
}
impl std::fmt::Debug for LogRecordWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let LogRecordWrapper(ref inner) = *self;
inner.fmt(f)
}
}
// read a file contains ResourceSpans in json format
pub fn read_logs_from_json(file: File) -> Result<Vec<ResourceLogs>> {
let reader = std::io::BufReader::new(file);
let log_data: LogsData = serde_json::from_reader(reader)?;
Ok(log_data.resource_logs)
}