opentelemetry_sdk/export/trace.rs
1//! Trace exporters
2use crate::Resource;
3use futures_util::future::BoxFuture;
4use opentelemetry::trace::{SpanContext, SpanId, SpanKind, Status, TraceError};
5use opentelemetry::KeyValue;
6use std::borrow::Cow;
7use std::fmt::Debug;
8use std::time::SystemTime;
9
10/// Describes the result of an export.
11pub type ExportResult = Result<(), TraceError>;
12
13/// `SpanExporter` defines the interface that protocol-specific exporters must
14/// implement so that they can be plugged into OpenTelemetry SDK and support
15/// sending of telemetry data.
16///
17/// The goal of the interface is to minimize burden of implementation for
18/// protocol-dependent telemetry exporters. The protocol exporter is expected to
19/// be primarily a simple telemetry data encoder and transmitter.
20pub trait SpanExporter: Send + Sync + Debug {
21 /// Exports a batch of readable spans. Protocol exporters that will
22 /// implement this function are typically expected to serialize and transmit
23 /// the data to the destination.
24 ///
25 /// This function will never be called concurrently for the same exporter
26 /// instance. It can be called again only after the current call returns.
27 ///
28 /// This function must not block indefinitely, there must be a reasonable
29 /// upper limit after which the call must time out with an error result.
30 ///
31 /// Any retry logic that is required by the exporter is the responsibility
32 /// of the exporter.
33 fn export(&mut self, batch: Vec<SpanData>) -> BoxFuture<'static, ExportResult>;
34
35 /// Shuts down the exporter. Called when SDK is shut down. This is an
36 /// opportunity for exporter to do any cleanup required.
37 ///
38 /// This function should be called only once for each `SpanExporter`
39 /// instance. After the call to `shutdown`, subsequent calls to `export` are
40 /// not allowed and should return an error.
41 ///
42 /// This function should not block indefinitely (e.g. if it attempts to
43 /// flush the data and the destination is unavailable). SDK authors
44 /// can decide if they want to make the shutdown timeout
45 /// configurable.
46 fn shutdown(&mut self) {}
47
48 /// This is a hint to ensure that the export of any Spans the exporter
49 /// has received prior to the call to this function SHOULD be completed
50 /// as soon as possible, preferably before returning from this method.
51 ///
52 /// This function SHOULD provide a way to let the caller know
53 /// whether it succeeded, failed or timed out.
54 ///
55 /// This function SHOULD only be called in cases where it is absolutely necessary,
56 /// such as when using some FaaS providers that may suspend the process after
57 /// an invocation, but before the exporter exports the completed spans.
58 ///
59 /// This function SHOULD complete or abort within some timeout. This function can be
60 /// implemented as a blocking API or an asynchronous API which notifies the caller via
61 /// a callback or an event. OpenTelemetry client authors can decide if they want to
62 /// make the flush timeout configurable.
63 fn force_flush(&mut self) -> BoxFuture<'static, ExportResult> {
64 Box::pin(async { Ok(()) })
65 }
66
67 /// Set the resource for the exporter.
68 fn set_resource(&mut self, _resource: &Resource) {}
69}
70
71/// `SpanData` contains all the information collected by a `Span` and can be used
72/// by exporters as a standard input.
73#[derive(Clone, Debug, PartialEq)]
74pub struct SpanData {
75 /// Exportable `SpanContext`
76 pub span_context: SpanContext,
77 /// Span parent id
78 pub parent_span_id: SpanId,
79 /// Span kind
80 pub span_kind: SpanKind,
81 /// Span name
82 pub name: Cow<'static, str>,
83 /// Span start time
84 pub start_time: SystemTime,
85 /// Span end time
86 pub end_time: SystemTime,
87 /// Span attributes
88 pub attributes: Vec<KeyValue>,
89 /// The number of attributes that were above the configured limit, and thus
90 /// dropped.
91 pub dropped_attributes_count: u32,
92 /// Span events
93 pub events: crate::trace::SpanEvents,
94 /// Span Links
95 pub links: crate::trace::SpanLinks,
96 /// Span status
97 pub status: Status,
98 /// Instrumentation library that produced this span
99 pub instrumentation_lib: crate::InstrumentationLibrary,
100}