taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

observability.rs (2288B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use std::sync::Arc;
     18 
     19 use axum::{
     20     Json, Router, extract::State, http::header::CONTENT_TYPE, response::IntoResponse as _,
     21     routing::get,
     22 };
     23 use prometheus_client::{encoding::text::encode, registry::Registry};
     24 use taler_common::api::observability::Config;
     25 
     26 use crate::{
     27     api::{RouterUtils as _, TalerApi},
     28     auth::AuthMethod,
     29     constants::OBSERVABILITY_API_VERSION,
     30     error::ApiResult,
     31 };
     32 
     33 pub trait Observability: TalerApi {
     34     fn metrics(&self) -> impl std::future::Future<Output = ApiResult<&Registry>> + Send;
     35 }
     36 
     37 pub fn router<I: Observability>(state: Arc<I>, auth: AuthMethod) -> Router {
     38     Router::new()
     39         .route(
     40             "/metrics",
     41             get(async |State(state): State<Arc<I>>| {
     42                 let registry = state.metrics().await?;
     43                 let mut buffer = String::new();
     44                 encode(&mut buffer, registry).unwrap();
     45                 ApiResult::Ok((
     46                     [(
     47                         CONTENT_TYPE,
     48                         "application/openmetrics-text; version=1.0.0; charset=utf-8",
     49                     )],
     50                     buffer,
     51                 ))
     52             }),
     53         )
     54         .auth(auth, "taler-obervability")
     55         .route(
     56             "/config",
     57             get(async |State(state): State<Arc<I>>| {
     58                 Json(Config {
     59                     name: (),
     60                     version: OBSERVABILITY_API_VERSION,
     61                     implementation: Some(state.implementation()),
     62                 })
     63                 .into_response()
     64             }),
     65         )
     66         .with_state(state)
     67 }