diff --git a/.gitmodules b/.gitmodules index aa0a0836..2f46d1c1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -22,3 +22,7 @@ [submodule "third_party/DCGM"] path = third_party/DCGM url = https://github.com/NVIDIA/DCGM.git +[submodule "third_party/prometheus-cpp"] + path = third_party/prometheus-cpp + url = https://github.com/jupp0r/prometheus-cpp.git + branch = v1.0.2 diff --git a/dynolog/src/FBRelayLogger.cpp b/dynolog/src/FBRelayLogger.cpp index c26890f4..14b133cc 100644 --- a/dynolog/src/FBRelayLogger.cpp +++ b/dynolog/src/FBRelayLogger.cpp @@ -11,8 +11,8 @@ #include #include #include -#include #include +#include "hbt/src/common/System.h" using json = nlohmann::json; @@ -24,18 +24,6 @@ DEFINE_string( namespace dynolog { -namespace { - -std::string hostname = []() { - static std::string hostname; - char buf[1024]; - gethostname(buf, sizeof(buf)); - hostname = buf; - return hostname; -}(); - -}; - int setup_ipv4_socket(const std::string& addr, int port) { int sock_fd, domain = AF_INET; struct sockaddr_in serv_addr; @@ -139,7 +127,7 @@ void FBRelayLogger::initSocket() { FLAGS_fbrelay_address, FLAGS_fbrelay_port); } -FBRelayLogger::FBRelayLogger() { +FBRelayLogger::FBRelayLogger() : hostname_(facebook::hbt::getHostName()) { initSocket(); } @@ -167,8 +155,8 @@ void FBRelayLogger::finalize() { {"@timestamp", timestampStr()}, {"agent", { - {"hostname", hostname}, - {"name", hostname}, + {"hostname", hostname_}, + {"name", hostname_}, {"type", entity}, {"version", "0.1.0"}, }}, diff --git a/dynolog/src/FBRelayLogger.h b/dynolog/src/FBRelayLogger.h index 3d0d4c01..c583825d 100644 --- a/dynolog/src/FBRelayLogger.h +++ b/dynolog/src/FBRelayLogger.h @@ -43,6 +43,7 @@ class FBRelayLogger : public JsonLogger { void initSocket(); std::unique_ptr socket; + std::string hostname_; }; } // namespace dynolog diff --git a/dynolog/src/Metrics.cpp b/dynolog/src/Metrics.cpp index 3dc9e0c9..82b8d7e5 100644 --- a/dynolog/src/Metrics.cpp +++ b/dynolog/src/Metrics.cpp @@ -5,18 +5,58 @@ #include "dynolog/src/Metrics.h" +#include +#include + namespace dynolog { -const std::vector& getAllMetrics() { +const std::vector getAllMetrics() { static std::vector metrics_ = { {.name = "cpu_util", .type = MetricType::Ratio, .desc = "Fraction of total CPU time spend on user or system mode."}, + {.name = "cpu_u", + .type = MetricType::Ratio, + .desc = "Fraction of total CPU time spent in user mode."}, + {.name = "cpu_s", + .type = MetricType::Ratio, + .desc = "Fraction of total CPU time spent in system mode."}, + {.name = "cpu_i", + .type = MetricType::Ratio, + .desc = "Fraction of total CPU time spent in idle mode."}, + {.name = "mips", + .type = MetricType::Ratio, + .desc = "Number of million instructions executed per second."}, + {.name = "mega_cycles_per_second", + .type = MetricType::Ratio, + .desc = "Number of active CPU clock cycles per second."}, {.name = "uptime", .type = MetricType::Instant, .desc = "How long the system has been running in seconds."}, }; - return metrics_; + static std::map cpustats = { + {"cpu_u_ms", "user"}, + {"cpu_s_ms", "system"}, + {"cpu_n_ms", "nice"}, + {"cpu_w_ms", "iowait"}, + {"cpu_x_ms", "irq"}, + {"cpu_y_ms", "softirq"}, + }; + + auto metrics = metrics_; + + for (const auto& [name, cpu_mode] : cpustats) { + MetricDesc m{ + .name = name, + .type = MetricType::Delta, + .desc = fmt::format( + "Total CPU time in milliseconds spent in {} mode. " + "For more details please see man page for /proc/stat", + cpu_mode)}; + metrics.push_back(m); + } + + return metrics; } } // namespace dynolog diff --git a/dynolog/src/Metrics.h b/dynolog/src/Metrics.h index 11bc3dd2..d27d1be4 100644 --- a/dynolog/src/Metrics.h +++ b/dynolog/src/Metrics.h @@ -23,6 +23,6 @@ struct MetricDesc { std::string desc; }; -const std::vector& getAllMetrics(); +const std::vector getAllMetrics(); } // namespace dynolog diff --git a/dynolog/src/ODSJsonLogger.cpp b/dynolog/src/ODSJsonLogger.cpp index 2c3a4274..e4f74ba7 100644 --- a/dynolog/src/ODSJsonLogger.cpp +++ b/dynolog/src/ODSJsonLogger.cpp @@ -4,6 +4,8 @@ // LICENSE file in the root directory of this source tree. #include "dynolog/src/ODSJsonLogger.h" +#include "hbt/src/common/System.h" + #ifdef USE_GRAPH_ENDPOINT #include // @manual #include // @manual @@ -11,20 +13,14 @@ #include #include #include -#include DEFINE_string(category_id, "", "The category id of the ODS endpoint"); DEFINE_string(ods_entity_prefix, "", "The prefix for ODS entity name"); namespace dynolog { -constexpr int HOSTNAME_MAX = 50; constexpr char kODSUrl[] = "https://graph.facebook.com/v2.2/ods_metrics"; -ODSJsonLogger::ODSJsonLogger() { - char hostname[HOSTNAME_MAX]; - gethostname(hostname, HOSTNAME_MAX); - hostname_ = std::string(hostname); -} +ODSJsonLogger::ODSJsonLogger() : hostname_(facebook::hbt::getHostName()) {} void ODSJsonLogger::finalize() { std::string entity = FLAGS_ods_entity_prefix + hostname_; diff --git a/dynolog/src/PrometheusLogger.cpp b/dynolog/src/PrometheusLogger.cpp index 49ed6ef9..fe87ae04 100644 --- a/dynolog/src/PrometheusLogger.cpp +++ b/dynolog/src/PrometheusLogger.cpp @@ -5,6 +5,7 @@ #include "dynolog/src/PrometheusLogger.h" #include "dynolog/src/Metrics.h" +#include "hbt/src/common/System.h" #include #include @@ -30,11 +31,13 @@ PrometheusManager::PrometheusManager() // setup registry registry_ = std::make_shared(); + const std::string hostname = facebook::hbt::getHostName(); // setup counters and gauges for (const auto& m : getAllMetrics()) { // all metric types fit with Gauges so far. - auto& g = buildGaugeFromMetric(m, *registry_).Add({{"host_name", "test"}}); + auto& g = + buildGaugeFromMetric(m, *registry_).Add({{"host_name", hostname}}); gauges_[m.name] = &g; } diff --git a/dynolog/src/ScubaLogger.cpp b/dynolog/src/ScubaLogger.cpp index f35d7e73..f7721c83 100644 --- a/dynolog/src/ScubaLogger.cpp +++ b/dynolog/src/ScubaLogger.cpp @@ -5,6 +5,7 @@ #include "dynolog/src/ScubaLogger.h" #include +#include "hbt/src/common/System.h" #ifdef USE_GRAPH_ENDPOINT #include // @manual #include // @manual @@ -24,11 +25,8 @@ DEFINE_string( "The scribe category name for scuba logging"); ScubaLogger::ScubaLogger(const std::string& scribe_category) - : scribe_category_(scribe_category) { - char hostname[HOSTNAME_MAX] = ""; - gethostname(hostname, HOSTNAME_MAX); - hostname_ = std::string(hostname); -} + : scribe_category_(scribe_category), + hostname_(facebook::hbt::getHostName()) {} void ScubaLogger::logInt(const std::string& key, int64_t val) { metrics_int_[key] = val; diff --git a/scripts/build.sh b/scripts/build.sh index 76a57be9..229a7613 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -8,6 +8,8 @@ # ./scripts/build.sh set -eux -o pipefail +BUILD_PROMETHEUS="${BUILD_PROMETHEUS:-0}" +USE_PROMETHEUS="OFF" # Check dependencies cmake --version || echo "Please install cmake for your platform using dnf/apt-get etc." @@ -15,12 +17,32 @@ ninja --version || echo "Please install ninja for your platform using dnf/apt-ge rustc --version || echo "Please install Rust and Cargo - see https://www.rust-lang.org/tools/install" cargo --version || echo "Please install Rust and Cargo - see https://www.rust-lang.org/tools/install" +## Build Prometheus if enabled +if [ "${BUILD_PROMETHEUS}" -eq 1 ] +then + mkdir -p ./third_party/prometheus-cpp/_build + pushd ./third_party/prometheus-cpp/ + + git submodule init + git submodule update + + cd ./_build + cmake .. -DBUILD_SHARED_LIBS=ON -DENABLE_PUSH=OFF -DENABLE_COMPRESSION=OFF \ + -DENABLE_TESTING=OFF + cmake --build . --parallel 4 + cmake --install . + + USE_PROMETHEUS="ON" + popd +fi + ## Build dynolog echo "Running cmake" mkdir -p build; cd build; # note we can build without ninja if not available on this system -cmake -DCMAKE_BUILD_TYPE=Release -G Ninja "$@" .. +cmake "-DUSE_PROMETHEUS=${USE_PROMETHEUS}" \ + -DCMAKE_BUILD_TYPE=Release -G Ninja "$@" .. cmake --build . mkdir -p bin diff --git a/third_party/prometheus-cpp b/third_party/prometheus-cpp new file mode 160000 index 00000000..b1234816 --- /dev/null +++ b/third_party/prometheus-cpp @@ -0,0 +1 @@ +Subproject commit b1234816facfdda29845c46696a02998a4af115a diff --git a/version.txt b/version.txt index 0d91a54c..9e11b32f 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.3.0 +0.3.1