-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathlog_exporter.rs
181 lines (154 loc) · 5.47 KB
/
log_exporter.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
/*
The benchmark results:
criterion = "0.5.1"
OS: Ubuntu 22.04.3 LTS (5.15.146.1-microsoft-standard-WSL2)
Hardware: AMD EPYC 7763 64-Core Processor - 2.44 GHz, 16vCPUs,
RAM: 64.0 GB
| Test | Average time|
|--------------------------------|-------------|
| LogExporterWithFuture | 111 ns |
| LogExporterWithoutFuture | 92 ns |
*/
use std::sync::Mutex;
use std::time::SystemTime;
use async_trait::async_trait;
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::logs::{LogRecord as _, LogResult, Logger as _, LoggerProvider as _, Severity};
use opentelemetry::InstrumentationLibrary;
use opentelemetry_sdk::export::logs::LogBatch;
use opentelemetry_sdk::logs::LogProcessor;
use opentelemetry_sdk::logs::LogRecord;
use opentelemetry_sdk::logs::LoggerProvider;
use pprof::criterion::{Output, PProfProfiler};
use std::fmt::Debug;
// Run this benchmark with:
// cargo bench --bench log_exporter
#[async_trait]
pub trait LogExporterWithFuture: Send + Sync + Debug {
async fn export(&mut self, batch: LogBatch<'_>);
}
pub trait LogExporterWithoutFuture: Send + Sync + Debug {
fn export(&mut self, batch: LogBatch<'_>);
}
#[derive(Debug)]
struct NoOpExporterWithFuture {}
#[async_trait]
impl LogExporterWithFuture for NoOpExporterWithFuture {
async fn export(&mut self, _batch: LogBatch<'_>) {}
}
#[derive(Debug)]
struct NoOpExporterWithoutFuture {}
impl LogExporterWithoutFuture for NoOpExporterWithoutFuture {
fn export(&mut self, _batch: LogBatch<'_>) {}
}
#[derive(Debug)]
struct ExportingProcessorWithFuture {
exporter: Mutex<NoOpExporterWithFuture>,
}
impl ExportingProcessorWithFuture {
fn new(exporter: NoOpExporterWithFuture) -> Self {
ExportingProcessorWithFuture {
exporter: Mutex::new(exporter),
}
}
}
impl LogProcessor for ExportingProcessorWithFuture {
fn emit(&self, record: &mut LogRecord, library: &InstrumentationLibrary) {
let mut exporter = self.exporter.lock().expect("lock error");
let logs = [(record as &LogRecord, library)];
futures_executor::block_on(exporter.export(LogBatch::new(&logs)));
}
fn force_flush(&self) -> LogResult<()> {
Ok(())
}
fn shutdown(&self) -> LogResult<()> {
Ok(())
}
}
#[derive(Debug)]
struct ExportingProcessorWithoutFuture {
exporter: Mutex<NoOpExporterWithoutFuture>,
}
impl ExportingProcessorWithoutFuture {
fn new(exporter: NoOpExporterWithoutFuture) -> Self {
ExportingProcessorWithoutFuture {
exporter: Mutex::new(exporter),
}
}
}
impl LogProcessor for ExportingProcessorWithoutFuture {
fn emit(&self, record: &mut LogRecord, library: &InstrumentationLibrary) {
let logs = [(record as &LogRecord, library)];
self.exporter
.lock()
.expect("lock error")
.export(LogBatch::new(&logs));
}
fn force_flush(&self) -> LogResult<()> {
Ok(())
}
fn shutdown(&self) -> LogResult<()> {
Ok(())
}
}
fn criterion_benchmark(c: &mut Criterion) {
exporter_with_future(c);
exporter_without_future(c);
}
fn exporter_with_future(c: &mut Criterion) {
let provider = LoggerProvider::builder()
.with_log_processor(ExportingProcessorWithFuture::new(NoOpExporterWithFuture {}))
.build();
let logger = provider.logger("benchmark");
c.bench_function("LogExporterWithFuture", |b| {
b.iter(|| {
let mut log_record = logger.create_log_record();
let now = SystemTime::now();
log_record.set_observed_timestamp(now);
log_record.set_target("my-target".to_string());
log_record.set_event_name("CheckoutFailed");
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text("WARN");
log_record.add_attribute("book_id", "12345");
log_record.add_attribute("book_title", "Rust Programming Adventures");
log_record.add_attribute("message", "Unable to process checkout.");
logger.emit(log_record);
});
});
}
fn exporter_without_future(c: &mut Criterion) {
let provider = LoggerProvider::builder()
.with_log_processor(ExportingProcessorWithoutFuture::new(
NoOpExporterWithoutFuture {},
))
.build();
let logger = provider.logger("benchmark");
c.bench_function("LogExporterWithoutFuture", |b| {
b.iter(|| {
let mut log_record = logger.create_log_record();
let now = SystemTime::now();
log_record.set_observed_timestamp(now);
log_record.set_target("my-target".to_string());
log_record.set_event_name("CheckoutFailed");
log_record.set_severity_number(Severity::Warn);
log_record.set_severity_text("WARN");
log_record.add_attribute("book_id", "12345");
log_record.add_attribute("book_title", "Rust Programming Adventures");
log_record.add_attribute("message", "Unable to process checkout.");
logger.emit(log_record);
});
});
}
#[cfg(not(target_os = "windows"))]
criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = criterion_benchmark
}
#[cfg(target_os = "windows")]
criterion_group! {
name = benches;
config = Criterion::default();
targets = criterion_benchmark
}
criterion_main!(benches);