Skip to content

Commit

Permalink
[PLATFORM-1601]: Fix otlp exporter endpoint (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad authored Mar 12, 2024
1 parent 072da11 commit 73b8fb4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 28 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion examples/datadog_json_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down
5 changes: 1 addition & 4 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down
5 changes: 1 addition & 4 deletions examples/pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);

Expand Down
15 changes: 14 additions & 1 deletion src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,22 @@ pub fn configure<T>(config: &SubscriberConfig<T>) -> 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()),
Expand Down
67 changes: 52 additions & 15 deletions tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Span>> {
async fn get_spans(f: impl FnOnce(), collector_url: &str) -> Option<Vec<Span>> {
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)
Expand Down Expand Up @@ -40,12 +39,15 @@ async fn get_spans(f: impl FnOnce()) -> Option<Vec<Span>> {
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");

Expand All @@ -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");

Expand All @@ -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");
}

0 comments on commit 73b8fb4

Please sign in to comment.