Skip to content

Commit 57c3aa3

Browse files
lalitbcijothomas
andauthored
Enable doc generation for testing module for InMemory*Exporters. (#1503)
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
1 parent 989fa3b commit 57c3aa3

File tree

7 files changed

+192
-175
lines changed

7 files changed

+192
-175
lines changed

opentelemetry-sdk/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- [#1410](https://github.com/open-telemetry/opentelemetry-rust/pull/1410) Add experimental synchronous gauge
88
- [#1471](https://github.com/open-telemetry/opentelemetry-rust/pull/1471) Configure batch log record processor via [`OTEL_BLRP_*`](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#batch-logrecord-processor) environment variables and via `OtlpLogPipeline::with_batch_config`
9+
- [#1503](https://github.com/open-telemetry/opentelemetry-rust/pull/1503) Make the documentation for In-Memory exporters visible.
910

1011
### Changed
1112

opentelemetry-sdk/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub mod propagation;
136136
pub mod resource;
137137
pub mod runtime;
138138
#[cfg(any(feature = "testing", test))]
139-
#[doc(hidden)]
139+
#[cfg_attr(docsrs, doc(cfg(any(feature = "testing", test))))]
140140
pub mod testing;
141141
#[cfg(feature = "trace")]
142142
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
pub use in_memory_exporter::{InMemoryLogsExporter, InMemoryLogsExporterBuilder};
1+
//! In-Memory log exporter for testing purpose.
22
3-
mod in_memory_exporter;
3+
/// The `in_memory_exporter` module provides in-memory log exporter.
4+
/// For detailed usage and examples, see `in_memory_exporter`.
5+
pub mod in_memory_exporter;
6+
pub use in_memory_exporter::{InMemoryLogsExporter, InMemoryLogsExporterBuilder};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
pub use in_memory_exporter::{InMemoryMetricsExporter, InMemoryMetricsExporterBuilder};
2-
pub use metric_reader::TestMetricReader;
1+
//! In-Memory metrics exporter for testing purpose.
32
3+
/// The `in_memory_exporter` module provides in-memory metrics exporter.
4+
/// For detailed usage and examples, see `in_memory_exporter`.
45
pub mod in_memory_exporter;
6+
pub use in_memory_exporter::{InMemoryMetricsExporter, InMemoryMetricsExporterBuilder};
7+
8+
#[doc(hidden)]
59
pub mod metric_reader;
10+
pub use metric_reader::TestMetricReader;

opentelemetry-sdk/src/testing/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! In-Memory exporters for testing purpose.
2+
13
#[cfg(all(feature = "testing", feature = "trace"))]
24
pub mod trace;
35

+8-170
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,10 @@
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.
1022
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};
1667

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

0 commit comments

Comments
 (0)