diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/exporter/util.cc | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/exporter/util.cc')
-rw-r--r-- | src/exporter/util.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/exporter/util.cc b/src/exporter/util.cc new file mode 100644 index 000000000..06a8338b8 --- /dev/null +++ b/src/exporter/util.cc @@ -0,0 +1,69 @@ +#include "util.h" + +#include <boost/algorithm/string/classification.hpp> +#include <boost/algorithm/string/replace.hpp> +#include <cctype> +#include <chrono> +#include <fstream> +#include <iostream> +#include <sstream> + +#include "common/debug.h" + +#define dout_context g_ceph_context +#define dout_subsys ceph_subsys_ceph_exporter + +BlockTimer::BlockTimer(std::string file, std::string function) + : file(file), function(function), stopped(false) { + t1 = std::chrono::high_resolution_clock::now(); +} +BlockTimer::~BlockTimer() { + dout(20) << file << ":" << function << ": " << ms.count() << "ms" << dendl; +} + +// useful with stop +double BlockTimer::get_ms() { + return ms.count(); +} + +// Manually stop the timer as you might want to get the time +void BlockTimer::stop() { + if (!stopped) { + stopped = true; + t2 = std::chrono::high_resolution_clock::now(); + ms = t2 - t1; + } +} + +bool string_is_digit(std::string s) { + size_t i = 0; + while (std::isdigit(s[i]) && i < s.size()) { + i++; + } + return i >= s.size(); +} + +std::string read_file_to_string(std::string path) { + std::ifstream is(path); + std::stringstream buffer; + buffer << is.rdbuf(); + return buffer.str(); +} + +// Must be kept in sync with promethize() in src/pybind/mgr/prometheus/module.py +void promethize(std::string &name) { + if (name[name.size() - 1] == '-') { + name[name.size() - 1] = '_'; + name += "minus"; + } + + auto should_be_underscore = [](char ch) { + return ch == '.' || ch == '/' || ch == ' ' || ch == '-'; + }; + std::replace_if(name.begin(), name.end(), should_be_underscore, '_'); + + boost::replace_all(name, "::", "_"); + boost::replace_all(name, "+", "_plus"); + + name = "ceph_" + name; +} |