Skip to content

Commit

Permalink
fix: inference of OTEL_EXPORTER_OTLP_TRACES_ENDPOINT for protocol `…
Browse files Browse the repository at this point in the history
…http/protobuf`
  • Loading branch information
davidB committed Dec 10, 2024
1 parent fd72087 commit 0f45ecb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
1 change: 0 additions & 1 deletion examples/axum-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 14 additions & 3 deletions examples/axum-otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions init-tracing-opentelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions init-tracing-opentelemetry/src/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn parse_headers(val: &str) -> impl Iterator<Item = (String, String)> + '_ {
s
})
}

fn read_headers_from_env() -> HashMap<String, String> {
let mut headers = HashMap::new();
headers.extend(parse_headers(
Expand All @@ -75,13 +76,21 @@ fn read_headers_from_env() -> HashMap<String, String> {
));
headers
}

fn read_protocol_and_endpoint_from_env() -> (Option<String>, Option<String>) {
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)
}

Expand Down Expand Up @@ -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
};

Expand All @@ -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(
Expand All @@ -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"),
Expand Down

0 comments on commit 0f45ecb

Please sign in to comment.