Skip to content

Overhaul stats: Refactor metrics (part 2) #1446

@josecelano

Description

@josecelano

Depends on: #1445

The API exposes the "old" metrics (/stats) and the new ones (/metrics)

Internally, we use only one repository to store both:

For example, in packages/http-tracker-core/src/statistics/metrics.rs:

pub struct Metrics {
    /// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
    pub tcp4_announces_handled: u64,

    /// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
    pub tcp4_scrapes_handled: u64,

    /// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
    pub tcp6_announces_handled: u64,

    /// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
    pub tcp6_scrapes_handled: u64,

    /// A collection of metrics.
    pub metric_collection: MetricCollection,
}

impl Metrics {
    pub fn increase_counter(&mut self, metric_name: &MetricName, labels: &LabelSet, now: DurationSinceUnixEpoch) {
        self.metric_collection.increase_counter(metric_name, labels, now);
    }

    pub fn set_gauge(&mut self, metric_name: &MetricName, labels: &LabelSet, value: f64, now: DurationSinceUnixEpoch) {
        self.metric_collection.set_gauge(metric_name, labels, value, now);
    }
}

I kept global and new metrics because I didn't want to make breaking changes in the API. However, we can remove the old ones internally and calculate them from the new ones only when the API replies to a /stats request.

That would have these benefits:

  • Simplify the bittorrent_http_tracker_core::statistics::event::handler
  • Improve performance; the tracker does not have to maintain both metrics.

The cons are:

  • The old /stats endpoint will be slower
  • We must implement new methods in the MetricColelction type to calculate aggregate data. These methods could be helpful for other tasks in the future.

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions