Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(API_SRC
requests/node_stats.cpp
requests/node_stats_reset.cpp
requests/node_file.cpp
requests/metrics.cpp
requests/paths.cpp
requests/path_info.cpp
requests/path_action.cpp
Expand Down
77 changes: 77 additions & 0 deletions lib/api/requests/metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* The Prometheus metrics endpoint.
*
* Author: Youssef Nakti <youssef.nakti@eonerc.rwth-aachen.de>
* SPDX-FileCopyrightText: 2025 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/

#include <chrono>
#include <jansson.h>
#include <villas/api/request.hpp>
#include <villas/api/response.hpp>
#include <villas/api/session.hpp>
#include <villas/node.hpp>
#include <villas/stats.hpp>
#include <villas/super_node.hpp>
#include <villas/utils.hpp>

namespace villas {
namespace node {
namespace api {

class MetricsRequest : public Request {
private:
std::unordered_map<Stats::Metric, std::string> metrics_subset = {
{Stats::Metric::SMPS_SKIPPED, "skipped"},
{Stats::Metric::OWD, "owd"},
{Stats::Metric::AGE, "age"},
{Stats::Metric::SIGNAL_COUNT, "signalcnt"},
{Stats::Metric::RTP_PKTS_LOST, "rtp_pkts_lost"}};

public:
using Request::Request;

virtual Response *execute() {
if (method != Session::Method::GET)
throw InvalidMethod(this);

if (body != nullptr)
throw BadRequest("Nodes endpoint does not accept any body data");

std::string text_res = "";
NodeList node_list = session->getSuperNode()->getNodes();
for (Node *node : node_list) {
auto stats = node->getStats();
if (!stats)
continue;
std::string node_name = node->getNameShort();
for (auto &metric : metrics_subset) {
Hist histogram = stats->getHistogram(metric.first);
std::string t = std::to_string(
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());

text_res += metric.second + " {node=\"" + node_name +
"\" acc=\"last\"} " + std::to_string(histogram.getLast()) +
" " + t + "\n";
text_res += metric.second + " {node=\"" + node_name +
"\" acc=\"total\"} " +
std::to_string(histogram.getTotal()) + " " + t + "\n";
}
}

return new Response(session, HTTP_STATUS_OK, "text/plain; charset=UTF-8",
Buffer(text_res.c_str(), text_res.size()));
}
};

// Register API request
static char n[] = "metrics";
static char r[] = "/metrics";
static char d[] = "Get Prometheus metrics from all nodes";
static RequestPlugin<MetricsRequest, n, r, d> p;

} // namespace api
} // namespace node
} // namespace villas
56 changes: 56 additions & 0 deletions tests/integration/api-metrics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
#
# Integration test for remote API
#
# Author: Steffen Vogel <post@steffenvogel.de>
# SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
# SPDX-License-Identifier: Apache-2.0

set -e

DIR=$(mktemp -d)
pushd ${DIR}

function finish {
popd
rm -rf ${DIR}
}
trap finish EXIT

cat > config.json <<EOF
{
"http": {
"port": 8080
},
"nodes": {
"signal": {
"type": "signal",
"count": 10,
"rate": 10,
"signal": "sine"
}
},
"paths": [
{
"in": "signal",
"hooks": [
"print"
]
}
]
}
EOF

# Start VILLASnode instance with local config
villas node config.json &

# Wait for node to complete init
sleep 1

# Fetch config via API
curl -s http://localhost:8080/api/v2/metrics > metrics

# Shutdown VILLASnode
kill $!

cat metrics