Skip to content

Commit bedbace

Browse files
committed
Include extra trace attributes from env
This allows us to include arbitrary values from the k8s downward API beyond just the pod labels that are included in the trace attributes file. See linkerd/linkerd2#13544 for the corresponding control plane change. Signed-off-by: Scott Fleener <[email protected]>
1 parent 387197e commit bedbace

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

linkerd/app/src/env.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const ENV_OUTBOUND_DISABLE_INFORMATIONAL_HEADERS: &str =
149149
const ENV_TRACE_ATTRIBUTES_PATH: &str = "LINKERD2_PROXY_TRACE_ATTRIBUTES_PATH";
150150
const ENV_TRACE_PROTOCOL: &str = "LINKERD2_PROXY_TRACE_PROTOCOL";
151151
const ENV_TRACE_SERVICE_NAME: &str = "LINKERD2_PROXY_TRACE_SERVICE_NAME";
152+
const ENV_TRACE_EXTRA_ATTRIBUTES: &str = "LINKERD2_PROXY_TRACE_EXTRA_ATTRIBUTES";
152153

153154
/// Constrains which destination names may be used for profile/route discovery.
154155
///
@@ -432,6 +433,7 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
432433
let hostname = strings.get(ENV_HOSTNAME);
433434

434435
let trace_attributes_file_path = strings.get(ENV_TRACE_ATTRIBUTES_PATH);
436+
let trace_extra_attributes = strings.get(ENV_TRACE_EXTRA_ATTRIBUTES);
435437
let trace_protocol = strings.get(ENV_TRACE_PROTOCOL);
436438
let trace_service_name = strings.get(ENV_TRACE_SERVICE_NAME);
437439

@@ -831,12 +833,17 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
831833
} else {
832834
outbound.http_request_queue.failfast_timeout
833835
};
834-
let attributes = trace_attributes_file_path
836+
let mut attributes = trace_attributes_file_path
835837
.map(|path| match path.and_then(|p| p.parse::<PathBuf>().ok()) {
836838
Some(path) => trace::read_trace_attributes(&path),
837839
None => HashMap::new(),
838840
})
839841
.unwrap_or_default();
842+
if let Ok(Some(attrs)) = trace_extra_attributes {
843+
if !attrs.is_empty() {
844+
attributes.extend(trace::parse_env_trace_attributes(&attrs));
845+
}
846+
}
840847

841848
let trace_protocol = trace_protocol
842849
.map(|proto| proto.and_then(|p| p.parse::<CollectorProtocol>().ok()))

linkerd/app/src/env/trace.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pub(super) fn read_trace_attributes(path: &std::path::Path) -> HashMap<String, S
1414
}
1515
}
1616

17+
pub(super) fn parse_env_trace_attributes(attrs: &str) -> HashMap<String, String> {
18+
parse_attrs(attrs)
19+
}
20+
1721
fn parse_attrs(attrs: &str) -> HashMap<String, String> {
1822
attrs
1923
.lines()

linkerd/app/src/trace_collector.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use linkerd_app_core::http_tracing::{CollectorProtocol, SpanSink};
2-
use linkerd_app_core::metrics::ControlHttp as HttpMetrics;
3-
use linkerd_app_core::svc::NewService;
4-
use linkerd_app_core::{control, dns, identity, opencensus, opentelemetry};
1+
use linkerd_app_core::{
2+
control, dns,
3+
http_tracing::{CollectorProtocol, SpanSink},
4+
identity,
5+
metrics::ControlHttp as HttpMetrics,
6+
opencensus, opentelemetry,
7+
svc::NewService,
8+
};
59
use linkerd_error::Error;
6-
use std::collections::HashMap;
7-
use std::future::Future;
8-
use std::pin::Pin;
10+
use otel_collector::OtelCollectorAttributes;
11+
use std::{collections::HashMap, future::Future, pin::Pin};
912

1013
pub mod oc_collector;
1114
pub mod otel_collector;
@@ -92,14 +95,19 @@ impl Config {
9295
svc,
9396
legacy_oc_metrics,
9497
),
95-
CollectorProtocol::OpenTelemetry => otel_collector::create_collector(
96-
addr.clone(),
97-
inner.hostname,
98-
svc_name,
99-
inner.attributes,
100-
svc,
101-
legacy_otel_metrics,
102-
),
98+
CollectorProtocol::OpenTelemetry => {
99+
let attributes = OtelCollectorAttributes {
100+
hostname: inner.hostname,
101+
service_name: svc_name,
102+
extra: inner.attributes,
103+
};
104+
otel_collector::create_collector(
105+
addr.clone(),
106+
attributes,
107+
svc,
108+
legacy_otel_metrics,
109+
)
110+
}
103111
};
104112

105113
Ok(TraceCollector::Enabled(Box::new(collector)))

linkerd/app/src/trace_collector/otel_collector.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@ use linkerd_app_core::{
44
};
55
use linkerd_opentelemetry::{
66
self as opentelemetry, metrics,
7-
proto::proto::common::v1::{any_value, AnyValue, KeyValue},
8-
proto::transform::common::ResourceAttributesWithSchema,
7+
proto::{
8+
proto::common::v1::{any_value, AnyValue, KeyValue},
9+
transform::common::ResourceAttributesWithSchema,
10+
},
11+
};
12+
use std::{
13+
collections::HashMap,
14+
time::{SystemTime, UNIX_EPOCH},
915
};
10-
use std::{collections::HashMap, time::SystemTime, time::UNIX_EPOCH};
1116
use tokio::sync::mpsc;
1217
use tokio_stream::wrappers::ReceiverStream;
1318
use tonic::{body::BoxBody, client::GrpcService};
1419
use tracing::Instrument;
1520

21+
pub(super) struct OtelCollectorAttributes {
22+
pub hostname: Option<String>,
23+
pub service_name: String,
24+
pub extra: HashMap<String, String>,
25+
}
26+
1627
pub(super) fn create_collector<S>(
1728
addr: ControlAddr,
18-
hostname: Option<String>,
19-
service_name: String,
20-
attributes: HashMap<String, String>,
29+
attributes: OtelCollectorAttributes,
2130
svc: S,
2231
legacy_metrics: metrics::Registry,
2332
) -> EnabledCollector
@@ -36,7 +45,7 @@ where
3645
resources
3746
.attributes
3847
.0
39-
.push(service_name.with_key("service.name"));
48+
.push(attributes.service_name.with_key("service.name"));
4049
resources
4150
.attributes
4251
.0
@@ -49,13 +58,16 @@ where
4958
.unwrap_or_else(|e| -(e.duration().as_secs() as i64))
5059
.with_key("process.start_timestamp"),
5160
);
52-
resources
53-
.attributes
54-
.0
55-
.push(hostname.unwrap_or_default().with_key("host.name"));
61+
resources.attributes.0.push(
62+
attributes
63+
.hostname
64+
.unwrap_or_default()
65+
.with_key("host.name"),
66+
);
5667

5768
resources.attributes.0.extend(
5869
attributes
70+
.extra
5971
.into_iter()
6072
.map(|(key, value)| value.with_key(&key)),
6173
);

0 commit comments

Comments
 (0)