logo
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
//! Stdout Metrics Exporter
use crate::{
    export::metrics::{
        aggregation::{stateless_temporality_selector, LastValue, Sum, TemporalitySelector},
        InstrumentationLibraryReader, MetricsExporter,
    },
    metrics::aggregators::{LastValueAggregator, SumAggregator},
    Resource,
};
use opentelemetry_api::{
    attributes::{default_encoder, AttributeSet, Encoder},
    metrics::{MetricsError, Result},
    Context, KeyValue,
};
use std::fmt;
use std::io;
use std::sync::Mutex;
use std::time::SystemTime;

/// Create a new stdout exporter builder with the configuration for a stdout exporter.
pub fn stdout() -> StdoutExporterBuilder<io::Stdout> {
    StdoutExporterBuilder::<io::Stdout>::builder()
}

/// An OpenTelemetry metric exporter that transmits telemetry to
/// the local STDOUT or via the registered implementation of `Write`.
#[derive(Debug)]
pub struct StdoutExporter<W> {
    /// Writer is the destination. If not set, `Stdout` is used.
    writer: Mutex<W>,

    /// Specifies if timestamps should be printed
    timestamps: bool,

    /// Encodes the attributes.
    attribute_encoder: Box<dyn Encoder + Send + Sync>,

    /// An optional user-defined function to format a given export batch.
    formatter: Option<Formatter>,
}

/// Individually exported metric
///
/// Can be formatted using [`StdoutExporterBuilder::with_formatter`].
#[derive(Default, Debug)]
pub struct ExportLine {
    /// metric name
    pub name: String,

    /// populated if using sum aggregator
    pub sum: Option<ExportNumeric>,

    /// populated if using last value aggregator
    pub last_value: Option<ExportNumeric>,

    /// metric timestamp
    pub timestamp: Option<SystemTime>,
}

/// A number exported as debug for serialization
pub struct ExportNumeric(Box<dyn fmt::Debug>);

impl fmt::Debug for ExportNumeric {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<W> StdoutExporter<W> {
    /// The temporality selector for this exporter
    pub fn temporality_selector(&self) -> impl TemporalitySelector {
        stateless_temporality_selector()
    }
}

impl<W> TemporalitySelector for StdoutExporter<W> {
    fn temporality_for(
        &self,
        descriptor: &crate::metrics::sdk_api::Descriptor,
        kind: &super::aggregation::AggregationKind,
    ) -> super::aggregation::Temporality {
        stateless_temporality_selector().temporality_for(descriptor, kind)
    }
}

impl<W> MetricsExporter for StdoutExporter<W>
where
    W: fmt::Debug + io::Write,
{
    fn export(
        &self,
        _cx: &Context,
        res: &Resource,
        reader: &dyn InstrumentationLibraryReader,
    ) -> Result<()> {
        let mut batch = Vec::new();
        reader.try_for_each(&mut |library, reader| {
            let mut attributes = Vec::new();
            if !library.name.is_empty() {
                attributes.push(KeyValue::new("instrumentation.name", library.name.clone()));
            }
            if let Some(version) = &library.version {
                attributes.push(KeyValue::new("instrumentation.version", version.clone()));
            }
            if let Some(schema) = &library.schema_url {
                attributes.push(KeyValue::new("instrumentation.schema_url", schema.clone()));
            }
            let inst_attributes = AttributeSet::from_attributes(attributes.into_iter());
            let encoded_inst_attributes =
                inst_attributes.encoded(Some(self.attribute_encoder.as_ref()));

            reader.try_for_each(self, &mut |record| {
                let desc = record.descriptor();
                let agg = record.aggregator().ok_or(MetricsError::NoDataCollected)?;
                let kind = desc.number_kind();

                let encoded_resource = res.encoded(self.attribute_encoder.as_ref());

                let mut expose = ExportLine::default();
                if let Some(sum) = agg.as_any().downcast_ref::<SumAggregator>() {
                    expose.sum = Some(ExportNumeric(sum.sum()?.to_debug(kind)));
                } else if let Some(last_value) = agg.as_any().downcast_ref::<LastValueAggregator>()
                {
                    let (value, timestamp) = last_value.last_value()?;
                    expose.last_value = Some(ExportNumeric(value.to_debug(kind)));

                    if self.timestamps {
                        expose.timestamp = Some(timestamp);
                    }
                }

                let mut encoded_attributes = String::new();
                let iter = record.attributes().iter();
                if let (0, _) = iter.size_hint() {
                    encoded_attributes = record
                        .attributes()
                        .encoded(Some(self.attribute_encoder.as_ref()));
                }

                let mut sb = String::new();

                sb.push_str(desc.name());

                if !encoded_attributes.is_empty()
                    || !encoded_resource.is_empty()
                    || !encoded_inst_attributes.is_empty()
                {
                    sb.push('{');
                    sb.push_str(&encoded_resource);
                    if !encoded_inst_attributes.is_empty() && !encoded_resource.is_empty() {
                        sb.push(',');
                    }
                    sb.push_str(&encoded_inst_attributes);
                    if !encoded_attributes.is_empty()
                        && (!encoded_inst_attributes.is_empty() || !encoded_resource.is_empty())
                    {
                        sb.push(',');
                    }
                    sb.push_str(&encoded_attributes);
                    sb.push('}');
                }

                expose.name = sb;

                batch.push(expose);
                Ok(())
            })
        })?;

        self.writer.lock().map_err(From::from).and_then(|mut w| {
            let formatted = match &self.formatter {
                Some(formatter) => formatter.0(batch)?,
                None => format!("{:?}\n", batch),
            };
            w.write_all(formatted.as_bytes())
                .map_err(|e| MetricsError::Other(e.to_string()))
        })
    }
}

/// A formatter for user-defined batch serialization.
struct Formatter(Box<dyn Fn(Vec<ExportLine>) -> Result<String> + Send + Sync>);
impl fmt::Debug for Formatter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Formatter(closure)")
    }
}

