Skip to content

Commit 49fd1e5

Browse files
committed
make linter happy
1 parent e7f1352 commit 49fd1e5

File tree

5 files changed

+49
-52
lines changed

5 files changed

+49
-52
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: Some("http://localhost:4318/v1/metrics".to_string()),
49+
endpoint: "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: Some("http://localhost:4317".to_string()),
36+
endpoint: "http://localhost:4317".to_string(),
3737
..ExportConfig::default()
3838
};
3939
let provider = opentelemetry_otlp::new_pipeline()

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

+26-25
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ impl HttpExporterBuilder {
153153
signal_timeout_var: &str,
154154
signal_http_headers_var: &str,
155155
) -> Result<OtlpHttpClient, crate::Error> {
156-
let endpoint = resolve_endpoint(
156+
let endpoint = resolve_http_endpoint(
157157
signal_endpoint_var,
158158
signal_endpoint_path,
159-
self.exporter_config.endpoint.as_deref(),
159+
self.exporter_config.endpoint.as_str(),
160160
)?;
161161

162162
let timeout = match env::var(signal_timeout_var)
@@ -376,10 +376,10 @@ fn build_endpoint_uri(endpoint: &str, path: &str) -> Result<Uri, crate::Error> {
376376
}
377377

378378
// see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp
379-
fn resolve_endpoint(
379+
fn resolve_http_endpoint(
380380
signal_endpoint_var: &str,
381381
signal_endpoint_path: &str,
382-
provided_endpoint: Option<&str>,
382+
provided_endpoint: &str,
383383
) -> Result<Uri, crate::Error> {
384384
// per signal env var is not modified
385385
if let Some(endpoint) = env::var(signal_endpoint_var)
@@ -397,12 +397,13 @@ fn resolve_endpoint(
397397
return Ok(endpoint);
398398
}
399399

400-
match provided_endpoint {
401-
Some(endpoint) => build_endpoint_uri(endpoint, ""),
402-
None => build_endpoint_uri(
400+
if provided_endpoint.is_empty() {
401+
build_endpoint_uri(
403402
OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT,
404403
signal_endpoint_path,
405-
),
404+
)
405+
} else {
406+
build_endpoint_uri(provided_endpoint, "")
406407
}
407408
}
408409

@@ -424,17 +425,17 @@ mod tests {
424425
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
425426
};
426427

427-
use super::{build_endpoint_uri, resolve_endpoint};
428+
use super::{build_endpoint_uri, resolve_http_endpoint};
428429

429430
#[test]
430431
fn test_append_signal_path_to_generic_env() {
431432
run_env_test(
432433
vec![(OTEL_EXPORTER_OTLP_ENDPOINT, "http://example.com")],
433434
|| {
434-
let endpoint = resolve_endpoint(
435+
let endpoint = resolve_http_endpoint(
435436
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
436437
"/v1/traces",
437-
Some("http://localhost:4317"),
438+
"http://localhost:4317",
438439
)
439440
.unwrap();
440441
assert_eq!(endpoint, "http://example.com/v1/traces");
@@ -447,10 +448,10 @@ mod tests {
447448
run_env_test(
448449
vec![(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, "http://example.com")],
449450
|| {
450-
let endpoint = super::resolve_endpoint(
451+
let endpoint = super::resolve_http_endpoint(
451452
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
452453
"/v1/traces",
453-
Some("http://localhost:4317"),
454+
"http://localhost:4317",
454455
)
455456
.unwrap();
456457
assert_eq!(endpoint, "http://example.com");
@@ -466,10 +467,10 @@ mod tests {
466467
(OTEL_EXPORTER_OTLP_ENDPOINT, "http://wrong.com"),
467468
],
468469
|| {
469-
let endpoint = super::resolve_endpoint(
470+
let endpoint = super::resolve_http_endpoint(
470471
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
471472
"/v1/traces",
472-
Some("http://localhost:4317"),
473+
"http://localhost:4317",
473474
)
474475
.unwrap();
475476
assert_eq!(endpoint, "http://example.com");
@@ -480,10 +481,10 @@ mod tests {
480481
#[test]
481482
fn test_use_provided_or_default_when_others_missing() {
482483
run_env_test(vec![], || {
483-
let endpoint = super::resolve_endpoint(
484+
let endpoint = super::resolve_http_endpoint(
484485
"NON_EXISTENT_VAR",
485486
"/v1/traces",
486-
Some("http://localhost:4317"),
487+
"http://localhost:4317",
487488
)
488489
.unwrap();
489490
assert_eq!(endpoint, "http://localhost:4317/");
@@ -515,10 +516,10 @@ mod tests {
515516
(OTEL_EXPORTER_OTLP_ENDPOINT, "http://example.com"),
516517
],
517518
|| {
518-
let endpoint = super::resolve_endpoint(
519+
let endpoint = super::resolve_http_endpoint(
519520
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
520521
"/v1/traces",
521-
Some("http://localhost:4317"),
522+
"http://localhost:4317",
522523
)
523524
.unwrap();
524525
assert_eq!(endpoint, "http://example.com/v1/traces");
@@ -529,10 +530,10 @@ mod tests {
529530
#[test]
530531
fn test_all_invalid_urls_falls_back_to_error() {
531532
run_env_test(vec![], || {
532-
let result = super::resolve_endpoint(
533+
let result = super::resolve_http_endpoint(
533534
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
534535
"/v1/traces",
535-
Some("-*/*-/*-//-/-/yet-another-invalid-uri"),
536+
"-*/*-/*-//-/-/yet-another-invalid-uri",
536537
);
537538
assert!(result.is_err());
538539
// You may also want to assert on the specific error type if applicable
@@ -631,10 +632,10 @@ mod tests {
631632
run_env_test(vec![], || {
632633
let exporter = new_exporter().http();
633634

634-
let url = resolve_endpoint(
635+
let url = resolve_http_endpoint(
635636
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
636637
"/v1/traces",
637-
exporter.exporter_config.endpoint.as_deref(),
638+
exporter.exporter_config.endpoint.as_str(),
638639
)
639640
.unwrap();
640641

@@ -647,10 +648,10 @@ mod tests {
647648
.http()
648649
.with_endpoint("http://localhost:4318/v1/tracesbutnotreally");
649650

650-
let url = resolve_endpoint(
651+
let url = resolve_http_endpoint(
651652
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
652653
"/v1/traces",
653-
exporter.exporter_config.endpoint.as_deref(),
654+
exporter.exporter_config.endpoint.as_str(),
654655
)
655656
.unwrap();
656657

opentelemetry-otlp/src/exporter/mod.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) mod tonic;
7272
pub struct ExportConfig {
7373
/// The address of the OTLP collector. If it's not provided via builder or environment variables.
7474
/// Default address will be used based on the protocol.
75-
pub endpoint: Option<String>,
75+
pub endpoint: String,
7676

7777
/// The protocol to use when communicating with the collector.
7878
pub protocol: Protocol,
@@ -86,7 +86,9 @@ impl Default for ExportConfig {
8686
let protocol = default_protocol();
8787

8888
ExportConfig {
89-
endpoint: None,
89+
endpoint: "".to_string(),
90+
// don't use default_endpoint(protocol) here otherwise we
91+
// won't know if user provided a value
9092
protocol,
9193
timeout: Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
9294
}
@@ -130,15 +132,6 @@ fn default_protocol() -> Protocol {
130132
}
131133
}
132134

133-
/// default endpoint for protocol
134-
fn default_endpoint(protocol: Protocol) -> String {
135-
match protocol {
136-
Protocol::Grpc => OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT.to_string(),
137-
Protocol::HttpBinary => OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT.to_string(),
138-
Protocol::HttpJson => OTEL_EXPORTER_OTLP_HTTP_ENDPOINT_DEFAULT.to_string(),
139-
}
140-
}
141-
142135
/// default user-agent headers
143136
#[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))]
144137
fn default_headers() -> std::collections::HashMap<String, String> {
@@ -185,7 +178,7 @@ impl HasExportConfig for HttpExporterBuilder {
185178
/// # }
186179
/// ```
187180
pub trait WithExportConfig {
188-
/// Set the address of the OTLP collector. If not set, the default address is used.
181+
/// Set the address of the OTLP collector. If not set or set to empty string, the default address is used.
189182
fn with_endpoint<T: Into<String>>(self, endpoint: T) -> Self;
190183
/// Set the protocol to use when communicating with the collector.
191184
///
@@ -203,7 +196,7 @@ pub trait WithExportConfig {
203196

204197
impl<B: HasExportConfig> WithExportConfig for B {
205198
fn with_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self {
206-
self.export_config().endpoint = Some(endpoint.into());
199+
self.export_config().endpoint = endpoint.into();
207200
self
208201
}
209202

@@ -299,15 +292,15 @@ mod tests {
299292
fn test_default_http_endpoint() {
300293
let exporter_builder = crate::new_exporter().http();
301294

302-
assert_eq!(exporter_builder.exporter_config.endpoint, None,);
295+
assert_eq!(exporter_builder.exporter_config.endpoint, "");
303296
}
304297

305298
#[cfg(feature = "grpc-tonic")]
306299
#[test]
307300
fn test_default_tonic_endpoint() {
308301
let exporter_builder = crate::new_exporter().tonic();
309302

310-
assert_eq!(exporter_builder.exporter_config.endpoint, None);
303+
assert_eq!(exporter_builder.exporter_config.endpoint, "");
311304
}
312305

313306
#[test]

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ impl TonicExporterBuilder {
212212
fn build_channel(
213213
self,
214214
signal_endpoint_var: &str,
215-
signal_endpoint_path: &str,
216215
signal_timeout_var: &str,
217216
signal_compression_var: &str,
218217
signal_headers_var: &str,
@@ -254,17 +253,24 @@ impl TonicExporterBuilder {
254253
}
255254

256255
let config = self.exporter_config;
256+
257+
// resolving endpoint string
258+
// grpc doesn't have a "path" like http(See https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md)
259+
// the path of grpc calls are based on the protobuf service definition
260+
// so we won't append one for default grpc endpoints
261+
// If users for some reason want to use a custom path, they can use env var or builder to pass it
257262
let endpoint = match env::var(signal_endpoint_var)
258263
.ok()
259264
.or(env::var(OTEL_EXPORTER_OTLP_ENDPOINT).ok())
260265
{
261266
Some(val) => val,
262-
None => format!(
263-
"{}{signal_endpoint_path}",
264-
config
265-
.endpoint
266-
.unwrap_or(OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT.to_string())
267-
),
267+
None => {
268+
if config.endpoint.is_empty() {
269+
OTEL_EXPORTER_OTLP_GRPC_ENDPOINT_DEFAULT.to_string()
270+
} else {
271+
config.endpoint
272+
}
273+
}
268274
};
269275

270276
let endpoint = Channel::from_shared(endpoint).map_err(crate::Error::from)?;
@@ -304,7 +310,6 @@ impl TonicExporterBuilder {
304310

305311
let (channel, interceptor, compression) = self.build_channel(
306312
crate::logs::OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
307-
"/v1/logs",
308313
crate::logs::OTEL_EXPORTER_OTLP_LOGS_TIMEOUT,
309314
crate::logs::OTEL_EXPORTER_OTLP_LOGS_COMPRESSION,
310315
crate::logs::OTEL_EXPORTER_OTLP_LOGS_HEADERS,
@@ -327,7 +332,6 @@ impl TonicExporterBuilder {
327332

328333
let (channel, interceptor, compression) = self.build_channel(
329334
crate::metric::OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
330-
"/v1/metrics",
331335
crate::metric::OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
332336
crate::metric::OTEL_EXPORTER_OTLP_METRICS_COMPRESSION,
333337
crate::metric::OTEL_EXPORTER_OTLP_METRICS_HEADERS,
@@ -351,7 +355,6 @@ impl TonicExporterBuilder {
351355

352356
let (channel, interceptor, compression) = self.build_channel(
353357
crate::span::OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
354-
"/v1/traces",
355358
crate::span::OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
356359
crate::span::OTEL_EXPORTER_OTLP_TRACES_COMPRESSION,
357360
crate::span::OTEL_EXPORTER_OTLP_TRACES_HEADERS,

0 commit comments

Comments
 (0)