1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Triadev\PrometheusExporter\Provider; |
3
|
|
|
|
4
|
|
|
use Illuminate\Routing\Router; |
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use Prometheus\Storage\InMemory; |
7
|
|
|
use Triadev\PrometheusExporter\Contract\PrometheusExporterContract; |
8
|
|
|
use Prometheus\Storage\Redis; |
9
|
|
|
use Prometheus\Storage\APC; |
10
|
|
|
use Prometheus\Storage\Adapter; |
11
|
|
|
use Triadev\PrometheusExporter\Middleware\RequestPerRoute; |
12
|
|
|
use Triadev\PrometheusExporter\PrometheusExporter; |
13
|
|
|
|
14
|
|
|
class PrometheusExporterServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Bootstrap the application events. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
6 |
|
public function boot() |
|
|
|
|
22
|
|
|
{ |
|
|
|
|
23
|
6 |
|
$this->bootConfig(); |
24
|
|
|
$this->bootRoutes(); |
25
|
6 |
|
} |
|
|
|
|
26
|
6 |
|
|
27
|
6 |
|
private function bootConfig() |
|
|
|
|
28
|
6 |
|
{ |
|
|
|
|
29
|
|
|
$source = realpath(__DIR__ . '/../Config/config.php'); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if (class_exists('Illuminate\Foundation\Application', false)) { |
32
|
|
|
$this->publishes([ |
|
|
|
|
33
|
6 |
|
__DIR__ . '/../Config/config.php' => config_path('prometheus-exporter.php'), |
|
|
|
|
34
|
|
|
], 'config'); |
|
|
|
|
35
|
6 |
|
} elseif (class_exists('Laravel\Lumen\Application', false)) { |
|
|
|
|
36
|
6 |
|
$this->app/** @scrutinizer ignore-call */->configure('prometheus-exporter'); |
|
|
|
|
37
|
|
|
} |
38
|
6 |
|
|
39
|
|
|
$this->mergeConfigFrom($source, 'prometheus-exporter'); |
40
|
|
|
} |
|
|
|
|
41
|
|
|
|
42
|
|
|
private function bootRoutes() |
|
|
|
|
43
|
|
|
{ |
|
|
|
|
44
|
|
|
if (class_exists('Illuminate\Foundation\Application', false)) { |
45
|
6 |
|
$this->loadRoutesFrom(__DIR__ . '/../Routes/routes.php'); |
|
|
|
|
46
|
|
|
} |
47
|
6 |
|
} |
|
|
|
|
48
|
|
|
|
49
|
6 |
|
/** |
50
|
6 |
|
* Register the service provider. |
51
|
3 |
|
* |
52
|
3 |
|
* @throws \ErrorException |
53
|
3 |
|
*/ |
|
|
|
|
54
|
|
|
public function register() |
55
|
1 |
|
{ |
|
|
|
|
56
|
1 |
|
$this->mergeConfigFrom(__DIR__ . '/../Config/config.php', 'prometheus-exporter'); |
|
|
|
|
57
|
1 |
|
|
58
|
2 |
|
$this->registerAdapter(); |
59
|
1 |
|
$this->registerMiddlewareForRequestMetrics(); |
60
|
1 |
|
|
61
|
1 |
|
$this->app->bind( |
62
|
1 |
|
PrometheusExporterContract::class, |
63
|
1 |
|
PrometheusExporter::class, |
64
|
|
|
true |
65
|
|
|
); |
66
|
|
|
} |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
6 |
|
* @throws \ErrorException |
70
|
6 |
|
*/ |
|
|
|
|
71
|
|
|
private function registerAdapter() |
|
|
|
|
72
|
6 |
|
{ |
|
|
|
|
73
|
6 |
|
switch (config('prometheus-exporter.adapter')) { |
74
|
|
|
case 'apc': |
|
|
|
|
75
|
|
|
$this->app->bind(Adapter::class, APC::class); |
76
|
|
|
break; |
|
|
|
|
77
|
|
|
case 'redis': |
|
|
|
|
78
|
|
|
$this->app->bind(Adapter::class, function () { |
|
|
|
|
79
|
|
|
return new Redis(config('prometheus-exporter.redis')); |
80
|
|
|
}); |
|
|
|
|
81
|
|
|
break; |
|
|
|
|
82
|
|
|
case 'push': |
|
|
|
|
83
|
|
|
$this->app->bind(Adapter::class, APC::class); |
84
|
|
|
break; |
|
|
|
|
85
|
|
|
case 'inmemory': |
|
|
|
|
86
|
|
|
$this->app->bind(Adapter::class, InMemory::class); |
87
|
|
|
break; |
|
|
|
|
88
|
|
|
default: |
|
|
|
|
89
|
|
|
throw new \ErrorException('"prometheus-exporter.adapter" must be either apc or redis'); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
|
|
|
|
92
|
|
|
|
93
|
|
|
private function registerMiddlewareForRequestMetrics() |
|
|
|
|
94
|
|
|
{ |
|
|
|
|
95
|
|
|
if (class_exists('Illuminate\Foundation\Application', false)) { |
96
|
|
|
/** @var Router $router */ |
|
|
|
|
97
|
|
|
$router = $this->app['router']; |
98
|
|
|
$router->aliasMiddleware('lpe.requestPerRoute', RequestPerRoute::class); |
99
|
|
|
} |
100
|
|
|
} |
|
|
|
|
101
|
|
|
} |
|
|
|
|
102
|
|
|
|