Skip to content

Commit 33e69ff

Browse files
committed
Merge #1558: Add new metric label server_binding_address_type
d4c43bd feat: [#1375] add new metric label server_binding_address_type (Jose Celano) Pull request description: Add new metric label `server_binding_address_type`. - Label name: `server_binding_address_type` - Label values: `plain`, `v4_mapped_v6` Usage example in Prometheus format: ``` udp_tracker_server_requests_accepted_total{request_kind="connect",server_binding_address_type="plain",server_binding_ip="0.0.0.0",server_binding_port="6969",server_binding_protocol="udp"} 1 ``` Example of IPv4-mapped-IPv6 IP: `[::ffff:192.0.2.33]` It should be included whenever another IP label value is used. cc @devnakx ACKs for top commit: josecelano: ACK d4c43bd Tree-SHA512: cfcf908a6b8c82130785d6e7cbe524cb85836ab625f25a3b154b54a1861643b406e638f439d7dfee4a9564c00dc573a5e3a68e86bdc29052274d30564d692161
2 parents a7cbdcb + d4c43bd commit 33e69ff

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ impl From<ConnectionContext> for LabelSet {
8686
label_name!("server_binding_ip"),
8787
LabelValue::new(&connection_context.server.service_binding.bind_address().ip().to_string()),
8888
),
89+
(
90+
label_name!("server_binding_address_type"),
91+
LabelValue::new(&connection_context.server.service_binding.bind_address_type().to_string()),
92+
),
8993
(
9094
label_name!("server_binding_port"),
9195
LabelValue::new(&connection_context.server.service_binding.bind_address().port().to_string()),

packages/primitives/src/service_binding.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::net::SocketAddr;
44
use serde::{Deserialize, Serialize};
55
use url::Url;
66

7+
const DUAL_STACK_IP_V4_MAPPED_V6_PREFIX: &str = "::ffff:";
8+
79
/// Represents the supported network protocols.
810
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
911
pub enum Protocol {
@@ -23,6 +25,29 @@ impl fmt::Display for Protocol {
2325
}
2426
}
2527

28+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
29+
pub enum AddressType {
30+
/// Represents a plain IPv4 or IPv6 address.
31+
Plain,
32+
33+
/// Represents an IPv6 address that is a mapped IPv4 address.
34+
///
35+
/// This is used for IPv6 addresses that represent an IPv4 address in a dual-stack network.
36+
///
37+
/// For example: `[::ffff:192.0.2.33]`
38+
V4MappedV6,
39+
}
40+
41+
impl fmt::Display for AddressType {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
let addr_type_str = match self {
44+
Self::Plain => "plain",
45+
Self::V4MappedV6 => "v4_mapped_v6",
46+
};
47+
write!(f, "{addr_type_str}")
48+
}
49+
}
50+
2651
#[derive(thiserror::Error, Debug, Clone)]
2752
pub enum Error {
2853
#[error("The port number cannot be zero. It must be an assigned valid port.")]
@@ -94,6 +119,15 @@ impl ServiceBinding {
94119
self.bind_address
95120
}
96121

122+
#[must_use]
123+
pub fn bind_address_type(&self) -> AddressType {
124+
if self.is_v4_mapped_v6() {
125+
return AddressType::V4MappedV6;
126+
}
127+
128+
AddressType::Plain
129+
}
130+
97131
/// # Panics
98132
///
99133
/// It never panics because the URL is always valid.
@@ -102,6 +136,15 @@ impl ServiceBinding {
102136
Url::parse(&format!("{}://{}", self.protocol, self.bind_address))
103137
.expect("Service binding can always be parsed into a URL")
104138
}
139+
140+
fn is_v4_mapped_v6(&self) -> bool {
141+
self.bind_address.ip().is_ipv6()
142+
&& self
143+
.bind_address
144+
.ip()
145+
.to_string()
146+
.starts_with(DUAL_STACK_IP_V4_MAPPED_V6_PREFIX)
147+
}
105148
}
106149

107150
impl From<ServiceBinding> for Url {
@@ -126,7 +169,7 @@ mod tests {
126169
use rstest::rstest;
127170
use url::Url;
128171

129-
use crate::service_binding::{Error, Protocol, ServiceBinding};
172+
use crate::service_binding::{AddressType, Error, Protocol, ServiceBinding};
130173

131174
#[rstest]
132175
#[case("wildcard_ip", Protocol::UDP, SocketAddr::from_str("0.0.0.0:6969").unwrap())]
@@ -156,6 +199,29 @@ mod tests {
156199
);
157200
}
158201

202+
#[test]
203+
fn should_return_the_bind_address_plain_type_for_ipv4_ips() {
204+
let service_binding = ServiceBinding::new(Protocol::UDP, SocketAddr::from_str("127.0.0.1:6969").unwrap()).unwrap();
205+
206+
assert_eq!(service_binding.bind_address_type(), AddressType::Plain);
207+
}
208+
209+
#[test]
210+
fn should_return_the_bind_address_plain_type_for_ipv6_ips() {
211+
let service_binding =
212+
ServiceBinding::new(Protocol::UDP, SocketAddr::from_str("[0:0:0:0:0:0:0:1]:6969").unwrap()).unwrap();
213+
214+
assert_eq!(service_binding.bind_address_type(), AddressType::Plain);
215+
}
216+
217+
#[test]
218+
fn should_return_the_bind_address_v4_mapped_v7_type_for_ipv4_ips_mapped_to_ipv6() {
219+
let service_binding =
220+
ServiceBinding::new(Protocol::UDP, SocketAddr::from_str("[::ffff:192.0.2.33]:6969").unwrap()).unwrap();
221+
222+
assert_eq!(service_binding.bind_address_type(), AddressType::V4MappedV6);
223+
}
224+
159225
#[test]
160226
fn should_return_the_corresponding_url() {
161227
let service_binding = ServiceBinding::new(Protocol::UDP, SocketAddr::from_str("127.0.0.1:6969").unwrap()).unwrap();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ impl From<ConnectionContext> for LabelSet {
5959
label_name!("server_binding_ip"),
6060
LabelValue::new(&connection_context.server_service_binding.bind_address().ip().to_string()),
6161
),
62+
(
63+
label_name!("server_binding_address_type"),
64+
LabelValue::new(&connection_context.server_service_binding.bind_address_type().to_string()),
65+
),
6266
(
6367
label_name!("server_binding_port"),
6468
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
@@ -118,6 +118,10 @@ impl From<ConnectionContext> for LabelSet {
118118
label_name!("server_binding_ip"),
119119
LabelValue::new(&connection_context.server_service_binding.bind_address().ip().to_string()),
120120
),
121+
(
122+
label_name!("server_binding_address_type"),
123+
LabelValue::new(&connection_context.server_service_binding.bind_address_type().to_string()),
124+
),
121125
(
122126
label_name!("server_binding_port"),
123127
LabelValue::new(&connection_context.server_service_binding.bind_address().port().to_string()),

0 commit comments

Comments
 (0)