servers/request_memory_metrics.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//! Unified metrics adapter for all server protocols.
16
17use common_memory_manager::MemoryMetrics;
18
19use crate::metrics::{REQUEST_MEMORY_IN_USE, REQUEST_MEMORY_LIMIT, REQUEST_MEMORY_REJECTED};
20
21/// Metrics adapter for unified request memory tracking.
22///
23/// This adapter tracks memory usage for all server protocols (HTTP, gRPC, Arrow Flight)
24/// without distinguishing between them. All requests contribute to the same set of metrics.
25#[derive(Clone, Copy, Debug, Default)]
26pub struct RequestMemoryMetrics;
27
28impl MemoryMetrics for RequestMemoryMetrics {
29 fn set_limit(&self, bytes: i64) {
30 REQUEST_MEMORY_LIMIT.set(bytes);
31 }
32
33 fn set_in_use(&self, bytes: i64) {
34 REQUEST_MEMORY_IN_USE.set(bytes);
35 }
36
37 fn inc_rejected(&self, reason: &str) {
38 REQUEST_MEMORY_REJECTED.with_label_values(&[reason]).inc();
39 }
40}