Skip to content

Commit 0f45ecb

Browse files
committed
fix: inference of OTEL_EXPORTER_OTLP_TRACES_ENDPOINT for protocol http/protobuf
1 parent fd72087 commit 0f45ecb

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

examples/axum-otlp/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ init-tracing-opentelemetry = { path = "../../init-tracing-opentelemetry", featur
1616
] }
1717
opentelemetry = { workspace = true }
1818
opentelemetry-otlp = { workspace = true, default-features = false, features = [
19-
"reqwest-client",
2019
"reqwest-rustls",
2120
"http-proto",
2221
"tls",

examples/axum-otlp/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
In a terminal, run
44

5+
Configure the [environment variables](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/) for the OTLP exporter:
6+
7+
```sh
8+
# For GRPC:
9+
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4317"
10+
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="grpc"
11+
export OTEL_TRACES_SAMPLER="always_on"
12+
13+
# For HTTP:
14+
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://127.0.0.1:4318/v1/traces"
15+
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf"
16+
export OTEL_TRACES_SAMPLER="always_on"
17+
```
18+
519
```sh
620
cd examples/axum-otlp
7-
> # or direnv allow
8-
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317
9-
export OTEL_TRACES_SAMPLER=always_on
1021
❯ cargo run
1122
Compiling examples-axum-otlp v0.1.0 (/home/david/src/github.com/davidB/axum-tracing-opentelemetry/examples/axum-otlp)
1223
Finished dev [unoptimized + debuginfo] target(s) in 3.60s

init-tracing-opentelemetry/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ Few other environment variables can also be used to configure OTLP exporter (eg
9191
- [`OTEL_EXPORTER_OTLP_HEADERS`](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/#otel_exporter_otlp_headers)
9292
- [`OTEL_EXPORTER_OTLP_TRACES_HEADERS`](https://opentelemetry.io/docs/languages/sdk-configuration/otlp-exporter/#otel_exporter_otlp_traces_headers)
9393

94+
```sh
95+
# For GRPC:
96+
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://localhost:4317"
97+
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="grpc"
98+
export OTEL_TRACES_SAMPLER="always_on"
99+
100+
# For HTTP:
101+
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://127.0.0.1:4318/v1/traces"
102+
export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL="http/protobuf"
103+
export OTEL_TRACES_SAMPLER="always_on"
104+
```
105+
94106
In the context of **kubernetes**, some of the above environment variables can be injected by the Opentelemetry operator (via `inject-sdk`):
95107

96108
```yaml

init-tracing-opentelemetry/src/otlp.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn parse_headers(val: &str) -> impl Iterator<Item = (String, String)> + '_ {
6565
s
6666
})
6767
}
68+
6869
fn read_headers_from_env() -> HashMap<String, String> {
6970
let mut headers = HashMap::new();
7071
headers.extend(parse_headers(
@@ -75,13 +76,21 @@ fn read_headers_from_env() -> HashMap<String, String> {
7576
));
7677
headers
7778
}
79+
7880
fn read_protocol_and_endpoint_from_env() -> (Option<String>, Option<String>) {
79-
let maybe_endpoint = std::env::var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
80-
.or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT"))
81-
.ok();
8281
let maybe_protocol = std::env::var("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL")
8382
.or_else(|_| std::env::var("OTEL_EXPORTER_OTLP_PROTOCOL"))
8483
.ok();
84+
let maybe_endpoint = std::env::var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
85+
.or_else(|_| {
86+
std::env::var("OTEL_EXPORTER_OTLP_ENDPOINT").map(|endpoint| match &maybe_protocol {
87+
Some(protocol) if protocol.contains("http") => {
88+
format!("{endpoint}/v1/traces")
89+
}
90+
_ => endpoint,
91+
})
92+
})
93+
.ok();
8594
(maybe_protocol, maybe_endpoint)
8695
}
8796

@@ -142,7 +151,7 @@ fn infer_protocol_and_endpoint(
142151
}
143152

144153
let endpoint = match protocol {
145-
"http/protobuf" => maybe_endpoint.unwrap_or("http://localhost:4318"), //Devskim: ignore DS137138
154+
"http/protobuf" => maybe_endpoint.unwrap_or("http://localhost:4318/v1/traces"), //Devskim: ignore DS137138
146155
_ => maybe_endpoint.unwrap_or("http://localhost:4317"), //Devskim: ignore DS137138
147156
};
148157

@@ -157,8 +166,13 @@ mod tests {
157166
use super::*;
158167

159168
#[rstest]
160-
#[case(None, None, "http/protobuf", "http://localhost:4318")] //Devskim: ignore DS137138
161-
#[case(Some("http/protobuf"), None, "http/protobuf", "http://localhost:4318")] //Devskim: ignore DS137138
169+
#[case(None, None, "http/protobuf", "http://localhost:4318/v1/traces")] //Devskim: ignore DS137138
170+
#[case(
171+
Some("http/protobuf"),
172+
None,
173+
"http/protobuf",
174+
"http://localhost:4318/v1/traces"
175+
)] //Devskim: ignore DS137138
162176
#[case(Some("grpc"), None, "grpc", "http://localhost:4317")] //Devskim: ignore DS137138
163177
#[case(None, Some("http://localhost:4317"), "grpc", "http://localhost:4317")] //Devskim: ignore DS137138
164178
#[cfg_attr(
@@ -181,15 +195,15 @@ mod tests {
181195
)]
182196
#[case(
183197
Some("http/protobuf"),
184-
Some("http://localhost:4318"), //Devskim: ignore DS137138
198+
Some("http://localhost:4318/v1/traces"), //Devskim: ignore DS137138
185199
"http/protobuf",
186-
"http://localhost:4318" //Devskim: ignore DS137138
200+
"http://localhost:4318/v1/traces" //Devskim: ignore DS137138
187201
)]
188202
#[case(
189203
Some("http/protobuf"),
190-
Some("https://examples.com:4318"),
204+
Some("https://examples.com:4318/v1/traces"),
191205
"http/protobuf",
192-
"https://examples.com:4318"
206+
"https://examples.com:4318/v1/traces"
193207
)]
194208
#[case(
195209
Some("http/protobuf"),

0 commit comments

Comments
 (0)