libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

observability.rs (2612B)


      1 /*
      2 * This file is part of LibEuFin.
      3 * Copyright (C) 2026 Taler Systems S.A.
      4 
      5 * LibEuFin is free software; you can redistribute it and/or modify
      6 * it under the terms of the GNU Affero General Public License as
      7 * published by the Free Software Foundation; either version 3, or
      8 * (at your option) any later version.
      9 
     10 * LibEuFin is distributed in the hope that it will be useful, but
     11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13 * Public License for more details.
     14 
     15 * You should have received a copy of the GNU Affero General Public
     16 * License along with LibEuFin; see the file COPYING.  If not, see
     17 * <http://www.gnu.org/licenses/>
     18 */
     19 
     20 use std::sync::Arc;
     21 
     22 use axum::{Json, Router, extract::State, http::header::CONTENT_TYPE, routing::get};
     23 use prometheus_client::encoding::text::encode;
     24 use taler_api::error::ApiResult;
     25 use taler_common::api::observability::Config;
     26 
     27 use crate::{
     28     api::{BankState, IMPLEMENTATION, OBSERVABILITY_API_VERSION},
     29     auth::{AdminAuth, ObservabilityScope},
     30 };
     31 
     32 pub fn observability_api() -> Router<Arc<BankState>> {
     33     Router::new()
     34         .route(
     35             "/taler-observability/config",
     36             get(async || {
     37                 Json(Config {
     38                     name: (),
     39                     version: OBSERVABILITY_API_VERSION,
     40                     implementation: Some(IMPLEMENTATION),
     41                 })
     42             }),
     43         )
     44         .route(
     45             "/taler-observability/metrics",
     46             get(
     47                 async |_: AdminAuth<ObservabilityScope>, State(state): State<Arc<BankState>>| {
     48                     state.metrics.sync(&state.db).await;
     49                     let mut buffer = String::new();
     50                     encode(&mut buffer, &state.registry).unwrap();
     51                     ApiResult::Ok((
     52                         [(
     53                             CONTENT_TYPE,
     54                             "application/openmetrics-text; version=1.0.0; charset=utf-8",
     55                         )],
     56                         buffer,
     57                     ))
     58                 },
     59             ),
     60         )
     61 }
     62 
     63 #[cfg(test)]
     64 mod test {
     65     use sqlx::postgres::PgConnectOptions;
     66     use taler_macros::db_test;
     67     use taler_test_utils::server::TestServer as _;
     68 
     69     use crate::api::test::bank_setup;
     70 
     71     #[db_test(raw)]
     72     async fn observability(db: PgConnectOptions) {
     73         let ctx = bank_setup(db).await;
     74 
     75         ctx.get("/taler-observability/config").await.assert_ok();
     76         ctx.get_admin("/taler-observability/metrics")
     77             .await
     78             .assert_ok();
     79     }
     80 }