Skip to content

Commit

Permalink
feat: #207 allow to collect metrics from non-swarm nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Oct 5, 2024
1 parent b64048c commit 4324458
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
6 changes: 1 addition & 5 deletions api-nodes/Http/Controllers/MetricsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class MetricsController
// Please don't judge me for this code. I'm a PHP developer.
public function __invoke(Request $request, Logger $log, Node $node)
{
if ($node->swarm === null) {
return new Response('{}', 204);
}

// TODO: cache this with the new Laravel cache system (stale-while-revalidate from the recent release)
$interfaces = collect($node->data->host->networks)->pluck('if_name')->unique()->toArray();

Expand Down Expand Up @@ -228,7 +224,7 @@ public function __invoke(Request $request, Logger $log, Node $node)
}
}

$response = Metrics::importPrometheusMetrics($node->swarm->id, $node->id, $ingestMetrics);
$response = Metrics::importPrometheusMetrics($node->id, $ingestMetrics);
}

$response = new Response('{}', 204);
Expand Down
5 changes: 2 additions & 3 deletions app/Services/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
// Multitenancy for single node: https://docs.victoriametrics.com/single-server-victoriametrics/#prometheus-querying-api-enhancements
class Metrics
{
public static function importPrometheusMetrics($swarmId, $nodeId, $metrics)
public static function importPrometheusMetrics($nodeId, $metrics)
{
$query = '';
$query .= '?extra_label=swarm_id='.$swarmId;
$query .= '&extra_label=node_id='.$nodeId;
$query .= '?extra_label=node_id='.$nodeId;

return Http::withBody(implode("\n", $metrics), 'text/plain')->post(config('database.victoriametrics.url').'/api/v1/import/prometheus'.$query);
}
Expand Down
20 changes: 15 additions & 5 deletions resources/js/Pages/Nodes/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,29 @@ function getMetric(metric) {
}
const avgLoad = computed(() => {
return `${getMetric("ptah_node_load_avg_1m")} / ${getMetric("ptah_node_load_avg_5m")} / ${getMetric("ptah_node_load_avg_15m")}`;
const metric1m = getMetric("ptah_node_load_avg_1m") ?? "...";
const metric5m = getMetric("ptah_node_load_avg_5m") ?? "...";
const metric15m = getMetric("ptah_node_load_avg_15m") ?? "...";
return `${metric1m} / ${metric5m} / ${metric15m}`;
});
const cpuUsage = computed(() => {
return getMetric("cpu_usage") + "%";
const metric = getMetric("cpu_usage") ?? "...";
return metric + "%";
});
const memoryUsage = computed(() => {
return getMetric("memory_usage") + "%";
const metric = getMetric("memory_usage") ?? "...";
return metric + "%";
});
const diskUsage = computed(() => {
return getMetric("disk_usage") + "%";
const metric = getMetric("disk_usage") ?? "...";
return metric + "%";
});
const refreshTimeoutId = ref(0);
Expand Down Expand Up @@ -126,7 +136,7 @@ onUnmounted(() => {
</template>
</div>
</div>
<div class="flex justify-between">
<div class="flex justify-between" v-if="node.online">
<div class="flex flex-col">
<div class="flex flex-row justify-between">
<div class="flex flex-col">
Expand Down

0 comments on commit 4324458

Please sign in to comment.