Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): add metrics on panics & query #12629

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/query/service/src/servers/http/http_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use poem::Route;
use super::v1::upload_to_stage;
use crate::auth::AuthMgr;
use crate::servers::http::middleware::HTTPSessionMiddleware;
use crate::servers::http::middleware::PanicHandler;
use crate::servers::http::v1::clickhouse_router;
use crate::servers::http::v1::list_suggestions;
use crate::servers::http::v1::query_route;
Expand Down Expand Up @@ -122,7 +123,7 @@ impl HttpHandler {
.nest("/health", ep_health),
};
ep.with(NormalizePath::new(TrailingSlash::Trim))
.with(CatchPanic::new())
.with(CatchPanic::new().with_handler(PanicHandler::new()))
.boxed()
}

Expand Down
9 changes: 9 additions & 0 deletions src/query/service/src/servers/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ pub fn metrics_incr_http_slow_request_count(method: String, api: String, status:
let labels = [("method", method), ("api", api), ("status", status)];
counter!("query_http_slow_requests_count", 1, &labels);
}

pub fn metrics_incr_http_response_errors_count(err: String, code: u16) {
let labels = [("err", err), ("code", code.to_string())];
counter!("query_http_response_errors_count", 1, &labels);
}

pub fn metrics_incr_http_response_panics_count() {
counter!("query_http_response_panics_count", 1);
}
20 changes: 20 additions & 0 deletions src/query/service/src/servers/http/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::any::Any;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
Expand Down Expand Up @@ -41,6 +42,7 @@ use super::v1::HttpQueryContext;
use crate::auth::AuthMgr;
use crate::auth::Credential;
use crate::servers::http::metrics::metrics_incr_http_request_count;
use crate::servers::http::metrics::metrics_incr_http_response_panics_count;
use crate::servers::http::metrics::metrics_incr_http_slow_request_count;
use crate::servers::HttpHandlerKind;
use crate::sessions::SessionManager;
Expand Down Expand Up @@ -314,3 +316,21 @@ impl<E: Endpoint> Endpoint for MetricsMiddlewareEndpoint<E> {
Ok(resp)
}
}

#[derive(Clone, Debug)]
pub(crate) struct PanicHandler {}

impl PanicHandler {
pub fn new() -> Self {
Self {}
}
}

impl poem::middleware::PanicHandler for PanicHandler {
type Response = (StatusCode, &'static str);

fn get_response(&self, _err: Box<dyn Any + Send + 'static>) -> Self::Response {
metrics_incr_http_response_panics_count();
(StatusCode::INTERNAL_SERVER_ERROR, "internal server error")
}
}
7 changes: 7 additions & 0 deletions src/query/service/src/servers/http/v1/http_query_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use serde_json::Value as JsonValue;
use super::query::ExecuteStateKind;
use super::query::HttpQueryRequest;
use super::query::HttpQueryResponseInternal;
use crate::servers::http::metrics::metrics_incr_http_response_errors_count;
use crate::servers::http::middleware::MetricsMiddleware;
use crate::servers::http::v1::query::Progresses;
use crate::servers::http::v1::HttpQueryContext;
Expand Down Expand Up @@ -156,13 +157,18 @@ impl QueryResponse {
}
};

if let Some(err) = &r.state.error {
metrics_incr_http_response_errors_count(err.name(), err.code());
}

let schema = data.schema().clone();
let session_id = r.session_id.clone();
let stats = QueryStats {
progresses: state.progresses.clone(),
running_time_ms: state.running_time_ms,
};
let rows = data.data.len();

Json(QueryResponse {
data: data.into(),
state: state.state,
Expand All @@ -184,6 +190,7 @@ impl QueryResponse {
}

pub(crate) fn fail_to_start_sql(err: &ErrorCode) -> impl IntoResponse {
metrics_incr_http_response_errors_count(err.name(), err.code());
Json(QueryResponse {
id: "".to_string(),
stats: QueryStats::default(),
Expand Down