Skip to content

Commit

Permalink
Include pod UID and container name as optional trace labels
Browse files Browse the repository at this point in the history
It's useful to include these fields in the trace span attributes to help correlate traffic within a cluster.

This propagates these labels through the trace initialization, similar to how we handle the service name (and other labels that last for the entire pod lifetime).

See linkerd/linkerd2#13501 for the corresponding control plane change.

Signed-off-by: Scott Fleener <[email protected]>
  • Loading branch information
sfleen committed Dec 20, 2024
1 parent a1a5713 commit a0ee879
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
10 changes: 10 additions & 0 deletions linkerd/app/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ const ENV_OUTBOUND_HTTP1_CONNECTION_POOL_IDLE_TIMEOUT: &str =

const ENV_SHUTDOWN_GRACE_PERIOD: &str = "LINKERD2_PROXY_SHUTDOWN_GRACE_PERIOD";

const ENV_POD_UID: &str = "_pod_uid";
const ENV_POD_CONTAINER_NAME: &str = "_pod_containerName";

// Default values for various configuration fields
const DEFAULT_OUTBOUND_LISTEN_ADDR: &str = "127.0.0.1:4140";
pub const DEFAULT_INBOUND_LISTEN_ADDR: &str = "0.0.0.0:4143";
Expand Down Expand Up @@ -395,6 +398,9 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>

let shutdown_grace_period = parse(strings, ENV_SHUTDOWN_GRACE_PERIOD, parse_duration);

let pod_uid = strings.get(ENV_POD_UID);
let container_name = strings.get(ENV_POD_CONTAINER_NAME);

let inbound_discovery_idle_timeout =
parse(strings, ENV_INBOUND_DISCOVERY_IDLE_TIMEOUT, parse_duration);
let outbound_discovery_idle_timeout =
Expand Down Expand Up @@ -845,10 +851,14 @@ pub fn parse_config<S: Strings>(strings: &S) -> Result<super::Config, EnvError>
.unwrap_or_default();

let trace_service_name = trace_service_name.ok().flatten();
let pod_uid = pod_uid.ok().flatten();
let container_name = container_name.ok().flatten();

Check warning on line 855 in linkerd/app/src/env.rs

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/env.rs#L854-L855

Added lines #L854 - L855 were not covered by tests

trace_collector::Config::Enabled(Box::new(trace_collector::EnabledConfig {
attributes,
hostname: hostname?,
pod_uid,
container_name,

Check warning on line 861 in linkerd/app/src/env.rs

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/env.rs#L860-L861

Added lines #L860 - L861 were not covered by tests
service_name: trace_service_name,
control: ControlConfig {
addr,
Expand Down
20 changes: 13 additions & 7 deletions linkerd/app/src/trace_collector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use linkerd_app_core::http_tracing::{CollectorProtocol, SpanSink};
use linkerd_app_core::metrics::ControlHttp as HttpMetrics;
use linkerd_app_core::svc::NewService;
use linkerd_app_core::{control, dns, identity, opencensus, opentelemetry};
use linkerd_app_core::{
control, dns,
http_tracing::{CollectorProtocol, SpanSink},
identity,
metrics::ControlHttp as HttpMetrics,
opencensus, opentelemetry,
svc::NewService,
};
use linkerd_error::Error;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::{collections::HashMap, future::Future, pin::Pin};

pub mod oc_collector;
pub mod otel_collector;
Expand All @@ -24,6 +26,8 @@ pub struct EnabledConfig {
pub control: control::Config,
pub attributes: HashMap<String, String>,
pub hostname: Option<String>,
pub pod_uid: Option<String>,
pub container_name: Option<String>,
pub service_name: Option<String>,
pub kind: CollectorProtocol,
}
Expand Down Expand Up @@ -95,6 +99,8 @@ impl Config {
CollectorProtocol::OpenTelemetry => otel_collector::create_collector(
addr.clone(),
inner.hostname,
inner.pod_uid,
inner.container_name,

Check warning on line 103 in linkerd/app/src/trace_collector.rs

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L102-L103

Added lines #L102 - L103 were not covered by tests
svc_name,
inner.attributes,
svc,
Expand Down
22 changes: 19 additions & 3 deletions linkerd/app/src/trace_collector/otel_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ use linkerd_app_core::{
};
use linkerd_opentelemetry::{
self as opentelemetry, metrics,
proto::proto::common::v1::{any_value, AnyValue, KeyValue},
proto::transform::common::ResourceAttributesWithSchema,
proto::{
proto::common::v1::{any_value, AnyValue, KeyValue},
transform::common::ResourceAttributesWithSchema,
},
};
use std::{
collections::HashMap,
time::{SystemTime, UNIX_EPOCH},
};
use std::{collections::HashMap, time::SystemTime, time::UNIX_EPOCH};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tonic::{body::BoxBody, client::GrpcService};
Expand All @@ -16,6 +21,8 @@ use tracing::Instrument;
pub(super) fn create_collector<S>(
addr: ControlAddr,
hostname: Option<String>,
pod_uid: Option<String>,
container_name: Option<String>,
service_name: String,
attributes: HashMap<String, String>,
svc: S,
Expand Down Expand Up @@ -53,6 +60,15 @@ where
.attributes
.0
.push(hostname.unwrap_or_default().with_key("host.name"));
if let Some(pod_uid) = pod_uid {
resources.attributes.0.push(pod_uid.with_key("k8s.pod.uid"));

Check warning on line 64 in linkerd/app/src/trace_collector/otel_collector.rs

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector/otel_collector.rs#L63-L64

Added lines #L63 - L64 were not covered by tests
}
if let Some(container_name) = container_name {
resources
.attributes
.0
.push(container_name.with_key("k8s.container.name"));

Check warning on line 70 in linkerd/app/src/trace_collector/otel_collector.rs

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector/otel_collector.rs#L66-L70

Added lines #L66 - L70 were not covered by tests
}

resources.attributes.0.extend(
attributes
Expand Down

0 comments on commit a0ee879

Please sign in to comment.