Skip to content

Commit 96bae36

Browse files
committed
feat: [#1446] add new metric label server_binding_address_ip_type
Example: ``` udp_tracker_core_requests_received_total{request_kind="connect",server_binding_address_ip_family="inet",server_binding_address_ip_type="plain",server_binding_ip="0.0.0.0",server_binding_port="6969",server_binding_protocol="udp"} 1 ``` It's needed to easily filter metric samples to calculate aggregate values for a given IP family (IPv4 or IPv6).
1 parent 1376a7c commit 96bae36

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

packages/http-tracker-core/src/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ impl From<ConnectionContext> for LabelSet {
9090
label_name!("server_binding_address_ip_type"),
9191
LabelValue::new(&connection_context.server.service_binding.bind_address_ip_type().to_string()),
9292
),
93+
(
94+
label_name!("server_binding_address_ip_family"),
95+
LabelValue::new(&connection_context.server.service_binding.bind_address_ip_family().to_string()),
96+
),
9397
(
9498
label_name!("server_binding_port"),
9599
LabelValue::new(&connection_context.server.service_binding.bind_address().port().to_string()),

packages/primitives/src/service_binding.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fmt;
2-
use std::net::SocketAddr;
2+
use std::net::{IpAddr, SocketAddr};
33

44
use serde::{Deserialize, Serialize};
55
use url::Url;
@@ -40,11 +40,43 @@ pub enum IpType {
4040

4141
impl fmt::Display for IpType {
4242
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43-
let addr_type_str = match self {
43+
let ip_type_str = match self {
4444
Self::Plain => "plain",
4545
Self::V4MappedV6 => "v4_mapped_v6",
4646
};
47-
write!(f, "{addr_type_str}")
47+
write!(f, "{ip_type_str}")
48+
}
49+
}
50+
51+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
52+
pub enum IpFamily {
53+
// IPv4
54+
Inet,
55+
// IPv6
56+
Inet6,
57+
}
58+
59+
impl fmt::Display for IpFamily {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61+
let ip_family_str = match self {
62+
Self::Inet => "inet",
63+
Self::Inet6 => "inet6",
64+
};
65+
write!(f, "{ip_family_str}")
66+
}
67+
}
68+
69+
impl From<IpAddr> for IpFamily {
70+
fn from(ip: IpAddr) -> Self {
71+
if ip.is_ipv4() {
72+
return IpFamily::Inet;
73+
}
74+
75+
if ip.is_ipv6() {
76+
return IpFamily::Inet6;
77+
}
78+
79+
panic!("Unsupported IP address type: {ip}");
4880
}
4981
}
5082

@@ -128,6 +160,11 @@ impl ServiceBinding {
128160
IpType::Plain
129161
}
130162

163+
#[must_use]
164+
pub fn bind_address_ip_family(&self) -> IpFamily {
165+
self.bind_address.ip().into()
166+
}
167+
131168
/// # Panics
132169
///
133170
/// It never panics because the URL is always valid.

packages/udp-tracker-core/src/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ impl From<ConnectionContext> for LabelSet {
6363
label_name!("server_binding_address_ip_type"),
6464
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_type().to_string()),
6565
),
66+
(
67+
label_name!("server_binding_address_ip_family"),
68+
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_family().to_string()),
69+
),
6670
(
6771
label_name!("server_binding_port"),
6872
LabelValue::new(&connection_context.server_service_binding.bind_address().port().to_string()),

packages/udp-tracker-server/src/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ impl From<ConnectionContext> for LabelSet {
122122
label_name!("server_binding_address_ip_type"),
123123
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_type().to_string()),
124124
),
125+
(
126+
label_name!("server_binding_address_ip_family"),
127+
LabelValue::new(&connection_context.server_service_binding.bind_address_ip_family().to_string()),
128+
),
125129
(
126130
label_name!("server_binding_port"),
127131
LabelValue::new(&connection_context.server_service_binding.bind_address().port().to_string()),

0 commit comments

Comments
 (0)