-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Product Data] First step in gateway usage data collection (#4963)
* add stats model * add stats collection * add stats route * propagate stuff and run stuff * cargo stuff * sqlx unused what? * add sessions started stat * session durations in miliseconds * apply Jon's comments * [Product Data] Second step in gateway usage data collection (#4964) * turn stats collection into event based * move events into a common crate for future use elsewhere * apply Jon's comments
- Loading branch information
1 parent
1fc7e07
commit 435f236
Showing
17 changed files
with
394 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2024 - Nym Technologies SA <[email protected]> | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
[package] | ||
name = "nym-statistics-common" | ||
version = "0.1.0" | ||
edition.workspace = true | ||
license.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures = { workspace = true } | ||
time = { workspace = true } | ||
|
||
nym-sphinx = { path = "../nymsphinx" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 - Nym Technologies SA <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use futures::channel::mpsc; | ||
use nym_sphinx::DestinationAddressBytes; | ||
use time::OffsetDateTime; | ||
|
||
pub type StatsEventSender = mpsc::UnboundedSender<StatsEvent>; | ||
pub type StatsEventReceiver = mpsc::UnboundedReceiver<StatsEvent>; | ||
pub enum StatsEvent { | ||
SessionStatsEvent(SessionEvent), | ||
} | ||
|
||
impl StatsEvent { | ||
pub fn new_session_start(client: DestinationAddressBytes) -> StatsEvent { | ||
StatsEvent::SessionStatsEvent(SessionEvent::SessionStart { | ||
start_time: OffsetDateTime::now_utc(), | ||
client, | ||
}) | ||
} | ||
|
||
pub fn new_session_stop(client: DestinationAddressBytes) -> StatsEvent { | ||
StatsEvent::SessionStatsEvent(SessionEvent::SessionStop { | ||
stop_time: OffsetDateTime::now_utc(), | ||
client, | ||
}) | ||
} | ||
} | ||
|
||
pub enum SessionEvent { | ||
SessionStart { | ||
start_time: OffsetDateTime, | ||
client: DestinationAddressBytes, | ||
}, | ||
SessionStop { | ||
stop_time: OffsetDateTime, | ||
client: DestinationAddressBytes, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright 2024 - Nym Technologies SA <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
pub mod events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 - Nym Technologies SA <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use futures::{channel::mpsc, StreamExt}; | ||
use nym_node_http_api::state::metrics::SharedSessionStats; | ||
use nym_statistics_common::events::{StatsEvent, StatsEventReceiver, StatsEventSender}; | ||
use nym_task::TaskClient; | ||
use sessions::SessionStatsHandler; | ||
use std::time::Duration; | ||
use time::OffsetDateTime; | ||
use tracing::trace; | ||
|
||
pub mod sessions; | ||
|
||
const STATISTICS_UPDATE_TIMER_INTERVAL: Duration = Duration::from_secs(3600); //update timer, no need to check everytime | ||
|
||
pub(crate) struct GatewayStatisticsCollector { | ||
stats_event_rx: StatsEventReceiver, | ||
session_stats: SessionStatsHandler, | ||
//here goes additionnal stats handler | ||
} | ||
|
||
impl GatewayStatisticsCollector { | ||
pub fn new( | ||
shared_session_stats: SharedSessionStats, | ||
) -> (GatewayStatisticsCollector, StatsEventSender) { | ||
let (stats_event_tx, stats_event_rx) = mpsc::unbounded(); | ||
let collector = GatewayStatisticsCollector { | ||
stats_event_rx, | ||
session_stats: SessionStatsHandler::new(shared_session_stats), | ||
}; | ||
(collector, stats_event_tx) | ||
} | ||
|
||
async fn update_shared_state(&mut self, update_time: OffsetDateTime) { | ||
self.session_stats.update_shared_state(update_time).await; | ||
//here goes additionnal stats handler update | ||
} | ||
|
||
pub async fn run(&mut self, mut shutdown: TaskClient) { | ||
let mut update_interval = tokio::time::interval(STATISTICS_UPDATE_TIMER_INTERVAL); | ||
while !shutdown.is_shutdown() { | ||
tokio::select! { | ||
biased; | ||
_ = shutdown.recv() => { | ||
trace!("StatisticsCollector: Received shutdown"); | ||
}, | ||
_ = update_interval.tick() => { | ||
let now = OffsetDateTime::now_utc(); | ||
self.update_shared_state(now).await; | ||
}, | ||
|
||
Some(stat_event) = self.stats_event_rx.next() => { | ||
//dispatching event to proper handler | ||
match stat_event { | ||
StatsEvent::SessionStatsEvent(event) => self.session_stats.handle_event(event), | ||
} | ||
}, | ||
|
||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.