Skip to content

Commit 2798fa9

Browse files
committed
feat: #207 metrics ingestion endpoint v0.2
1 parent 409674e commit 2798fa9

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Ptah.sh takes the pain out of deployment by easing common tasks used in many pro
1616
- Load balancing of an incoming traffic and SSL auto-provisioning via Caddy Server.
1717
- And many more features.
1818

19+
## Components
20+
21+
Ptah.sh is a collection of several interdependent services:
22+
23+
- [Ptah.sh Server](https://github.com/ptah-sh/ptah-server) - the core of the platform, responsible for managing the infrastructure, scaling, and load balancing.
24+
- [Ptah.sh Agent](https://github.com/ptah-sh/ptah-agent) - the component installed on the target machine, responsible for running the containers and services.
25+
- [Ptah.sh Caddy](https://github.com/ptah-sh/ptah-caddy) - the component installed on the target machine, responsible for running the Caddy Server and providing metrics to the Ptah.sh Server.
26+
- [Ptah.sh GitHub Action](https://github.com/ptah-sh/deploy-action) - the component responsible for deploying the application to the target machine.
27+
- [Ptah.sh Website](https://github.com/ptah-sh/ptah-sh.github.io) - the website of the Ptah.sh platform available at [ptah.sh](https://ptah.sh), containing the documentation, 1-Click Apps templates and the public-facing information.
28+
1929
## Ptah.sh Sponsors
2030

2131
We would like to extend our thanks to the following sponsors for funding Ptah.sh development. If you are interested in becoming a sponsor, please send an e-mail to Bohdan Shulha via [contact@ptah.sh](mailto:contact@ptah.sh).

api-nodes/Http/Controllers/MetricsController.php

+15-26
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use App\Models\DeploymentData\Process;
66
use App\Models\Node;
7+
use App\Services\Metrics;
78
use App\Util\Promexport;
89
use Illuminate\Http\Request;
910
use Illuminate\Http\Response;
1011
use Illuminate\Log\Logger;
11-
use Illuminate\Support\Facades\Http;
1212

1313
class MetricsController
1414
{
@@ -97,10 +97,6 @@ public function __invoke(Request $request, Logger $log, Node $node)
9797
continue;
9898
}
9999

100-
$query = '';
101-
$query .= '?extra_label=swarm_id='.$node->swarm->id;
102-
$query .= '&extra_label=node_id='.$node->id;
103-
104100
$ingestMetrics = [];
105101

106102
$lines = explode("\n", $metricsDoc);
@@ -192,9 +188,9 @@ public function __invoke(Request $request, Logger $log, Node $node)
192188
$ingestMetrics[] = $line;
193189

194190
break;
195-
case 'ptah_node_disk_usage_free':
196-
case 'ptah_node_disk_usage_total':
197-
case 'ptah_node_disk_usage_used':
191+
case 'ptah_node_disk_free_bytes':
192+
case 'ptah_node_disk_total_bytes':
193+
case 'ptah_node_disk_used_bytes':
198194
if (empty($labels['path'])) {
199195
break;
200196
}
@@ -211,13 +207,16 @@ public function __invoke(Request $request, Logger $log, Node $node)
211207
case 'ptah_node_cpu_system':
212208
case 'ptah_node_cpu_total':
213209
case 'ptah_node_cpu_user':
214-
case 'ptah_node_load_avg_1':
215-
case 'ptah_node_load_avg_5':
216-
case 'ptah_node_load_avg_15':
217-
case 'ptah_node_memory_free':
218-
case 'ptah_node_memory_total':
219-
case 'ptah_node_memory_used':
220-
case 'ptah_node_uptime':
210+
case 'ptah_node_load_avg_1m':
211+
case 'ptah_node_load_avg_5m':
212+
case 'ptah_node_load_avg_15m':
213+
case 'ptah_node_memory_total_bytes':
214+
case 'ptah_node_memory_used_bytes':
215+
case 'ptah_node_memory_free_bytes':
216+
case 'ptah_node_swap_total_bytes':
217+
case 'ptah_node_swap_used_bytes':
218+
case 'ptah_node_swap_free_bytes':
219+
case 'ptah_node_uptime_seconds':
221220
$ingestMetrics[] = $line;
222221

223222
break;
@@ -229,17 +228,7 @@ public function __invoke(Request $request, Logger $log, Node $node)
229228
}
230229
}
231230

232-
$log->info('Metrics:', [
233-
'metrics' => $ingestMetrics,
234-
]);
235-
236-
// TODO: use value from config (/env vars)
237-
$response = Http::withBody(implode("\n", $ingestMetrics), 'text/plain')->post("http://127.0.0.1:8080/api/v1/import/prometheus$query");
238-
239-
$log->info('VictoriaMetrics response:', [
240-
'status' => $response->status(),
241-
'body' => $response->body(),
242-
]);
231+
$response = Metrics::importPrometheusMetrics($node->swarm->id, $node->id, $ingestMetrics);
243232
}
244233

245234
$response = new Response('{}', 204);

app/Services/Metrics.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Illuminate\Support\Facades\Http;
6+
7+
// Multitenancy for single node: https://docs.victoriametrics.com/single-server-victoriametrics/#prometheus-querying-api-enhancements
8+
class Metrics
9+
{
10+
public static function importPrometheusMetrics($swarmId, $nodeId, $metrics)
11+
{
12+
$query = '';
13+
$query .= '?extra_label=swarm_id='.$swarmId;
14+
$query .= '&extra_label=node_id='.$nodeId;
15+
16+
return Http::withBody(implode("\n", $metrics), 'text/plain')->post(config('database.victoriametrics.url').'/api/v1/import/prometheus'.$query);
17+
}
18+
19+
public static function getMetrics($query, $nodeIds)
20+
{
21+
return Http::asForm()->post(config('database.victoriametrics.url').'/api/v1/query', [
22+
'query' => $query,
23+
'step' => '30s',
24+
'extra_filters' => '{node_id=~"'.implode('|', $nodeIds).'"}',
25+
'latency_offset' => '5s',
26+
])->json();
27+
}
28+
29+
public static function getMetricsRange($query, $nodeIds)
30+
{
31+
return Http::asForm()->post(config('database.victoriametrics.url').'/api/v1/query_range', [
32+
'query' => $query,
33+
'start' => now()->subMinutes(5)->subSeconds(30)->getTimestampMs() / 1000,
34+
'end' => now()->subSeconds(30)->getTimestampMs() / 1000,
35+
'step' => '5s',
36+
'extra_filters' => '{node_id=~"'.implode('|', $nodeIds).'"}',
37+
'latency_offset' => '5s',
38+
])->json();
39+
}
40+
}

config/database.php

+3
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,7 @@
170170

171171
],
172172

173+
'victoriametrics' => [
174+
'url' => rtrim(env('VICTORIAMETRICS_URL', 'http://localhost:8428'), '/'),
175+
],
173176
];

0 commit comments

Comments
 (0)