Skip to content

Commit a307f3d

Browse files
committed
feat: replace OtlpPipeline with exporter builders
1 parent 4852a5e commit a307f3d

File tree

10 files changed

+171
-561
lines changed

10 files changed

+171
-561
lines changed

examples/tracing-jaeger/src/main.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@ use opentelemetry::{
44
trace::{TraceContextExt, TraceError, Tracer},
55
KeyValue,
66
};
7-
use opentelemetry_otlp::WithExportConfig;
7+
use opentelemetry_sdk::trace::TracerProvider;
88
use opentelemetry_sdk::{runtime, trace as sdktrace, Resource};
99
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;
1010

1111
use std::error::Error;
1212

1313
fn init_tracer_provider() -> Result<opentelemetry_sdk::trace::TracerProvider, TraceError> {
14-
opentelemetry_otlp::new_pipeline()
15-
.tracing()
16-
.with_exporter(
17-
opentelemetry_otlp::new_exporter()
18-
.tonic()
19-
.with_endpoint("http://localhost:4317"),
20-
)
21-
.with_trace_config(
14+
let exporter = opentelemetry_otlp::SpanExporter::builder()
15+
.with_tonic()
16+
.build()?;
17+
18+
Ok(TracerProvider::builder()
19+
.with_batch_exporter(exporter, runtime::Tokio)
20+
.with_config(
2221
sdktrace::Config::default().with_resource(Resource::new(vec![KeyValue::new(
2322
SERVICE_NAME,
2423
"tracing-jaeger",
2524
)])),
2625
)
27-
.install_batch(runtime::Tokio)
26+
.build())
2827
}
2928

3029
#[tokio::main]

opentelemetry-otlp/examples/basic-otlp/src/main.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use opentelemetry::{
88
KeyValue,
99
};
1010
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
11-
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
12-
use opentelemetry_sdk::trace::Config;
11+
use opentelemetry_otlp::{ExportConfig, LogExporter, MetricsExporter, SpanExporter};
12+
use opentelemetry_sdk::logs::LoggerProvider;
13+
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
1314
use opentelemetry_sdk::{runtime, trace as sdktrace, Resource};
1415
use std::error::Error;
1516
use tracing::info;
@@ -24,43 +25,31 @@ static RESOURCE: Lazy<Resource> = Lazy::new(|| {
2425
});
2526

2627
fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
27-
opentelemetry_otlp::new_pipeline()
28-
.tracing()
29-
.with_exporter(
30-
opentelemetry_otlp::new_exporter()
31-
.tonic()
32-
.with_endpoint("http://localhost:4317"),
33-
)
34-
.with_trace_config(Config::default().with_resource(RESOURCE.clone()))
35-
.install_batch(runtime::Tokio)
28+
let b = SpanExporter::builder().with_tonic().build()?;
29+
Ok(sdktrace::TracerProvider::builder()
30+
.with_batch_exporter(b, runtime::Tokio)
31+
.build())
3632
}
3733

3834
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
3935
let export_config = ExportConfig {
4036
endpoint: "http://localhost:4317".to_string(),
4137
..ExportConfig::default()
4238
};
43-
opentelemetry_otlp::new_pipeline()
44-
.metrics(runtime::Tokio)
45-
.with_exporter(
46-
opentelemetry_otlp::new_exporter()
47-
.tonic()
48-
.with_export_config(export_config),
49-
)
50-
.with_resource(RESOURCE.clone())
51-
.build()
39+
40+
let exporter = MetricsExporter::builder().with_tonic().build()?;
41+
42+
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
43+
44+
Ok(SdkMeterProvider::builder().with_reader(reader).build())
5245
}
5346

