Skip to content

Commit f388bc2

Browse files
authored
metrics: add target_addr label for accepted transport metrics (#861)
This branch adds a `target_addr` label to the Prometheus metrics generated for accepted (`peer="src"`) connections, containing the original destination address of the accepted connection. When we add the proxy's admin listener to the inbound metrics, this will allow distinguishing between admin and inbound proxy traffic. Additionally, it allows partitioning transport metrics by the client's target address.
1 parent 2537560 commit f388bc2

File tree

5 files changed

+62
-23
lines changed

5 files changed

+62
-23
lines changed

linkerd/app/core/src/transport/labels.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use crate::metrics::{Direction, OutboundEndpointLabels};
22
use linkerd_conditional::Conditional;
33
use linkerd_metrics::FmtLabels;
44
use linkerd_tls as tls;
5-
use std::fmt;
5+
use std::{fmt, net::SocketAddr};
66

77
/// Describes a class of transport.
88
///
@@ -11,7 +11,11 @@ use std::fmt;
1111
/// Implements `FmtLabels`.
1212
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
1313
pub enum Key {
14-
Accept(Direction, tls::server::ConditionalTls),
14+
Accept {
15+
direction: Direction,
16+
id: tls::server::ConditionalTls,
17+
target_addr: SocketAddr,
18+
},
1519
OutboundConnect(OutboundEndpointLabels),
1620
InboundConnect,
1721
}
@@ -25,18 +29,30 @@ pub struct TlsConnect<'t>(&'t tls::ConditionalServerId);
2529
// === impl Key ===
2630

