Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Feb 28, 2025
1 parent ea637fb commit 49d18e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ impl OperationDispatcher {
OperationDispatcher {
operations: vec![],
waiting_operations: HashMap::default(),
auth_service_metrics: Default::default(),
rl_service_metrics: Default::default(),
header_resolver: Rc::new(HeaderResolver::default()),
}
}
Expand Down Expand Up @@ -406,6 +408,7 @@ mod tests {
result: RefCell::new(Ok(0)),
action: Rc::new(action),
service_handler: build_grpc_service_handler(),
service_metrics: Default::default(),
grpc_call_fn: grpc_call_fn_stub,
get_map_values_bytes_fn: get_map_values_bytes_fn_stub,
grpc_message_build_fn: grpc_message_build_fn_stub,
Expand Down
44 changes: 38 additions & 6 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,57 @@ impl ServiceMetrics {
}
}

fn report(metric_id: u32, offset: i64) {
#[cfg(not(test))]
fn hostcall_increment_metric(metric_id: u32, offset: i64) {
if let Err(e) = hostcalls::increment_metric(metric_id, offset) {
warn!("report metric {metric_id}, error: {e:?}");
warn!("hostcalls::increment_metric metric {metric_id}, offset {offset} , error: {e:?}");
}
}

fn increment_metric(metric_id: u32, offset: i64) {
#[cfg(test)]
tests::increment_metric(metric_id, offset);
#[cfg(not(test))]
Self::hostcall_increment_metric(metric_id, offset);
}

pub fn report_error(&self) {
Self::report(self.error_metric_id, 1);
Self::increment_metric(self.error_metric_id, 1);
}

pub fn report_allowed_on_failure(&self) {
Self::report(self.failure_mode_allowed_metric_id, 1);
Self::increment_metric(self.failure_mode_allowed_metric_id, 1);
}

pub fn report_ok(&self) {
Self::report(self.ok_metric_id, 1);
Self::increment_metric(self.ok_metric_id, 1);
}

pub fn report_rejected(&self) {
Self::report(self.rejected_metric_id, 1);
Self::increment_metric(self.rejected_metric_id, 1);
}
}

#[cfg(test)]
mod tests {
use log::debug;
use std::cell::Cell;

thread_local!(
pub static TEST_INCREMENT_METRIC_VALUE: Cell<Option<(u32, i64)>> =
const { Cell::new(None) };
);

pub fn increment_metric(metric_id: u32, offset: i64) {
debug!("increment_metric: metric_id: {metric_id}, offset: {offset}");
match TEST_INCREMENT_METRIC_VALUE.take() {
None => panic!(
"unexpected call to increment metric metric_id: {metric_id} offset: {offset}"
),
Some((expected_metric_id, expected_offset)) => {
assert_eq!(expected_metric_id, metric_id);
assert_eq!(expected_offset, offset);
}
}
}
}

0 comments on commit 49d18e4

Please sign in to comment.