servers/metrics_handler.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 prometheus::{Encoder, TextEncoder};
16
17/// a server that serves metrics
18/// only start when datanode starts in distributed mode
19#[derive(Copy, Clone)]
20pub struct MetricsHandler;
21
22impl MetricsHandler {
23 pub fn render(&self) -> String {
24 let mut buffer = Vec::new();
25 let encoder = TextEncoder::new();
26 // Gather the metrics.
27 let metric_families = prometheus::gather();
28 // Encode them to send.
29 match encoder.encode(&metric_families, &mut buffer) {
30 Ok(_) => match String::from_utf8(buffer) {
31 Ok(s) => s,
32 Err(e) => e.to_string(),
33 },
34 Err(e) => e.to_string(),
35 }
36 }
37}