Skip to content

Commit 5ca76f0

Browse files
committed
1 parent ccd5f08 commit 5ca76f0

File tree

6 files changed

+80
-34
lines changed

6 files changed

+80
-34
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
4646

4747
fn init_metrics() -> Result<(), MetricsError> {
4848
let export_config = opentelemetry_otlp::ExportConfig {
49-
endpoint: "http://localhost:4318/v1/metrics".to_string(),
49+
endpoint: Some("http://localhost:4318/v1/metrics".to_string()),
5050
..opentelemetry_otlp::ExportConfig::default()
5151
};
5252
let provider = opentelemetry_otlp::new_pipeline()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
3333

3434
fn init_metrics() -> Result<(), MetricsError> {
3535
let export_config = ExportConfig {
36-
endpoint: "http://localhost:4317".to_string(),
36+
endpoint: Some("http://localhost:4317".to_string()),
3737
..ExportConfig::default()
3838
};
3939
let provider = opentelemetry_otlp::new_pipeline()

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

+62-16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use std::str::FromStr;
2020
use std::sync::{Arc, Mutex};
2121
use std::time::Duration;
2222

23+
use super::{default_headers, parse_header_string, OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT};
24+
2325
#[cfg(feature = "metrics")]
2426
mod metrics;
2527

@@ -153,7 +155,7 @@ impl HttpExporterBuilder {
153155
let endpoint = resolve_endpoint(
154156
signal_endpoint_var,
155157
signal_endpoint_path,
156-
self.exporter_config.endpoint.as_str(),
158+
self.exporter_config.endpoint.as_deref(),
157159
)?;
158160

159161
let timeout = match env::var(signal_timeout_var)
@@ -376,7 +378,7 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
376378
fn resolve_endpoint(
377379
signal_endpoint_var: &str,
378380
signal_endpoint_path: &str,
379-
provided_or_default_endpoint: &str,
381+
provided_endpoint: Option<&str>,
380382
) -> Result<Uri, crate::Error> {
381383
// per signal env var is not modified
382384
if let Some(endpoint) = env::var(signal_endpoint_var)
@@ -394,8 +396,13 @@ fn resolve_endpoint(
394396
return Ok(endpoint);
395397
}
396398

397-
// if neither works, we use the one provided in pipeline. If user never provides one, we will use the default one
398-
build_endpoint_uri(provided_or_default_endpoint, signal_endpoint_path)
399+
match provided_endpoint {
400+
Some(endpoint) => build_endpoint_uri(endpoint, ""),
401+
None => build_endpoint_uri(
402+
OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT,
403+
signal_endpoint_path,
404+
),
405+
}
399406
}
400407

401408
#[allow(clippy::mutable_key_type)] // http headers are not mutated
@@ -411,19 +418,22 @@ fn add_header_from_string(input: &str, headers: &mut HashMap<HeaderName, HeaderV
411418
#[cfg(test)]
412419
mod tests {
413420
use crate::exporter::tests::run_env_test;
414-
use crate::{OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT};
421+
use crate::{
422+
new_exporter, WithExportConfig, OTEL_EXPORTER_OTLP_ENDPOINT,
423+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
424+
};
415425

416-
use super::build_endpoint_uri;
426+
use super::{build_endpoint_uri, resolve_endpoint};
417427

418428
#[test]
419429
fn test_append_signal_path_to_generic_env() {
420430
run_env_test(
421431
vec![(OTEL_EXPORTER_OTLP_ENDPOINT, "http://example.com")],
422432
|| {
423-
let endpoint = super::resolve_endpoint(
433+
let endpoint = resolve_endpoint(
424434
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
425435
"/v1/traces",
426-
"http://localhost:4317",
436+
Some("http://localhost:4317"),
427437
)
428438
.unwrap();
429439
assert_eq!(endpoint, "http://example.com/v1/traces");
@@ -439,7 +449,7 @@ mod tests {
439449
let endpoint = super::resolve_endpoint(
440450
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
441451
"/v1/traces",
442-
"http://localhost:4317",
452+
Some("http://localhost:4317"),
443453
)
444454
.unwrap();
445455
assert_eq!(endpoint, "http://example.com");
@@ -458,7 +468,7 @@ mod tests {
458468
let endpoint = super::resolve_endpoint(
459469
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
460470
"/v1/traces",
461-
"http://localhost:4317",
471+
Some("http://localhost:4317"),
462472
)
463473
.unwrap();
464474
assert_eq!(endpoint, "http://example.com");
@@ -469,10 +479,13 @@ mod tests {
469479
#[test]
470480
fn test_use_provided_or_default_when_others_missing() {
471481
run_env_test(vec![], || {
472-
let endpoint =
473-
super::resolve_endpoint("NON_EXISTENT_VAR", "/v1/traces", "http://localhost:4317")
474-
.unwrap();
475-
assert_eq!(endpoint, "http://localhost:4317/v1/traces");
482+
let endpoint = super::resolve_endpoint(
483+
"NON_EXISTENT_VAR",
484+
"/v1/traces",
485+
Some("http://localhost:4317"),
486+
)
487+
.unwrap();
488+
assert_eq!(endpoint, "http://localhost:4317/");
476489
});
477490
}
478491

@@ -504,7 +517,7 @@ mod tests {
504517
let endpoint = super::resolve_endpoint(
505518
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
506519
"/v1/traces",
507-
"http://localhost:4317",
520+
Some("http://localhost:4317"),
508521
)
509522
.unwrap();
510523
assert_eq!(endpoint, "http://example.com/v1/traces");
@@ -518,7 +531,7 @@ mod tests {
518531
let result = super::resolve_endpoint(
519532
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
520533
"/v1/traces",
521-
"-*/*-/*-//-/-/yet-another-invalid-uri",
534+
Some("-*/*-/*-//-/-/yet-another-invalid-uri"),
522535
);
523536
assert!(result.is_err());
524537
// You may also want to assert on the specific error type if applicable
@@ -610,4 +623,37 @@ mod tests {
610623
}
611624
}
612625
}
626+
627+
#[test]
628+
fn test_http_exporter_endpoint() {
629+
// default endpoint should add signal path
630+
run_env_test(vec![], || {
631+
let exporter = new_exporter().http();
632+
633+
let url = resolve_endpoint(
634+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
635+
"/v1/traces",
636+
exporter.exporter_config.endpoint.as_deref(),
637+
)
638+
.unwrap();
639+
640+
assert_eq!(url, "http://localhost:4318/v1/traces");
641+
});
642+
643+
// if builder endpoint is set, it should not add signal path
644+
run_env_test(vec![], || {
645+
let exporter = new_exporter()
646+
.http()
647+
.with_endpoint("http://localhost:4318/v1/tracesbutnotreally");
648+
649+
let url = resolve_endpoint(
650+
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
651+
"/v1/traces",
652+
exporter.exporter_config.endpoint.as_deref(),
653+
)
654+
.unwrap();
655+
656+
assert_eq!(url, "http://localhost:4318/v1/tracesbutnotreally");
657+
});
658+
}
613659
}

opentelemetry-otlp/src/exporter/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
5858
pub const OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT: u64 = 10;
5959

6060
// Endpoints per protocol https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
61+
#[cfg(feature = "grpc-tonic")]
6162
const OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT: &str = "http://localhost:4317";
6263
const OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT: &str = "http://localhost:4318";
6364

@@ -69,8 +70,9 @@ pub(crate) mod tonic;
6970
/// Configuration for the OTLP exporter.
7071
#[derive(Debug)]
7172
pub struct ExportConfig {
72-
/// The base address of the OTLP collector. If not set, the default address is used.
73-
pub endpoint: String,
73+
/// The address of the OTLP collector. If it's not provided via builder or environment variables.
74+
/// Default address will be used based on the protocol.
75+
pub endpoint: Option<String>,
7476

7577
/// The protocol to use when communicating with the collector.
7678
pub protocol: Protocol,
@@ -84,7 +86,7 @@ impl Default for ExportConfig {
8486
let protocol = default_protocol();
8587

8688
ExportConfig {
87-
endpoint: default_endpoint(protocol),
89+
endpoint: None,
8890
protocol,
8991
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
9092
}
@@ -201,7 +203,7 @@ pub trait WithExportConfig {
201203

202204
impl<B: HasExportConfig> WithExportConfig for B {
203205
fn with_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
204-
self.export_config().endpoint = endpoint.into();
206+
self.export_config().endpoint = Some(endpoint.into());
205207
self
206208
}
207209

@@ -297,21 +299,15 @@ mod tests {
297299
fn test_default_http_endpoint() {
298300
let exporter_builder = crate::new_exporter().http();
299301

300-
assert_eq!(
301-
exporter_builder.exporter_config.endpoint,
302-
"http://localhost:4318"
303-
);
302+
assert_eq!(exporter_builder.exporter_config.endpoint, None,);
304303
}
305304

306305
#[cfg(feature = "grpc-tonic")]
307306
#[test]
308307
fn test_default_tonic_endpoint() {
309308
let exporter_builder = crate::new_exporter().tonic();
310309

311-
assert_eq!(
312-
exporter_builder.exporter_config.endpoint,
313-
"http://localhost:4317"
314-
);
310+
assert_eq!(exporter_builder.exporter_config.endpoint, None);
315311
}
316312

317313
#[test]

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use tonic::transport::Channel;
1111
#[cfg(feature = "tls")]
1212
use tonic::transport::ClientTlsConfig;
1313

14-
use super::{default_headers, parse_header_string};
14+
use super::{default_headers, parse_header_string, OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT};
1515
use crate::exporter::Compression;
1616
use crate::{
1717
ExportConfig, OTEL_EXPORTER_OTLP_COMPRESSION, OTEL_EXPORTER_OTLP_ENDPOINT,
@@ -149,7 +149,6 @@ impl Default for TonicExporterBuilder {
149149
TonicExporterBuilder {
150150
exporter_config: ExportConfig {
151151
protocol: crate::Protocol::Grpc,
152-
endpoint: crate::exporter::default_endpoint(crate::Protocol::Grpc),
153152
..Default::default()
154153
},
155154
tonic_config,
@@ -260,7 +259,12 @@ impl TonicExporterBuilder {
260259
.or(env::var(OTEL_EXPORTER_OTLP_ENDPOINT).ok())
261260
{
262261
Some(val) => val,
263-
None => format!("{}{signal_endpoint_path}", config.endpoint),
262+
None => format!(
263+
"{}{signal_endpoint_path}",
264+
config
265+
.endpoint
266+
.unwrap_or(OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT.to_string())
267+
),
264268
};
265269

266270
let endpoint = Channel::from_shared(endpoint).map_err(crate::Error::from)?;

opentelemetry-otlp/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
//! # #[cfg(all(feature = "metrics", feature = "grpc-tonic"))]
163163
//! # {
164164
//! let export_config = ExportConfig {
165-
//! endpoint: "http://localhost:4317".to_string(),
165+
//! endpoint: Some("http://localhost:4317".to_string()),
166166
//! timeout: Duration::from_secs(3),
167167
//! protocol: Protocol::Grpc
168168
//! };

0 commit comments

Comments
 (0)