Skip to main content

arti/logging/
otlp_file_exporter.rs

1//! Tracing exporter to write spans to a file in the OTLP JSON format.
2
3// TODO: If https://github.com/open-telemetry/opentelemetry-rust/issues/2602 gets fixed, we can
4// replace this entire file with whatever upstream has for doing this.
5
6use opentelemetry_proto::transform::common::tonic::ResourceAttributesWithSchema;
7use opentelemetry_proto::transform::trace::tonic::group_spans_by_resource_and_scope;
8use opentelemetry_sdk::{
9    Resource,
10    error::{OTelSdkError, OTelSdkResult},
11    trace::SpanExporter,
12};
13use std::{
14    fmt::Debug,
15    io::{LineWriter, Write},
16    sync::{Arc, Mutex},
17};
18
19/// Tracing exporter to write OTLP JSON to a file (or anything else that implements [`LineWriter`].
20#[derive(Debug)]
21pub(crate) struct FileExporter<W: Write + Send + Debug> {
22    /// The [`LineWriter`] to write to.
23    writer: Arc<Mutex<LineWriter<W>>>,
24    /// The [`Resource`] to associate spans with.
25    resource: Resource,
26}
27
28impl<W: Write + Send + Debug> FileExporter<W> {
29    /// Create a new [`FileExporter`]
30    pub(crate) fn new(writer: W, resource: Resource) -> Self {
31        Self {
32            writer: Arc::new(Mutex::new(LineWriter::new(writer))),
33            resource,
34        }
35    }
36}
37
38// Note that OpenTelemetry can only represent events as children of spans, so this exporter only
39// works on spans. If you want a event to be exported, you need to make sure it exists within some
40// span.
41impl<W: Write + Send + Debug> SpanExporter for FileExporter<W> {
42    fn export(
43        &self,
44        batch: Vec<opentelemetry_sdk::trace::SpanData>,
45    ) -> impl futures::Future<
46        Output = std::result::Result<(), opentelemetry_sdk::error::OTelSdkError>,
47    > + std::marker::Send {
48        let resource = ResourceAttributesWithSchema::from(&self.resource);
49        let data = group_spans_by_resource_and_scope(batch, &resource);
50        let mut writer = self.writer.lock().expect("Lock poisoned");
51        Box::pin(std::future::ready('write: {
52            // See https://opentelemetry.io/docs/specs/otel/protocol/file-exporter/ for format
53
54            if let Err(err) = serde_json::to_writer(
55                writer.get_mut(),
56                &serde_json::json!({"resourceSpans": data}),
57            ) {
58                break 'write Err(OTelSdkError::InternalFailure(err.to_string()));
59            }
60
61            if let Err(err) = writer.write(b"\n") {
62                break 'write Err(OTelSdkError::InternalFailure(err.to_string()));
63            }
64
65            Ok(())
66        }))
67    }
68
69    fn force_flush(&mut self) -> OTelSdkResult {
70        let mut writer = self
71            .writer
72            .lock()
73            .map_err(|e| OTelSdkError::InternalFailure(e.to_string()))?;
74
75        writer
76            .flush()
77            .map_err(|e| OTelSdkError::InternalFailure(e.to_string()))
78    }
79
80    fn set_resource(&mut self, res: &opentelemetry_sdk::Resource) {
81        self.resource = res.clone();
82    }
83}