Skip to content

Commit

Permalink
feat: format prefix labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-personio committed Sep 14, 2023
1 parent 69b4763 commit 82b55ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
17 changes: 17 additions & 0 deletions linkerd/app/core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ where
Some(out)
}

pub fn prefix_outbound_endpoint_labels<'i, I>(prefix: &str, mut labels_iter: I) -> Option<String>
where
I: Iterator<Item = (&'i String, &'i String)>,
{
let (k0, v0) = labels_iter.next()?;
let mut out = format!("{}_{}=\"{}\"", prefix, k0, v0);

for (k, v) in labels_iter {
if k == "pod" || k == "pod_template_hash" {
continue;
}

write!(out, ",{}_{}=\"{}\"", prefix, k, v).expect("label concat must succeed");
}
Some(out)
}

// === impl Metrics ===

impl Metrics {
Expand Down
32 changes: 28 additions & 4 deletions linkerd/app/integration/src/tests/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ mod outbound_dst_labels {
let (
Fixture {
client,
metrics: _metrics,
metrics,
proxy: _proxy,
_profile,
dst_tx,
pol_out_tx: _pol_out_tx,
labels: _labels,
labels,
..
},
addr,
Expand All @@ -582,6 +582,18 @@ mod outbound_dst_labels {

info!("client.get(/)");
assert_eq!(client.get("/").await, "hello");

let labels = labels
.label("dst_addr_label1", "foo")
.label("dst_addr_label2", "bar");

for &metric in &[
"request_total",
"response_total",
"response_latency_ms_count",
] {
labels.metric(metric).assert_in(&metrics).await;
}
}

#[tokio::test]
Expand All @@ -590,11 +602,11 @@ mod outbound_dst_labels {
let (
Fixture {
client,
metrics: _metrics,
metrics,
proxy: _proxy,
_profile,
dst_tx,
labels: _labels,
labels,
..
},
addr,
Expand All @@ -614,6 +626,18 @@ mod outbound_dst_labels {

info!("client.get(/)");
assert_eq!(client.get("/").await, "hello");

let labels = labels
.label("dst_set_label1", "foo")
.label("dst_set_label2", "bar");

for &metric in &[
"request_total",
"response_total",
"response_latency_ms_count",
] {
labels.metric(metric).assert_in(&metrics).await;
}
}

#[tokio::test]
Expand Down
22 changes: 3 additions & 19 deletions linkerd/app/outbound/src/http/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{balance, breaker, client, handle_proxy_error_headers};
use crate::{http, stack_labels, BackendRef, Outbound, ParentRef};
use linkerd_app_core::{
classify,
metrics::{prefix_labels, EndpointLabels, OutboundEndpointLabels},
metrics::{prefix_outbound_endpoint_labels, EndpointLabels, OutboundEndpointLabels},
profiles,
proxy::{
api_resolve::{ConcreteAddr, Metadata, ProtocolHint},
Expand All @@ -18,7 +18,7 @@ use linkerd_app_core::{
Error, Infallible, NameAddr,
};
use linkerd_proxy_client_policy::FailureAccrual;
use std::{collections::BTreeMap, fmt::Debug, net::SocketAddr, sync::Arc};
use std::{fmt::Debug, net::SocketAddr, sync::Arc};
use tracing::info_span;

mod metrics;
Expand Down Expand Up @@ -342,24 +342,8 @@ where
T: svc::Param<Option<http::uri::Authority>>,
{
fn param(&self) -> OutboundEndpointLabels {
let original_labels = self.metadata.labels().clone();
// self.metadata.labels() could return Err in some cases
// if that case the dst_labels won't carry any value
let dst_labels = match Arc::try_unwrap(self.metadata.labels()) {
Ok(result) => result,
Err(_e) => BTreeMap::new(),
};

// dst_labels.remove("pod");
// dst_labels.remove("pod_template_hash");

let label_iterator = match dst_labels.is_empty() {
true => dst_labels.iter(),
false => original_labels.iter()
};

OutboundEndpointLabels {
labels: prefix_labels("dst", label_iterator),
labels: prefix_outbound_endpoint_labels("dst", self.metadata.labels().iter()),
server_id: self.param(),
}
}
Expand Down

0 comments on commit 82b55ce

Please sign in to comment.