5447
fn init_logs() -> Result<opentelemetry_sdk::logs::LoggerProvider, LogError> {
55-
opentelemetry_otlp::new_pipeline()
56-
.logging()
57-
.with_resource(RESOURCE.clone())
58-
.with_exporter(
59-
opentelemetry_otlp::new_exporter()
60-
.tonic()
61-
.with_endpoint("http://localhost:4317"),
62-
)
63-
.install_batch(runtime::Tokio)
48+
let exporter = LogExporter::builder().with_tonic().build()?;
49+
50+
Ok(LoggerProvider::builder()
51+
.with_batch_exporter(exporter, runtime::Tokio)
52+
.build())
6453
}
6554

6655
#[tokio::main]

opentelemetry-otlp/src/exporter/http/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,7 @@ fn add_header_from_string(input: &str, headers: &mut HashMap<HeaderName, HeaderV
409409
mod tests {
410410
use crate::exporter::tests::run_env_test;
411411
use crate::{
412-
new_exporter, WithExportConfig, OTEL_EXPORTER_OTLP_ENDPOINT,
413-
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
412+
HttpExporterBuilder, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
414413
};
415414

416415
use super::{build_endpoint_uri, resolve_http_endpoint};
@@ -618,7 +617,7 @@ mod tests {
618617
fn test_http_exporter_endpoint() {
619618
// default endpoint should add signal path
620619
run_env_test(vec![], || {
621-
let exporter = new_exporter().http();
620+
let exporter = HttpExporterBuilder::default();
622621

623622
let url = resolve_http_endpoint(
624623
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
@@ -632,9 +631,7 @@ mod tests {
632631

633632
// if builder endpoint is set, it should not add signal path
634633
run_env_test(vec![], || {
635-
let exporter = new_exporter()
636-
.http()
637-
.with_endpoint("http://localhost:4318/v1/tracesbutnotreally");
634+
let exporter = HttpExporterBuilder::default();
638635

639636
let url = resolve_http_endpoint(
640637
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,

opentelemetry-otlp/src/exporter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,15 @@ mod tests {
291291
#[cfg(any(feature = "http-proto", feature = "http-json"))]
292292
#[test]
293293
fn test_default_http_endpoint() {
294-
let exporter_builder = crate::new_exporter().http();
294+
let exporter_builder = crate::HttpExporterBuilder::default();
295295

296296
assert_eq!(exporter_builder.exporter_config.endpoint, "");
297297
}
298298

299299
#[cfg(feature = "grpc-tonic")]
300300
#[test]
301301
fn test_default_tonic_endpoint() {
302-
let exporter_builder = crate::new_exporter().tonic();
302+
let exporter_builder = crate::TonicExporterBuilder::default();
303303

304304
assert_eq!(exporter_builder.exporter_config.endpoint, "");
305305
}

opentelemetry-otlp/src/exporter/tonic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod logs;
2525
mod metrics;
2626

2727
#[cfg(feature = "trace")]
28-
mod trace;
28+
pub(crate) mod trace;
2929

3030
/// Configuration for [tonic]
3131
///

opentelemetry-otlp/src/lib.rs

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,21 @@ pub use crate::exporter::Compression;
225225
pub use crate::exporter::ExportConfig;
226226
#[cfg(feature = "trace")]
227227
pub use crate::span::{
228-
OtlpTracePipeline, SpanExporter, SpanExporterBuilder, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
228+
SpanExporter, SpanExporterBuilder, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
229229
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_HEADERS,
230230
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
231231
};
232232

233233
#[cfg(feature = "metrics")]
234234
pub use crate::metric::{
235-
MetricsExporter, MetricsExporterBuilder, OtlpMetricPipeline,
236-
OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
235+
MetricsExporter, OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
237236
OTEL_EXPORTER_OTLP_METRICS_HEADERS, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
238237
};
239238

240239
#[cfg(feature = "logs")]
241240
pub use crate::logs::{
242-
LogExporter, LogExporterBuilder, OtlpLogPipeline, OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
243-
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_HEADERS,
244-
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
241+
LogExporter, OTEL_EXPORTER_OTLP_LOGS_COMPRESSION, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
242+
OTEL_EXPORTER_OTLP_LOGS_HEADERS, OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
245243
};
246244

247245
pub use crate::exporter::{
@@ -253,6 +251,38 @@ pub use crate::exporter::{
253251

254252
use opentelemetry_sdk::export::ExportError;
255253

254+
/// OTLP span exporter builder.
255+
#[derive(Debug)]
256+
// This enum only used during initialization stage of application. The overhead should be OK.
257+
// Users can also disable the unused features to make the overhead on object size smaller.
258+
#[allow(clippy::large_enum_variant)]
259+
#[non_exhaustive]
260+
pub enum ExporterOption {
261+
/// Tonic span exporter builder
262+
#[cfg(feature = "grpc-tonic")]
263+
Tonic(TonicExporterBuilder),
264+
/// Http span exporter builder
265+
#[cfg(any(feature = "http-proto", feature = "http-json"))]
266+
Http(HttpExporterBuilder),
267+
/// Missing exporter builder
268+
#[doc(hidden)]
269+
#[cfg(not(any(feature = "http-proto", feature = "grpc-tonic")))]
270+
Unconfigured,
271+
}
272+
273+
impl Default for ExporterOption {
274+
fn default() -> Self {
275+
#[cfg(feature = "grpc-tonic")]
276+
ExporterOption::Tonic(TonicExporterBuilder::default())
277+
}
278+
}
279+
280+
#[derive(Debug, Default, Clone)]
281+
pub struct NoExporterBuilder;
282+
283+
#[derive(Debug, Default)]
284+
pub struct ExporterBuilder(ExporterOption);
285+
256286
#[cfg(any(feature = "http-proto", feature = "http-json"))]
257287
pub use crate::exporter::http::HttpExporterBuilder;
258288

@@ -262,55 +292,6 @@ pub use crate::exporter::tonic::{TonicConfig, TonicExporterBuilder};
262292
#[cfg(feature = "serialize")]
263293
use serde::{Deserialize, Serialize};
264294

265-
/// General builder for both tracing and metrics.
266-
#[derive(Debug)]
267-
pub struct OtlpPipeline;
268-
269-
/// Build a OTLP metrics or tracing exporter builder. See functions below to understand
270-
/// what's currently supported.
271-
#[derive(Debug)]
272-
pub struct OtlpExporterPipeline;
273-
274-
impl OtlpExporterPipeline {
275-
/// Use tonic as grpc layer, return a `TonicExporterBuilder` to config tonic and build the exporter.
276-
///
277-
/// This exporter can be used in both `tracing` and `metrics` pipeline.
278-
#[cfg(feature = "grpc-tonic")]
279-
pub fn tonic(self) -> TonicExporterBuilder {
280-
TonicExporterBuilder::default()
281-
}
282-
283-
/// Use HTTP as transport layer, return a `HttpExporterBuilder` to config the http transport
284-
/// and build the exporter.
285-
///
286-
/// This exporter can be used in both `tracing` and `metrics` pipeline.
287-
#[cfg(any(feature = "http-proto", feature = "http-json"))]
288-
pub fn http(self) -> HttpExporterBuilder {
289-
HttpExporterBuilder::default()
290-
}
291-
}
292-
293-
/// Create a new pipeline builder with the recommended configuration.
294-
///
295-
/// ## Examples
296-
///
297-
/// ```no_run
298-
/// fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
299-
/// # #[cfg(feature = "trace")]
300-
/// let tracing_builder = opentelemetry_otlp::new_pipeline().tracing();
301-
///
302-
/// Ok(())
303-
/// }
304-
/// ```
305-
pub fn new_pipeline() -> OtlpPipeline {
306-
OtlpPipeline
307-
}
308-
309-
/// Create a builder to build OTLP metrics exporter or tracing exporter.
310-
pub fn new_exporter() -> OtlpExporterPipeline {
311-
OtlpExporterPipeline
312-
}
313-
314295
/// Wrap type for errors from this crate.
315296
#[derive(thiserror::Error, Debug)]
316297
pub enum Error {

0 commit comments

Comments
 (0)