refactor: put together HTTP headers (#3337)

* refactor: put together HTTP headers

Signed-off-by: tison <wander4096@gmail.com>

* do refactor

Signed-off-by: tison <wander4096@gmail.com>

* drop dirty commit

Signed-off-by: tison <wander4096@gmail.com>

* reduce changeset

Signed-off-by: tison <wander4096@gmail.com>

* fixup compilations

Signed-off-by: tison <wander4096@gmail.com>

* tidy files

Signed-off-by: tison <wander4096@gmail.com>

* drop common-api

Signed-off-by: tison <wander4096@gmail.com>

* fmt

Signed-off-by: tison <wander4096@gmail.com>

---------

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison
2024-02-21 17:51:10 +08:00
committed by GitHub
parent a7bf458a37
commit 4c07606da6
10 changed files with 60 additions and 27 deletions

View File

@@ -19,7 +19,9 @@ pub mod format;
pub mod mock;
pub mod status_code;
pub use snafu;
// HACK - these headers are here for shared in gRPC services. For common HTTP headers,
// please define in `src/servers/src/http/header.rs`.
pub const GREPTIME_DB_HEADER_ERROR_CODE: &str = "x-greptime-err-code";
pub const GREPTIME_DB_HEADER_ERROR_MSG: &str = "x-greptime-err-msg";
pub use snafu;

View File

@@ -596,9 +596,11 @@ macro_rules! define_into_tonic_status {
($Error: ty) => {
impl From<$Error> for tonic::Status {
fn from(err: $Error) -> Self {
use common_error::{GREPTIME_DB_HEADER_ERROR_CODE, GREPTIME_DB_HEADER_ERROR_MSG};
use tonic::codegen::http::{HeaderMap, HeaderValue};
use tonic::metadata::MetadataMap;
use $crate::http::header::constants::{
GREPTIME_DB_HEADER_ERROR_CODE, GREPTIME_DB_HEADER_ERROR_MSG,
};
let mut headers = HeaderMap::<HeaderValue>::with_capacity(2);

View File

@@ -17,7 +17,7 @@ use std::sync::Arc;
use arrow::datatypes::Schema;
use arrow_ipc::writer::FileWriter;
use axum::http::{header, HeaderName, HeaderValue};
use axum::http::{header, HeaderValue};
use axum::response::{IntoResponse, Response};
use common_error::status_code::StatusCode;
use common_query::Output;
@@ -122,15 +122,15 @@ impl IntoResponse for ArrowResponse {
(
[
(
header::CONTENT_TYPE,
&header::CONTENT_TYPE,
HeaderValue::from_static("application/arrow"),
),
(
HeaderName::from_static(GREPTIME_DB_HEADER_FORMAT),
&GREPTIME_DB_HEADER_FORMAT,
HeaderValue::from_static("ARROW"),
),
(
HeaderName::from_static(GREPTIME_DB_HEADER_EXECUTION_TIME),
&GREPTIME_DB_HEADER_EXECUTION_TIME,
HeaderValue::from(execution_time),
),
],

View File

@@ -101,9 +101,9 @@ impl IntoResponse for CsvResponse {
)
.into_response();
resp.headers_mut()
.insert(GREPTIME_DB_HEADER_FORMAT, HeaderValue::from_static("CSV"));
.insert(&GREPTIME_DB_HEADER_FORMAT, HeaderValue::from_static("CSV"));
resp.headers_mut().insert(
GREPTIME_DB_HEADER_EXECUTION_TIME,
&GREPTIME_DB_HEADER_EXECUTION_TIME,
HeaderValue::from(execution_time),
);
resp

View File

@@ -17,11 +17,11 @@ use axum::response::{IntoResponse, Response};
use axum::Json;
use common_error::ext::ErrorExt;
use common_error::status_code::StatusCode;
use common_error::{GREPTIME_DB_HEADER_ERROR_CODE, GREPTIME_DB_HEADER_ERROR_MSG};
use common_telemetry::logging::{debug, error};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::http::header::constants::{GREPTIME_DB_HEADER_ERROR_CODE, GREPTIME_DB_HEADER_ERROR_MSG};
use crate::http::header::{GREPTIME_DB_HEADER_EXECUTION_TIME, GREPTIME_DB_HEADER_FORMAT};
use crate::http::ResponseFormat;
@@ -88,9 +88,9 @@ impl IntoResponse for ErrorResponse {
HeaderValue::from_str(&msg).expect("malformed error msg"),
);
resp.headers_mut()
.insert(GREPTIME_DB_HEADER_FORMAT, HeaderValue::from_static(ty));
.insert(&GREPTIME_DB_HEADER_FORMAT, HeaderValue::from_static(ty));
resp.headers_mut().insert(
GREPTIME_DB_HEADER_EXECUTION_TIME,
&GREPTIME_DB_HEADER_EXECUTION_TIME,
HeaderValue::from(execution_time),
);
resp

View File

@@ -76,15 +76,15 @@ impl IntoResponse for GreptimedbV1Response {
let mut resp = Json(self).into_response();
resp.headers_mut().insert(
GREPTIME_DB_HEADER_FORMAT,
&GREPTIME_DB_HEADER_FORMAT,
HeaderValue::from_static("greptimedb_v1"),
);
resp.headers_mut().insert(
GREPTIME_DB_HEADER_EXECUTION_TIME,
&GREPTIME_DB_HEADER_EXECUTION_TIME,
HeaderValue::from(execution_time),
);
if let Some(m) = metrics.and_then(|m| HeaderValue::from_str(&m).ok()) {
resp.headers_mut().insert(GREPTIME_DB_HEADER_METRICS, m);
resp.headers_mut().insert(&GREPTIME_DB_HEADER_METRICS, m);
}
resp

View File

@@ -14,16 +14,45 @@
use headers::{Header, HeaderName, HeaderValue};
pub const GREPTIME_DB_HEADER_FORMAT: &str = "x-greptime-format";
pub const GREPTIME_DB_HEADER_EXECUTION_TIME: &str = "x-greptime-execution-time";
pub const GREPTIME_DB_HEADER_METRICS: &str = "x-greptime-metrics";
pub mod constants {
// New HTTP headers would better distinguish use cases among:
// * GreptimeDB
// * GreptimeCloud
// * ...
//
// And thus trying to use:
// * x-greptime-db-xxx
// * x-greptime-cloud-xxx
//
// ... accordingly
//
// Most of the headers are for GreptimeDB and thus using `x-greptime-db-` as prefix.
// Only use `x-greptime-cloud` when it's intentionally used by GreptimeCloud.
// LEGACY HEADERS - KEEP IT UNMODIFIED
pub const GREPTIME_DB_HEADER_FORMAT: &str = "x-greptime-format";
pub const GREPTIME_DB_HEADER_EXECUTION_TIME: &str = "x-greptime-execution-time";
pub const GREPTIME_DB_HEADER_METRICS: &str = "x-greptime-metrics";
pub const GREPTIME_DB_HEADER_NAME: &str = "x-greptime-db-name";
pub const GREPTIME_TIMEZONE_HEADER_NAME: &str = "x-greptime-timezone";
pub const GREPTIME_DB_HEADER_ERROR_CODE: &str = common_error::GREPTIME_DB_HEADER_ERROR_CODE;
pub const GREPTIME_DB_HEADER_ERROR_MSG: &str = common_error::GREPTIME_DB_HEADER_ERROR_MSG;
}
pub static GREPTIME_DB_HEADER_FORMAT: HeaderName =
HeaderName::from_static(constants::GREPTIME_DB_HEADER_FORMAT);
pub static GREPTIME_DB_HEADER_EXECUTION_TIME: HeaderName =
HeaderName::from_static(constants::GREPTIME_DB_HEADER_EXECUTION_TIME);
pub static GREPTIME_DB_HEADER_METRICS: HeaderName =
HeaderName::from_static(constants::GREPTIME_DB_HEADER_METRICS);
/// Header key of `db-name`. Example format of the header value is `greptime-public`.
pub static GREPTIME_DB_HEADER_NAME: HeaderName = HeaderName::from_static("x-greptime-db-name");
/// Header key of query specific timezone.
/// Example format of the header value is `Asia/Shanghai` or `+08:00`.
pub static GREPTIME_DB_HEADER_NAME: HeaderName =
HeaderName::from_static(constants::GREPTIME_DB_HEADER_NAME);
/// Header key of query specific timezone. Example format of the header value is `Asia/Shanghai` or `+08:00`.
pub static GREPTIME_TIMEZONE_HEADER_NAME: HeaderName =
HeaderName::from_static("x-greptime-timezone");
HeaderName::from_static(constants::GREPTIME_TIMEZONE_HEADER_NAME);
pub struct GreptimeDbName(Option<String>);

View File

@@ -217,11 +217,11 @@ impl IntoResponse for InfluxdbV1Response {
let execution_time = self.execution_time_ms;
let mut resp = Json(self).into_response();
resp.headers_mut().insert(
GREPTIME_DB_HEADER_FORMAT,
&GREPTIME_DB_HEADER_FORMAT,
HeaderValue::from_static("influxdb_v1"),
);
resp.headers_mut().insert(
GREPTIME_DB_HEADER_EXECUTION_TIME,
&GREPTIME_DB_HEADER_EXECUTION_TIME,
HeaderValue::from(execution_time),
);
resp

View File

@@ -66,7 +66,7 @@ impl IntoResponse for PrometheusJsonResponse {
let mut resp = Json(self).into_response();
if let Some(m) = metrics.and_then(|m| HeaderValue::from_str(&m).ok()) {
resp.headers_mut().insert(GREPTIME_DB_HEADER_METRICS, m);
resp.headers_mut().insert(&GREPTIME_DB_HEADER_METRICS, m);
}
resp

View File

@@ -169,7 +169,7 @@ async fn test_influxdb_write() {
.await;
assert_eq!(result.status(), 401);
assert_eq!(
result.headers().get(GREPTIME_DB_HEADER_FORMAT).unwrap(),
result.headers().get(&GREPTIME_DB_HEADER_FORMAT).unwrap(),
"influxdb_v1",
);
assert_eq!(
@@ -185,7 +185,7 @@ async fn test_influxdb_write() {
.await;
assert_eq!(result.status(), 401);
assert_eq!(
result.headers().get(GREPTIME_DB_HEADER_FORMAT).unwrap(),
result.headers().get(&GREPTIME_DB_HEADER_FORMAT).unwrap(),
"influxdb_v1",
);
assert_eq!(