-
Notifications
You must be signed in to change notification settings - Fork 18
feat(api): Add a new metrics endpoint for exporting statistics to Prometheus #932
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
Merged
Merged
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| /* The metrics API ressource. | ||
| * | ||
| * Author: Steffen Vogel <post@steffenvogel.de> | ||
| * Author: Youssef Nakti <naktiyoussef@proton.me> | ||
| * SPDX-FileCopyrightText: 2014-2023 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 { | ||
| public: | ||
| using Request::Request; | ||
|
|
||
| virtual Response *execute() { | ||
| if (method != Session::Method::GET) { | ||
| throw InvalidMethod(this); | ||
| } | ||
|
|
||
| if (body != nullptr) { | ||
| throw BadRequest("The metrics endpoint does not accept any body data"); | ||
| } | ||
|
|
||
| std::stringstream ss; | ||
| 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 : Stats::metrics) { | ||
| std::string metric_name = metric.second.name; | ||
| std::replace(metric_name.begin(), metric_name.end(), '.', '_'); | ||
| ss << stats->getHistogram(metric.first) | ||
| .toPrometheusText(metric_name, node->getNameShort()) | ||
| << "\n\n"; | ||
| } | ||
| } | ||
|
|
||
| auto str = ss.str(); | ||
| return new Response(session, HTTP_STATUS_OK, "text/plain; charset=UTF-8", | ||
| Buffer(str.c_str(), str.size())); | ||
| } | ||
| }; | ||
|
|
||
| // Register API request | ||
| static char n[] = "metrics"; | ||
| static char r[] = "/metrics"; | ||
| static char d[] = "Get stats of all nodes in Prometheus metrics format"; | ||
| static RequestPlugin<MetricsRequest, n, r, d> p; | ||
|
|
||
| } // namespace api | ||
| } // namespace node | ||
| } // namespace villas | ||
This file contains hidden or 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,158 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Integration test for remote API | ||
| # | ||
| # Author: Steffen Vogel <steffen.vogel@opal-rt.com> | ||
| # SPDX-FileCopyrightText: 2025 OPAL-RT Germany GmbH | ||
| # 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": { | ||
| "node1": { | ||
| "type": "signal", | ||
|
|
||
| "signal": "sine", | ||
| "limit": 10, | ||
| "values": 1, | ||
|
|
||
| "in": { | ||
| "hooks": [ | ||
| { | ||
| "type": "stats", | ||
| "warmup": 0, | ||
| "buckets": 5 | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "paths": [ | ||
| { | ||
| "in": "node1" | ||
| } | ||
| ] | ||
| } | ||
| EOF | ||
|
|
||
| cat <<EOF > expected | ||
| #TYPE HISTOGRAM rtp_jitter | ||
| rtp_jitter {node="node1" le="0"} 0 | ||
| rtp_jitter {node="node1" le="0"} 0 | ||
| rtp_jitter {node="node1" le="0"} 0 | ||
| rtp_jitter {node="node1" le="0"} 0 | ||
| rtp_jitter {node="node1" le="0"} 0 | ||
| rtp_jitter {node="node1" le="+Inf"} 0 | ||
| rtp_jitter_count {node="node1"} 0 | ||
|
|
||
| #TYPE HISTOGRAM rtp_pkts_lost | ||
| rtp_pkts_lost {node="node1" le="0"} 0 | ||
| rtp_pkts_lost {node="node1" le="0"} 0 | ||
| rtp_pkts_lost {node="node1" le="0"} 0 | ||
| rtp_pkts_lost {node="node1" le="0"} 0 | ||
| rtp_pkts_lost {node="node1" le="0"} 0 | ||
| rtp_pkts_lost {node="node1" le="+Inf"} 0 | ||
| rtp_pkts_lost_count {node="node1"} 0 | ||
|
|
||
| #TYPE HISTOGRAM rtp_loss_fraction | ||
| rtp_loss_fraction {node="node1" le="0"} 0 | ||
| rtp_loss_fraction {node="node1" le="0"} 0 | ||
| rtp_loss_fraction {node="node1" le="0"} 0 | ||
| rtp_loss_fraction {node="node1" le="0"} 0 | ||
| rtp_loss_fraction {node="node1" le="0"} 0 | ||
| rtp_loss_fraction {node="node1" le="+Inf"} 0 | ||
| rtp_loss_fraction_count {node="node1"} 0 | ||
|
|
||
| #TYPE HISTOGRAM signal_cnt | ||
| signal_cnt {node="node1" le="-8"} 0 | ||
| signal_cnt {node="node1" le="-4"} 0 | ||
| signal_cnt {node="node1" le="0"} 0 | ||
| signal_cnt {node="node1" le="4"} 9 | ||
| signal_cnt {node="node1" le="8"} 9 | ||
| signal_cnt {node="node1" le="+Inf"} 10 | ||
| signal_cnt_count {node="node1"} 10 | ||
|
|
||
| #TYPE HISTOGRAM age | ||
| age {node="node1" le="0"} 0 | ||
| age {node="node1" le="0"} 0 | ||
| age {node="node1" le="0"} 0 | ||
| age {node="node1" le="0"} 0 | ||
| age {node="node1" le="0"} 0 | ||
| age {node="node1" le="+Inf"} 0 | ||
| age_count {node="node1"} 0 | ||
|
|
||
| #TYPE HISTOGRAM owd | ||
| owd {node="node1" le="-8"} 0 | ||
| owd {node="node1" le="-4"} 0 | ||
| owd {node="node1" le="0"} 0 | ||
| owd {node="node1" le="4"} 8 | ||
| owd {node="node1" le="8"} 8 | ||
| owd {node="node1" le="+Inf"} 9 | ||
| owd_count {node="node1"} 9 | ||
|
|
||
| #TYPE HISTOGRAM gap_received | ||
| gap_received {node="node1" le="-8"} 0 | ||
| gap_received {node="node1" le="-4"} 0 | ||
| gap_received {node="node1" le="0"} 0 | ||
| gap_received {node="node1" le="4"} 8 | ||
| gap_received {node="node1" le="8"} 8 | ||
| gap_received {node="node1" le="+Inf"} 9 | ||
| gap_received_count {node="node1"} 9 | ||
|
|
||
| #TYPE HISTOGRAM gap_sent | ||
| gap_sent {node="node1" le="-8"} 0 | ||
| gap_sent {node="node1" le="-4"} 0 | ||
| gap_sent {node="node1" le="0"} 0 | ||
| gap_sent {node="node1" le="4"} 8 | ||
| gap_sent {node="node1" le="8"} 8 | ||
| gap_sent {node="node1" le="+Inf"} 9 | ||
| gap_sent_count {node="node1"} 9 | ||
|
|
||
| #TYPE HISTOGRAM reordered | ||
| reordered {node="node1" le="0"} 0 | ||
| reordered {node="node1" le="0"} 0 | ||
| reordered {node="node1" le="0"} 0 | ||
| reordered {node="node1" le="0"} 0 | ||
| reordered {node="node1" le="0"} 0 | ||
| reordered {node="node1" le="+Inf"} 0 | ||
| reordered_count {node="node1"} 0 | ||
|
|
||
| #TYPE HISTOGRAM skipped | ||
| skipped {node="node1" le="0"} 0 | ||
| skipped {node="node1" le="0"} 0 | ||
| skipped {node="node1" le="0"} 0 | ||
| skipped {node="node1" le="0"} 0 | ||
| skipped {node="node1" le="0"} 0 | ||
| skipped {node="node1" le="+Inf"} 0 | ||
| skipped_count {node="node1"} 0 | ||
|
|
||
| EOF | ||
|
|
||
| # Start VILLASnode instance with local config | ||
| villas node config.json & | ||
|
|
||
| # Wait for node to complete init | ||
| sleep 2 | ||
|
|
||
| # Fetch config via API | ||
| curl -s http://localhost:8080/api/v2/metrics > metrics | ||
|
|
||
| # Shutdown VILLASnode | ||
| kill $! | ||
|
|
||
| # Check if metrics contain expected values | ||
| diff -u expected metrics |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.