(&self, request: HttpRequest)
| 80 | } |
| 81 | |
| 82 | async fn metrics(&self, request: HttpRequest) -> HttpResponse { |
| 83 | if request.method.to_lowercase() != "get" { |
| 84 | return HttpResponse { |
| 85 | status_code: 405, |
| 86 | headers: vec![HeaderField("Allow".into(), "GET".into())], |
| 87 | body: "405 Method Not Allowed".as_bytes().to_owned(), |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | // Trigger active users metric update. |
| 92 | METRIC_ACTIVE_USERS.with(|metric| metric.borrow_mut().refresh(time())); |
| 93 | |
| 94 | // Add dynamic metrics, dropped after the request since query calls don't save state changes. |
| 95 | with_metrics_registry(SERVICE_NAME, |registry| { |
| 96 | registry |
| 97 | .gauge_mut( |
| 98 | "canister_cycles_balance", |
| 99 | "cycles balance available to the canister", |
| 100 | ) |
| 101 | .set(canister_balance() as f64); |
| 102 | }); |
| 103 | |
| 104 | with_metrics_registry(SERVICE_NAME, |registry| { |
| 105 | registry.export_metrics_as_http_response() |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[cfg(test)] |
no test coverage detected