forked from open-telemetry/opentelemetry-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
213 lines (185 loc) · 7.76 KB
/
mod.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
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
//! # OpenTelemetry Log SDK
mod batch_log_processor;
mod error;
mod export;
mod log_processor;
mod logger;
mod logger_provider;
pub(crate) mod record;
mod simple_log_processor;
/// In-Memory log exporter for testing purpose.
#[cfg(any(feature = "testing", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "testing", test))))]
pub mod in_memory_exporter;
#[cfg(any(feature = "testing", test))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "testing", test))))]
pub use in_memory_exporter::{InMemoryLogExporter, InMemoryLogExporterBuilder};
pub use batch_log_processor::{
BatchConfig, BatchConfigBuilder, BatchLogProcessor, BatchLogProcessorBuilder,
};
pub use error::{LogError, LogResult};
pub use export::{LogBatch, LogExporter};
pub use log_processor::LogProcessor;
pub use logger::SdkLogger;
pub use logger_provider::{LoggerProviderBuilder, SdkLoggerProvider};
pub use record::{SdkLogRecord, TraceContext};
pub use simple_log_processor::SimpleLogProcessor;
#[cfg(feature = "experimental_logs_batch_log_processor_with_async_runtime")]
/// Module for BatchLogProcessor with async runtime.
pub mod log_processor_with_async_runtime;
#[cfg(all(test, feature = "testing"))]
mod tests {
use super::*;
use crate::Resource;
use opentelemetry::baggage::BaggageExt;
use opentelemetry::logs::LogRecord;
use opentelemetry::logs::{Logger, LoggerProvider, Severity};
use opentelemetry::{logs::AnyValue, Key, KeyValue};
use opentelemetry::{Context, InstrumentationScope};
use std::borrow::Borrow;
use std::collections::HashMap;
#[test]
fn logging_sdk_test() {
// Arrange
let resource = Resource::builder_empty()
.with_attributes([
KeyValue::new("k1", "v1"),
KeyValue::new("k2", "v2"),
KeyValue::new("k3", "v3"),
KeyValue::new("k4", "v4"),
])
.build();
let exporter: InMemoryLogExporter = InMemoryLogExporter::default();
let logger_provider = SdkLoggerProvider::builder()
.with_resource(resource.clone())
.with_log_processor(SimpleLogProcessor::new(exporter.clone()))
.build();
// Act
let logger = logger_provider.logger("test-logger");
let mut log_record = logger.create_log_record();
log_record.set_severity_number(Severity::Error);
log_record.set_severity_text("Error");
// Adding attributes using a vector with explicitly constructed Key and AnyValue objects.
log_record.add_attributes(vec![
(Key::new("key1"), AnyValue::from("value1")),
(Key::new("key2"), AnyValue::from("value2")),
]);
// Adding attributes using an array with explicitly constructed Key and AnyValue objects.
log_record.add_attributes([
(Key::new("key3"), AnyValue::from("value3")),
(Key::new("key4"), AnyValue::from("value4")),
]);
// Adding attributes using a vector with tuple auto-conversion to Key and AnyValue.
log_record.add_attributes(vec![("key5", "value5"), ("key6", "value6")]);
// Adding attributes using an array with tuple auto-conversion to Key and AnyValue.
log_record.add_attributes([("key7", "value7"), ("key8", "value8")]);
// Adding Attributes from a HashMap
let mut attributes_map = HashMap::new();
attributes_map.insert("key9", "value9");
attributes_map.insert("key10", "value10");
log_record.add_attributes(attributes_map);
logger.emit(log_record);
// Assert
let exported_logs = exporter
.get_emitted_logs()
.expect("Logs are expected to be exported.");
assert_eq!(exported_logs.len(), 1);
let log = exported_logs
.first()
.expect("Atleast one log is expected to be present.");
assert_eq!(log.instrumentation.name(), "test-logger");
assert_eq!(log.record.severity_number, Some(Severity::Error));
assert_eq!(log.record.attributes_len(), 10);
for i in 1..=10 {
assert!(log.record.attributes_contains(
&Key::new(format!("key{}", i)),
&AnyValue::String(format!("value{}", i).into())
));
}
// validate Resource
assert_eq!(&resource, log.resource.borrow());
}
#[test]
fn logger_attributes() {
let exporter: InMemoryLogExporter = InMemoryLogExporter::default();
let provider = SdkLoggerProvider::builder()
.with_log_processor(SimpleLogProcessor::new(exporter.clone()))
.build();
let scope = InstrumentationScope::builder("test_logger")
.with_schema_url("https://opentelemetry.io/schema/1.0.0")
.with_attributes(vec![(KeyValue::new("test_k", "test_v"))])
.build();
let logger = provider.logger_with_scope(scope);
let mut log_record = logger.create_log_record();
log_record.set_severity_number(Severity::Error);
logger.emit(log_record);
let mut exported_logs = exporter
.get_emitted_logs()
.expect("Logs are expected to be exported.");
assert_eq!(exported_logs.len(), 1);
let log = exported_logs.remove(0);
assert_eq!(log.record.severity_number, Some(Severity::Error));
let instrumentation_scope = log.instrumentation;
assert_eq!(instrumentation_scope.name(), "test_logger");
assert_eq!(
instrumentation_scope.schema_url(),
Some("https://opentelemetry.io/schema/1.0.0")
);
assert!(instrumentation_scope
.attributes()
.eq(&[KeyValue::new("test_k", "test_v")]));
}
#[derive(Debug)]
struct EnrichWithBaggageProcessor;
impl LogProcessor for EnrichWithBaggageProcessor {
fn emit(&self, data: &mut SdkLogRecord, _instrumentation: &InstrumentationScope) {
Context::map_current(|cx| {
for (kk, vv) in cx.baggage().iter() {
data.add_attribute(kk.clone(), vv.0.clone());
}
});
}
fn force_flush(&self) -> crate::error::OTelSdkResult {
Ok(())
}
fn shutdown(&self) -> crate::error::OTelSdkResult {
Ok(())
}
}
#[test]
fn log_and_baggage() {
// Arrange
let exporter: InMemoryLogExporter = InMemoryLogExporter::default();
let logger_provider = SdkLoggerProvider::builder()
.with_log_processor(EnrichWithBaggageProcessor)
.with_log_processor(SimpleLogProcessor::new(exporter.clone()))
.build();
// Act
let logger = logger_provider.logger("test-logger");
let context_with_baggage =
Context::current_with_baggage(vec![KeyValue::new("key-from-bag", "value-from-bag")]);
let _cx_guard = context_with_baggage.attach();
let mut log_record = logger.create_log_record();
log_record.add_attribute("key", "value");
logger.emit(log_record);
// Assert
let exported_logs = exporter
.get_emitted_logs()
.expect("Logs are expected to be exported.");
assert_eq!(exported_logs.len(), 1);
let log = exported_logs
.first()
.expect("Atleast one log is expected to be present.");
assert_eq!(log.instrumentation.name(), "test-logger");
assert_eq!(log.record.attributes_len(), 2);
// Assert that the log record contains the baggage attribute
// and the attribute added to the log record.
assert!(log
.record
.attributes_contains(&Key::new("key"), &AnyValue::String("value".into())));
assert!(log.record.attributes_contains(
&Key::new("key-from-bag"),
&AnyValue::String("value-from-bag".into())
));
}
}