Skip to content

Commit 7087e76

Browse files
committed
exporter: promethize counter names
Signed-off-by: Pere Diaz Bou <[email protected]> Fixes: https://tracker.ceph.com/issues/59475
1 parent 7e93b03 commit 7087e76

File tree

7 files changed

+712
-12
lines changed

7 files changed

+712
-12
lines changed

src/exporter/DaemonMetricCollector.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ std::string boost_string_to_std(boost::json::string js) {
8080

8181
std::string quote(std::string value) { return "\"" + value + "\""; }
8282

83-
bool is_hyphen(char ch) { return ch == '-'; }
84-
8583
void DaemonMetricCollector::dump_asok_metrics() {
8684
BlockTimer timer(__FILE__, __FUNCTION__);
8785

@@ -120,12 +118,14 @@ void DaemonMetricCollector::dump_asok_metrics() {
120118
json_object counter_dump = boost::json::parse(counter_dump_response).as_object();
121119
json_object counter_schema = boost::json::parse(counter_schema_response).as_object();
122120

123-
for (auto &labeled_perf : counter_schema) {
124-
std::string labeled_perf_group = {labeled_perf.key().begin(), labeled_perf.key().end()};
125-
json_object labeled_perf_group_object = labeled_perf.value().as_object();
126-
auto counters = labeled_perf_group_object["counters"].as_object();
127-
auto counters_labels = labeled_perf_group_object["labels"].as_object();
128-
auto labeled_perf_group_counters = counter_dump[labeled_perf_group].as_object()["counters"].as_object();
121+
for (auto &perf_group_item : counter_schema) {
122+
std::string perf_group = {perf_group_item.key().begin(),
123+
perf_group_item.key().end()};
124+
json_object perf_group_object = perf_group_item.value().as_object();
125+
auto counters = perf_group_object["counters"].as_object();
126+
auto counters_labels = perf_group_object["labels"].as_object();
127+
auto counters_values =
128+
counter_dump[perf_group].as_object()["counters"].as_object();
129129
labels_t labels;
130130

131131
for(auto &label: counters_labels) {
@@ -138,8 +138,8 @@ void DaemonMetricCollector::dump_asok_metrics() {
138138
continue;
139139
}
140140
std::string counter_name_init = {counter.key().begin(), counter.key().end()};
141-
std::string counter_name = "ceph_" + labeled_perf_group + "_" + counter_name_init;
142-
std::replace_if(counter_name.begin(), counter_name.end(), is_hyphen, '_');
141+
std::string counter_name = perf_group + "_" + counter_name_init;
142+
promethize(counter_name);
143143

144144
if (counters_labels.empty()) {
145145
auto labels_and_name = get_labels_and_metric_name(daemon_name, counter_name);
@@ -153,7 +153,7 @@ void DaemonMetricCollector::dump_asok_metrics() {
153153
counter_name = multisite_labels_and_name.second;
154154
}
155155
labels.insert({"ceph_daemon", quote(daemon_name)});
156-
auto perf_values = labeled_perf_group_counters.at(counter_name_init);
156+
auto perf_values = counters_values.at(counter_name_init);
157157
dump_asok_metric(counter_group, perf_values, counter_name, labels);
158158
}
159159
}

src/exporter/util.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include "util.h"
2-
#include "common/debug.h"
2+
33
#include <boost/algorithm/string/classification.hpp>
4+
#include <boost/algorithm/string/replace.hpp>
45
#include <cctype>
56
#include <chrono>
67
#include <fstream>
78
#include <iostream>
89
#include <sstream>
910

11+
#include "common/debug.h"
12+
1013
#define dout_context g_ceph_context
1114
#define dout_subsys ceph_subsys_ceph_exporter
1215

@@ -46,3 +49,21 @@ std::string read_file_to_string(std::string path) {
4649
buffer << is.rdbuf();
4750
return buffer.str();
4851
}
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+
}

src/exporter/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ class BlockTimer {
2020
bool string_is_digit(std::string s);
2121
std::string read_file_to_string(std::string path);
2222
std::string get_hostname(std::string path);
23+
24+
void promethize(std::string &name);

src/pybind/mgr/prometheus/module.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ def set(self, value: Number, labelvalues: Optional[LabelValues] = None) -> None:
327327

328328
def str_expfmt(self) -> str:
329329

330+
# Must be kept in sync with promethize() in src/exporter/util.cc
330331
def promethize(path: str) -> str:
331332
''' replace illegal metric name characters '''
332333
result = re.sub(r'[./\s]|::', '_', path).replace('+', '_plus')

src/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ if(NOT WIN32)
7373
add_subdirectory(osd)
7474
add_subdirectory(osdc)
7575
add_subdirectory(immutable_object_cache)
76+
add_subdirectory(exporter)
7677
endif(NOT WIN32)
7778

7879
if(WITH_RADOSGW)

src/test/exporter/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_executable(unittest_exporter
2+
test_exporter.cc
3+
"${CMAKE_SOURCE_DIR}/src/exporter/util.cc"
4+
)
5+
6+
target_link_libraries(unittest_exporter
7+
global
8+
${UNITTEST_LIBS}
9+
)
10+
add_ceph_unittest(unittest_exporter)
11+

0 commit comments

Comments
 (0)