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
use crate::attributes::{Attributes, Buffered, MetricId, OnFlush, Prefixed, WithAttributes};
use crate::input::InputKind;
use crate::name::MetricName;
use crate::Flush;
use crate::{CachedInput, QueuedInput};
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::path::Path;
use std::sync::Arc;
#[cfg(not(feature = "parking_lot"))]
use std::sync::RwLock;
#[cfg(feature = "parking_lot")]
use parking_lot::RwLock;
use crate::{Formatting, Input, InputMetric, InputScope, LineFormat, SimpleFormat};
pub struct Stream<W: Write + Send + Sync + 'static> {
attributes: Attributes,
format: Arc<dyn LineFormat + Send + Sync>,
inner: Arc<RwLock<W>>,
}
impl<W: Write + Send + Sync + 'static> QueuedInput for Stream<W> {}
impl<W: Write + Send + Sync + 'static> CachedInput for Stream<W> {}
impl<W: Write + Send + Sync + 'static> Formatting for Stream<W> {
fn formatting(&self, format: impl LineFormat + 'static) -> Self {
let mut cloned = self.clone();
cloned.format = Arc::new(format);
cloned
}
}
impl<W: Write + Send + Sync + 'static> Stream<W> {
pub fn write_to(write: W) -> Stream<W> {
Stream {
attributes: Attributes::default(),
format: Arc::new(SimpleFormat::default()),
inner: Arc::new(RwLock::new(write)),
}
}
}
impl Stream<File> {
#[deprecated(since = "0.8.0", note = "Use write_to_file()")]
#[allow(clippy::wrong_self_convention)]
pub fn to_file<P: AsRef<Path>>(file: P) -> io::Result<Stream<File>> {
Self::write_to_file(file)
}
pub fn write_to_file<P: AsRef<Path>>(file: P) -> io::Result<Stream<File>> {
let file = OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(file)?;
Ok(Stream::write_to(file))
}
#[deprecated(since = "0.8.0", note = "Use write_to_new_file()")]
#[allow(clippy::wrong_self_convention)]
pub fn to_new_file<P: AsRef<Path>>(file: P, clobber: bool) -> io::Result<Stream<File>> {
Self::write_to_new_file(file, clobber)
}
pub fn write_to_new_file<P: AsRef<Path>>(file: P, clobber: bool) -> io::Result<Stream<File>> {
let file = OpenOptions::new()
.write(true)
.create(true)
.create_new(!clobber)
.open(file)?;
Ok(Stream::write_to(file))
}
}
impl Stream<io::Stderr> {
#[deprecated(since = "0.8.0", note = "Use write_to_stderr()")]
pub fn to_stderr() -> Stream<io::Stderr> {
Stream::write_to(io::stderr())
}
pub fn write_to_stderr() -> Stream<io::Stderr> {
Stream::write_to(io::stderr())
}
}
impl Stream<io::Stdout> {
#[deprecated(since = "0.8.0", note = "Use write_to_stdout()")]
pub fn to_stdout() -> Stream<io::Stdout> {
Stream::write_to(io::stdout())
}
pub fn write_to_stdout() -> Stream<io::Stdout> {
Stream::write_to(io::stdout())
}
}
impl<W: Write + Send + Sync + 'static> Clone for Stream<W> {
fn clone(&self) -> Self {
Stream {
attributes: self.attributes.clone(),
format: self.format.clone(),
inner: self.inner.clone(),
}
}
}
impl<W: Write + Send + Sync + 'static> WithAttributes for Stream<W> {
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
}
impl<W: Write + Send + Sync + 'static> Buffered for Stream<W> {}
impl<W: Write + Send + Sync + 'static> Input for Stream<W> {
type SCOPE = TextScope<W>;
fn metrics(&self) -> Self::SCOPE {
TextScope {
attributes: self.attributes.clone(),
entries: Arc::new(RwLock::new(Vec::new())),
input: self.clone(),
}
}
}
pub struct TextScope<W: Write + Send + Sync + 'static> {
attributes: Attributes,
entries: Arc<RwLock<Vec<Vec<u8>>>>,
input: Stream<W>,
}
impl<W: Write + Send + Sync + 'static> Clone for TextScope<W> {
fn clone(&self) -> Self {
TextScope {
attributes: self.attributes.clone(),
entries: self.entries.clone(),
input: self.input.clone(),
}
}
}
impl<W: Write + Send + Sync + 'static> WithAttributes for TextScope<W> {
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
}
impl<W: Write + Send + Sync + 'static> Buffered for TextScope<W> {}
impl<W: Write + Send + Sync + 'static> InputScope for TextScope<W> {
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric {
let name = self.prefix_append(name);
let template = self.input.format.template(&name, kind);
let entries = self.entries.clone();
let metric_id = MetricId::forge("stream", name);
if self.is_buffered() {
InputMetric::new(metric_id, move |value, labels| {
let mut buffer = Vec::with_capacity(32);
match template.print(&mut buffer, value, |key| labels.lookup(key)) {
Ok(()) => {
let mut entries = write_lock!(entries);
entries.push(buffer)
}
Err(err) => debug!("{}", err),
}
})
} else {
let input = self.input.clone();
InputMetric::new(metric_id, move |value, labels| {
let mut buffer = Vec::with_capacity(32);
match template.print(&mut buffer, value, |key| labels.lookup(key)) {
Ok(()) => {
let mut input = write_lock!(input.inner);
if let Err(e) = input.write_all(&buffer).and_then(|_| input.flush()) {
debug!("Could not write text metrics: {}", e)
}
}
Err(err) => debug!("{}", err),
}
})
}
}
}
impl<W: Write + Send + Sync + 'static> Flush for TextScope<W> {
fn flush(&self) -> io::Result<()> {
self.notify_flush_listeners();
let mut entries = write_lock!(self.entries);
if !entries.is_empty() {
let mut input = write_lock!(self.input.inner);
for entry in entries.drain(..) {
input.write_all(&entry)?
}
input.flush()?;
}
Ok(())
}
}
impl<W: Write + Send + Sync + 'static> Drop for TextScope<W> {
fn drop(&mut self) {
if let Err(e) = self.flush() {
warn!("Could not flush text metrics on Drop. {}", e)
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::input::InputKind;
use std::io;
#[test]
fn sink_print() {
let c = super::Stream::write_to(io::stdout()).metrics();
let m = c.new_metric("test".into(), InputKind::Marker);
m.write(33, labels![]);
}
}