Skip to main content

object_store/
layers.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
15#[cfg(feature = "hdfs-object-store")]
16mod hdfs;
17#[cfg(feature = "testing")]
18pub mod mock;
19
20#[cfg(feature = "hdfs-object-store")]
21pub use hdfs::HdfsCompatibilityLayer;
22pub use opendal::layers::*;
23pub use prometheus::build_prometheus_metrics_layer;
24
25mod prometheus {
26    use std::sync::{Mutex, OnceLock};
27
28    use opendal::layers::PrometheusLayer;
29
30    static PROMETHEUS_LAYER: OnceLock<Mutex<PrometheusLayer>> = OnceLock::new();
31
32    /// This logical tries to extract parent path from the object storage operation
33    /// the function also relies on assumption that the region path is built from
34    /// pattern `<data|index>/catalog/schema/table_id/...` OR `greptimedb/object_cache/<read|write>/...`
35    ///
36    /// We'll get the data/catalog/schema from path.
37    pub fn build_prometheus_metrics_layer(_with_path_label: bool) -> PrometheusLayer {
38        PROMETHEUS_LAYER
39            .get_or_init(|| {
40                // let path_level = if with_path_label { 3 } else { 0 };
41
42                // opendal doesn't support dynamic path label trim
43                // we have uuid in index path, which causes the label size to explode
44                // remove path label first, waiting for later fix
45                // TODO(shuiyisong): add dynamic path label trim for opendal
46
47                let layer = PrometheusLayer::builder().register_default().unwrap();
48                Mutex::new(layer)
49            })
50            .lock()
51            .unwrap()
52            .clone()
53    }
54}