|
1 |
| -pub use in_memory_exporter::{InMemorySpanExporter, InMemorySpanExporterBuilder}; |
2 |
| - |
3 |
| -mod in_memory_exporter; |
4 |
| - |
5 |
| -use crate::{ |
6 |
| - export::{ |
7 |
| - trace::{ExportResult, SpanData, SpanExporter}, |
8 |
| - ExportError, |
9 |
| - }, |
10 |
| - trace::{Config, SpanEvents, SpanLinks}, |
11 |
| - InstrumentationLibrary, |
12 |
| -}; |
13 |
| -use async_trait::async_trait; |
14 |
| -use crossbeam_channel::{unbounded, Receiver, SendError, Sender}; |
15 |
| -use futures_util::future::BoxFuture; |
16 |
| -pub use opentelemetry::testing::trace::TestSpan; |
17 |
| -use opentelemetry::trace::{ |
18 |
| - SpanContext, SpanId, SpanKind, Status, TraceFlags, TraceId, TraceState, |
19 |
| -}; |
20 |
| -use std::fmt::{Display, Formatter}; |
21 |
| - |
22 |
| -pub fn new_test_export_span_data() -> SpanData { |
23 |
| - let config = Config::default(); |
24 |
| - SpanData { |
25 |
| - span_context: SpanContext::new( |
26 |
| - TraceId::from_u128(1), |
27 |
| - SpanId::from_u64(1), |
28 |
| - TraceFlags::SAMPLED, |
29 |
| - false, |
30 |
| - TraceState::default(), |
31 |
| - ), |
32 |
| - parent_span_id: SpanId::INVALID, |
33 |
| - span_kind: SpanKind::Internal, |
34 |
| - name: "opentelemetry".into(), |
35 |
| - start_time: opentelemetry::time::now(), |
36 |
| - end_time: opentelemetry::time::now(), |
37 |
| - attributes: Vec::new(), |
38 |
| - dropped_attributes_count: 0, |
39 |
| - events: SpanEvents::default(), |
40 |
| - links: SpanLinks::default(), |
41 |
| - status: Status::Unset, |
42 |
| - resource: config.resource, |
43 |
| - instrumentation_lib: InstrumentationLibrary::default(), |
44 |
| - } |
45 |
| -} |
46 |
| - |
47 |
| -#[derive(Debug)] |
48 |
| -pub struct TestSpanExporter { |
49 |
| - tx_export: Sender<SpanData>, |
50 |
| - tx_shutdown: Sender<()>, |
51 |
| -} |
52 |
| - |
53 |
| -#[async_trait] |
54 |
| -impl SpanExporter for TestSpanExporter { |
55 |
| - fn export(&mut self, batch: Vec<SpanData>) -> BoxFuture<'static, ExportResult> { |
56 |
| - for span_data in batch { |
57 |
| - if let Err(err) = self |
58 |
| - .tx_export |
59 |
| - .send(span_data) |
60 |
| - .map_err::<TestExportError, _>(Into::into) |
61 |
| - { |
62 |
| - return Box::pin(std::future::ready(Err(Into::into(err)))); |
63 |
| - } |
64 |
| - } |
65 |
| - Box::pin(std::future::ready(Ok(()))) |
66 |
| - } |
67 |
| - |
68 |
| - fn shutdown(&mut self) { |
69 |
| - let _ = self.tx_shutdown.send(()); // ignore error |
70 |
| - } |
71 |
| -} |
72 |
| - |
73 |
| -pub fn new_test_exporter() -> (TestSpanExporter, Receiver<SpanData>, Receiver<()>) { |
74 |
| - let (tx_export, rx_export) = unbounded(); |
75 |
| - let (tx_shutdown, rx_shutdown) = unbounded(); |
76 |
| - let exporter = TestSpanExporter { |
77 |
| - tx_export, |
78 |
| - tx_shutdown, |
79 |
| - }; |
80 |
| - (exporter, rx_export, rx_shutdown) |
81 |
| -} |
82 |
| - |
83 |
| -#[derive(Debug)] |
84 |
| -pub struct TokioSpanExporter { |
85 |
| - tx_export: tokio::sync::mpsc::UnboundedSender<SpanData>, |
86 |
| - tx_shutdown: tokio::sync::mpsc::UnboundedSender<()>, |
87 |
| -} |
88 |
| - |
89 |
| -impl SpanExporter for TokioSpanExporter { |
90 |
| - fn export(&mut self, batch: Vec<SpanData>) -> BoxFuture<'static, ExportResult> { |
91 |
| - for span_data in batch { |
92 |
| - if let Err(err) = self |
93 |
| - .tx_export |
94 |
| - .send(span_data) |
95 |
| - .map_err::<TestExportError, _>(Into::into) |
96 |
| - { |
97 |
| - return Box::pin(std::future::ready(Err(Into::into(err)))); |
98 |
| - } |
99 |
| - } |
100 |
| - Box::pin(std::future::ready(Ok(()))) |
101 |
| - } |
| 1 | +//! In-Memory trace exporter for testing purpose. |
102 | 2 |
|
103 |
| - fn shutdown(&mut self) { |
104 |
| - self.tx_shutdown.send(()).unwrap(); |
105 |
| - } |
106 |
| -} |
107 |
| - |
108 |
| -pub fn new_tokio_test_exporter() -> ( |
109 |
| - TokioSpanExporter, |
110 |
| - tokio::sync::mpsc::UnboundedReceiver<SpanData>, |
111 |
| - tokio::sync::mpsc::UnboundedReceiver<()>, |
112 |
| -) { |
113 |
| - let (tx_export, rx_export) = tokio::sync::mpsc::unbounded_channel(); |
114 |
| - let (tx_shutdown, rx_shutdown) = tokio::sync::mpsc::unbounded_channel(); |
115 |
| - let exporter = TokioSpanExporter { |
116 |
| - tx_export, |
117 |
| - tx_shutdown, |
118 |
| - }; |
119 |
| - (exporter, rx_export, rx_shutdown) |
120 |
| -} |
121 |
| - |
122 |
| -#[derive(Debug)] |
123 |
| -pub struct TestExportError(String); |
124 |
| - |
125 |
| -impl std::error::Error for TestExportError {} |
126 |
| - |
127 |
| -impl ExportError for TestExportError { |
128 |
| - fn exporter_name(&self) -> &'static str { |
129 |
| - "test" |
130 |
| - } |
131 |
| -} |
132 |
| - |
133 |
| -impl Display for TestExportError { |
134 |
| - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
135 |
| - write!(f, "{}", self.0) |
136 |
| - } |
137 |
| -} |
138 |
| - |
139 |
| -#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))] |
140 |
| -impl<T> From<tokio::sync::mpsc::error::SendError<T>> for TestExportError { |
141 |
| - fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self { |
142 |
| - TestExportError(err.to_string()) |
143 |
| - } |
144 |
| -} |
145 |
| - |
146 |
| -impl<T> From<crossbeam_channel::SendError<T>> for TestExportError { |
147 |
| - fn from(err: SendError<T>) -> Self { |
148 |
| - TestExportError(err.to_string()) |
149 |
| - } |
150 |
| -} |
151 |
| - |
152 |
| -/// A no-op instance of an [`SpanExporter`]. |
153 |
| -/// |
154 |
| -/// [`SpanExporter`]: crate::export::trace::SpanExporter |
155 |
| -#[derive(Debug, Default)] |
156 |
| -pub struct NoopSpanExporter { |
157 |
| - _private: (), |
158 |
| -} |
159 |
| - |
160 |
| -impl NoopSpanExporter { |
161 |
| - /// Create a new noop span exporter |
162 |
| - pub fn new() -> Self { |
163 |
| - NoopSpanExporter { _private: () } |
164 |
| - } |
165 |
| -} |
| 3 | +/// The `in_memory_exporter` module provides in-memory trace exporter. |
| 4 | +/// For detailed usage and examples, see `in_memory_exporter`. |
| 5 | +pub mod in_memory_exporter; |
| 6 | +pub use in_memory_exporter::{InMemorySpanExporter, InMemorySpanExporterBuilder}; |
166 | 7 |
|
167 |
| -#[async_trait::async_trait] |
168 |
| -impl SpanExporter for NoopSpanExporter { |
169 |
| - fn export(&mut self, _: Vec<SpanData>) -> BoxFuture<'static, ExportResult> { |
170 |
| - Box::pin(std::future::ready(Ok(()))) |
171 |
| - } |
172 |
| -} |
| 8 | +#[doc(hidden)] |
| 9 | +mod span_exporters; |
| 10 | +pub use span_exporters::*; |
0 commit comments