Skip to content

Commit 6d66554

Browse files
committed
chore(tracing): Use upstream tracing batch exporter
The proxy used its own custom trace exporter for some time. It was built before the upstream OpenTelemetry libraries were available, and they have outlived their usefulness. This replaces the custom exporter with a batch exporter configured to export to the same endpoint with the same service configuration. In the future, this exporter can be installed as a global default trace processor, which would decouple it from the service layer where the proxy generates spans for requests. Signed-off-by: Scott Fleener <[email protected]>
1 parent 2e106b8 commit 6d66554

File tree

4 files changed

+121
-263
lines changed

4 files changed

+121
-263
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,7 @@ dependencies = [
20832083
"linkerd-trace-context",
20842084
"opentelemetry",
20852085
"opentelemetry-proto",
2086+
"opentelemetry-semantic-conventions",
20862087
"opentelemetry_sdk",
20872088
"tokio",
20882089
"tonic",
Lines changed: 26 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use super::EnabledCollector;
22
use linkerd_app_core::{control::ControlAddr, proxy::http::Body, Error};
3-
use linkerd_opentelemetry::{
4-
self as opentelemetry, metrics,
5-
proto::{
6-
tonic::common::v1::{any_value, AnyValue, KeyValue},
7-
transform::common::tonic::ResourceAttributesWithSchema,
8-
},
9-
};
3+
use linkerd_opentelemetry::{self as opentelemetry, metrics, otel::KeyValue, sdk, semconv};
104
use std::{
115
collections::HashMap,
126
time::{SystemTime, UNIX_EPOCH},
@@ -28,7 +22,7 @@ pub(super) fn create_collector<S>(
2822
legacy_metrics: metrics::Registry,
2923
) -> EnabledCollector
3024
where
31-
S: GrpcService<TonicBody> + Clone + Send + 'static,
25+
S: GrpcService<TonicBody> + Clone + Send + Sync + 'static,
3226
S::Error: Into<Error>,
3327
S::Future: Send,
3428
S::ResponseBody: Body<Data = tonic::codegen::Bytes> + Send + 'static,
@@ -37,37 +31,33 @@ where
3731
let (span_sink, spans_rx) = mpsc::channel(crate::trace_collector::SPAN_BUFFER_CAPACITY);
3832
let spans_rx = ReceiverStream::new(spans_rx);
3933

40-
let mut resources = ResourceAttributesWithSchema::default();
41-
42-
resources
43-
.attributes
44-
.0
45-
.push((std::process::id() as i64).with_key("process.pid"));
46-
47-
resources.attributes.0.push(
48-
SystemTime::now()
49-
.duration_since(UNIX_EPOCH)
50-
.map(|d| d.as_secs() as i64)
51-
.unwrap_or_else(|e| -(e.duration().as_secs() as i64))
52-
.with_key("process.start_timestamp"),
53-
);
54-
resources.attributes.0.push(
55-
attributes
56-
.hostname
57-
.unwrap_or_default()
58-
.with_key("host.name"),
59-
);
60-
61-
resources.attributes.0.extend(
62-
attributes
63-
.extra
64-
.into_iter()
65-
.map(|(key, value)| value.with_key(&key)),
66-
);
34+
let resource = sdk::Resource::builder()
35+
.with_attribute(KeyValue::new(
36+
semconv::attribute::PROCESS_PID,
37+
std::process::id() as i64,
38+
))
39+
.with_attribute(KeyValue::new(
40+
"process.start_timestamp",
41+
SystemTime::now()
42+
.duration_since(UNIX_EPOCH)
43+
.map(|d| d.as_secs() as i64)
44+
.unwrap_or_else(|e| -(e.duration().as_secs() as i64)),
45+
))
46+
.with_attribute(KeyValue::new(
47+
semconv::attribute::HOST_NAME,
48+
attributes.hostname.unwrap_or_default(),
49+
))
50+
.with_attributes(
51+
attributes
52+
.extra
53+
.into_iter()
54+
.map(|(k, v)| KeyValue::new(k, v)),
55+
)
56+
.build();
6757

6858
let addr = addr.clone();
6959
let task = Box::pin(
70-
opentelemetry::export_spans(svc, spans_rx, resources, legacy_metrics)
60+
opentelemetry::export_spans(svc, spans_rx, resource, legacy_metrics)
7161
.instrument(tracing::debug_span!("opentelemetry", peer.addr = %addr).or_current()),
7262
);
7363

@@ -77,41 +67,3 @@ where
7767
span_sink,
7868
}
7969
}
80-
81-
trait IntoAnyValue
82-
where
83-
Self: Sized,
84-
{
85-
fn into_any_value(self) -> AnyValue;
86-
87-
fn with_key(self, key: &str) -> KeyValue {
88-
KeyValue {
89-
key: key.to_string(),
90-
value: Some(self.into_any_value()),
91-
}
92-
}
93-
}
94-
95-
impl IntoAnyValue for String {
96-
fn into_any_value(self) -> AnyValue {
97-
AnyValue {
98-
value: Some(any_value::Value::StringValue(self)),
99-
}
100-
}
101-
}
102-
103-
impl IntoAnyValue for &str {
104-
fn into_any_value(self) -> AnyValue {
105-
AnyValue {
106-
value: Some(any_value::Value::StringValue(self.to_string())),
107-
}
108-
}
109-
}
110-
111-
impl IntoAnyValue for i64 {
112-
fn into_any_value(self) -> AnyValue {
113-
AnyValue {
114-
value: Some(any_value::Value::IntValue(self)),
115-
}
116-
}
117-
}

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
] }

0 commit comments

Comments
 (0)