From c0525f2ea3536f04ba1f32446af0d77404c1f050 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 15 Jun 2019 15:48:48 +0100 Subject: [PATCH] Move local publishers to their own files --- llarp/CMakeLists.txt | 3 +- llarp/context.cpp | 3 +- llarp/metrics/json_publisher.cpp | 177 ++++++++++++++++++ .../{publishers.hpp => json_publisher.hpp} | 30 +-- .../{publishers.cpp => stream_publisher.cpp} | 165 +--------------- llarp/metrics/stream_publisher.hpp | 30 +++ test/metrics/test_llarp_metrics_publisher.cpp | 2 +- 7 files changed, 219 insertions(+), 191 deletions(-) create mode 100644 llarp/metrics/json_publisher.cpp rename llarp/metrics/{publishers.hpp => json_publisher.hpp} (65%) rename llarp/metrics/{publishers.cpp => stream_publisher.cpp} (53%) create mode 100644 llarp/metrics/stream_publisher.hpp diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 6c713acf5..ac4cfbdb0 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -67,7 +67,8 @@ set(LIB_PLATFORM_SRC ev/ev.cpp ev/pipe.cpp metrics/metrictank_publisher.cpp - metrics/publishers.cpp + metrics/json_publisher.cpp + metrics/stream_publisher.cpp net/net.cpp net/net_addr.cpp net/net_inaddr.cpp diff --git a/llarp/context.cpp b/llarp/context.cpp index ab66b0bfe..640b125c3 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -8,7 +8,8 @@ #include #include #include -#include +#include +#include #include #include #include diff --git a/llarp/metrics/json_publisher.cpp b/llarp/metrics/json_publisher.cpp new file mode 100644 index 000000000..dc1aaa854 --- /dev/null +++ b/llarp/metrics/json_publisher.cpp @@ -0,0 +1,177 @@ +#include + +#include +#include +#include + +namespace llarp +{ + namespace metrics + { + namespace + { + nlohmann::json + tagsToJson(const Tags &tags) + { + nlohmann::json result; + + std::for_each(tags.begin(), tags.end(), [&](const auto &tag) { + absl::visit([&](const auto &t) { result[tag.first] = t; }, + tag.second); + }); + + return result; + } + + template < typename Value > + nlohmann::json + formatValue(const Record< Value > &record, const Tags &tags, + double elapsedTime, Publication::Type publicationType) + { + switch(publicationType) + { + case Publication::Type::Unspecified: + { + assert(false && "Invalid publication type"); + } + break; + case Publication::Type::Total: + { + return {{"tags", tagsToJson(tags)}, {"total", record.total()}}; + } + break; + case Publication::Type::Count: + { + return {{"tags", tagsToJson(tags)}, {"count", record.count()}}; + } + break; + case Publication::Type::Min: + { + return {{"tags", tagsToJson(tags)}, {"min", record.min()}}; + } + break; + case Publication::Type::Max: + { + return {{"tags", tagsToJson(tags)}, {"max", record.max()}}; + } + break; + case Publication::Type::Avg: + { + return {{"tags", tagsToJson(tags)}, + {"avg", record.total() / record.count()}}; + } + break; + case Publication::Type::Rate: + { + return {{"tags", tagsToJson(tags)}, + {"rate", record.total() / elapsedTime}}; + } + break; + case Publication::Type::RateCount: + { + return {{"tags", tagsToJson(tags)}, + {"rateCount", record.count() / elapsedTime}}; + } + break; + } + } + + template < typename Value > + nlohmann::json + recordToJson(const TaggedRecords< Value > &taggedRecord, + double elapsedTime) + { + nlohmann::json result; + result["id"] = taggedRecord.id.toString(); + + auto publicationType = taggedRecord.id.description()->type(); + + for(const auto &rec : taggedRecord.data) + { + const auto &record = rec.second; + if(publicationType != Publication::Type::Unspecified) + { + result["publicationType"] = Publication::repr(publicationType); + + result["metrics"].push_back( + formatValue(record, rec.first, elapsedTime, publicationType)); + } + else + { + nlohmann::json tmp; + tmp["tags"] = tagsToJson(rec.first); + tmp["count"] = record.count(); + tmp["total"] = record.total(); + + if(Record< Value >::DEFAULT_MIN() != record.min()) + { + tmp["min"] = record.min(); + } + if(Record< Value >::DEFAULT_MAX() == record.max()) + { + tmp["max"] = record.max(); + } + + result["metrics"].push_back(tmp); + } + } + + return result; + } + } // namespace + + void + JsonPublisher::publish(const Sample &values) + { + if(values.recordCount() == 0) + { + // nothing to publish + return; + } + + nlohmann::json result; + result["sampleTime"] = absl::UnparseFlag(values.sampleTime()); + result["recordCount"] = values.recordCount(); + auto gIt = values.begin(); + auto prev = values.begin(); + for(; gIt != values.end(); ++gIt) + { + const double elapsedTime = absl::ToDoubleSeconds(samplePeriod(*gIt)); + + if(gIt == prev || samplePeriod(*gIt) != samplePeriod(*prev)) + { + result["elapsedTime"] = elapsedTime; + } + + absl::visit( + [&](const auto &x) -> void { + for(const auto &record : x) + { + result["record"].emplace_back( + recordToJson(record, elapsedTime)); + } + }, + *gIt); + + prev = gIt; + } + + m_publish(result); + } + + void + JsonPublisher::directoryPublisher(const nlohmann::json &result, + const fs::path &path) + { + std::ofstream fstream(path.string(), std::ios_base::app); + if(!fstream) + { + std::cerr << "Skipping metrics publish, " << path << " is not a file\n"; + return; + } + + fstream << std::setw(0) << result << '\n'; + fstream.close(); + } + } // namespace metrics +} // namespace llarp diff --git a/llarp/metrics/publishers.hpp b/llarp/metrics/json_publisher.hpp similarity index 65% rename from llarp/metrics/publishers.hpp rename to llarp/metrics/json_publisher.hpp index fe0b6db2c..d0cc2b06c 100644 --- a/llarp/metrics/publishers.hpp +++ b/llarp/metrics/json_publisher.hpp @@ -1,34 +1,18 @@ -#ifndef LLARP_METRICS_PUBLISHERS_HPP -#define LLARP_METRICS_PUBLISHERS_HPP - -#include +#ifndef LLARP_METRICS_JSON_PUBLISHER_HPP +#define LLARP_METRICS_JSON_PUBLISHER_HPP #include +#include -#include #include +#include +#include + namespace llarp { namespace metrics { - class StreamPublisher final : public Publisher - { - std::ostream& m_stream; - - public: - StreamPublisher(std::ostream& stream) : m_stream(stream) - { - } - - ~StreamPublisher() - { - } - - void - publish(const Sample& values) override; - }; - class JsonPublisher final : public Publisher { public: @@ -53,7 +37,5 @@ namespace llarp directoryPublisher(const nlohmann::json& result, const fs::path& path); }; } // namespace metrics - } // namespace llarp - #endif diff --git a/llarp/metrics/publishers.cpp b/llarp/metrics/stream_publisher.cpp similarity index 53% rename from llarp/metrics/publishers.cpp rename to llarp/metrics/stream_publisher.cpp index c12fdb38f..5608b36ad 100644 --- a/llarp/metrics/publishers.cpp +++ b/llarp/metrics/stream_publisher.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -155,115 +155,6 @@ namespace llarp } stream << "\n\t\t]\n"; } - - nlohmann::json - tagsToJson(const Tags &tags) - { - nlohmann::json result; - - std::for_each(tags.begin(), tags.end(), [&](const auto &tag) { - absl::visit([&](const auto &t) { result[tag.first] = t; }, - tag.second); - }); - - return result; - } - - template < typename Value > - nlohmann::json - formatValue(const Record< Value > &record, const Tags &tags, - double elapsedTime, Publication::Type publicationType) - { - switch(publicationType) - { - case Publication::Type::Unspecified: - { - assert(false && "Invalid publication type"); - } - break; - case Publication::Type::Total: - { - return {{"tags", tagsToJson(tags)}, {"total", record.total()}}; - } - break; - case Publication::Type::Count: - { - return {{"tags", tagsToJson(tags)}, {"count", record.count()}}; - } - break; - case Publication::Type::Min: - { - return {{"tags", tagsToJson(tags)}, {"min", record.min()}}; - } - break; - case Publication::Type::Max: - { - return {{"tags", tagsToJson(tags)}, {"max", record.max()}}; - } - break; - case Publication::Type::Avg: - { - return {{"tags", tagsToJson(tags)}, - {"avg", record.total() / record.count()}}; - } - break; - case Publication::Type::Rate: - { - return {{"tags", tagsToJson(tags)}, - {"rate", record.total() / elapsedTime}}; - } - break; - case Publication::Type::RateCount: - { - return {{"tags", tagsToJson(tags)}, - {"rateCount", record.count() / elapsedTime}}; - } - break; - } - } - - template < typename Value > - nlohmann::json - recordToJson(const TaggedRecords< Value > &taggedRecord, - double elapsedTime) - { - nlohmann::json result; - result["id"] = taggedRecord.id.toString(); - - auto publicationType = taggedRecord.id.description()->type(); - - for(const auto &rec : taggedRecord.data) - { - const auto &record = rec.second; - if(publicationType != Publication::Type::Unspecified) - { - result["publicationType"] = Publication::repr(publicationType); - - result["metrics"].push_back( - formatValue(record, rec.first, elapsedTime, publicationType)); - } - else - { - nlohmann::json tmp; - tmp["tags"] = tagsToJson(rec.first); - tmp["count"] = record.count(); - tmp["total"] = record.total(); - - if(Record< Value >::DEFAULT_MIN() != record.min()) - { - tmp["min"] = record.min(); - } - if(Record< Value >::DEFAULT_MAX() == record.max()) - { - tmp["max"] = record.max(); - } - - result["metrics"].push_back(tmp); - } - } - - return result; - } } // namespace void @@ -301,59 +192,5 @@ namespace llarp prev = gIt; } } - - void - JsonPublisher::publish(const Sample &values) - { - if(values.recordCount() == 0) - { - // nothing to publish - return; - } - - nlohmann::json result; - result["sampleTime"] = absl::UnparseFlag(values.sampleTime()); - result["recordCount"] = values.recordCount(); - auto gIt = values.begin(); - auto prev = values.begin(); - for(; gIt != values.end(); ++gIt) - { - const double elapsedTime = absl::ToDoubleSeconds(samplePeriod(*gIt)); - - if(gIt == prev || samplePeriod(*gIt) != samplePeriod(*prev)) - { - result["elapsedTime"] = elapsedTime; - } - - absl::visit( - [&](const auto &x) -> void { - for(const auto &record : x) - { - result["record"].emplace_back( - recordToJson(record, elapsedTime)); - } - }, - *gIt); - - prev = gIt; - } - - m_publish(result); - } - - void - JsonPublisher::directoryPublisher(const nlohmann::json &result, - const fs::path &path) - { - std::ofstream fstream(path.string(), std::ios_base::app); - if(!fstream) - { - std::cerr << "Skipping metrics publish, " << path << " is not a file\n"; - return; - } - - fstream << std::setw(0) << result << '\n'; - fstream.close(); - } } // namespace metrics } // namespace llarp diff --git a/llarp/metrics/stream_publisher.hpp b/llarp/metrics/stream_publisher.hpp new file mode 100644 index 000000000..a50ebe686 --- /dev/null +++ b/llarp/metrics/stream_publisher.hpp @@ -0,0 +1,30 @@ +#ifndef LLARP_METRICS_STREAM_PUBLISHER_HPP +#define LLARP_METRICS_STREAM_PUBLISHER_HPP + +#include + +#include + +namespace llarp +{ + namespace metrics + { + class StreamPublisher final : public Publisher + { + std::ostream& m_stream; + + public: + StreamPublisher(std::ostream& stream) : m_stream(stream) + { + } + + ~StreamPublisher() = default; + + void + publish(const Sample& values) override; + }; + } // namespace metrics + +} // namespace llarp + +#endif diff --git a/test/metrics/test_llarp_metrics_publisher.cpp b/test/metrics/test_llarp_metrics_publisher.cpp index bf21df05f..89bdfc3a4 100644 --- a/test/metrics/test_llarp_metrics_publisher.cpp +++ b/test/metrics/test_llarp_metrics_publisher.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include