Skip to content

Commit 0fd3265

Browse files
committed
added build_default to MetricExporterBuilder
1 parent 8499914 commit 0fd3265

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

opentelemetry-otlp/src/exporter/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = OTEL_EXPORTER_OTLP_PROTOCO
4646
/// Default protocol if no features are enabled.
4747
pub const OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT: &str = "";
4848

49-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
50-
const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
51-
const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
49+
/// http-protobuf Exporter protocol
50+
pub const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF: &str = "http/protobuf";
51+
52+
/// grpc Exporter protocol
53+
pub const OTEL_EXPORTER_OTLP_PROTOCOL_GRPC: &str = "grpc";
54+
55+
/// http-json Exporter protocol
56+
pub const OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON: &str = "http/json";
5257

5358
/// Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
5459
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";

opentelemetry-otlp/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ pub use crate::exporter::tonic::{HasTonicConfig, WithTonicConfig};
401401
pub use crate::exporter::{
402402
HasExportConfig, WithExportConfig, OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
403403
OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_PROTOCOL,
404-
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT, OTEL_EXPORTER_OTLP_TIMEOUT,
405-
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
404+
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT, OTEL_EXPORTER_OTLP_PROTOCOL_GRPC,
405+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON, OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF,
406+
OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
406407
};
407408

408409
/// Type to indicate the builder does not have a client set.

opentelemetry-otlp/src/logs.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ use opentelemetry_sdk::{error::OTelSdkResult, logs::LogBatch};
88
use std::fmt::Debug;
99
use std::time;
1010

11-
use crate::{ExporterBuildError, HasExportConfig, NoExporterBuilderSet};
11+
use crate::{
12+
ExporterBuildError, HasExportConfig, NoExporterBuilderSet, OTEL_EXPORTER_OTLP_PROTOCOL,
13+
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT,
14+
};
1215

1316
#[cfg(feature = "grpc-tonic")]
14-
use crate::{HasTonicConfig, TonicExporterBuilder, TonicExporterBuilderSet};
17+
use crate::{
18+
HasTonicConfig, TonicExporterBuilder, TonicExporterBuilderSet, OTEL_EXPORTER_OTLP_PROTOCOL_GRPC,
19+
};
20+
21+
#[cfg(feature = "http-proto")]
22+
use crate::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF;
23+
24+
#[cfg(feature = "http-json")]
25+
use crate::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
1526

1627
#[cfg(any(feature = "http-proto", feature = "http-json"))]
1728
use crate::{HasHttpConfig, HttpExporterBuilder, HttpExporterBuilderSet};
@@ -123,6 +134,24 @@ impl LogExporter {
123134
LogExporterBuilder::default()
124135
}
125136

