Skip to content

Commit

Permalink
Reduce memory allocation in StdCout backend (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Nov 30, 2018
1 parent 231c775 commit 01f78cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/Monitoring/Metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Metric
/// \return metric name
std::string getName() const;

/// Const name getter
const std::string& getConstName() const;

/// Timestamp getter
/// \return metric timestamp
std::chrono::time_point<std::chrono::system_clock> getTimestamp() const;
Expand All @@ -86,6 +89,9 @@ class Metric
/// return timestamp as std::chrono::system_clock
static auto getCurrentTimestamp() -> decltype(std::chrono::system_clock::now());

/// Tagset vector size getter
std::size_t tagSize() const;

protected:
/// Metric value
boost::variant< int, std::string, double, uint64_t > mValue;
Expand Down
26 changes: 14 additions & 12 deletions src/Backends/StdOut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,29 @@ void StdOut::sendMultiple(std::string measurement, std::vector<Metric>&& metrics
if (!metricTags.empty()) {
metricTags = "," + metricTags;
}
mStream << "[METRIC] " << measurement << "/" << metric.getName() << "," << metric.getType() << " "
mStream << "[METRIC] " << measurement << "/" << metric.getConstName() << "," << metric.getType() << " "
<< metric.getValue() << " " << convertTimestamp(metric.getTimestamp()) << " " << tagString
<< metricTags << "\n";
}
}

void StdOut::send(const Metric& metric)
{
std::string metricTags{};
for (const auto& tag : metric.getTags()) {
if (!metricTags.empty()) {
metricTags += ",";
if (metric.tagSize() == 0) {
mStream << "[METRIC] " << metric.getConstName() << "," << metric.getType() << " " << metric.getValue()
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << "\n";
} else {
std::string metricTags{};
for (const auto& tag : metric.getTags()) {
metricTags += "," + tag.name + "=" + tag.value;
}
metricTags += tag.name + "=" + tag.value;
}
if (!metricTags.empty()) {
metricTags = "," + metricTags;
if (tagString.empty()) {
metricTags.erase(0, 1);
}

mStream << "[METRIC] " << metric.getConstName() << "," << metric.getType() << " " << metric.getValue()
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << metricTags << "\n";
}
mStream << "[METRIC] " << metric.getName() << "," << metric.getType() << " " << metric.getValue()
<< " " << convertTimestamp(metric.getTimestamp()) << " " << tagString << metricTags
<< "\n";
}

} // namespace backends
Expand Down
10 changes: 10 additions & 0 deletions src/Metric.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ std::string Metric::getName() const
return mName;
}

const std::string& Metric::getConstName() const
{
return mName;
}

std::size_t Metric::tagSize() const
{
return tagSet.size();
}

Metric::Metric(int value, const std::string& name, std::chrono::time_point<std::chrono::system_clock> timestamp) :
mValue(value), mName(name), mTimestamp(timestamp)
{}
Expand Down

0 comments on commit 01f78cf

Please sign in to comment.