Skip to main content

otelite_api/api/
stats.rs

1// Storage statistics endpoint
2
3use crate::server::AppState;
4use axum::{extract::State, http::StatusCode, Json};
5use otelite_core::api::ErrorResponse;
6use serde::{Deserialize, Serialize};
7
8/// Storage statistics response
9#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
10pub struct StatsResponse {
11    /// Total number of log records stored
12    pub log_count: u64,
13    /// Total number of spans stored
14    pub span_count: u64,
15    /// Total number of metric data points stored
16    pub metric_count: u64,
17    /// Total storage size in bytes
18    pub storage_size_bytes: u64,
19}
20
21/// Storage statistics handler
22#[utoipa::path(
23    get,
24    path = "/api/stats",
25    responses(
26        (status = 200, description = "Storage statistics", body = StatsResponse),
27        (status = 500, description = "Internal server error", body = ErrorResponse)
28    ),
29    tag = "stats"
30)]
31pub async fn get_stats(
32    State(state): State<AppState>,
33) -> Result<Json<StatsResponse>, (StatusCode, Json<ErrorResponse>)> {
34    let storage_stats = state.storage.stats().await.map_err(|e| {
35        (
36            StatusCode::INTERNAL_SERVER_ERROR,
37            Json(ErrorResponse::storage_error(format!("get stats: {}", e))),
38        )
39    })?;
40
41    Ok(Json(StatsResponse {
42        log_count: storage_stats.log_count,
43        span_count: storage_stats.span_count,
44        metric_count: storage_stats.metric_count,
45        storage_size_bytes: storage_stats.storage_size_bytes,
46    }))
47}