-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http handler add X-DATABEND-VERSION in each response. (#16518)
* refactor: split middleware.rs. * small refactors. * not check type of databend_token when in management_mode. * http handler add X-DATABEND-VERSION in each response. * small refactor. * add license. * update tests. * test X-DATABEND-VERSION. * test X-DATABEND-VERSION.
- Loading branch information
1 parent
9f9e508
commit 6473d8b
Showing
12 changed files
with
179 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::time::Instant; | ||
|
||
use databend_common_metrics::http::metrics_incr_http_request_count; | ||
use databend_common_metrics::http::metrics_incr_http_slow_request_count; | ||
use databend_common_metrics::http::metrics_observe_http_response_duration; | ||
use poem::Endpoint; | ||
use poem::IntoResponse; | ||
use poem::Middleware; | ||
use poem::Request; | ||
use poem::Response; | ||
|
||
pub struct MetricsMiddleware { | ||
api: String, | ||
} | ||
|
||
impl MetricsMiddleware { | ||
pub fn new(api: impl Into<String>) -> Self { | ||
Self { api: api.into() } | ||
} | ||
} | ||
|
||
impl<E: Endpoint> Middleware<E> for MetricsMiddleware { | ||
type Output = MetricsMiddlewareEndpoint<E>; | ||
|
||
fn transform(&self, ep: E) -> Self::Output { | ||
MetricsMiddlewareEndpoint { | ||
ep, | ||
api: self.api.clone(), | ||
} | ||
} | ||
} | ||
|
||
pub struct MetricsMiddlewareEndpoint<E> { | ||
api: String, | ||
ep: E, | ||
} | ||
|
||
impl<E: Endpoint> Endpoint for MetricsMiddlewareEndpoint<E> { | ||
type Output = Response; | ||
|
||
async fn call(&self, req: Request) -> poem::error::Result<Self::Output> { | ||
let start_time = Instant::now(); | ||
let method = req.method().to_string(); | ||
let output = self.ep.call(req).await?; | ||
let resp = output.into_response(); | ||
let status_code = resp.status().to_string(); | ||
let duration = start_time.elapsed(); | ||
metrics_incr_http_request_count(method.clone(), self.api.clone(), status_code.clone()); | ||
metrics_observe_http_response_duration(method.clone(), self.api.clone(), duration); | ||
if duration.as_secs_f64() > 60.0 { | ||
// TODO: replace this into histogram | ||
metrics_incr_http_slow_request_count(method, self.api.clone(), status_code); | ||
} | ||
Ok(resp) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
mod metrics; | ||
mod panic_handler; | ||
mod session; | ||
|
||
pub(crate) use metrics::MetricsMiddleware; | ||
pub(crate) use panic_handler::PanicHandler; | ||
pub use session::json_response; | ||
pub(crate) use session::sanitize_request_headers; | ||
pub use session::EndpointKind; | ||
// for it tests only | ||
pub use session::HTTPSessionEndpoint; | ||
pub use session::HTTPSessionMiddleware; |
36 changes: 36 additions & 0 deletions
36
src/query/service/src/servers/http/middleware/panic_handler.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::any::Any; | ||
|
||
use databend_common_metrics::http::metrics_incr_http_response_panics_count; | ||
use http::StatusCode; | ||
|
||
#[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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.