-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
basic prototype for prometheus logging #146
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "dynolog/src/Metrics.h" | ||
|
||
namespace dynolog { | ||
|
||
const std::vector<MetricDesc>& getAllMetrics() { | ||
static std::vector<MetricDesc> metrics_ = { | ||
{.name = "cpu_util", | ||
.type = MetricType::Ratio, | ||
.desc = "Fraction of total CPU time spend on user or system mode."}, | ||
{.name = "uptime", | ||
.type = MetricType::Instant, | ||
.desc = "How long the system has been running in seconds."}, | ||
}; | ||
return metrics_; | ||
} | ||
|
||
} // namespace dynolog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are Metrics specific to Prometheus? If so, should the naming reflect that? |
||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace dynolog { | ||
|
||
enum class MetricType { | ||
Delta, | ||
Instant, | ||
Ratio, | ||
Rate, | ||
}; | ||
|
||
struct MetricDesc { | ||
std::string name; | ||
MetricType type; | ||
std::string desc; | ||
}; | ||
|
||
const std::vector<MetricDesc>& getAllMetrics(); | ||
|
||
} // namespace dynolog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "dynolog/src/PrometheusLogger.h" | ||
#include "dynolog/src/Metrics.h" | ||
|
||
#include <fmt/format.h> | ||
#include <glog/logging.h> | ||
|
||
#ifdef USE_PROMETHEUS | ||
using namespace prometheus; | ||
|
||
DEFINE_int32( | ||
prometheus_port, | ||
8080, | ||
"Port to setup HTTP server for Prometheus to scrape."); | ||
|
||
namespace dynolog { | ||
|
||
inline auto& buildGaugeFromMetric(const MetricDesc& m, Registry& registry) { | ||
return BuildGauge().Name(m.name).Help(m.desc).Register(registry); | ||
} | ||
|
||
PrometheusManager::PrometheusManager() | ||
: exposer_(fmt::format("127.0.0.1:{}", FLAGS_prometheus_port)) { | ||
LOG(INFO) << "Initialized prometheus HTTP server on port = " | ||
<< FLAGS_prometheus_port; | ||
|
||
// setup registry | ||
registry_ = std::make_shared<Registry>(); | ||
|
||
// 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"}}); | ||
gauges_[m.name] = &g; | ||
} | ||
|
||
// setup registry | ||
exposer_.RegisterCollectable(registry_); | ||
} | ||
|
||
void PrometheusManager::log(const std::string& key, double val) { | ||
auto it = gauges_.find(key); | ||
if (it == gauges_.end()) { | ||
return; | ||
} | ||
auto g = it->second; | ||
if (!g) { | ||
return; | ||
} | ||
g->Set(val); | ||
} | ||
|
||
static std::shared_ptr<PrometheusManager> singleton_() { | ||
static std::shared_ptr<PrometheusManager> manager_ = | ||
std::make_shared<PrometheusManager>(); | ||
return manager_; | ||
} | ||
|
||
// static | ||
PrometheusManager::LoggingGuard PrometheusManager::singleton() { | ||
auto s = singleton_(); | ||
return LoggingGuard{.manager = s, .lock_guard = s->lock()}; | ||
} | ||
|
||
void PrometheusLogger::logImpl(const std::string& key, double val) { | ||
kvs_[key] = val; | ||
} | ||
|
||
void PrometheusLogger::finalize() { | ||
auto logging_guard = PrometheusManager::singleton(); | ||
auto prom = logging_guard.manager; | ||
for (const auto& [key, val] : kvs_) { | ||
prom->log(key, val); | ||
} | ||
} | ||
|
||
} // namespace dynolog | ||
#endif // USE_PROMETHEUS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include "dynolog/src/Logger.h" | ||
|
||
#ifdef USE_PROMETHEUS | ||
#include <prometheus/counter.h> | ||
#include <prometheus/exposer.h> | ||
#include <prometheus/gauge.h> | ||
#include <prometheus/registry.h> | ||
#endif | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <unordered_map> | ||
|
||
#ifdef USE_PROMETHEUS | ||
|
||
DECLARE_int32(prometheus_port); | ||
|
||
namespace dynolog { | ||
|
||
class PrometheusManager { | ||
public: | ||
struct LoggingGuard { | ||
std::shared_ptr<PrometheusManager> manager; | ||
std::lock_guard<std::mutex> lock_guard; | ||
}; | ||
|
||
PrometheusManager(); | ||
|
||
// Note that this method is not thread-safe and so | ||
// should only be used with the LoggingGuard in scope | ||
void log(const std::string& key, double val); | ||
|
||
static LoggingGuard singleton(); | ||
|
||
private: | ||
std::lock_guard<std::mutex> lock() { | ||
return std::lock_guard{mutex_}; | ||
} | ||
|
||
std::mutex mutex_; | ||
prometheus::Exposer exposer_; | ||
std::shared_ptr<prometheus::Registry> registry_; | ||
|
||
// only store a reference to Gauge because copying is not allowed | ||
std::unordered_map<std::string, prometheus::Gauge*> gauges_; | ||
|
||
// Should match googletest/include/gtest/gtest_prod.h | ||
// friend class test_case_name##_##test_name##_Test | ||
friend class PrometheusLoggerTest_ExporterTest_Test; | ||
}; | ||
|
||
class PrometheusLogger : public Logger { | ||
public: | ||
// Timestamp is set during logging part. | ||
void setTimestamp(Timestamp /*ts*/) override {} | ||
|
||
void logInt(const std::string& key, int64_t val) override { | ||
logImpl(key, static_cast<double>(val)); | ||
} | ||
|
||
void logFloat(const std::string& key, float val) override { | ||
logImpl(key, static_cast<double>(val)); | ||
} | ||
|
||
void logUint(const std::string& key, uint64_t val) override { | ||
logImpl(key, static_cast<double>(val)); | ||
} | ||
|
||
// not supported | ||
void logStr(const std::string& /*key*/, const std::string& /*val*/) override { | ||
} | ||
|
||
void finalize() override; | ||
|
||
private: | ||
void logImpl(const std::string& key, double val); | ||
|
||
std::unordered_map<std::string, double> kvs_; | ||
|
||
friend class PrometheusLoggerTest_BasicTest_Test; | ||
friend class PrometheusLoggerTest_ExporterTest_Test; | ||
}; | ||
|
||
} // namespace dynolog | ||
#endif // USE_PROMETHEUS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// | ||
// This source code is licensed under the MIT license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#include "dynolog/src/PrometheusLogger.h" | ||
#include <glog/logging.h> | ||
#include <gtest/gtest.h> | ||
#include "dynolog/src/Metrics.h" | ||
|
||
namespace dynolog { | ||
|
||
TEST(PrometheusLoggerTest, BasicTest) { | ||
// check that basic logger class is collecting values | ||
// from various log functions. | ||
|
||
PrometheusLogger logger; | ||
|
||
logger.logInt("uptime", 10000); | ||
logger.logFloat("pi", 3.1457); | ||
logger.logUint("cpu_util", 25); | ||
|
||
auto& kvs = logger.kvs_; | ||
EXPECT_FLOAT_EQ(kvs["uptime"], 10000); | ||
EXPECT_FLOAT_EQ(kvs["pi"], 3.1457); | ||
EXPECT_FLOAT_EQ(kvs["cpu_util"], 25); | ||
|
||
// DO NOT RUN finalize() on logger to avoid sending data to prometheus | ||
// exporter. | ||
} | ||
|
||
TEST(PrometheusLoggerTest, ExporterTest) { | ||
/* Allow Prometheus exporter to use any available port*/ | ||
FLAGS_prometheus_port = 0; | ||
|
||
float i = 0, j = 0; | ||
{ | ||
PrometheusLogger logger; | ||
for (const auto& m : getAllMetrics()) { | ||
logger.logFloat(m.name, i++); | ||
} | ||
logger.finalize(); | ||
|
||
auto logging_guard = PrometheusManager::singleton(); | ||
auto prom = logging_guard.manager; | ||
for (const auto& m : getAllMetrics()) { | ||
EXPECT_FLOAT_EQ(prom->gauges_[m.name]->Value(), j) | ||
<< "Metric " << m.name << " did not match expected value"; | ||
j++; | ||
} | ||
} | ||
ASSERT_GT(i, 0) << "No metrics were logged!"; | ||
ASSERT_EQ(i, j); | ||
} | ||
|
||
} // namespace dynolog |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Have the Prometheus server port/ip binding parameterized
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes added that :)