137+
/// Builds a default [LogExporter] based on the value of `OTEL_EXPORTER_OTLP_PROTOCOL`.
138+
pub fn build_default(self) -> Result<LogExporter, ExporterBuildError> {
139+
match std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL)
140+
.unwrap_or(OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT.to_string())
141+
.as_str()
142+
{
143+
#[cfg(feature = "grpc-tonic")]
144+
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Self::builder().with_tonic().build(),
145+
#[cfg(feature = "http-proto")]
146+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Self::builder().with_http().build(),
147+
#[cfg(feature = "http-json")]
148+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Self::builder().with_http().build(),
149+
other => Err(ExporterBuildError::InternalFailure(format!(
150+
"Invalid {OTEL_EXPORTER_OTLP_PROTOCOL}: {other}"
151+
))),
152+
}
153+
}
154+
126155
#[cfg(any(feature = "http-proto", feature = "http-json"))]
127156
pub(crate) fn from_http(client: crate::exporter::http::OtlpHttpClient) -> Self {
128157
LogExporter {

opentelemetry-otlp/src/metric.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ use crate::HasExportConfig;
99
#[cfg(any(feature = "http-proto", feature = "http-json"))]
1010
use crate::{exporter::http::HttpExporterBuilder, HasHttpConfig, HttpExporterBuilderSet};
1111

12+
#[cfg(feature = "http-proto")]
13+
use crate::exporter::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF;
14+
15+
#[cfg(feature = "http-json")]
16+
use crate::exporter::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
17+
1218
#[cfg(feature = "grpc-tonic")]
13-
use crate::{exporter::tonic::TonicExporterBuilder, HasTonicConfig, TonicExporterBuilderSet};
19+
use crate::{
20+
exporter::{tonic::TonicExporterBuilder, OTEL_EXPORTER_OTLP_PROTOCOL_GRPC},
21+
HasTonicConfig, TonicExporterBuilderSet,
22+
};
1423

15-
use crate::{ExporterBuildError, NoExporterBuilderSet};
24+
use crate::{
25+
ExporterBuildError, NoExporterBuilderSet, OTEL_EXPORTER_OTLP_PROTOCOL,
26+
OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT,
27+
};
1628

1729
use core::fmt;
1830
use opentelemetry_sdk::error::OTelSdkResult;
@@ -187,6 +199,24 @@ impl MetricExporter {
187199
MetricExporterBuilder::default()
188200
}
189201

202+
/// Builds a default [MetricExporter] based on the value of `OTEL_EXPORTER_OTLP_PROTOCOL`.
203+
pub fn build_default(self) -> Result<MetricExporter, ExporterBuildError> {
204+
match std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL)
205+
.unwrap_or(OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT.to_string())
206+
.as_str()
207+
{
208+
#[cfg(feature = "grpc-tonic")]
209+
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Self::builder().with_tonic().build(),
210+
#[cfg(feature = "http-proto")]
211+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Self::builder().with_http().build(),
212+
#[cfg(feature = "http-json")]
213+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Self::builder().with_http().build(),
214+
other => Err(ExporterBuildError::InternalFailure(format!(
215+
"Invalid {OTEL_EXPORTER_OTLP_PROTOCOL}: {other}"
216+
))),
217+
}
218+
}
219+
190220
#[cfg(feature = "grpc-tonic")]
191221
pub(crate) fn from_tonic(
192222
client: crate::exporter::tonic::metrics::TonicMetricsClient,

opentelemetry-otlp/src/span.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ use std::fmt::Debug;
77
use opentelemetry_sdk::error::OTelSdkResult;
88
use opentelemetry_sdk::trace::SpanData;
99

10-
use crate::ExporterBuildError;
1110
#[cfg(feature = "grpc-tonic")]
1211
use crate::{
1312
exporter::tonic::{HasTonicConfig, TonicExporterBuilder},
14-
TonicExporterBuilderSet,
13+
TonicExporterBuilderSet, OTEL_EXPORTER_OTLP_PROTOCOL_GRPC,
1514
};
15+
use crate::{ExporterBuildError, OTEL_EXPORTER_OTLP_PROTOCOL, OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT};
16+
17+
#[cfg(feature = "http-proto")]
18+
use crate::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF;
19+
20+
#[cfg(feature = "http-json")]
21+
use crate::OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON;
1622

1723
#[cfg(any(feature = "http-proto", feature = "http-json"))]
1824
use crate::{
@@ -126,6 +132,24 @@ impl SpanExporter {
126132
SpanExporterBuilder::default()
127133
}
128134

135+
/// Builds a default [SpanExporter] based on the value of `OTEL_EXPORTER_OTLP_PROTOCOL`.
136+
pub fn build_default(self) -> Result<SpanExporter, ExporterBuildError> {
137+
match std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL)
138+
.unwrap_or(OTEL_EXPORTER_OTLP_PROTOCOL_DEFAULT.to_string())
139+
.as_str()
140+
{
141+
#[cfg(feature = "grpc-tonic")]
142+
OTEL_EXPORTER_OTLP_PROTOCOL_GRPC => Self::builder().with_tonic().build(),
143+
#[cfg(feature = "http-proto")]
144+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_PROTOBUF => Self::builder().with_http().build(),
145+
#[cfg(feature = "http-json")]
146+
OTEL_EXPORTER_OTLP_PROTOCOL_HTTP_JSON => Self::builder().with_http().build(),
147+
other => Err(ExporterBuildError::InternalFailure(format!(
148+
"Invalid {OTEL_EXPORTER_OTLP_PROTOCOL}: {other}"
149+
))),
150+
}
151+
}
152+
129153
#[cfg(any(feature = "http-proto", feature = "http-json"))]
130154
pub(crate) fn from_http(client: crate::exporter::http::OtlpHttpClient) -> Self {
131155
SpanExporter {

0 commit comments

Comments
 (0)