Skip to main content

servers/http/
header.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
15use std::collections::HashMap;
16use std::sync::Arc;
17
18use common_plugins::GREPTIME_EXEC_PREFIX;
19use datafusion::physical_plan::ExecutionPlan;
20use datafusion::physical_plan::metrics::MetricValue;
21use headers::{Header, HeaderName, HeaderValue};
22use hyper::HeaderMap;
23use serde_json::Value;
24
25pub mod constants {
26    // New HTTP headers would better distinguish use cases among:
27    // * GreptimeDB
28    // * GreptimeCloud
29    // * ...
30    //
31    // And thus trying to use:
32    // * x-greptime-db-xxx
33    // * x-greptime-cloud-xxx
34    //
35    // ... accordingly
36    //
37    // Most of the headers are for GreptimeDB and thus using `x-greptime-db-` as prefix.
38    // Only use `x-greptime-cloud` when it's intentionally used by GreptimeCloud.
39
40    // LEGACY HEADERS - KEEP IT UNMODIFIED
41    pub const GREPTIME_DB_HEADER_FORMAT: &str = "x-greptime-format";
42    pub const GREPTIME_DB_HEADER_TIMEOUT: &str = "x-greptime-timeout";
43    pub const GREPTIME_DB_HEADER_EXECUTION_TIME: &str = "x-greptime-execution-time";
44    pub const GREPTIME_DB_HEADER_METRICS: &str = "x-greptime-metrics";
45    pub const GREPTIME_DB_HEADER_NAME: &str = "x-greptime-db-name";
46    pub const GREPTIME_DB_HEADER_READ_PREFERENCE: &str = "x-greptime-read-preference";
47    pub const GREPTIME_TIMEZONE_HEADER_NAME: &str = "x-greptime-timezone";
48    pub const GREPTIME_DB_HEADER_ERROR_CODE: &str = common_error::GREPTIME_DB_HEADER_ERROR_CODE;
49
50    // Deprecated: pipeline is also used with trace, so we remove log from it.
51    pub const GREPTIME_LOG_PIPELINE_NAME_HEADER_NAME: &str = "x-greptime-log-pipeline-name";
52    pub const GREPTIME_LOG_PIPELINE_VERSION_HEADER_NAME: &str = "x-greptime-log-pipeline-version";
53
54    // More generic pipeline header name
55    pub const GREPTIME_PIPELINE_NAME_HEADER_NAME: &str = "x-greptime-pipeline-name";
56    pub const GREPTIME_PIPELINE_VERSION_HEADER_NAME: &str = "x-greptime-pipeline-version";
57
58    pub const GREPTIME_LOG_TABLE_NAME_HEADER_NAME: &str = "x-greptime-log-table-name";
59    pub const GREPTIME_LOG_EXTRACT_KEYS_HEADER_NAME: &str = "x-greptime-log-extract-keys";
60    pub const GREPTIME_TRACE_TABLE_NAME_HEADER_NAME: &str = "x-greptime-trace-table-name";
61
62    // OTLP headers
63    pub const GREPTIME_OTLP_METRIC_PROMOTE_ALL_RESOURCE_ATTRS_HEADER_NAME: &str =
64        "x-greptime-otlp-metric-promote-all-resource-attrs";
65    pub const GREPTIME_OTLP_METRIC_PROMOTE_RESOURCE_ATTRS_HEADER_NAME: &str =
66        "x-greptime-otlp-metric-promote-resource-attrs";
67    pub const GREPTIME_OTLP_METRIC_IGNORE_RESOURCE_ATTRS_HEADER_NAME: &str =
68        "x-greptime-otlp-metric-ignore-resource-attrs";
69    pub const GREPTIME_OTLP_METRIC_PROMOTE_SCOPE_ATTRS_HEADER_NAME: &str =
70        "x-greptime-otlp-metric-promote-scope-attrs";
71    pub const GREPTIME_OTLP_METRIC_TRANSLATION_STRATEGY_HEADER_NAME: &str =
72        "x-greptime-otlp-metric-translation-strategy";
73
74    /// The header key that contains the pipeline params.
75    pub const GREPTIME_PIPELINE_PARAMS_HEADER: &str = "x-greptime-pipeline-params";
76}
77
78pub static GREPTIME_DB_HEADER_FORMAT: HeaderName =
79    HeaderName::from_static(constants::GREPTIME_DB_HEADER_FORMAT);
80pub static GREPTIME_DB_HEADER_EXECUTION_TIME: HeaderName =
81    HeaderName::from_static(constants::GREPTIME_DB_HEADER_EXECUTION_TIME);
82pub static GREPTIME_DB_HEADER_METRICS: HeaderName =
83    HeaderName::from_static(constants::GREPTIME_DB_HEADER_METRICS);
84
85/// Header key of `db-name`. Example format of the header value is `greptime-public`.
86pub static GREPTIME_DB_HEADER_NAME: HeaderName =
87    HeaderName::from_static(constants::GREPTIME_DB_HEADER_NAME);
88
89/// Header key of query specific timezone. Example format of the header value is `Asia/Shanghai` or `+08:00`.
90pub static GREPTIME_TIMEZONE_HEADER_NAME: HeaderName =
91    HeaderName::from_static(constants::GREPTIME_TIMEZONE_HEADER_NAME);
92
93/// Header key of query specific read preference. Example format of the header value is `leader`.
94pub static GREPTIME_DB_HEADER_READ_PREFERENCE: HeaderName =
95    HeaderName::from_static(constants::GREPTIME_DB_HEADER_READ_PREFERENCE);
96
97pub static CONTENT_TYPE_PROTOBUF_STR: &str = "application/x-protobuf";
98pub static CONTENT_TYPE_PROTOBUF: HeaderValue = HeaderValue::from_static(CONTENT_TYPE_PROTOBUF_STR);
99pub static CONTENT_ENCODING_SNAPPY: HeaderValue = HeaderValue::from_static("snappy");
100
101pub static CONTENT_TYPE_NDJSON_STR: &str = "application/x-ndjson";
102pub static CONTENT_TYPE_NDJSON_SUBTYPE_STR: &str = "x-ndjson";
103
104pub struct GreptimeDbName(Option<String>);
105
106impl Header for GreptimeDbName {
107    fn name() -> &'static HeaderName {
108        &GREPTIME_DB_HEADER_NAME
109    }
110
111    fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
112    where
113        Self: Sized,
114        I: Iterator<Item = &'i HeaderValue>,
115    {
116        if let Some(value) = values.next() {
117            let str_value = value.to_str().map_err(|_| headers::Error::invalid())?;
118            Ok(Self(Some(str_value.to_owned())))
119        } else {
120            Ok(Self(None))
121        }
122    }
123
124    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
125        if let Some(name) = &self.0
126            && let Ok(value) = HeaderValue::from_str(name)
127        {
128            values.extend(std::iter::once(value));
129        }
130    }
131}
132
133impl GreptimeDbName {
134    pub fn value(&self) -> Option<&String> {
135        self.0.as_ref()
136    }
137}
138
139// collect write
140pub fn write_cost_header_map(cost: usize) -> HeaderMap {
141    let mut header_map = HeaderMap::new();
142    if cost > 0 {
143        let mut map: HashMap<String, Value> = HashMap::new();
144        map.insert(
145            common_plugins::GREPTIME_EXEC_WRITE_COST.to_string(),
146            Value::from(cost),
147        );
148        let _ = serde_json::to_string(&map)
149            .ok()
150            .and_then(|s| HeaderValue::from_str(&s).ok())
151            .and_then(|v| header_map.insert(&GREPTIME_DB_HEADER_METRICS, v));
152    }
153    header_map
154}
155
156fn collect_into_maps(name: &str, value: u64, maps: &mut [&mut HashMap<String, u64>]) {
157    if name.starts_with(GREPTIME_EXEC_PREFIX) && value > 0 {
158        maps.iter_mut().for_each(|map| {
159            map.entry(name.to_string())
160                .and_modify(|v| *v += value)
161                .or_insert(value);
162        });
163    }
164}
165
166pub fn collect_plan_metrics(plan: &Arc<dyn ExecutionPlan>, maps: &mut [&mut HashMap<String, u64>]) {
167    if let Some(m) = plan.metrics() {
168        m.iter().for_each(|m| match m.value() {
169            MetricValue::Count { name, count } => {
170                collect_into_maps(name, count.value() as u64, maps);
171            }
172            MetricValue::Gauge { name, gauge } => {
173                collect_into_maps(name, gauge.value() as u64, maps);
174            }
175            MetricValue::Time { name, time } if name.starts_with(GREPTIME_EXEC_PREFIX) => {
176                // override
177                maps.iter_mut().for_each(|map| {
178                    map.insert(name.to_string(), time.value() as u64);
179                });
180            }
181            _ => {}
182        });
183    }
184
185    for c in plan.children() {
186        collect_plan_metrics(c, maps);
187    }
188}