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