diff --git a/examples/axum-otlp/Cargo.toml b/examples/axum-otlp/Cargo.toml index d161dbf..9b5160e 100644 --- a/examples/axum-otlp/Cargo.toml +++ b/examples/axum-otlp/Cargo.toml @@ -16,7 +16,6 @@ init-tracing-opentelemetry = { path = "../../init-tracing-opentelemetry", featur ] } opentelemetry = { workspace = true } opentelemetry-otlp = { workspace = true, default-features = false, features = [ - "reqwest-client", "reqwest-rustls", "http-proto", "tls", diff --git a/examples/axum-otlp/README.md b/examples/axum-otlp/README.md index 533a620..044536f 100644 --- a/examples/axum-otlp/README.md +++ b/examples/axum-otlp/README.md @@ -2,11 +2,22 @@ In a terminal, run +Configure the [environment variables](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/) for the OTLP exporter: + +```sh +# For GRPC: +export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4317" +export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="grpc" +export OTEL_TRACES_SAMPLER="always_on" + +# For HTTP: +export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://127.0.0.1:4318/v1/traces" +export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf" +export OTEL_TRACES_SAMPLER="always_on" +``` + ```sh ❯ cd examples/axum-otlp -> # or direnv allow -❯ export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317 -❯ export OTEL_TRACES_SAMPLER=always_on ❯ cargo run Compiling examples-axum-otlp v0.1.0 (/home/david/src/github.com/davidB/axum-tracing-opentelemetry/examples/axum-otlp) Finished dev [unoptimized + debuginfo] target(s) in 3.60s diff --git a/init-tracing-opentelemetry/README.md b/init-tracing-opentelemetry/README.md index c83613f..0cb04ab 100644 --- a/init-tracing-opentelemetry/README.md +++ b/init-tracing-opentelemetry/README.md @@ -91,6 +91,18 @@ Few other environment variables can also be used to configure OTLP exporter (eg - [`OTEL_EXPORTER_OTLP_HEADERS`](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/#otel_exporter_otlp_headers) - [`OTEL_EXPORTER_OTLP_TRACES_HEADERS`](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/#otel_exporter_otlp_traces_headers) +```sh +# For GRPC: +export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4317" +export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="grpc" +export OTEL_TRACES_SAMPLER="always_on" + +# For HTTP: +export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://127.0.0.1:4318/v1/traces" +export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf" +export OTEL_TRACES_SAMPLER="always_on" +``` + In the context of **kubernetes**, some of the above environment variables can be injected by the Opentelemetry operator (via `inject-sdk`): ```yaml diff --git a/init-tracing-opentelemetry/src/otlp.rs b/init-tracing-opentelemetry/src/otlp.rs index ccf1e5b..ddb91bc 100644 --- a/init-tracing-opentelemetry/src/otlp.rs +++ b/init-tracing-opentelemetry/src/otlp.rs @@ -65,6 +65,7 @@ fn parse_headers(val: &str) -> impl Iterator + '_ { s }) } + fn read_headers_from_env() -> HashMap { let mut headers = HashMap::new(); headers.extend(parse_headers( @@ -75,13 +76,21 @@ fn read_headers_from_env() -> HashMap { )); headers } + fn read_protocol_and_endpoint_from_env() -> (Option, Option) { - let maybe_endpoint = std::env::var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT") - .or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT")) - .ok(); let maybe_protocol = std::env::var("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL") .or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_PROTOCOL")) .ok(); + let maybe_endpoint = std::env::var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT") + .or_else(|_| { + std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT").map(|endpoint| match &maybe_protocol { + Some(protocol) if protocol.contains("http") => { + format!("{endpoint}/v1/traces") + } + _ => endpoint, + }) + }) + .ok(); (maybe_protocol, maybe_endpoint) } @@ -142,7 +151,7 @@ fn infer_protocol_and_endpoint( } let endpoint = match protocol { - "http/protobuf" => maybe_endpoint.unwrap_or("http://localhost:4318"), //Devskim: ignore DS137138 + "http/protobuf" => maybe_endpoint.unwrap_or("http://localhost:4318/v1/traces"), //Devskim: ignore DS137138 _ => maybe_endpoint.unwrap_or("http://localhost:4317"), //Devskim: ignore DS137138 }; @@ -157,8 +166,13 @@ mod tests { use super::*; #[rstest] - #[case(None, None, "http/protobuf", "http://localhost:4318")] //Devskim: ignore DS137138 - #[case(Some("http/protobuf"), None, "http/protobuf", "http://localhost:4318")] //Devskim: ignore DS137138 + #[case(None, None, "http/protobuf", "http://localhost:4318/v1/traces")] //Devskim: ignore DS137138 + #[case( + Some("http/protobuf"), + None, + "http/protobuf", + "http://localhost:4318/v1/traces" + )] //Devskim: ignore DS137138 #[case(Some("grpc"), None, "grpc", "http://localhost:4317")] //Devskim: ignore DS137138 #[case(None, Some("http://localhost:4317"), "grpc", "http://localhost:4317")] //Devskim: ignore DS137138 #[cfg_attr( @@ -181,15 +195,15 @@ mod tests { )] #[case( Some("http/protobuf"), - Some("http://localhost:4318"), //Devskim: ignore DS137138 + Some("http://localhost:4318/v1/traces"), //Devskim: ignore DS137138 "http/protobuf", - "http://localhost:4318" //Devskim: ignore DS137138 + "http://localhost:4318/v1/traces" //Devskim: ignore DS137138 )] #[case( Some("http/protobuf"), - Some("https://examples.com:4318"), + Some("https://examples.com:4318/v1/traces"), "http/protobuf", - "https://examples.com:4318" + "https://examples.com:4318/v1/traces" )] #[case( Some("http/protobuf"),