From 9bc501bbf77de1f08272fc5c805d70ef821ee6bb Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 14 Apr 2019 17:08:51 +0100 Subject: [PATCH] Integrate metric tank into build --- include/llarp.hpp | 5 ++++ llarp/CMakeLists.txt | 1 + llarp/context.cpp | 70 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/include/llarp.hpp b/include/llarp.hpp index 036349457..d0f5457ec 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -48,6 +49,10 @@ namespace llarp bool singleThreaded = false; bool disableMetrics = false; bool disableMetricLogs = false; + std::string jsonMetricsPath; + std::string metricTankHost; + std::map< std::string, std::string > metricTags; + std::unique_ptr< Crypto > crypto; std::unique_ptr< AbstractRouter > router; std::unique_ptr< llarp_threadpool > worker; diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index a64714451..bbc95f520 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -61,6 +61,7 @@ set(LIB_PLATFORM_SRC # for networking ev/ev.cpp ev/pipe.cpp + metrics/metrictank_publisher.cpp metrics/publishers.cpp net/net.cpp net/net_addr.cpp diff --git a/llarp/context.cpp b/llarp/context.cpp index 46d6e7d8f..04af998f0 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include #include +#include #include #include @@ -81,17 +83,22 @@ namespace llarp { disableMetrics = true; } - if(!strcmp(key, "disable-metrics-log")) + else if(!strcmp(key, "disable-metrics-log")) { disableMetricLogs = true; } - if(!strcmp(key, "json-metrics-path")) + else if(!strcmp(key, "json-metrics-path")) { - setupMetrics(); - m_metricsManager->instance()->addGlobalPublisher( - std::make_shared< metrics::JsonPublisher >( - std::bind(&metrics::JsonPublisher::directoryPublisher, - std::placeholders::_1, val))); + jsonMetricsPath = val; + } + else if(!strcmp(key, "metric-tank-host")) + { + metricTankHost = val; + } + else + { + // consume everything else as a metric tag + metricTags[key] = val; } } if(!strcmp(section, "router")) @@ -139,7 +146,50 @@ namespace llarp *m_scheduler, m_metricsManager->instance()); } - m_metricsPublisher->setDefault(absl::Seconds(30)); + m_metricsManager->instance()->addGlobalPublisher( + std::make_shared< metrics::JsonPublisher >( + std::bind(&metrics::JsonPublisher::directoryPublisher, + std::placeholders::_1, jsonMetricsPath))); + + if(!metricTankHost.empty()) + { + if(std::getenv("LOKINET_ENABLE_METRIC_TANK")) + { + static std::string WARNING = R"( +__ ___ ____ _ _ ___ _ _ ____ +\ \ / / \ | _ \| \ | |_ _| \ | |/ ___| + \ \ /\ / / _ \ | |_) | \| || || \| | | _ + \ V V / ___ \| _ <| |\ || || |\ | |_| | + \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____| + +This Lokinet session is not private + +Sending connection metrics to metrictank +__ ___ ____ _ _ ___ _ _ ____ +\ \ / / \ | _ \| \ | |_ _| \ | |/ ___| + \ \ /\ / / _ \ | |_) | \| || || \| | | _ + \ V V / ___ \| _ <| |\ || || |\ | |_| | + \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____| + + )"; + + std::cerr << WARNING << '\n'; + + std::pair< std::string, std::string > split = + absl::StrSplit(metricTankHost, ':'); + + m_metricsManager->instance()->addGlobalPublisher( + std::make_shared< metrics::MetricTankPublisher >( + metricTags, split.first, stoi(split.second))); + } + else + { + std::cerr << "metrictank host specified, but " + "LOKINET_ENABLE_METRIC_TANK not set, skipping\n"; + } + } + + m_metricsPublisher->setDefault(absl::Seconds(1)); m_scheduler->start(); } @@ -227,8 +277,8 @@ namespace llarp return 1; } // must be done after router is made so we can use its disk io worker - // must also be done after configure so that netid is properly set if it is - // provided by config + // must also be done after configure so that netid is properly set if it + // is provided by config if(!this->LoadDatabase()) return 1; return 0;