diff --git a/CHANGELOG.md b/CHANGELOG.md index ee0f02d..898ef90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Updated + +- Bumped opentelemetry version to 0.23 + --- ## [0.9.4] - 2024-05-20 diff --git a/Cargo.toml b/Cargo.toml index c95c3e5..9d79a37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,12 +37,12 @@ required-features = ["json-logger"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -opentelemetry = {version = "0.22", optional = true} -opentelemetry-otlp = {version = "0.15", features = ["http-proto", "reqwest-client"], default-features = false, optional = true} -opentelemetry_sdk = {version = "0.22", features = ["rt-tokio"], optional = true} +opentelemetry = {version = "0.23", optional = true} +opentelemetry-otlp = {version = "0.16", features = ["http-proto", "reqwest-client"], default-features = false, optional = true} +opentelemetry_sdk = {version = "0.23", features = ["rt-tokio"], optional = true} tracing = {version = "0.1", features = ["max_level_debug", "release_max_level_info"]} tracing-log = {version = "0.2"} -tracing-opentelemetry = {version = "0.23", optional = true} +tracing-opentelemetry = {version = "0.24", optional = true} tracing-subscriber = {version = "0.3", features = ["env-filter"]} # serialization/deserialization @@ -56,7 +56,7 @@ url = "2.5.0" [dev-dependencies] actix-web = "4.0.1" -opentelemetry-jaeger = {version = "0.21", features = ["integration_test"]} +opentelemetry-jaeger = {version = "0.22", features = ["integration_test"]} prima_bridge = "0.16" tokio = {version = "1.17", features = ["rt", "macros", "rt-multi-thread"]} tracing-actix-web = {version = "0.7.0", features = ["opentelemetry_0_21"]} diff --git a/src/telemetry.rs b/src/telemetry.rs index 043a20c..4fc8aca 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -7,6 +7,25 @@ use opentelemetry_sdk::{ use crate::SubscriberConfig; +fn normalize_collector_url(collector_url: &str) -> String { + // 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); + + // And now starting from version 0.23 opentelemetry randomly stopped appending + // the url suffix, so we need to do it ourselves. + // This was not announced in the changelogs 🙃 + collector_url.to_string() + "/v1/traces" +} + pub fn configure(config: &SubscriberConfig) -> Tracer { let telemetry = config .telemetry @@ -24,22 +43,11 @@ 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 collector_url = normalize_collector_url(&telemetry.collector_url); let otlp_exporter = opentelemetry_otlp::new_exporter() .http() - .with_endpoint(collector_url.to_string()); + .with_endpoint(collector_url); let resource = Resource::new(vec![ KeyValue::new("environment", config.env.to_string()), @@ -54,3 +62,25 @@ pub fn configure(config: &SubscriberConfig) -> Tracer { .install_batch(runtime) .expect("Failed to configure the OpenTelemetry tracer") } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn normalize_collector_url_test() { + let base = "http://localhost:8080"; + let expected = "http://localhost:8080/v1/traces"; + + assert_eq!(normalize_collector_url(base), expected); + + let with_trailing_slash = format!("{}/", base); + assert_eq!( + normalize_collector_url(with_trailing_slash.as_str()), + expected + ); + + let complete = format!("{}/v1/traces", base); + assert_eq!(normalize_collector_url(complete.as_str()), expected); + } +}