/// Configuration for a given stdout exporter.
#[derive(Debug)]
pub struct StdoutExporterBuilder<W> {
    writer: Mutex<W>,
    timestamps: bool,
    attribute_encoder: Option<Box<dyn Encoder + Send + Sync>>,
    formatter: Option<Formatter>,
}

impl<W> StdoutExporterBuilder<W>
where
    W: io::Write + fmt::Debug + Send + Sync + 'static,
{
    fn builder() -> StdoutExporterBuilder<io::Stdout> {
        StdoutExporterBuilder {
            writer: Mutex::new(io::stdout()),
            timestamps: true,
            attribute_encoder: None,
            formatter: None,
        }
    }
    /// Set the writer that this exporter will use.
    pub fn with_writer<W2: io::Write>(self, writer: W2) -> StdoutExporterBuilder<W2> {
        StdoutExporterBuilder {
            writer: Mutex::new(writer),
            timestamps: self.timestamps,
            attribute_encoder: self.attribute_encoder,
            formatter: self.formatter,
        }
    }

    /// Hide the timestamps from exported results
    pub fn with_do_not_print_time(self, do_not_print_time: bool) -> Self {
        StdoutExporterBuilder {
            timestamps: do_not_print_time,
            ..self
        }
    }

    /// Set the attribute encoder that this exporter will use.
    pub fn with_attribute_encoder<E>(self, attribute_encoder: E) -> Self
    where
        E: Encoder + Send + Sync + 'static,
    {
        StdoutExporterBuilder {
            attribute_encoder: Some(Box::new(attribute_encoder)),
            ..self
        }
    }

    /// Set a formatter for serializing export batch data
    pub fn with_formatter<T>(self, formatter: T) -> Self
    where
        T: Fn(Vec<ExportLine>) -> Result<String> + Send + Sync + 'static,
    {
        StdoutExporterBuilder {
            formatter: Some(Formatter(Box::new(formatter))),
            ..self
        }
    }

    /// Build a new push controller, returning errors if they arise.
    pub fn build(self) -> Result<StdoutExporter<W>> {
        Ok(StdoutExporter {
            writer: self.writer,
            timestamps: self.timestamps,
            attribute_encoder: self.attribute_encoder.unwrap_or_else(default_encoder),
            formatter: self.formatter,
        })
    }
}