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