]> git.proxmox.com Git - ceph.git/blob - ceph/src/exporter/util.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / exporter / util.cc
1 #include "util.h"
2
3 #include <boost/algorithm/string/classification.hpp>
4 #include <boost/algorithm/string/replace.hpp>
5 #include <cctype>
6 #include <chrono>
7 #include <fstream>
8 #include <iostream>
9 #include <sstream>
10
11 #include "common/debug.h"
12
13 #define dout_context g_ceph_context
14 #define dout_subsys ceph_subsys_ceph_exporter
15
16 BlockTimer::BlockTimer(std::string file, std::string function)
17 : file(file), function(function), stopped(false) {
18 t1 = std::chrono::high_resolution_clock::now();
19 }
20 BlockTimer::~BlockTimer() {
21 dout(20) << file << ":" << function << ": " << ms.count() << "ms" << dendl;
22 }
23
24 // useful with stop
25 double BlockTimer::get_ms() {
26 return ms.count();
27 }
28
29 // Manually stop the timer as you might want to get the time
30 void BlockTimer::stop() {
31 if (!stopped) {
32 stopped = true;
33 t2 = std::chrono::high_resolution_clock::now();
34 ms = t2 - t1;
35 }
36 }
37
38 bool string_is_digit(std::string s) {
39 size_t i = 0;
40 while (std::isdigit(s[i]) && i < s.size()) {
41 i++;
42 }
43 return i >= s.size();
44 }
45
46 std::string read_file_to_string(std::string path) {
47 std::ifstream is(path);
48 std::stringstream buffer;
49 buffer << is.rdbuf();
50 return buffer.str();
51 }
52
53 // Must be kept in sync with promethize() in src/pybind/mgr/prometheus/module.py
54 void promethize(std::string &name) {
55 if (name[name.size() - 1] == '-') {
56 name[name.size() - 1] = '_';
57 name += "minus";
58 }
59
60 auto should_be_underscore = [](char ch) {
61 return ch == '.' || ch == '/' || ch == ' ' || ch == '-';
62 };
63 std::replace_if(name.begin(), name.end(), should_be_underscore, '_');
64
65 boost::replace_all(name, "::", "_");
66 boost::replace_all(name, "+", "_plus");
67
68 name = "ceph_" + name;
69 }