Skip to content

Commit 2022ace

Browse files
authored
Move RuntimeChannel type arg T to associated types (#1314)
RuntimeChannel::batch_message_channel needs to be generic over the message type. The type used to be declared on the RuntimeChannel<T> trait. This means a RuntimeChannel can only be used with one particular message type, which feels unfortunate. fn install<R: RuntimeChannel<??::BatchMessage>>(runtime: R) { // Can't use the same runtime here. :-( TracerProvider::builder().with_batch_exporter(e, runtime); LoggerProvider::builder().with_batch_exporter(e, runtime); } This change moves the type argument to the batch_message_channel<T> function and the associated types Receiver<T> and Sender<T>. Channels are still specific to a message type, but a RuntimeChannel can be used with any number of message types. fn install<R: RuntimeChannel>(runtime: R) { // It works. :-) TracerProvider::builder().with_batch_exporter(e, runtime); LoggerProvider::builder().with_batch_exporter(e, runtime); } This also means the BatchMessage types no longer need to be public.
1 parent ad18037 commit 2022ace

File tree

17 files changed

+77
-76
lines changed

17 files changed

+77
-76
lines changed

opentelemetry-contrib/src/trace/exporter/jaeger_json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use opentelemetry::trace::{SpanId, TraceError};
88
use opentelemetry_sdk::{
99
export::trace::{ExportResult, SpanData, SpanExporter},
1010
runtime::RuntimeChannel,
11-
trace::{BatchMessage, Tracer, TracerProvider},
11+
trace::{Tracer, TracerProvider},
1212
};
1313
use opentelemetry_semantic_conventions::SCHEMA_URL;
1414
use std::collections::HashMap;
@@ -213,7 +213,7 @@ fn opentelemetry_value_to_json(value: &opentelemetry::Value) -> (&str, serde_jso
213213
///
214214
/// [`RuntimeChannel`]: opentelemetry_sdk::runtime::RuntimeChannel
215215
#[async_trait]
216-
pub trait JaegerJsonRuntime: RuntimeChannel<BatchMessage> + std::fmt::Debug {
216+
pub trait JaegerJsonRuntime: RuntimeChannel + std::fmt::Debug {
217217
/// Create a new directory if the given path does not exist yet
218218
async fn create_dir(&self, path: &Path) -> ExportResult;
219219
/// Write the provided content to a new file at the given path

opentelemetry-datadog/src/exporter/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use opentelemetry_sdk::{
1515
export::trace::{ExportResult, SpanData, SpanExporter},
1616
resource::{ResourceDetector, SdkProvidedResourceDetector},
1717
runtime::RuntimeChannel,
18-
trace::{BatchMessage, Config, Tracer, TracerProvider},
18+
trace::{Config, Tracer, TracerProvider},
1919
Resource,
2020
};
2121
use opentelemetry_semantic_conventions as semcov;
@@ -300,10 +300,7 @@ impl DatadogPipelineBuilder {
300300

301301
/// Install the Datadog trace exporter pipeline using a batch span processor with the specified
302302
/// runtime.
303-
pub fn install_batch<R: RuntimeChannel<BatchMessage>>(
304-
mut self,
305-
runtime: R,
306-
) -> Result<Tracer, TraceError> {
303+
pub fn install_batch<R: RuntimeChannel>(mut self, runtime: R) -> Result<Tracer, TraceError> {
307304
let (config, service_name) = self.build_config_and_service_name();
308305
let exporter = self.build_exporter_with_service_name(service_name)?;
309306
let mut provider_builder = TracerProvider::builder().with_batch_exporter(exporter, runtime);

opentelemetry-jaeger/src/exporter/runtime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
))]
66
use crate::exporter::addrs_and_family;
77
use async_trait::async_trait;
8-
use opentelemetry_sdk::{runtime::RuntimeChannel, trace::BatchMessage};
8+
use opentelemetry_sdk::runtime::RuntimeChannel;
99
use std::net::ToSocketAddrs;
1010

1111
/// Jaeger Trace Runtime is an extension to [`RuntimeChannel`].
1212
///
1313
/// [`RuntimeChannel`]: opentelemetry_sdk::runtime::RuntimeChannel
1414
#[async_trait]
15-
pub trait JaegerTraceRuntime: RuntimeChannel<BatchMessage> + std::fmt::Debug {
15+
pub trait JaegerTraceRuntime: RuntimeChannel + std::fmt::Debug {
1616
/// A communication socket between Jaeger client and agent.
1717
type Socket: std::fmt::Debug + Send + Sync;
1818

opentelemetry-otlp/src/logs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use opentelemetry::{
1919
global,
2020
logs::{LogError, LoggerProvider},
2121
};
22-
use opentelemetry_sdk::{self, export::logs::LogData, logs::BatchMessage, runtime::RuntimeChannel};
22+
use opentelemetry_sdk::{self, export::logs::LogData, runtime::RuntimeChannel};
2323

2424
/// Compression algorithm to use, defaults to none.
2525
pub const OTEL_EXPORTER_OTLP_LOGS_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_LOGS_COMPRESSION";
@@ -166,7 +166,7 @@ impl OtlpLogPipeline<LogExporterBuilder> {
166166
/// Returns a [`Logger`] with the name `opentelemetry-otlp` and the current crate version.
167167
///
168168
/// [`Logger`]: opentelemetry_sdk::logs::Logger
169-
pub fn install_batch<R: RuntimeChannel<BatchMessage>>(
169+
pub fn install_batch<R: RuntimeChannel>(
170170
self,
171171
runtime: R,
172172
) -> Result<opentelemetry_sdk::logs::Logger, LogError> {
@@ -198,7 +198,7 @@ fn build_simple_with_exporter(
198198
logger
199199
}
200200

201-
fn build_batch_with_exporter<R: RuntimeChannel<BatchMessage>>(
201+
fn build_batch_with_exporter<R: RuntimeChannel>(
202202
exporter: LogExporter,
203203
log_config: Option<opentelemetry_sdk::logs::Config>,
204204
runtime: R,

opentelemetry-otlp/src/span.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use opentelemetry::{
1212
use opentelemetry_sdk::{
1313
self as sdk,
1414
export::trace::{ExportResult, SpanData},
15-
trace::BatchMessage,
1615
};
1716
use opentelemetry_semantic_conventions::SCHEMA_URL;
1817
use sdk::runtime::RuntimeChannel;
@@ -122,7 +121,7 @@ impl OtlpTracePipeline<SpanExporterBuilder> {
122121
/// `install_batch` will panic if not called within a tokio runtime
123122
///
124123
/// [`Tracer`]: opentelemetry::trace::Tracer
125-
pub fn install_batch<R: RuntimeChannel<BatchMessage>>(
124+
pub fn install_batch<R: RuntimeChannel>(
126125
self,
127126
runtime: R,
128127
) -> Result<sdk::trace::Tracer, TraceError> {
@@ -154,7 +153,7 @@ fn build_simple_with_exporter(
154153
tracer
155154
}
156155

157-
fn build_batch_with_exporter<R: RuntimeChannel<BatchMessage>>(
156+
fn build_batch_with_exporter<R: RuntimeChannel>(
158157
exporter: SpanExporter,
159158
trace_config: Option<sdk::trace::Config>,
160159
runtime: R,

opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252

5353
`should_sample` changes `attributes` from `OrderMap<Key, Value>` to
5454
`Vec<KeyValue>`.
55+
- **Breaking** Move type argument from `RuntimeChannel<T>` to associated types [#1314](https://github.com/open-telemetry/opentelemetry-rust/pull/1314)
5556

5657
### Removed
5758

5859
- Remove context from Metric force_flush [#1245](https://github.com/open-telemetry/opentelemetry-rust/pull/1245)
60+
- Remove `logs::BatchMessage` and `trace::BatchMessage` types [#1314](https://github.com/open-telemetry/opentelemetry-rust/pull/1314)
5961

6062
### Fixed
6163

opentelemetry-sdk/src/logs/log_emitter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{BatchLogProcessor, BatchMessage, Config, LogProcessor, SimpleLogProcessor};
1+
use super::{BatchLogProcessor, Config, LogProcessor, SimpleLogProcessor};
22
use crate::{
33
export::logs::{LogData, LogExporter},
44
runtime::RuntimeChannel,
@@ -140,7 +140,7 @@ impl Builder {
140140
}
141141

142142
/// The `LogExporter` setup using a default `BatchLogProcessor` that this provider should use.
143-
pub fn with_batch_exporter<T: LogExporter + 'static, R: RuntimeChannel<BatchMessage>>(
143+
pub fn with_batch_exporter<T: LogExporter + 'static, R: RuntimeChannel>(
144144
self,
145145
exporter: T,
146146
runtime: R,

opentelemetry-sdk/src/logs/log_processor.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,19 @@ impl LogProcessor for SimpleLogProcessor {
109109

110110
/// A [`LogProcessor`] that asynchronously buffers log records and reports
111111
/// them at a preconfigured interval.
112-
pub struct BatchLogProcessor<R: RuntimeChannel<BatchMessage>> {
113-
message_sender: R::Sender,
112+
pub struct BatchLogProcessor<R: RuntimeChannel> {
113+
message_sender: R::Sender<BatchMessage>,
114114
}
115115

116-
impl<R: RuntimeChannel<BatchMessage>> Debug for BatchLogProcessor<R> {
116+
impl<R: RuntimeChannel> Debug for BatchLogProcessor<R> {
117117
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
118118
f.debug_struct("BatchLogProcessor")
119119
.field("message_sender", &self.message_sender)
120120
.finish()
121121
}
122122
}
123123

124-
impl<R: RuntimeChannel<BatchMessage>> LogProcessor for BatchLogProcessor<R> {
124+
impl<R: RuntimeChannel> LogProcessor for BatchLogProcessor<R> {
125125
fn emit(&self, data: LogData) {
126126
let result = self.message_sender.try_send(BatchMessage::ExportLog(data));
127127

@@ -158,7 +158,7 @@ impl<R: RuntimeChannel<BatchMessage>> LogProcessor for BatchLogProcessor<R> {
158158
}
159159
}
160160

161-
impl<R: RuntimeChannel<BatchMessage>> BatchLogProcessor<R> {
161+
impl<R: RuntimeChannel> BatchLogProcessor<R> {
162162
pub(crate) fn new(mut exporter: Box<dyn LogExporter>, config: BatchConfig, runtime: R) -> Self {
163163
let (message_sender, message_receiver) =
164164
runtime.batch_message_channel(config.max_queue_size);
@@ -262,7 +262,7 @@ async fn export_with_timeout<R, E>(
262262
batch: Vec<LogData>,
263263
) -> ExportResult
264264
where
265-
R: RuntimeChannel<BatchMessage>,
265+
R: RuntimeChannel,
266266
E: LogExporter + ?Sized,
267267
{
268268
if batch.is_empty() {
@@ -323,7 +323,7 @@ pub struct BatchLogProcessorBuilder<E, R> {
323323
impl<E, R> BatchLogProcessorBuilder<E, R>
324324
where
325325
E: LogExporter + 'static,
326-
R: RuntimeChannel<BatchMessage>,
326+
R: RuntimeChannel,
327327
{
328328
/// Set max queue size for batches
329329
pub fn with_max_queue_size(self, size: usize) -> Self {
@@ -372,7 +372,7 @@ where
372372
/// Messages sent between application thread and batch log processor's work thread.
373373
#[allow(clippy::large_enum_variant)]
374374
#[derive(Debug)]
375-
pub enum BatchMessage {
375+
enum BatchMessage {
376376
/// Export logs, usually called when the log is emitted.
377377
ExportLog(LogData),
378378
/// Flush the current buffer to the backend, it can be triggered by

opentelemetry-sdk/src/logs/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ mod log_processor;
77
pub use config::{config, Config};
88
pub use log_emitter::{Builder, Logger, LoggerProvider};
99
pub use log_processor::{
10-
BatchConfig, BatchLogProcessor, BatchLogProcessorBuilder, BatchMessage, LogProcessor,
11-
SimpleLogProcessor,
10+
BatchConfig, BatchLogProcessor, BatchLogProcessorBuilder, LogProcessor, SimpleLogProcessor,
1211
};

opentelemetry-sdk/src/runtime.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,22 @@ impl Runtime for AsyncStd {
133133
}
134134
}
135135

136-
/// `MessageRuntime` is an extension to [`Runtime`]. Currently, it provides a
136+
/// `RuntimeChannel` is an extension to [`Runtime`]. Currently, it provides a
137137
/// channel that is used by the [log] and [span] batch processors.
138138
///
139139
/// [log]: crate::logs::BatchLogProcessor
140140
/// [span]: crate::trace::BatchSpanProcessor
141-
pub trait RuntimeChannel<T: Debug + Send>: Runtime {
141+
pub trait RuntimeChannel: Runtime {
142142
/// A future stream to receive batch messages from channels.
143-
type Receiver: Stream<Item = T> + Send;
143+
type Receiver<T: Debug + Send>: Stream<Item = T> + Send;
144144
/// A batch messages sender that can be sent across threads safely.
145-
type Sender: TrySend<Message = T> + Debug;
145+
type Sender<T: Debug + Send>: TrySend<Message = T> + Debug;
146146

147147
/// Return the sender and receiver used to send batch messages.
148-
fn batch_message_channel(&self, capacity: usize) -> (Self::Sender, Self::Receiver);
148+
fn batch_message_channel<T: Debug + Send>(
149+
&self,
150+
capacity: usize,
151+
) -> (Self::Sender<T>, Self::Receiver<T>);
149152
}
150153

151154
/// Error returned by a [`TrySend`] implementation.
@@ -187,11 +190,14 @@ impl<T: Send> TrySend for tokio::sync::mpsc::Sender<T> {
187190

188191
#[cfg(feature = "rt-tokio")]
189192
#[cfg_attr(docsrs, doc(cfg(feature = "rt-tokio")))]
190-
impl<T: Debug + Send> RuntimeChannel<T> for Tokio {
191-
type Receiver = tokio_stream::wrappers::ReceiverStream<T>;
192-
type Sender = tokio::sync::mpsc::Sender<T>;
193-
194-
fn batch_message_channel(&self, capacity: usize) -> (Self::Sender, Self::Receiver) {
193+
impl RuntimeChannel for Tokio {
194+
type Receiver<T: Debug + Send> = tokio_stream::wrappers::ReceiverStream<T>;
195+
type Sender<T: Debug + Send> = tokio::sync::mpsc::Sender<T>;
196+
197+
fn batch_message_channel<T: Debug + Send>(
198+
&self,
199+
capacity: usize,
200+
) -> (Self::Sender<T>, Self::Receiver<T>) {
195201
let (sender, receiver) = tokio::sync::mpsc::channel(capacity);
196202
(
197203
sender,
@@ -202,11 +208,14 @@ impl<T: Debug + Send> RuntimeChannel<T> for Tokio {
202208

203209
#[cfg(feature = "rt-tokio-current-thread")]
204210
#[cfg_attr(docsrs, doc(cfg(feature = "rt-tokio-current-thread")))]
205-
impl<T: Debug + Send> RuntimeChannel<T> for TokioCurrentThread {
206-
type Receiver = tokio_stream::wrappers::ReceiverStream<T>;
207-
type Sender = tokio::sync::mpsc::Sender<T>;
208-
209-
fn batch_message_channel(&self, capacity: usize) -> (Self::Sender, Self::Receiver) {
211+
impl RuntimeChannel for TokioCurrentThread {
212+
type Receiver<T: Debug + Send> = tokio_stream::wrappers::ReceiverStream<T>;
213+
type Sender<T: Debug + Send> = tokio::sync::mpsc::Sender<T>;
214+
215+
fn batch_message_channel<T: Debug + Send>(
216+
&self,
217+
capacity: usize,
218+
) -> (Self::Sender<T>, Self::Receiver<T>) {
210219
let (sender, receiver) = tokio::sync::mpsc::channel(capacity);
211220
(
212221
sender,
@@ -229,11 +238,14 @@ impl<T: Send> TrySend for async_std::channel::Sender<T> {
229238

230239
#[cfg(feature = "rt-async-std")]
231240
#[cfg_attr(docsrs, doc(cfg(feature = "rt-async-std")))]
232-
impl<T: Debug + Send> RuntimeChannel<T> for AsyncStd {
233-
type Receiver = async_std::channel::Receiver<T>;
234-
type Sender = async_std::channel::Sender<T>;
235-
236-
fn batch_message_channel(&self, capacity: usize) -> (Self::Sender, Self::Receiver) {
241+
impl RuntimeChannel for AsyncStd {
242+
type Receiver<T: Debug + Send> = async_std::channel::Receiver<T>;
243+
type Sender<T: Debug + Send> = async_std::channel::Sender<T>;
244+
245+
fn batch_message_channel<T: Debug + Send>(
246+
&self,
247+
capacity: usize,
248+
) -> (Self::Sender<T>, Self::Receiver<T>) {
237249
async_std::channel::bounded(capacity)
238250
}
239251
}

opentelemetry-sdk/src/trace/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub use sampler::{Sampler, ShouldSample};
2626
pub use span::Span;
2727
pub use span_limit::SpanLimits;
2828
pub use span_processor::{
29-
BatchConfig, BatchMessage, BatchSpanProcessor, BatchSpanProcessorBuilder, SimpleSpanProcessor,
30-
SpanProcessor,
29+
BatchConfig, BatchSpanProcessor, BatchSpanProcessorBuilder, SimpleSpanProcessor, SpanProcessor,
3130
};
3231
pub use tracer::Tracer;
3332

opentelemetry-sdk/src/trace/provider.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! not duplicate this data to avoid that different [`Tracer`] instances
1010
//! of the [`TracerProvider`] have different versions of these data.
1111
use crate::runtime::RuntimeChannel;
12-
use crate::trace::{BatchMessage, BatchSpanProcessor, SimpleSpanProcessor, Tracer};
12+
use crate::trace::{BatchSpanProcessor, SimpleSpanProcessor, Tracer};
1313
use crate::{export::trace::SpanExporter, trace::SpanProcessor};
1414
use crate::{InstrumentationLibrary, Resource};
1515
use once_cell::sync::OnceCell;
@@ -166,7 +166,7 @@ impl Builder {
166166
}
167167

168168
/// The [`SpanExporter`] setup using a default [`BatchSpanProcessor`] that this provider should use.
169-
pub fn with_batch_exporter<T: SpanExporter + 'static, R: RuntimeChannel<BatchMessage>>(
169+
pub fn with_batch_exporter<T: SpanExporter + 'static, R: RuntimeChannel>(
170170
self,
171171
exporter: T,
172172
runtime: R,

opentelemetry-sdk/src/trace/runtime_tests.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::export::trace::{ExportResult, SpanExporter};
66
use crate::runtime;
77
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
88
use crate::runtime::RuntimeChannel;
9-
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
10-
use crate::trace::BatchMessage;
119
use futures_util::future::BoxFuture;
1210
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
1311
use opentelemetry::global::*;
@@ -42,7 +40,7 @@ impl SpanCountExporter {
4240
}
4341

4442
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
45-
fn build_batch_tracer_provider<R: RuntimeChannel<BatchMessage>>(
43+
fn build_batch_tracer_provider<R: RuntimeChannel>(
4644
exporter: SpanCountExporter,
4745
runtime: R,
4846
) -> crate::trace::TracerProvider {
@@ -61,9 +59,7 @@ fn build_simple_tracer_provider(exporter: SpanCountExporter) -> crate::trace::Tr
6159
}
6260

6361
#[cfg(any(feature = "rt-tokio", feature = "rt-tokio-current-thread"))]
64-
async fn test_set_provider_in_tokio<R: RuntimeChannel<BatchMessage>>(
65-
runtime: R,
66-
) -> Arc<AtomicUsize> {
62+
async fn test_set_provider_in_tokio<R: RuntimeChannel>(runtime: R) -> Arc<AtomicUsize> {
6763
let exporter = SpanCountExporter::new();
6864
let span_count = exporter.span_count.clone();
6965
let _ = set_tracer_provider(build_batch_tracer_provider(exporter, runtime));

opentelemetry-sdk/src/trace/sampler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Sampler {
156156
where
157157
C: HttpClient + 'static,
158158
Sampler: ShouldSample,
159-
R: crate::runtime::RuntimeChannel<crate::trace::BatchMessage>,
159+
R: crate::runtime::RuntimeChannel,
160160
Svc: Into<String>,
161161
{
162162
JaegerRemoteSamplerBuilder::new(runtime, http_client, default_sampler, service_name)

0 commit comments

Comments
 (0)