servers/http/
mem_prof.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "mem-prof")]
16use axum::extract::Query;
17use axum::http::StatusCode;
18use axum::response::IntoResponse;
19use serde::{Deserialize, Serialize};
20
21/// Output format.
22#[derive(Debug, Default, Serialize, Deserialize)]
23#[serde(rename_all = "snake_case")]
24pub enum Output {
25    /// google’s pprof format report in protobuf.
26    Proto,
27    /// jeheap text format. Define by jemalloc.
28    #[default]
29    Text,
30    /// svg flamegraph.
31    Flamegraph,
32}
33
34#[derive(Default, Serialize, Deserialize, Debug)]
35#[serde(default)]
36pub struct MemPprofQuery {
37    output: Output,
38}
39
40#[cfg(feature = "mem-prof")]
41#[axum_macros::debug_handler]
42pub async fn mem_prof_handler(
43    Query(req): Query<MemPprofQuery>,
44) -> crate::error::Result<impl IntoResponse> {
45    use snafu::ResultExt;
46
47    use crate::error::DumpProfileDataSnafu;
48
49    let dump = match req.output {
50        Output::Proto => common_mem_prof::dump_pprof().await,
51        Output::Text => common_mem_prof::dump_profile().await,
52        Output::Flamegraph => common_mem_prof::dump_flamegraph().await,
53    }
54    .context(DumpProfileDataSnafu)?;
55
56    Ok((StatusCode::OK, dump))
57}
58
59#[cfg(not(feature = "mem-prof"))]
60#[axum_macros::debug_handler]
61pub async fn mem_prof_handler() -> crate::error::Result<impl IntoResponse> {
62    Ok((
63        StatusCode::NOT_IMPLEMENTED,
64        "The 'mem-prof' feature is disabled",
65    ))
66}