Skip to content

Commit 432f06b

Browse files
committed
chore(tracing): Use upstream semantic convention definitions
The proxy's tracing labels currently use hardcoded strings for semantic convention labels. This replaces those with the upstream ones defined by the OpenTelemetry libraries. Signed-off-by: Scott Fleener <[email protected]>
1 parent c21d8be commit 432f06b

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

Cargo.lock

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,26 @@ dependencies = [
379379
"serde_core",
380380
]
381381

382+
[[package]]
383+
name = "const_format"
384+
version = "0.2.35"
385+
source = "registry+https://github.com/rust-lang/crates.io-index"
386+
checksum = "7faa7469a93a566e9ccc1c73fe783b4a65c274c5ace346038dca9c39fe0030ad"
387+
dependencies = [
388+
"const_format_proc_macros",
389+
]
390+
391+
[[package]]
392+
name = "const_format_proc_macros"
393+
version = "0.2.34"
394+
source = "registry+https://github.com/rust-lang/crates.io-index"
395+
checksum = "1d57c2eccfb16dbac1f4e61e206105db5820c9d26c3c472bc17c774259ef7744"
396+
dependencies = [
397+
"proc-macro2",
398+
"quote",
399+
"unicode-xid",
400+
]
401+
382402
[[package]]
383403
name = "cpp_demangle"
384404
version = "0.4.5"
@@ -2066,6 +2086,7 @@ dependencies = [
20662086
"linkerd-tracing",
20672087
"opentelemetry",
20682088
"opentelemetry-proto",
2089+
"opentelemetry-semantic-conventions",
20692090
"opentelemetry_sdk",
20702091
"tokio",
20712092
"tonic",
@@ -2583,13 +2604,15 @@ version = "0.1.0"
25832604
dependencies = [
25842605
"base64",
25852606
"bytes",
2607+
"const_format",
25862608
"futures",
25872609
"hex",
25882610
"http",
25892611
"linkerd-error",
25902612
"linkerd-http-box",
25912613
"linkerd-stack",
25922614
"linkerd-tracing",
2615+
"opentelemetry-semantic-conventions",
25932616
"rand 0.9.2",
25942617
"thiserror",
25952618
"tokio",
@@ -2946,6 +2969,12 @@ dependencies = [
29462969
"tonic-prost",
29472970
]
29482971

2972+
[[package]]
2973+
name = "opentelemetry-semantic-conventions"
2974+
version = "0.31.0"
2975+
source = "registry+https://github.com/rust-lang/crates.io-index"
2976+
checksum = "e62e29dfe041afb8ed2a6c9737ab57db4907285d999ef8ad3a59092a36bdc846"
2977+
29492978
[[package]]
29502979
name = "opentelemetry_sdk"
29512980
version = "0.31.0"
@@ -4217,6 +4246,12 @@ version = "1.0.19"
42174246
source = "registry+https://github.com/rust-lang/crates.io-index"
42184247
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
42194248

4249+
[[package]]
4250+
name = "unicode-xid"
4251+
version = "0.2.6"
4252+
source = "registry+https://github.com/rust-lang/crates.io-index"
4253+
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
4254+
42204255
[[package]]
42214256
name = "untrusted"
42224257
version = "0.9.0"

linkerd/app/src/trace_collector/otel_collector.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use linkerd_opentelemetry::{
66
tonic::common::v1::{any_value, AnyValue, KeyValue},
77
transform::common::tonic::ResourceAttributesWithSchema,
88
},
9+
semconv,
910
};
1011
use std::{
1112
collections::HashMap,
@@ -42,7 +43,7 @@ where
4243
resources
4344
.attributes
4445
.0
45-
.push((std::process::id() as i64).with_key("process.pid"));
46+
.push((std::process::id() as i64).with_key(semconv::attribute::PROCESS_PID));
4647

4748
resources.attributes.0.push(
4849
SystemTime::now()
@@ -55,7 +56,7 @@ where
5556
attributes
5657
.hostname
5758
.unwrap_or_default()
58-
.with_key("host.name"),
59+
.with_key(semconv::attribute::HOST_NAME),
5960
);
6061

6162
resources.attributes.0.extend(

linkerd/opentelemetry/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ linkerd-trace-context = { path = "../trace-context" }
1515
opentelemetry = { version = "0.31", default-features = false, features = ["trace"] }
1616
opentelemetry_sdk = { version = "0.31", default-features = false, features = ["trace"] }
1717
opentelemetry-proto = { version = "0.31" }
18+
opentelemetry-semantic-conventions = { version = "0.31", default-features = false, features = ["semconv_experimental"] }
1819
tonic = { workspace = true, default-features = false, features = [
1920
"codegen",
2021
] }

linkerd/opentelemetry/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use opentelemetry_proto::{
2828
};
2929
use opentelemetry_sdk::trace::{SpanData, SpanLinks};
3030
pub use opentelemetry_sdk::{self as sdk};
31+
pub use opentelemetry_semantic_conventions as semconv;
3132
use tokio::{
3233
sync::mpsc,
3334
time::{self, Instant, MissedTickBehavior},

linkerd/trace-context/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ publish = { workspace = true }
99
[dependencies]
1010
base64 = "0.22"
1111
bytes = { workspace = true }
12+
const_format = "0.2"
1213
futures = { version = "0.3", default-features = false }
1314
hex = "0.4"
1415
http = { workspace = true }
1516
linkerd-error = { path = "../error" }
1617
linkerd-stack = { path = "../stack" }
18+
opentelemetry-semantic-conventions = { version = "0.31", default-features = false, features = ["semconv_experimental"] }
1719
rand = "0.9"
1820
thiserror = "2"
1921
tower = { workspace = true, default-features = false, features = ["util"] }

linkerd/trace-context/src/service.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{propagation, Span, SpanSink};
22
use futures::{future::Either, prelude::*};
33
use http::Uri;
44
use linkerd_stack::layer;
5+
use opentelemetry_semantic_conventions as semconv;
56
use std::{
67
collections::HashMap,
78
fmt::{Display, Formatter},
@@ -43,21 +44,24 @@ impl<K: Clone, S> TraceContext<K, S> {
4344
/// https://opentelemetry.io/docs/specs/semconv/http/http-spans/
4445
fn request_labels<B>(req: &http::Request<B>) -> HashMap<&'static str, String> {
4546
let mut labels = HashMap::with_capacity(13);
46-
labels.insert("http.request.method", format!("{}", req.method()));
47+
labels.insert(
48+
semconv::trace::HTTP_REQUEST_METHOD,
49+
format!("{}", req.method()),
50+
);
4751

4852
let url = req.uri();
4953
if let Some(scheme) = url.scheme_str() {
50-
labels.insert("url.scheme", scheme.to_string());
54+
labels.insert(semconv::trace::URL_SCHEME, scheme.to_string());
5155
}
52-
labels.insert("url.path", url.path().to_string());
56+
labels.insert(semconv::trace::URL_PATH, url.path().to_string());
5357
if let Some(query) = url.query() {
54-
labels.insert("url.query", query.to_string());
58+
labels.insert(semconv::trace::URL_QUERY, query.to_string());
5559
}
5660

57-
labels.insert("url.full", UrlLabel(url).to_string());
61+
labels.insert(semconv::trace::URL_FULL, UrlLabel(url).to_string());
5862

5963
// linkerd currently only proxies tcp-based connections
60-
labels.insert("network.transport", "tcp".to_string());
64+
labels.insert(semconv::trace::NETWORK_TRANSPORT, "tcp".to_string());
6165

6266
// This is the order of precedence for host headers,
6367
// see https://opentelemetry.io/docs/specs/semconv/http/http-spans/
@@ -71,10 +75,10 @@ impl<K: Clone, S> TraceContext<K, S> {
7175
if let Ok(host) = host.to_str() {
7276
if let Ok(uri) = host.parse::<Uri>() {
7377
if let Some(host) = uri.host() {
74-
labels.insert("server.address", host.to_string());
78+
labels.insert(semconv::trace::SERVER_ADDRESS, host.to_string());
7579
}
7680
if let Some(port) = uri.port() {
77-
labels.insert("server.port", port.to_string());
81+
labels.insert(semconv::trace::SERVER_PORT, port.to_string());
7882
}
7983
}
8084
}
@@ -96,11 +100,20 @@ impl<K: Clone, S> TraceContext<K, S> {
96100
req: &http::Request<B>,
97101
) {
98102
static HEADER_LABELS: &[(&str, &str)] = &[
99-
("user-agent", "user_agent.original"),
103+
("user-agent", semconv::trace::USER_AGENT_ORIGINAL),
100104
// http.request.body.size is available as a semantic convention, but is not stable.
101-
("content-length", "http.request.header.content-length"),
102-
("content-type", "http.request.header.content-type"),
103-
("l5d-orig-proto", "http.request.header.l5d-orig-proto"),
105+
(
106+
"content-length",
107+
const_format::concatcp!(semconv::trace::HTTP_REQUEST_HEADER, ".content-length"),
108+
),
109+
(
110+
"content-type",
111+
const_format::concatcp!(semconv::trace::HTTP_REQUEST_HEADER, ".content-type"),
112+
),
113+
(
114+
"l5d-orig-proto",
115+
const_format::concatcp!(semconv::trace::HTTP_REQUEST_HEADER, ".l5d-orig-proto"),
116+
),
104117
];
105118
for &(header, label) in HEADER_LABELS {
106119
if let Some(value) = req.headers().get(header) {
@@ -114,7 +127,7 @@ impl<K: Clone, S> TraceContext<K, S> {
114127
rsp: &http::Response<B>,
115128
) -> HashMap<&'static str, String> {
116129
labels.insert(
117-
"http.response.status_code",
130+
semconv::trace::HTTP_RESPONSE_STATUS_CODE,
118131
rsp.status().as_str().to_string(),
119132
);
120133
labels

0 commit comments

Comments
 (0)