diff --git a/include/Monitoring/Metric.h b/include/Monitoring/Metric.h index 7793566ac..853e971e4 100644 --- a/include/Monitoring/Metric.h +++ b/include/Monitoring/Metric.h @@ -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 getTimestamp() const; @@ -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; diff --git a/src/Backends/StdOut.cxx b/src/Backends/StdOut.cxx index 2475aae02..8215b863d 100644 --- a/src/Backends/StdOut.cxx +++ b/src/Backends/StdOut.cxx @@ -56,7 +56,7 @@ void StdOut::sendMultiple(std::string measurement, std::vector&& 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"; } @@ -64,19 +64,21 @@ void StdOut::sendMultiple(std::string measurement, std::vector&& metrics 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 diff --git a/src/Metric.cxx b/src/Metric.cxx index 5d8583d0f..e3c135688 100644 --- a/src/Metric.cxx +++ b/src/Metric.cxx @@ -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 timestamp) : mValue(value), mName(name), mTimestamp(timestamp) {}