Skip to content

fix: Expose SpanExporterBuilder and MetricExporterBuilder #2966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -374,22 +374,25 @@ pub use crate::exporter::ExporterBuildError;
#[cfg(feature = "trace")]
#[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))]
pub use crate::span::{
SpanExporter, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_TRACES_HEADERS, OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
SpanExporter, SpanExporterBuilder, OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_HEADERS,
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
};

#[cfg(feature = "metrics")]
#[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))]
pub use crate::metric::{
MetricExporter, OTEL_EXPORTER_OTLP_METRICS_COMPRESSION, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_HEADERS, OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
MetricExporter, MetricExporterBuilder, OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, OTEL_EXPORTER_OTLP_METRICS_HEADERS,
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
};

#[cfg(feature = "logs")]
#[cfg(any(feature = "http-proto", feature = "http-json", feature = "grpc-tonic"))]
pub use crate::logs::{
LogExporter, OTEL_EXPORTER_OTLP_LOGS_COMPRESSION, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
OTEL_EXPORTER_OTLP_LOGS_HEADERS, OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
LogExporter, LogExporterBuilder, OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_HEADERS,
OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
};

#[cfg(any(feature = "http-proto", feature = "http-json"))]
6 changes: 6 additions & 0 deletions opentelemetry-otlp/src/logs.rs
Original file line number Diff line number Diff line change
@@ -31,17 +31,20 @@ pub const OTEL_EXPORTER_OTLP_LOGS_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_LOGS_TIMEO
/// Note: this is only supported for HTTP.
pub const OTEL_EXPORTER_OTLP_LOGS_HEADERS: &str = "OTEL_EXPORTER_OTLP_LOGS_HEADERS";

/// Builder for creating a new [LogExporter].
#[derive(Debug, Default, Clone)]
pub struct LogExporterBuilder<C> {
client: C,
endpoint: Option<String>,
}

