Skip to content

Commit

Permalink
Move pprof routes under api/developer
Browse files Browse the repository at this point in the history
  • Loading branch information
guilload committed Jun 5, 2024
1 parent 01571db commit a3e912a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
6 changes: 3 additions & 3 deletions quickwit/quickwit-indexing/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ pub async fn check_source_connectivity(
#[allow(unused_variables)]
SourceParams::Kafka(params) => {
#[cfg(not(feature = "kafka"))]
anyhow::bail!("Quickwit binary was not compiled with the `kafka` feature");
anyhow::bail!("Quickwit was compiled without the `kafka` feature");

#[cfg(feature = "kafka")]
{
Expand All @@ -432,7 +432,7 @@ pub async fn check_source_connectivity(
#[allow(unused_variables)]
SourceParams::Kinesis(params) => {
#[cfg(not(feature = "kinesis"))]
anyhow::bail!("Quickwit binary was not compiled with the `kinesis` feature");
anyhow::bail!("Quickwit was compiled without the `kinesis` feature");

#[cfg(feature = "kinesis")]
{
Expand All @@ -443,7 +443,7 @@ pub async fn check_source_connectivity(
#[allow(unused_variables)]
SourceParams::Pulsar(params) => {
#[cfg(not(feature = "pulsar"))]
anyhow::bail!("Quickwit binary was not compiled with the `pulsar` feature");
anyhow::bail!("Quickwit was compiled without the `pulsar` feature");

#[cfg(feature = "pulsar")]
{
Expand Down
9 changes: 7 additions & 2 deletions quickwit/quickwit-serve/src/developer_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

mod debug;
mod log_level;
mod pprof;
mod server;

use debug::debug_handler;
use log_level::log_level_handler;
use pprof::pprof_handlers;
use quickwit_cluster::Cluster;
pub(crate) use server::DeveloperApiServer;
use warp::{Filter, Rejection};
Expand All @@ -37,6 +39,9 @@ pub(crate) fn developer_api_routes(
cluster: Cluster,
env_filter_reload_fn: EnvFilterReloadFn,
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
warp::path!("api" / "developer" / ..)
.and(debug_handler(cluster.clone()).or(log_level_handler(env_filter_reload_fn.clone())))
warp::path!("api" / "developer" / ..).and(
debug_handler(cluster.clone())
.or(log_level_handler(env_filter_reload_fn.clone()))
.or(pprof_handlers()),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@ use warp::Filter;
/// pprof/start disabled
/// pprof/flamegraph disabled
#[cfg(not(feature = "pprof"))]
pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
pub fn pprof_handlers() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
{
let start_profiler = {
warp::path!("pprof" / "start").map(move || {
warp::reply::with_status(
"not compiled with pprof feature",
warp::http::StatusCode::BAD_REQUEST,
"Quickwit was compiled without the `pprof` feature",
warp::http::StatusCode::NOT_IMPLEMENTED,
)
})
};

let stop_profiler = {
warp::path!("pprof" / "flamegraph").map(move || {
warp::reply::with_status(
"not compiled with pprof feature",
warp::http::StatusCode::BAD_REQUEST,
"Quickwit was compiled without the `pprof` feature",
warp::http::StatusCode::NOT_IMPLEMENTED,
)
})
};

start_profiler.or(stop_profiler)
}

Expand All @@ -54,7 +53,8 @@ pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::R
/// 300
/// - sampling: the sampling rate, default is 100, max value is 1000
#[cfg(feature = "pprof")]
pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
pub fn pprof_handlers() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
{
use std::sync::{Arc, Mutex};

use pprof::ProfilerGuard;
Expand Down Expand Up @@ -101,6 +101,7 @@ pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::R
params: ProfilerQueryParams,
) -> Result<impl warp::Reply, warp::Rejection> {
let mut state = profiler_state.lock().unwrap();

if state.profiler_guard.is_none() {
let max_duration = params.max_duration.unwrap_or(30).min(300);
let sampling = params.sampling.unwrap_or(100).min(1000);
Expand Down Expand Up @@ -128,11 +129,12 @@ pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::R
save_flamegraph(profiler_state.clone()).await;

let state = profiler_state.lock().unwrap();

if let Some(data) = state.flamegraph_data.clone() {
Ok(warp::reply::with_header(data, "Content-Type", "image/svg+xml").into_response())
} else {
Ok(warp::reply::with_status(
"No flamegraph available",
"flamegraph is not available",
warp::http::StatusCode::BAD_REQUEST,
)
.into_response())
Expand All @@ -144,6 +146,7 @@ pub fn pprof_routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::R
) -> tokio::task::JoinHandle<()> {
spawn_blocking(move || {
let mut state = profiler_state.lock().unwrap();

if let Some(profiler) = state.profiler_guard.take() {
if let Ok(report) = profiler.report().build() {
let mut buffer = Vec::new();
Expand Down
1 change: 0 additions & 1 deletion quickwit/quickwit-serve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ mod metrics_api;
mod node_info_handler;
mod openapi;
mod otlp_api;
mod pprof;
mod rate_modulator;
mod rest;
mod rest_api_response;
Expand Down
6 changes: 0 additions & 6 deletions quickwit/quickwit-serve/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use crate::jaeger_api::jaeger_api_handlers;
use crate::metrics_api::metrics_handler;
use crate::node_info_handler::node_info_handler;
use crate::otlp_api::otlp_ingest_api_handlers;
use crate::pprof::pprof_routes;
use crate::rest_api_response::{RestApiError, RestApiResponse};
use crate::search_api::{search_get_handler, search_post_handler, search_stream_handler};
use crate::template_api::index_template_api_handlers;
Expand Down Expand Up @@ -158,10 +157,6 @@ pub(crate) async fn start_rest_server(
quickwit_services.cluster.clone(),
quickwit_services.env_filter_reload_fn.clone(),
);

// `/pprof` route.
let cpu_pprof_routes = pprof_routes();

// `/api/v1/*` routes.
let api_v1_root_route = api_v1_routes(quickwit_services.clone());

Expand All @@ -185,7 +180,6 @@ pub(crate) async fn start_rest_server(
.or(health_check_routes)
.or(metrics_routes)
.or(developer_routes)
.or(cpu_pprof_routes)
.with(request_counter)
.recover(recover_fn)
.with(extra_headers)
Expand Down

0 comments on commit a3e912a

Please sign in to comment.