Skip to content

Commit 01f78cf

Browse files
authored
Reduce memory allocation in StdCout backend (#96)
1 parent 231c775 commit 01f78cf

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

include/Monitoring/Metric.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class Metric
6161
/// \return metric name
6262
std::string getName() const;
6363

64+
/// Const name getter
65+
const std::string& getConstName() const;
66+
6467
/// Timestamp getter
6568
/// \return metric timestamp
6669
std::chrono::time_point<std::chrono::system_clock> getTimestamp() const;
@@ -86,6 +89,9 @@ class Metric
8689
/// return timestamp as std::chrono::system_clock
8790
static auto getCurrentTimestamp() -> decltype(std::chrono::system_clock::now());
8891

92+
/// Tagset vector size getter
93+
std::size_t tagSize() const;
94+
8995
protected:
9096
/// Metric value
9197
boost::variant< int, std::string, double, uint64_t > mValue;

src/Backends/StdOut.cxx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,29 @@ void StdOut::sendMultiple(std::string measurement, std::vector<Metric>&& metrics
5656
if (!metricTags.empty()) {
5757
metricTags = "," + metricTags;
5858
}
59-
mStream << "[METRIC] " << measurement << "/" << metric.getName() << "," << metric.getType() << " "
59+
mStream << "[METRIC] " << measurement << "/" << metric.getConstName() << "," << metric.getType() << " "
6060
<< metric.getValue() << " " << convertTimestamp(metric.getTimestamp()) << " " << tagString
6161
<< metricTags << "\n";
6262
}
6363
}
6464

6565
void StdOut::send(const Metric& metric)
6666
{
67-
std::string metricTags{};
68-
for (const auto& tag : metric.getTags()) {
69-
if (!metricTags.empty()) {
70-
metricTags += ",";
67+
if (metric.tagSize() == 0) {
68+
mStream << "[METRIC] " << metric.getConstName() << "," << metric.getType() << " " << metric.getValue()
69+
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << "\n";
70+
} else {
71+
std::string metricTags{};
72+
for (const auto& tag : metric.getTags()) {
73+
metricTags += "," + tag.name + "=" + tag.value;
7174
}
72-
metricTags += tag.name + "=" + tag.value;
73-
}
74-
if (!metricTags.empty()) {
75-
metricTags = "," + metricTags;
75+
if (tagString.empty()) {
76+
metricTags.erase(0, 1);
77+
}
78+
79+
mStream << "[METRIC] " << metric.getConstName() << "," << metric.getType() << " " << metric.getValue()
80+
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << metricTags << "\n";
7681
}
77-
mStream << "[METRIC] " << metric.getName() << "," << metric.getType() << " " << metric.getValue()
78-
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << metricTags
79-
<< "\n";
8082
}
8183

8284
} // namespace backends

src/Metric.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ std::string Metric::getName() const
3030
return mName;
3131
}
3232

33+
const std::string& Metric::getConstName() const
34+
{
35+
return mName;
36+
}
37+
38+
std::size_t Metric::tagSize() const
39+
{
40+
return tagSet.size();
41+
}
42+
3343
Metric::Metric(int value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp) :
3444
mValue(value), mName(name), mTimestamp(timestamp)
3545
{}

0 commit comments

Comments
 (0)