impl LogExporterBuilder<NoExporterBuilderSet> {
/// Create a new [LogExporterBuilder] with default settings.
pub fn new() -> Self {
LogExporterBuilder::default()
}

/// With the gRPC Tonic transport.
#[cfg(feature = "grpc-tonic")]
pub fn with_tonic(self) -> LogExporterBuilder<TonicExporterBuilderSet> {
LogExporterBuilder {
@@ -50,6 +53,7 @@ impl LogExporterBuilder<NoExporterBuilderSet> {
}
}

/// With the HTTP transport.
#[cfg(any(feature = "http-proto", feature = "http-json"))]
pub fn with_http(self) -> LogExporterBuilder<HttpExporterBuilderSet> {
LogExporterBuilder {
@@ -61,6 +65,7 @@ impl LogExporterBuilder<NoExporterBuilderSet> {

#[cfg(feature = "grpc-tonic")]
impl LogExporterBuilder<TonicExporterBuilderSet> {
/// Build the [LogExporter] with the gRPC Tonic transport.
pub fn build(self) -> Result<LogExporter, ExporterBuildError> {
let result = self.client.0.build_log_exporter();
otel_debug!(name: "LogExporterBuilt", result = format!("{:?}", &result));
@@ -70,6 +75,7 @@ impl LogExporterBuilder<TonicExporterBuilderSet> {

#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl LogExporterBuilder<HttpExporterBuilderSet> {
/// Build the [LogExporter] with the HTTP transport.
pub fn build(self) -> Result<LogExporter, ExporterBuildError> {
self.client.0.build_log_exporter()
}
7 changes: 7 additions & 0 deletions opentelemetry-otlp/src/metric.rs
Original file line number Diff line number Diff line change
@@ -37,19 +37,22 @@ pub const OTEL_EXPORTER_OTLP_METRICS_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_MET
/// Note: this is only supported for HTTP.
pub const OTEL_EXPORTER_OTLP_METRICS_HEADERS: &str = "OTEL_EXPORTER_OTLP_METRICS_HEADERS";

/// A builder for creating a new [MetricExporter].
#[derive(Debug, Default, Clone)]
pub struct MetricExporterBuilder<C> {
client: C,
temporality: Temporality,
}

impl MetricExporterBuilder<NoExporterBuilderSet> {
/// Create a new [MetricExporterBuilder] with default settings.
pub fn new() -> Self {
MetricExporterBuilder::default()
}
}

impl<C> MetricExporterBuilder<C> {
/// With the gRPC Tonic transport.
#[cfg(feature = "grpc-tonic")]
pub fn with_tonic(self) -> MetricExporterBuilder<TonicExporterBuilderSet> {
MetricExporterBuilder {
@@ -58,6 +61,7 @@ impl<C> MetricExporterBuilder<C> {
}
}

/// With the HTTP transport.
#[cfg(any(feature = "http-proto", feature = "http-json"))]
pub fn with_http(self) -> MetricExporterBuilder<HttpExporterBuilderSet> {
MetricExporterBuilder {
@@ -66,6 +70,7 @@ impl<C> MetricExporterBuilder<C> {
}
}

/// Set the temporality for the metrics.
pub fn with_temporality(self, temporality: Temporality) -> MetricExporterBuilder<C> {
MetricExporterBuilder {
client: self.client,
@@ -76,6 +81,7 @@ impl<C> MetricExporterBuilder<C> {

#[cfg(feature = "grpc-tonic")]
impl MetricExporterBuilder<TonicExporterBuilderSet> {
/// Build the [MetricExporter] with the gRPC Tonic transport.
pub fn build(self) -> Result<MetricExporter, ExporterBuildError> {
let exporter = self.client.0.build_metrics_exporter(self.temporality)?;
opentelemetry::otel_debug!(name: "MetricExporterBuilt");
@@ -85,6 +91,7 @@ impl MetricExporterBuilder<TonicExporterBuilderSet> {

#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl MetricExporterBuilder<HttpExporterBuilderSet> {
/// Build the [MetricExporter] with the HTTP transport.
pub fn build(self) -> Result<MetricExporter, ExporterBuildError> {
let exporter = self.client.0.build_metrics_exporter(self.temporality)?;
Ok(exporter)
6 changes: 6 additions & 0 deletions opentelemetry-otlp/src/span.rs
Original file line number Diff line number Diff line change
@@ -36,23 +36,27 @@ pub const OTEL_EXPORTER_OTLP_TRACES_COMPRESSION: &str = "OTEL_EXPORTER_OTLP_TRAC
/// Note: this is only supported for HTTP.
pub const OTEL_EXPORTER_OTLP_TRACES_HEADERS: &str = "OTEL_EXPORTER_OTLP_TRACES_HEADERS";

/// OTLP span exporter builder
#[derive(Debug, Default, Clone)]
pub struct SpanExporterBuilder<C> {
client: C,
}

impl SpanExporterBuilder<NoExporterBuilderSet> {
/// Create a new [SpanExporterBuilder] with default settings.
pub fn new() -> Self {
SpanExporterBuilder::default()
}

/// With the gRPC Tonic transport.
#[cfg(feature = "grpc-tonic")]
pub fn with_tonic(self) -> SpanExporterBuilder<TonicExporterBuilderSet> {
SpanExporterBuilder {
client: TonicExporterBuilderSet(TonicExporterBuilder::default()),
}
}

/// With the HTTP transport.
#[cfg(any(feature = "http-proto", feature = "http-json"))]
pub fn with_http(self) -> SpanExporterBuilder<HttpExporterBuilderSet> {
SpanExporterBuilder {
@@ -63,6 +67,7 @@ impl SpanExporterBuilder<NoExporterBuilderSet> {

#[cfg(feature = "grpc-tonic")]
impl SpanExporterBuilder<TonicExporterBuilderSet> {
/// Build the [SpanExporter] with the gRPC Tonic transport.
pub fn build(self) -> Result<SpanExporter, ExporterBuildError> {
let span_exporter = self.client.0.build_span_exporter()?;
opentelemetry::otel_debug!(name: "SpanExporterBuilt");
@@ -72,6 +77,7 @@ impl SpanExporterBuilder<TonicExporterBuilderSet> {

#[cfg(any(feature = "http-proto", feature = "http-json"))]
impl SpanExporterBuilder<HttpExporterBuilderSet> {
/// Build the [SpanExporter] with the HTTP transport.
pub fn build(self) -> Result<SpanExporter, ExporterBuildError> {
let span_exporter = self.client.0.build_span_exporter()?;
Ok(span_exporter)