From 73b8fb46a36f97121c5809fc8cb2fecc286b90c9 Mon Sep 17 00:00:00 2001 From: MaeIsBad <26093674+MaeIsBad@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:58:55 +0100 Subject: [PATCH] [PLATFORM-1601]: Fix otlp exporter endpoint (#94) --- CHANGELOG.md | 14 ++++++- Cargo.toml | 3 +- README.md | 2 +- examples/datadog_json_logger.rs | 2 +- examples/ping.rs | 5 +-- examples/pong.rs | 5 +-- src/telemetry.rs | 15 +++++++- tests/e2e/mod.rs | 67 +++++++++++++++++++++++++-------- 8 files changed, 85 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8baccb..3cdce93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --- +## [0.9.0-rc.1] - 2024-03-11 + +### Changed + +- Automatically append /v1/traces to the collector endpoint + +This is only done if a /v1/traces suffix isn't already there, meaning old configurations should continue to function + +--- + ## [0.9.0-rc.0] - 2024-03-04 ### Changed @@ -137,7 +147,9 @@ If you are using Jaeger to collect traces locally on your machine, you will need -[Unreleased]: https://github.com/primait/prima_tracing.rs/compare/0.9.0...HEAD + +[Unreleased]: https://github.com/primait/prima_tracing.rs/compare/0.9.0-rc.1...HEAD +[0.9.0-rc.1]: https://github.com/primait/prima_tracing.rs/compare/0.9.0-rc.0...0.9.0-rc.1 [0.9.0]: https://github.com/primait/prima_tracing.rs/compare/0.8.1...0.9.0 [0.8.1]: https://github.com/primait/prima_tracing.rs/compare/0.8.0...0.8.1 [0.8.0]: https://github.com/primait/prima_tracing.rs/compare/0.7.2...0.8.0 diff --git a/Cargo.toml b/Cargo.toml index 4c705c8..bdbfbb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" name = "prima-tracing" readme = "README.md" repository = "https://github.com/primait/prima_tracing.rs" -version = "0.9.0-rc.0" +version = "0.9.0-rc.1" [features] default = [] @@ -52,6 +52,7 @@ serde_json = "^1.0" # dates chrono = {version = "^0.4", default-features = false, features = ["serde", "clock"]} +url = "2.5.0" [dev-dependencies] actix-web = "4.0.1" diff --git a/README.md b/README.md index 946daf7..85d8cf0 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ fn main() -> std::io::Result<()> { .with_env(Environment::Dev) .with_version("1.0".to_string()) .with_telemetry( - "http://localhost:55681/v1/traces".to_string(), + "http://localhost:55681".to_string(), "myapp".to_string(), ) .build(), diff --git a/examples/datadog_json_logger.rs b/examples/datadog_json_logger.rs index 2027851..0003f4b 100644 --- a/examples/datadog_json_logger.rs +++ b/examples/datadog_json_logger.rs @@ -11,7 +11,7 @@ async fn main() { .with_version("1.0".to_string()) // We need a tracer if we want trace and span IDs to be created and propagated, otherwise logs won't contain these correlation IDs // You can also setup custom tracer and custom subscriber if you don't wanna use the `traces` feature - .with_telemetry("http://localhost:55681/v1/traces".to_string(), service_name) + .with_telemetry("http://localhost:55681".to_string(), service_name) .build(), ); diff --git a/examples/ping.rs b/examples/ping.rs index 3a9a34b..c407370 100644 --- a/examples/ping.rs +++ b/examples/ping.rs @@ -15,10 +15,7 @@ async fn main() -> std::io::Result<()> { .with_env(Environment::Dev) .with_country(Country::Common) .with_version("1.0".to_string()) - .with_telemetry( - "http://localhost:55681/v1/traces".to_string(), - "ping".to_string(), - ) + .with_telemetry("http://localhost:55681".to_string(), "ping".to_string()) .build(), ); diff --git a/examples/pong.rs b/examples/pong.rs index 50bdf62..5277b75 100644 --- a/examples/pong.rs +++ b/examples/pong.rs @@ -10,10 +10,7 @@ async fn main() -> std::io::Result<()> { .with_env(Environment::Dev) .with_country(Country::Common) .with_version("1.0".to_string()) - .with_telemetry( - "http://localhost:55681/v1/traces".to_string(), - "pong".to_string(), - ) + .with_telemetry("http://localhost:55681".to_string(), "pong".to_string()) .build(), ); diff --git a/src/telemetry.rs b/src/telemetry.rs index 02b1dd7..334dd01 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -27,9 +27,22 @@ pub fn configure(config: &SubscriberConfig) -> Tracer { } }; + let collector_url = telemetry.collector_url.as_str(); + // OTLP before version 0.15 didn't append a /v1/traces suffix, but started doing so there. + // For backwards compatibility we strip it from configurations that do have it + let collector_url = collector_url + // In case of a trailing slash strip it + .strip_suffix('/') + .unwrap_or(collector_url) + .strip_suffix("/v1/traces") + .unwrap_or(collector_url); + + // Backport https://github.com/open-telemetry/opentelemetry-rust/pull/1553 + let collector_url = collector_url.strip_suffix('/').unwrap_or(collector_url); + let otlp_exporter = opentelemetry_otlp::new_exporter() .http() - .with_endpoint(telemetry.collector_url.as_str()); + .with_endpoint(collector_url.to_string()); let resource = Resource::new(vec![ KeyValue::new("environment", config.env.to_string()), diff --git a/tests/e2e/mod.rs b/tests/e2e/mod.rs index 16c674e..212ef73 100644 --- a/tests/e2e/mod.rs +++ b/tests/e2e/mod.rs @@ -2,15 +2,14 @@ use opentelemetry_jaeger::testing::jaeger_api_v2::Span; use opentelemetry_jaeger::testing::jaeger_client::JaegerTestClient; use prima_tracing::{builder, configure_subscriber, init_subscriber, Country, Environment}; -async fn get_spans(f: impl FnOnce()) -> Option> { +async fn get_spans(f: impl FnOnce(), collector_url: &str) -> Option> { std::env::set_var("RUST_LOG", "info"); // Unique id for this test run let seed = std::fs::read_to_string("/proc/sys/kernel/random/uuid").unwrap(); let service_name = format!("e2e-test-{seed}"); - let collector_url = "http://jaeger:55681"; - let query_api_url = "http://jaeger:16685"; + let query_api_url = "http://jaeger:16685/"; let subscriber = configure_subscriber( builder(&service_name) @@ -40,12 +39,15 @@ async fn get_spans(f: impl FnOnce()) -> Option> { async fn traces_are_sent_to_datadog() { let log_message = "hello traces_are_sent_to_datadog"; - let spans = get_spans(|| { - let span = tracing::info_span!("my span"); - span.in_scope(|| { - tracing::info!("{log_message}"); - }); - }) + let spans = get_spans( + || { + let span = tracing::info_span!("my span"); + span.in_scope(|| { + tracing::info!("{log_message}"); + }); + }, + "http://jaeger:55681", + ) .await .expect("Failed to fetch traces from jaeger"); @@ -59,12 +61,15 @@ async fn traces_are_sent_to_datadog() { #[cfg(feature = "traces")] #[tokio::test(flavor = "multi_thread")] async fn events_contain_metadata() { - let spans = get_spans(|| { - let span = tracing::info_span!("my span"); - span.in_scope(|| { - tracing::info!(hello = "meta!", "meta?"); - }); - }) + let spans = get_spans( + || { + let span = tracing::info_span!("my span"); + span.in_scope(|| { + tracing::info!(hello = "meta!", "meta?"); + }); + }, + "http://jaeger:55681", + ) .await .expect("Failed to fetch traces from jaeger"); @@ -78,3 +83,35 @@ async fn events_contain_metadata() { .v_str ); } + +#[cfg(feature = "traces")] +#[tokio::test(flavor = "multi_thread")] +async fn strips_trailing_slash() { + get_spans( + || { + let span = tracing::info_span!("my span"); + span.in_scope(|| { + tracing::info!(hello = "meta!", "meta?"); + }); + }, + "http://jaeger:55681/", + ) + .await + .expect("Failed to fetch traces from jaeger"); +} + +#[cfg(feature = "traces")] +#[tokio::test(flavor = "multi_thread")] +async fn strips_trailing_endpoint() { + get_spans( + || { + let span = tracing::info_span!("my span"); + span.in_scope(|| { + tracing::info!(hello = "meta!", "meta?"); + }); + }, + "http://jaeger:55681/v1/traces/", + ) + .await + .expect("Failed to fetch traces from jaeger"); +}