feat: Log error in GreptimeRequestHandler (#1507)

* feat(common-error): Add should_log_error

* feat(servers): log error in grpc handler
This commit is contained in:
Yingwen
2023-05-04 15:48:38 +08:00
committed by GitHub
parent d7a906e0bd
commit 2818f466d3
2 changed files with 46 additions and 4 deletions

View File

@@ -83,10 +83,12 @@ pub enum StatusCode {
}
impl StatusCode {
/// Returns `true` if `code` is success.
pub fn is_success(code: u32) -> bool {
Self::Success as u32 == code
}
/// Returns `true` if the error with this code is retryable.
pub fn is_retryable(&self) -> bool {
match self {
StatusCode::StorageUnavailable
@@ -114,6 +116,35 @@ impl StatusCode {
| StatusCode::AccessDenied => false,
}
}
/// Returns `true` if we should print an error log for an error with
/// this status code.
pub fn should_log_error(&self) -> bool {
match self {
StatusCode::Unknown
| StatusCode::Unsupported
| StatusCode::Unexpected
| StatusCode::Internal
| StatusCode::PlanQuery
| StatusCode::EngineExecuteQuery
| StatusCode::StorageUnavailable
| StatusCode::RuntimeResourcesExhausted => true,
StatusCode::Success
| StatusCode::InvalidArguments
| StatusCode::InvalidSyntax
| StatusCode::TableAlreadyExists
| StatusCode::TableNotFound
| StatusCode::TableColumnNotFound
| StatusCode::TableColumnExists
| StatusCode::DatabaseNotFound
| StatusCode::UserNotFound
| StatusCode::UnsupportedPasswordType
| StatusCode::UserPasswordMismatch
| StatusCode::AuthHeaderNotFound
| StatusCode::InvalidAuthHeader
| StatusCode::AccessDenied => false,
}
}
}
impl fmt::Display for StatusCode {

View File

@@ -19,7 +19,7 @@ use api::v1::{Basic, GreptimeRequest, RequestHeader};
use common_error::prelude::ErrorExt;
use common_query::Output;
use common_runtime::Runtime;
use common_telemetry::timer;
use common_telemetry::{logging, timer};
use metrics::increment_counter;
use session::context::{QueryContext, QueryContextRef};
use snafu::OptionExt;
@@ -73,11 +73,22 @@ impl GreptimeRequestHandler {
// - Obtaining a `JoinHandle` to get the panic message (if there's any).
// From its docs, `JoinHandle` is cancel safe. The task keeps running even it's handle been dropped.
// 2. avoid the handler blocks the gRPC runtime incidentally.
let handle = self
.runtime
.spawn(async move { handler.do_query(query, query_ctx).await });
let handle = self.runtime.spawn(async move {
handler.do_query(query, query_ctx).await.map_err(|e| {
if e.status_code().should_log_error() {
logging::error!(e; "Failed to handle request");
} else {
// Currently, we still print a debug log.
logging::debug!("Failed to handle request, err: {}", e);
}
e
})
});
let output = handle.await.map_err(|e| {
// logs the runtime join error.
logging::error!("Failed to join handle, err: {}", e);
if e.is_cancelled() {
Status::cancelled(e.to_string())
} else if e.is_panic() {