Skip to content
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

add __all support for prometheus metics #142

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/CoreServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,27 +339,37 @@ void CoreServer::_setup_routes(const PrometheusConfig &prom_config)
}
});
_svr.Get(fmt::format("/api/v1/policies/({})/metrics/prometheus", AbstractModule::MODULE_ID_REGEX).c_str(), [&](const httplib::Request &req, httplib::Response &res) {
auto name = req.matches[1];
if (!_registry.policy_manager()->module_exists(name)) {
res.status = 404;
res.set_content("policy does not exists", "text/plain");
return;
std::vector<std::string> plist;
{
auto name = req.matches[1];
if (name == "__all") {
// special route to get all policy metrics in one call, for scraping performance reasons
plist = _registry.policy_manager()->module_get_keys();
} else if (!_registry.policy_manager()->module_exists(name)) {
res.status = 404;
res.set_content("policy does not exists", "text/plain");
return;
} else {
plist.emplace_back(name);
}
}
try {
std::stringstream output;
auto [policy, lock] = _registry.policy_manager()->module_get_locked(name);
for (auto &mod : policy->modules()) {
auto hmod = dynamic_cast<StreamHandler *>(mod);
if (hmod) {
spdlog::stopwatch sw;
hmod->window_prometheus(output, {{"policy", name}, {"module", hmod->name()}});
_logger->debug("{} window_prometheus elapsed time: {}", hmod->name(), sw);
std::stringstream output;
for (const auto &p_mname : plist) {
try {
auto [policy, lock] = _registry.policy_manager()->module_get_locked(p_mname);
for (auto &mod : policy->modules()) {
auto hmod = dynamic_cast<StreamHandler *>(mod);
if (hmod) {
spdlog::stopwatch sw;
hmod->window_prometheus(output, {{"policy", p_mname}, {"module", hmod->name()}});
_logger->debug("{} window_prometheus elapsed time: {}", hmod->name(), sw);
}
}
} catch (const std::exception &e) {
res.status = 500;
res.set_content(e.what(), "text/plain");
}
res.set_content(output.str(), "text/plain");
} catch (const std::exception &e) {
res.status = 500;
res.set_content(e.what(), "text/plain");
}
});
}
Expand Down