Skip to content

Overhaul stats: Refactor events #1341

@josecelano

Description

@josecelano

There are three types of statistics events (one for the HTTP tracker and another for the UDP tracker):

bittorrent_http_tracker_core::statistics::event::Event:

pub enum Event {
    Tcp4Announce,
    Tcp4Scrape,
    Tcp6Announce,
    Tcp6Scrape,
}

bittorrent_udp_tracker_core::statistics::event::Event

pub enum Event {
    Udp4Connect,
    Udp4Announce,
    Udp4Scrape,
    Udp6Connect,
    Udp6Announce,
    Udp6Scrape,
}

torrust_udp_tracker_server::statistics::event::Event:

pub enum Event {
    UdpRequestAborted,
    UdpRequestBanned,

    // UDP4
    Udp4IncomingRequest,
    Udp4Request {
        kind: UdpResponseKind,
    },
    Udp4Response {
        kind: UdpResponseKind,
        req_processing_time: Duration,
    },
    Udp4Error,

    // UDP6
    Udp6IncomingRequest,
    Udp6Request {
        kind: UdpResponseKind,
    },
    Udp6Response {
        kind: UdpResponseKind,
        req_processing_time: Duration,
    },
    Udp6Error,
}

I think the code would be simpler and the events would be more useful if we enrich them. Something like this:

bittorrent_http_tracker_core::statistics::event::Event:

pub enum Event {
    Announce { ip: IpAddr }
    Scrape { ip: IpAddr }
}

bittorrent_udp_tracker_core::statistics::event::Event:

pub enum Event {
    Connect { ip: IpAddr }
    Announce { ip: IpAddr }
    Scrape { ip: IpAddr }
}

torrust-udp-tracker-server::statistics::event::Event:

pub enum Event {
    UdpRequestAborted { ip: IpAddr }
    UdpRequestBanned { ip: IpAddr }
    UdpRequest { ip: IpAddr }
    UdpResponse {
        ip: IpAddr,
        req_processing_time: Duration,
    },
    UdpError { ip: IpAddr }
}

That would also simplify the way we send them, from this:

if let Some(udp_stats_event_sender) = self.udp_tracker_container.udp_stats_event_sender.as_deref() {
    match target.ip() {
        IpAddr::V4(_) => {
            udp_stats_event_sender
                .send_event(statistics::event::Event::Udp4Response {
                    kind: udp_response_kind,
                    req_processing_time,
                })
                .await;
        }
        IpAddr::V6(_) => {
            udp_stats_event_sender
                .send_event(statistics::event::Event::Udp6Response {
                    kind: udp_response_kind,
                    req_processing_time,
                })
                .await;
        }
    }
}

To this:

if let Some(udp_stats_event_sender) = self.udp_tracker_container.udp_stats_event_sender.as_deref() {
    udp_stats_event_sender
        .send_event(statistics::event::Event::UdpResponse {
            ip: target.ip(),
            req_processing_time,
        })
        .await;
}

These events would be more useful for external consumers. For example, adding the IP would allow external event consumers to extract more information and build their own aggregate data. This relates to #1307.

We might need to add more info like the server socket address of the UDP server that handled the request.

pub enum Event {
    Connect { client_ip: IpAddr, server_socket_addr: SocketAddr }
    Announce { client_ip: IpAddr, server_socket_addr: SocketAddr }
    Scrape { client_ip: IpAddr, server_socket_addr: SocketAddr }
}

See #1263 (comment)

Sub-issues

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions