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 Jan 6, 2025
1 parent 387197e commit 89d0203
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 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
42 changes: 27 additions & 15 deletions linkerd/app/src/trace_collector.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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 otel_collector::OtelCollectorAttributes;
use std::{collections::HashMap, future::Future, pin::Pin};

pub mod oc_collector;
pub mod otel_collector;
Expand All @@ -24,6 +27,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 @@ -92,14 +97,21 @@ impl Config {
svc,
legacy_oc_metrics,
),
CollectorProtocol::OpenTelemetry => otel_collector::create_collector(
addr.clone(),
inner.hostname,
svc_name,
inner.attributes,
svc,
legacy_otel_metrics,
),
CollectorProtocol::OpenTelemetry => {
let attributes = OtelCollectorAttributes {
hostname: inner.hostname,
pod_uid: inner.pod_uid,
container_name: inner.container_name,

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L102 - L104 were not covered by tests
service_name: svc_name,
extra: inner.attributes,

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L106

Added line #L106 was not covered by tests
};
otel_collector::create_collector(
addr.clone(),
attributes,
svc,
legacy_otel_metrics,

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L109-L112

Added lines #L109 - L112 were not covered by tests
)
}
};

Ok(TraceCollector::Enabled(Box::new(collector)))
Expand Down
45 changes: 34 additions & 11 deletions linkerd/app/src/trace_collector/otel_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@ 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};
use tracing::Instrument;

pub(super) struct OtelCollectorAttributes {
pub hostname: Option<String>,
pub pod_uid: Option<String>,
pub container_name: Option<String>,
pub service_name: String,
pub extra: HashMap<String, String>,
}

pub(super) fn create_collector<S>(
addr: ControlAddr,
hostname: Option<String>,
service_name: String,
attributes: HashMap<String, String>,
attributes: OtelCollectorAttributes,
svc: S,
legacy_metrics: metrics::Registry,
) -> EnabledCollector
Expand All @@ -36,7 +47,7 @@ where
resources
.attributes
.0
.push(service_name.with_key("service.name"));
.push(attributes.service_name.with_key("service.name"));

Check warning on line 50 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#L50

Added line #L50 was not covered by tests
resources
.attributes
.0
Expand All @@ -49,13 +60,25 @@ where
.unwrap_or_else(|e| -(e.duration().as_secs() as i64))
.with_key("process.start_timestamp"),
);
resources
.attributes
.0
.push(hostname.unwrap_or_default().with_key("host.name"));
resources.attributes.0.push(
attributes
.hostname
.unwrap_or_default()
.with_key("host.name"),

Check warning on line 67 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-L67

Added lines #L63 - L67 were not covered by tests
);
if let Some(pod_uid) = attributes.pod_uid {
resources.attributes.0.push(pod_uid.with_key("k8s.pod.uid"));

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#L69-L70

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

Check warning on line 76 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#L72-L76

Added lines #L72 - L76 were not covered by tests
}

resources.attributes.0.extend(
attributes
.extra

Check warning on line 81 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#L81

Added line #L81 was not covered by tests
.into_iter()
.map(|(key, value)| value.with_key(&key)),
);
Expand Down

0 comments on commit 89d0203

Please sign in to comment.