-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerStats.php
122 lines (106 loc) · 3.75 KB
/
ServerStats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
use Kanata\Interfaces\KanataPluginInterface;
use Psr\Container\ContainerInterface;
use Kanata\Annotations\Plugin;
use Kanata\Annotations\Description;
use Kanata\Annotations\Author;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use ServerStats\Http\Controllers\ServerStatsController;
use Swoole\Table;
/**
* @Plugin(name="ServerStats")
* @Description(value="Serve Stats about Kanata Server")
* @Author(name="Savio Resende",email="[email protected]")
*/
class ServerStats implements KanataPluginInterface
{
/**
* Metrics key of Swoole Table at the container.
*/
const METRICS_SKIP_TABLE = 'metrics-skip-table';
protected Table $serverStatsTable;
public function __construct(
protected ContainerInterface $container
) {}
/**
* @return void
*/
public function start(): void
{
if (is_websocket_execution()) {
$this->register_views();
add_filter('error_template', fn($t) => 'stats::ws-error');
}
if (is_http_execution() || is_websocket_execution()) {
$this->register_requests_counter_table();
$this->register_metrics_endpoints();
}
}
/**
* Register view for WS Server.
*
* @return void
*/
private function register_views(): void
{
add_filter('view_folders', function($templates) {
$templates['stats'] = untrailingslashit(plugin_path('server-stats')) . '/views';
return $templates;
});
}
/**
* Register "/metrics" endpoint for WS and Http Servers.
*
* @return void
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
private function register_metrics_endpoints()
{
add_filter('routes', function($app) {
// here we cancel any request to home
if (is_websocket_execution()) {
$app->get('/', function(Request $request, Response $response) {
return view($response, 'stats::ws-error', [
'content' => 'Nothing here.',
]);
})->setName('home');
}
// --------------------------------------------------------------
// metrics
// --------------------------------------------------------------
$metricsRoute = $app->get(config('server-stats.endpoint'), [ServerStatsController::class, 'metrics'])->setName('metrics');
if (
is_plugin_active('user-authorization')
&& config('server-stats.secure-metrics-jwt')
) {
$metricsRoute->add(new \UserAuthorization\Http\Middlewares\JwtAuthMiddleware);
}
// --------------------------------------------------------------
// logs
// --------------------------------------------------------------
$logsRoute = $app->get(config('server-stats.logs-endpoint'), [ServerStatsController::class, 'logs'])->setName('logs');
if (
is_plugin_active('user-authorization')
&& config('server-stats.secure-logs-jwt')
) {
$logsRoute->add(new \UserAuthorization\Http\Middlewares\JwtAuthMiddleware);
}
return $app;
});
}
/**
* This table servers the purpose of avoiding the /metrics
* request to affect stats.
*
* @return void
*/
private function register_requests_counter_table()
{
$table = new Table(1024);
$table->column('counter', Table::TYPE_INT, 20);
$table->create();
container()->set(self::METRICS_SKIP_TABLE, $table);
}
}