2731
impl Key {
28-
pub fn accept(direction: Direction, id: tls::server::ConditionalTls) -> Self {
29-
Self::Accept(direction, id)
32+
pub fn accept(
33+
direction: Direction,
34+
id: tls::server::ConditionalTls,
35+
target_addr: SocketAddr,
36+
) -> Self {
37+
Self::Accept {
38+
direction,
39+
id,
40+
target_addr,
41+
}
3042
}
3143
}
3244

3345
impl FmtLabels for Key {
3446
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3547
match self {
36-
Self::Accept(direction, identity) => {
48+
Self::Accept {
49+
direction,
50+
id,
51+
target_addr,
52+
} => {
3753
direction.fmt_labels(f)?;
38-
write!(f, ",peer=\"src\",")?;
39-
TlsAccept::from(identity).fmt_labels(f)
54+
write!(f, ",peer=\"src\",target_addr=\"{}\",", target_addr)?;
55+
TlsAccept::from(id).fmt_labels(f)
4056
}
4157
Self::OutboundConnect(endpoint) => {
4258
Direction::Out.fmt_labels(f)?;

linkerd/app/inbound/src/target.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ impl Into<SocketAddr> for &'_ TcpAccept {
8282

8383
impl Into<transport::labels::Key> for &'_ TcpAccept {
8484
fn into(self) -> transport::labels::Key {
85-
transport::labels::Key::accept(transport::labels::Direction::In, self.client_id.clone())
85+
transport::labels::Key::accept(
86+
transport::labels::Direction::In,
87+
self.client_id.clone(),
88+
self.target_addr,
89+
)
8690
}
8791
}
8892

linkerd/app/integration/src/tcp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ impl TcpServer {
153153
}
154154

155155
impl TcpConn {
156+
pub fn target_addr(&self) -> SocketAddr {
157+
self.addr
158+
}
159+
156160
pub async fn read(&self) -> Vec<u8> {
157161
self.try_read()
158162
.await

linkerd/app/integration/src/tests/telemetry.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -922,12 +922,15 @@ mod transport {
922922
.await;
923923
}
924924

925-
async fn test_tcp_accept(fixture: impl Future<Output = TcpFixture>, labels: metrics::Labels) {
925+
async fn test_tcp_accept(
926+
fixture: impl Future<Output = TcpFixture>,
927+
labels: impl Fn(&proxy::Listening) -> metrics::Labels,
928+
) {
926929
let _trace = trace_init();
927930
let TcpFixture {
928931
client,
929932
metrics,
930-
proxy: _proxy,
933+
proxy,
931934
dst: _dst,
932935
profile: _profile,
933936
} = fixture.await;
@@ -937,7 +940,7 @@ mod transport {
937940
tcp_client.write(TcpFixture::HELLO_MSG).await;
938941
assert_eq!(tcp_client.read().await, TcpFixture::BYE_MSG.as_bytes());
939942

940-
let labels = labels.label("peer", "src");
943+
let labels = labels(&proxy).label("peer", "src");
941944
let mut opens = labels.metric("tcp_open_total").value(1u64);
942945
let mut closes = labels
943946
.metric("tcp_close_total")
@@ -1088,10 +1091,12 @@ mod transport {
10881091

10891092
#[tokio::test]
10901093
async fn inbound_http_accept() {
1091-
test_http_accept(Fixture::inbound(), |_| {
1094+
test_http_accept(Fixture::inbound(), |proxy| {
1095+
let orig_dst = proxy.inbound_server.as_ref().unwrap().addr;
10921096
metrics::labels()
10931097
.label("direction", "inbound")
10941098
.label("tls", "disabled")
1099+
.label("target_addr", orig_dst)
10951100
})
10961101
.await;
10971102
}
@@ -1109,11 +1114,13 @@ mod transport {
11091114

11101115
#[tokio::test]
11111116
async fn outbound_http_accept() {
1112-
test_http_accept(Fixture::outbound(), |_| {
1117+
test_http_accept(Fixture::outbound(), |proxy| {
1118+
let orig_dst = proxy.outbound_server.as_ref().unwrap().addr;
11131119
metrics::labels()
11141120
.label("direction", "outbound")
11151121
.label("tls", "no_identity")
11161122
.label("no_tls_reason", "loopback")
1123+
.label("target_addr", orig_dst)
11171124
})
11181125
.await;
11191126
}
@@ -1147,12 +1154,13 @@ mod transport {
11471154

11481155
#[tokio::test]
11491156
async fn inbound_tcp_accept() {
1150-
test_tcp_accept(
1151-
TcpFixture::inbound(),
1157+
test_tcp_accept(TcpFixture::inbound(), |proxy| {
1158+
let orig_dst = proxy.inbound_server.as_ref().unwrap().addr;
11521159
metrics::labels()
11531160
.label("direction", "inbound")
1154-
.label("tls", "disabled"),
1155-
)
1161+
.label("tls", "disabled")
1162+
.label("target_addr", orig_dst)
1163+
})
11561164
.await
11571165
}
11581166

@@ -1170,13 +1178,15 @@ mod transport {
11701178

11711179
#[tokio::test]
11721180
async fn outbound_tcp_accept() {
1173-
test_tcp_accept(
1174-
TcpFixture::outbound(),
1181+
test_tcp_accept(TcpFixture::outbound(), |proxy| {
1182+
let orig_dst = proxy.outbound_server.as_ref().unwrap().addr;
1183+
11751184
metrics::labels()
11761185
.label("direction", "outbound")
11771186
.label("tls", "no_identity")
1178-
.label("no_tls_reason", "loopback"),
1179-
)
1187+
.label("no_tls_reason", "loopback")
1188+
.label("target_addr", orig_dst)
1189+
})
11801190
.await;
11811191
}
11821192

linkerd/app/outbound/src/target.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ impl<P> Into<Addr> for &'_ Accept<P> {
6969
impl<P> Into<transport::labels::Key> for &'_ Accept<P> {
7070
fn into(self) -> transport::labels::Key {
7171
const NO_TLS: tls::server::ConditionalTls = Conditional::None(tls::server::NoTls::Loopback);
72-
transport::labels::Key::accept(transport::labels::Direction::Out, NO_TLS)
72+
transport::labels::Key::accept(transport::labels::Direction::Out, NO_TLS, self.orig_dst)
7373
}
7474
}
7575

7676
// === impl Logical ===
7777

7878
impl<P> From<(Option<profiles::Receiver>, Accept<P>)> for Logical<P> {
7979
fn from(
80-
(profile, Accept { orig_dst, protocol }): (Option<profiles::Receiver>, Accept<P>),
80+
(
81+
profile,
82+
Accept {
83+
orig_dst, protocol, ..
84+
},
85+
): (Option<profiles::Receiver>, Accept<P>),
8186
) -> Self {
8287
Self {
8388
profile,

0 commit comments

Comments
 (0)