Skip to main content

otelite_api/api/
admin.rs

1// Admin endpoints
2
3use crate::server::AppState;
4use axum::{extract::State, http::StatusCode, Json};
5use otelite_core::api::ErrorResponse;
6use serde::{Deserialize, Serialize};
7
8/// Response returned by the purge-all endpoint
9#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
10pub struct PurgeAllResponse {
11    /// Number of log records deleted
12    pub logs_deleted: u64,
13    /// Number of spans deleted
14    pub spans_deleted: u64,
15    /// Number of metric data points deleted
16    pub metrics_deleted: u64,
17}
18
19/// Delete all telemetry data
20#[utoipa::path(
21    post,
22    path = "/api/admin/purge",
23    responses(
24        (status = 200, description = "All telemetry data deleted", body = PurgeAllResponse),
25        (status = 500, description = "Internal server error", body = ErrorResponse)
26    ),
27    tag = "admin"
28)]
29pub async fn purge_all(
30    State(state): State<AppState>,
31) -> Result<Json<PurgeAllResponse>, (StatusCode, Json<ErrorResponse>)> {
32    let stats = state.storage.purge_all().await.map_err(|e| {
33        (
34            StatusCode::INTERNAL_SERVER_ERROR,
35            Json(ErrorResponse::storage_error(format!("purge all: {}", e))),
36        )
37    })?;
38
39    // Purged data must not be served from any cache.
40    state.cache_state.clear_all();
41    state.cache.clear_all();
42
43    Ok(Json(PurgeAllResponse {
44        logs_deleted: stats.logs_deleted,
45        spans_deleted: stats.spans_deleted,
46        metrics_deleted: stats.metrics_deleted,
47    }))
48}