Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include pod UID and container name as optional trace labels #3483

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion linkerd/app/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
const ENV_TRACE_ATTRIBUTES_PATH: &str = "LINKERD2_PROXY_TRACE_ATTRIBUTES_PATH";
const ENV_TRACE_PROTOCOL: &str = "LINKERD2_PROXY_TRACE_PROTOCOL";
const ENV_TRACE_SERVICE_NAME: &str = "LINKERD2_PROXY_TRACE_SERVICE_NAME";
const ENV_TRACE_EXTRA_ATTRIBUTES: &str = "LINKERD2_PROXY_TRACE_EXTRA_ATTRIBUTES";

/// Constrains which destination names may be used for profile/route discovery.
///
Expand Down Expand Up @@ -432,6 +433,7 @@
let hostname = strings.get(ENV_HOSTNAME);

let trace_attributes_file_path = strings.get(ENV_TRACE_ATTRIBUTES_PATH);
let trace_extra_attributes = strings.get(ENV_TRACE_EXTRA_ATTRIBUTES);
let trace_protocol = strings.get(ENV_TRACE_PROTOCOL);
let trace_service_name = strings.get(ENV_TRACE_SERVICE_NAME);

Expand Down Expand Up @@ -831,12 +833,17 @@
} else {
outbound.http_request_queue.failfast_timeout
};
let attributes = trace_attributes_file_path
let mut attributes = trace_attributes_file_path

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/env.rs#L836

Added line #L836 was not covered by tests
.map(|path| match path.and_then(|p| p.parse::<PathBuf>().ok()) {
Some(path) => trace::read_trace_attributes(&path),
None => HashMap::new(),
})
.unwrap_or_default();
if let Ok(Some(attrs)) = trace_extra_attributes {
if !attrs.is_empty() {
attributes.extend(trace::parse_env_trace_attributes(&attrs));

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/env.rs#L842-L844

Added lines #L842 - L844 were not covered by tests
}
}

let trace_protocol = trace_protocol
.map(|proto| proto.and_then(|p| p.parse::<CollectorProtocol>().ok()))
Expand Down
4 changes: 4 additions & 0 deletions linkerd/app/src/env/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
}
}

pub(super) fn parse_env_trace_attributes(attrs: &str) -> HashMap<String, String> {
parse_attrs(attrs)

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/env/trace.rs#L17-L18

Added lines #L17 - L18 were not covered by tests
}

fn parse_attrs(attrs: &str) -> HashMap<String, String> {
attrs
.lines()
Expand Down
38 changes: 23 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 Down Expand Up @@ -92,14 +95,19 @@
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,

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L100

Added line #L100 was not covered by tests
service_name: svc_name,
extra: inner.attributes,

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L102

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

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

View check run for this annotation

Codecov / codecov/patch

linkerd/app/src/trace_collector.rs#L105-L108

Added lines #L105 - L108 were not covered by tests
)
}
};

Ok(TraceCollector::Enabled(Box::new(collector)))
Expand Down
34 changes: 23 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,29 @@
};
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 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 +45,7 @@
resources
.attributes
.0
.push(service_name.with_key("service.name"));
.push(attributes.service_name.with_key("service.name"));

Check warning on line 48 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#L48

Added line #L48 was not covered by tests
resources
.attributes
.0
Expand All @@ -49,13 +58,16 @@
.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 65 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#L61-L65

Added lines #L61 - L65 were not covered by tests
);

resources.attributes.0.extend(
attributes
.extra

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

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