-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored logic into server and service. Server working (#868)
- Loading branch information
Showing
7 changed files
with
87 additions
and
47 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod state; | ||
pub mod service; | ||
pub mod server; | ||
pub mod state; |
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,54 @@ | ||
use prometheus::{Encoder, TextEncoder}; | ||
|
||
use crate::telemetry::service::MetricUpdate; | ||
use crossbeam_channel::Sender; | ||
use tracing::info; | ||
use crate::telemetry::state::TelemetryState; | ||
|
||
pub struct TelemetryServer { | ||
pub state: TelemetryState, | ||
} | ||
|
||
impl TelemetryServer { | ||
fn new(state: TelemetryState) -> TelemetryServer { | ||
TelemetryServer { state } | ||
} | ||
} | ||
|
||
impl TelemetryServer { | ||
fn run(listen_port: u16) -> () { | ||
let telemetry_state = TelemetryState::new(); | ||
rouille::start_server(format!("localhost:{}", listen_port), move |request| { | ||
router!(request, | ||
// The prometheus endpoint | ||
(GET) (/metrics) => { | ||
telemetry_state.packets_relayed.add(1); | ||
info!("metrics called on telemetry server"); | ||
let mut buffer = vec![]; | ||
let encoder = TextEncoder::new(); | ||
let metric_families = telemetry_state.exporter.registry().gather(); | ||
encoder.encode(&metric_families, &mut buffer).unwrap(); | ||
dbg!(metric_families); | ||
rouille::Response::from_data(encoder.format_type().to_string(), buffer) | ||
}, | ||
|
||
// Any route other than /metrics | ||
// return an empty response with a 404 status code. | ||
_ => { | ||
rouille::Response::empty_404() | ||
} | ||
) | ||
}); | ||
} | ||
|
||
pub fn spawn(port: u16) -> Sender<MetricUpdate> { | ||
|
||
let (tx, _rx) = crossbeam_channel::unbounded(); | ||
//let (service, tx) = TelemetryService::new(app_state.clone()); | ||
//let server = TelemetryServer::new(app_state.clone()); | ||
std::thread::spawn(move || TelemetryServer::run( port)); | ||
//std::thread::spawn(|| service.run()); | ||
|
||
tx | ||
} | ||
} |
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 |
---|---|---|
@@ -1,33 +1,27 @@ | ||
use prometheus::{Encoder, TextEncoder}; | ||
|
||
use crate::telemetry::state::TelemetryState; | ||
use std::sync::Arc; | ||
use crossbeam_channel::Receiver; | ||
use crate::telemetry::state::TelemetryState; | ||
|
||
pub struct TelemetryService {} | ||
|
||
impl TelemetryService { | ||
pub fn run(state: Arc<TelemetryState>, listen_port: u16) -> () { | ||
rouille::start_server(format!("localhost:{}", listen_port), move |request| { | ||
router!(request, | ||
// The prometheus endpoint | ||
(GET) (/metrics) => { | ||
|
||
state.tx_counter.add(1); | ||
pub enum MetricUpdate { | ||
PacketsRelayed(u64) | ||
} | ||
|
||
let mut buffer = vec![]; | ||
let encoder = TextEncoder::new(); | ||
let metric_families = state.exporter.registry().gather(); | ||
encoder.encode(&metric_families, &mut buffer).unwrap(); | ||
pub struct TelemetryService { | ||
pub state: Arc<TelemetryState>, | ||
pub rx: Receiver<MetricUpdate> | ||
} | ||
|
||
rouille::Response::from_data(encoder.format_type().to_string(), buffer) | ||
}, | ||
impl TelemetryService { | ||
fn run(self) { | ||
while let Ok(update) = self.rx.recv() { | ||
self.apply_update(update); | ||
} | ||
} | ||
|
||
// Any route other than /metrics | ||
// return an empty response with a 404 status code. | ||
_ => { | ||
rouille::Response::empty_404() | ||
} | ||
) | ||
}); | ||
fn apply_update(&self, update: MetricUpdate) { | ||
match update { | ||
MetricUpdate::PacketsRelayed(n) => self.state.packets_relayed.add(n ), | ||
} | ||
} | ||
} |
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