8000 simplify metrics creation code (#19967) · SwathyPraveen/arangodb@79e0fc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79e0fc0

Browse files
jsteemannMBkkt
andauthored
simplify metrics creation code (arangodb#19967)
Co-authored-by: Valery Mironov <32071355+MBkkt@users.noreply.github.com>
1 parent ed56cfa commit 79e0fc0

File tree

14 files changed

+153
-145
lines changed

14 files changed

+153
-145
lines changed

arangod/Agency/Node.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
#include "Agency/PathComponent.h"
2828
#include "AgencyStrings.h"
29-
#include "Basics/StringUtils.h"
3029
#include "Logger/LogMacros.h"
3130
#include "Logger/Logger.h"
3231
#include "Logger/LoggerStream.h"
3332
#include "Metrics/GaugeBuilder.h"
33+
#include "Metrics/Metric.h"
34+
35+
#include <absl/strings/str_cat.h>
3436

3537
#include <velocypack/Compare.h>
3638
#include <velocypack/Iterator.h>
@@ -999,12 +1001,14 @@ constexpr std::string_view nodeMetricsHelpText =
9991001

10001002
DECLARE_GAUGE(arangodb_agency_node_memory_usage, uint64_t, nodeMetricsHelpText);
10011003

1002-
void Node::toPrometheus(std::string& result) {
1004+
void Node::toPrometheus(std::string& result, std::string_view globals,
1005+
bool ensureWhitespace) {
10031006
auto name = arangodb_agency_node_memory_usage{}.name();
10041007
auto nodeMemoryUsage = consensus::Node::getMemoryUsage();
1005-
result += basics::StringUtils::concatT(
1006-
"# HELP ", name, " ", nodeMetricsHelpText, "\n# TYPE ", name, " gauge\n",
1007-
name, " ", nodeMemoryUsage, "\n");
1008+
1009+
metrics::Metric::addInfo(result, name, nodeMetricsHelpText, "gauge");
1010+
metrics::Metric::addMark(result, name, globals, "");
1011+
absl::StrAppend(&result, ensureWhitespace ? " " : "", nodeMemoryUsage, "\n");
10081012
}
10091013

10101014
template<typename... Args>

arangod/Agency/Node.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
#include <chrono>
3131
#include <cstdint>
3232
#include <cstring>
33+
#include <span>
3334
#include <string>
3435
#include <string_view>
35-
#include <span>
3636
#include <vector>
3737

38-
#include <velocypack/String.h>
3938
#include <velocypack/Slice.h>
39+
#include <velocypack/String.h>
4040

4141
#if (_MSC_VER >= 1)
4242
// suppress warnings:
@@ -149,7 +149,8 @@ class Node : public std::enable_shared_from_this<Node> {
149149
using allocator_type = AccountingAllocator<Node>;
150150

151151
static std::size_t getMemoryUsage() noexcept { return memoryUsage; }
152-
static void toPrometheus(std::string&);
152+
static void toPrometheus(std::string& result, std::string_view globals,
153+
bool ensureWhitespace);
153154

154155
// If you want those types to be accounted for as well, please do so.
155156
// But be aware that this will change the type and the interface and you have

arangod/Cluster/ClusterInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
#include "VocBase/VocbaseInfo.h"
9090
#include "VocBase/Methods/Indexes.h"
9191

92+
#include <absl/strings/str_cat.h>
93+
9294
#include <velocypack/Builder.h>
9395
#include <velocypack/Collection.h>
9496
#include <velocypack/Iterator.h>

arangod/Metrics/Batch.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "Metrics/IBatch.h"
2828
#include "Metrics/Metric.h"
2929

30+
#include <absl/strings/str_cat.h>
3031
#include <velocypack/Builder.h>
3132
#include <velocypack/Value.h>
3233
#include <vector>
@@ -51,10 +52,8 @@ class Batch final : public IBatch {
5152
Metric::addInfo(result, T::kName[i], T::kHelp[i], T::kType[i]);
5253
for (size_t j = 0; auto& [labels, _] : _metrics) {
5354
Metric::addMark(result, T::kName[i], globals, labels);
54-
if (ensureWhitespace) {
55-
result.push_back(' ');
56-
}
57-
result.append(T::kToString[i](metrics[j++])) += '\n';
55+
absl::StrAppend(&result, ensureWhitespace ? " " : "",
56+
T::kToString[i](metrics[j++]), "\n");
5857
}
5958
}
6059
}

arangod/Metrics/Counter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "Metrics/Counter.h"
2525

26+
#include <absl/strings/str_cat.h>
2627
#include <ostream>
2728

2829
namespace arangodb::metrics {
@@ -39,10 +40,7 @@ void Counter::toPrometheus(std::string& result, std::string_view globals,
3940
bool ensureWhitespace) const {
4041
_b.push();
4142
Metric::addMark(result, name(), globals, labels());
42-
if (ensureWhitespace) {
43-
result.push_back(' ');
44-
}
45-
result.append(std::to_string(load())) += '\n';
43+
absl::StrAppend(&result, ensureWhitespace ? " " : "", load(), "\n");
4644
}
4745

4846
uint64_t Counter::load() const noexcept {

arangod/Metrics/Gauge.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Basics/debugging.h"
2626
#include "Metrics/Metric.h"
2727

28+
#include <absl/strings/str_cat.h>
2829
#include <velocypack/Value.h>
2930
#include <velocypack/Builder.h>
3031

@@ -46,10 +47,16 @@ class Gauge : public Metric {
4647
void toPrometheus(std::string& result, std::string_view globals,
4748
bool ensureWhitespace) const final {
4849
Metric::addMark(result, name(), globals, labels());
49-
if (ensureWhitespace) {
50-
result.push_back(' ');
50+
if constexpr (std::is_integral_v<T>) {
51+
absl::StrAppend(&result, ensureWhitespace ? " " : "", load(), "\n");
52+
} else {
53+
// must use std::to_string() here because it produces a different
54+
// string representation of large floating-point numbers than absl
55+
// does. absl uses scientific notation for numbers that exceed 6
56+
// digits, and std::to_string() doesn't.
57+
absl::StrAppend(&result, ensureWhitespace ? " " : "",
58+
std::to_string(load()), "\n");
5159
}
52-
result.append(std::to_string(load())) += '\n';
5360
}
5461

5562
void toVPack(velocypack::Builder& builder, ArangodServer&) const final {

arangod/Metrics/Metric.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,23 @@
2323

2424
#include "Metrics/Metric.h"
2525

26+
#include <absl/strings/str_cat.h>
27+
2628
#include <ostream>
2729

2830
namespace arangodb::metrics {
2931

3032
void Metric::addInfo(std::string& result, std::string_view name,
3133
std::string_view help, std::string_view type) {
32-
(result.append("# HELP ").append(name) += ' ').append(help) += '\n';
33-
(result.append("# TYPE ").append(name) += ' ').append(type) += '\n';
34+
absl::StrAppend(&result, "# HELP ", name, " ", help, "\n", "# TYPE ", name,
35+
" ", type, "\n");
3436
}
3537

3638
void Metric::addMark(std::string& result, std::string_view name,
3739
std::string_view globals, std::string_view labels) {
38-
(result.append(name) += '{').append(globals);
39-
if (!labels.empty()) {
40-
if (!globals.empty()) {
41-
result += ',';
42-
}
43-
result += labels;
44-
}
45-
result += '}';
40+
absl::StrAppend(&result, name, "{", globals,
41+
((labels.empty() || globals.empty()) ? "" : ","), labels,
42+
"}");
4643
}
4744

4845
Metric::Metric(std::string_view name, std::string_view help,

arangod/Metrics/MetricsFeature.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ std::shared_ptr<Metric> MetricsFeature::doAdd(Builder& builder) {
9292
MetricKeyView key{metric->name(), metric->labels()};
9393
std::lock_guard lock{_mutex};
9494
if (!_registry.try_emplace(key, metric).second) {
95-
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL,
96-
std::string{builder.type()} + " " +
97-
std::string{builder.name()} +
98-
" already exists");
95+
THROW_ARANGO_EXCEPTION_MESSAGE(
96+
TRI_ERROR_INTERNAL,
97+
absl::StrCat(builder.type(), " ", builder.name(), " already exists"));
9998
}
10099
return metric;
101100
}
@@ -158,16 +157,16 @@ void MetricsFeature::toPrometheus(std::string& result, CollectMode mode) const {
158157
auto& sf = server().getFeature<StatisticsFeature>();
159158
auto time = std::chrono::duration<double, std::milli>(
160159
std::chrono::system_clock::now().time_since_epoch());
161-
sf.toPrometheus(result, time.count(), _ensureWhitespace);
160+
sf.toPrometheus(result, time.count(), _globals, _ensureWhitespace);
162161
auto& es = server().getFeature<EngineSelectorFeature>().engine();
163162
if (es.typeName() == RocksDBEngine::kEngineName) {
164-
es.getStatistics(result);
163+
es.toPrometheus(result, _globals, _ensureWhitespace);
165164
}
166165
auto& cm = server().getFeature<ClusterMetricsFeature>();
167166
if (hasGlobals && cm.isEnabled() && mode != CollectMode::Local) {
168167
cm.toPrometheus(result, _globals, _ensureWhitespace);
169168
}
170-
consensus::Node::toPrometheus(result);
169+
consensus::Node::toPrometheus(result, _globals, _ensureWhitespace);
171170
}
172171

173172
////////////////////////////////////////////////////////////////////////////////
@@ -224,15 +223,15 @@ std::shared_lock<std::shared_mutex> MetricsFeature::initGlobalLabels() const {
224223
// isn't yet known. This check here is to prevent that the label is
225224
// permanently empty if metrics are requested too early.
226225
if (auto shortname = instance->getShortName(); !shortname.empty()) {
227-
auto label = "shortname=\"" + shortname + "\"";
228-
_globals = label + (_globals.empty() ? "" : "," + _globals);
226+
_globals = absl::StrCat("shortname=\"", shortname, "\"",
227+
(_globals.empty() ? "" : ","), _globals);
229228
hasShortname = true;
230229
}
231230
}
232231
if (!hasRole) {
233232
if (auto role = instance->getRole(); role != ServerState::ROLE_UNDEFINED) {
234-
auto label = "role=\"" + ServerState::roleToString(role) + "\"";
235-
_globals += (_globals.empty() ? "" : ",") + label;
233+
absl::StrAppend(&_globals, (_globals.empty() ? "" : ","), "role=\"",
234+
ServerState::roleToString(role), "\"");
236235
hasRole = true;
237236
}
238237
}

arangod/RocksDBEngine/RocksDBEngine.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
#include "Logger/LogMacros.h"
5252
#include "Logger/Logger.h"
5353
#include "Logger/LoggerStream.h"
54+
#include "Metrics/CounterBuilder.h"
55+
#include "Metrics/GaugeBuilder.h"
56+
#include "Metrics/HistogramBuilder.h"
57+
#include "Metrics/Metric.h"
58+
#include "Metrics/MetricsFeature.h"
5459
#include "ProgramOptions/ProgramOptions.h"
5560
#include "ProgramOptions/Section.h"
5661
#include "Replication/ReplicationClients.h"
@@ -61,10 +66,6 @@
6166
#include "RestServer/DatabaseFeature.h"
6267
#include "RestServer/DatabasePathFeature.h"
6368
#include "RestServer/FlushFeature.h"
64-
#include "Metrics/CounterBuilder.h"
65-
#include "Metrics/GaugeBuilder.h"
66-
#include "Metrics/HistogramBuilder.h"
67-
#include "Metrics/MetricsFeature.h"
6869
#include "RestServer/DumpLimitsFeature.h"
6970
#include "RestServer/LanguageCheckFeature.h"
7071
#include "RestServer/ServerIdFeature.h"
@@ -3428,7 +3429,8 @@ void RocksDBEngine::getCapabilities(velocypack::Builder& builder) const {
34283429
VPackCollection::merge(builder, main.slice(), own.slice(), false);
34293430
}
34303431

3431-
void RocksDBEngine::getStatistics(std::string& result) const {
3432+
void RocksDBEngine::toPrometheus(std::string& result, std::string_view globals,
3433+
bool ensureWhitespace) const {
34323434
VPackBuffer<uint8_t> buffer;
34333435
VPackBuilder stats(buffer);
34343436
getStatistics(stats);
@@ -3444,17 +3446,12 @@ void RocksDBEngine::getStatistics(std::string& result) const {
34443446
// prepend name wit 10000 h "rocksdb_"
34453447
name = absl::StrCat(kEngineName, "_", name);
34463448
}
3447-
if (name.ends_with("_total")) {
3448-
// counter
3449-
result += absl::StrCat("\n# HELP ", name, " ", name, "\n# TYPE ", name,
3450-
" counter\n", name, " ",
3451-
a.value.getNumber<uint64_t>(), "\n");
3452-
} else {
3453-
// gauge
3454-
result += absl::StrCat("\n# HELP ", name, " ", name, "\n# TYPE ", name,
3455-
" gauge\n", name, " ",
3456-
a.value.getNumber<uint64_t>(), "\n");
3457-
}
3449+
3450+
metrics::Metric::addInfo(result, name, /*help*/ name,
3451+
name.ends_with("_total") ? "counter" : "gauge");
3452+
metrics::Metric::addMark(result, name, globals, "");
3453+
absl::StrAppend(&result, ensureWhitespace ? " " : "",
3454+
a.value.getNumber<uint64_t>(), "\n");
34583455
}
34593456
}
34603457
}

arangod/RocksDBEngine/RocksDBEngine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ class RocksDBEngine final : public StorageEngine {
192192

193193
void getCapabilities(velocypack::Builder& builder) const override;
194194
void getStatistics(velocypack::Builder& builder) const override;
195-
void getStatistics(std::string& result) const override;
195+
void toPrometheus(std::string& result, std::string_view globals,
196+
bool ensureWhitespace) const override;
196197

197198
// inventory functionality
198199
// -----------------------

0 commit comments

Comments
 (0)
0