Repository: LKaemmerling/laravel-horizon-prometheus-exporter Branch: master Commit: 9574e946bb41 Files: 30 Total size: 28.1 KB Directory structure: gitextract_ur9a7upc/ ├── .editorconfig ├── .gitattributes ├── .github/ │ └── workflows/ │ └── tests.yml ├── .gitignore ├── .styleci.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config/ │ ├── .gitkeep │ └── config.php ├── phpunit.xml.dist ├── routes/ │ └── api.php ├── src/ │ ├── Contracts/ │ │ └── Exporter.php │ ├── Exporter/ │ │ ├── CurrentMasterSupervisors.php │ │ ├── CurrentProcessesPerQueue.php │ │ ├── CurrentWorkload.php │ │ ├── FailedJobsPerHour.php │ │ ├── HorizonStatus.php │ │ ├── JobsPerMinute.php │ │ └── RecentJobs.php │ ├── HorizonPrometheusExporterServiceProvider.php │ ├── Http/ │ │ ├── Controller/ │ │ │ └── HorizonPrometheusExporterController.php │ │ └── Middleware/ │ │ └── IPWhitelistingMiddleware.php │ └── Repository/ │ └── ExporterRepository.php └── tests/ ├── Http/ │ ├── Controller/ │ │ └── HorizonPrometheusExporterControllerTest.php │ └── Middleware/ │ └── IPWhitelistingMiddlewareTest.php ├── Repository/ │ └── ExporterRepositoryTest.php ├── TestCase.php └── Util/ └── NoopExporter.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ ; This file is for unifying the coding style for different editors and IDEs. ; More information at https://editorconfig.org root = true [*] charset = utf-8 indent_size = 4 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false [*.yml] indent_size = 2 ================================================ FILE: .gitattributes ================================================ # Path-based git attributes # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". /.gitattributes export-ignore /.gitignore export-ignore /.travis.yml export-ignore /phpunit.xml.dist export-ignore /.scrutinizer.yml export-ignore /tests export-ignore /.editorconfig export-ignore ================================================ FILE: .github/workflows/tests.yml ================================================ name: Tests on: - push - pull_request jobs: test: runs-on: ${{ matrix.os }} strategy: fail-fast: true matrix: os: [ubuntu-latest] php: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] dependency-version: [prefer-stable] redis-version: [6] name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} steps: - name: Checkout code uses: actions/checkout@v2 - name: Cache dependencies uses: actions/cache@v2 with: path: ~/.composer/cache/files key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - name: Install dependencies run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest - name: Execute tests run: vendor/bin/phpunit ================================================ FILE: .gitignore ================================================ build composer.lock docs vendor coverage .phpunit.result.cache ================================================ FILE: .styleci.yml ================================================ preset: laravel disabled: - single_class_element_per_statement - self_accessor ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing Contributions are **welcome** and will be fully **credited**. Please read and understand the contribution guide before creating an issue or pull request. ## Etiquette This project is open source, and as such, the maintainers give their free time to build and maintain the source code held within. They make the code freely available in the hope that it will be of use to other developers. It would be extremely unfair for them to suffer abuse or anger for their hard work. Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the world that developers are civilized and selfless people. It's the duty of the maintainer to ensure that all submissions to the project are of sufficient quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. ## Viability When requesting or submitting new features, first consider whether it might be useful to others. Open source projects are used by many developers, who may have entirely different needs to your own. Think about whether or not your feature is likely to be used by other users of the project. ## Procedure Before filing an issue: - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. - Check to make sure your feature suggestion isn't already present within the project. - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. - Check the pull requests tab to ensure that the feature isn't already in progress. Before submitting a pull request: - Check the codebase to ensure that your feature doesn't already exist. - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. ## Requirements If the project maintainer has any additional requirements, you will find them listed here. - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). - **Add tests!** - Your patch won't be accepted if it doesn't have tests. - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. **Happy coding**! ================================================ FILE: LICENSE.md ================================================ The MIT License (MIT) Copyright (c) LK-Development Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Laravel Horizon Prometheus Exporter [![Latest Version on Packagist](https://img.shields.io/packagist/v/lkaemmerling/laravel-horizon-prometheus-exporter.svg?style=flat-square)](https://packagist.org/packages/lkaemmerling/laravel-horizon-prometheus-exporter) [![Actions Status](https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter/workflows/Tests/badge.svg)](https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter/actions) [![Total Downloads](https://img.shields.io/packagist/dt/lkaemmerling/laravel-horizon-prometheus-exporter.svg?style=flat-square)](https://packagist.org/packages/lkaemmerling/laravel-horizon-prometheus-exporter) This package allows an easy way to expose the Laravel Horizon Metrics to Prometheus. ## Prom... What? Prometheus is a scraping service which allows you to easily store and scrape information from your application, server or even from your router! Prometheus itself does not know about your application, so you need a exporter on your app. This small package is exactly this, an exporter which allows Prometheus to understand some information from your application. With Prometheus and a visualisation tool called `Grafana` you can build something like this beautiful Dashboard: ![Laravel Horizon Prometheus Exporter Dashboard](https://pbs.twimg.com/media/EHdSoNGX4AEpbia?format=jpg&name=4096x4096) ## Installation You can install the package via composer: ```bash composer require lkaemmerling/laravel-horizon-prometheus-exporter ``` ## Configuration ```bash php artisan vendor:publish --provider=LKDevelopment\\HorizonPrometheusExporter\\HorizonPrometheusExporterServiceProvider ``` You can configure this package by changing the values in `config/horizon-exporter.php`. ## Custom Metrics You can also use this package easily to expose custom metrics. You just need to implement the `LKDevelopment\HorizonPrometheusExporter\Contracts\Exporter` interface and then add your implementation to your `config/horizon-exporter.php` like we do it for the Horizon exporters: https://github.com/LKaemmerling/laravel-horizon-prometheus-exporter/blob/master/config/config.php#L17 ## Dashboard You can find a sample dashboard using this metrics on the [Grafana Marketplace](https://grafana.com/grafana/dashboards/11034). ### Testing ``` bash composer test ``` ### Changelog Please see [Releases](https://github.com/LKaemmerling/laravel-horizon-prometheus-exporter/releases) for more information on what has changed recently. ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ### Security If you discover any security related issues, please email kontakt@lukas-kaemmerling.de instead of using the issue tracker. ## Credits - [Lukas Kämmerling](https://github.com/LKaemmerling) - [All Contributors](../../contributors) ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. ================================================ FILE: composer.json ================================================ { "name": "lkaemmerling/laravel-horizon-prometheus-exporter", "description": "A small package to gain and export long time information from Laravel & Horizon for Prometheus.", "keywords": [ "laravel-horizon-prometheus-exporter", "horizon", "laravel", "prometheus", "exporter" ], "homepage": "https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter", "license": "MIT", "authors": [ { "name": "Lukas Kämmerling", "email": "kontakt@lukas-kaemmerling.de", "homepage": "https://lukas-kaemmerling.de", "role": "Developer" } ], "require": { "php": "^7.1|^8.0", "promphp/prometheus_client_php": "^1.0.3|^2.0.0", "illuminate/routing": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0", "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0", "illuminate/config": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0", "laravel/horizon": "^4.0|^5.0" }, "require-dev": { "orchestra/testbench": "^v4.9|^5.3|^6.3|^7.0|^8.0|^9.0|^10.0|^11.0", "phpunit/phpunit": "^8.2|^9.0|^10.5|^11.5.3|^12.5.12", "symfony/var-dumper": "^4.3|^5.1|^7.2" }, "autoload": { "psr-4": { "LKDevelopment\\HorizonPrometheusExporter\\": "src" } }, "autoload-dev": { "psr-4": { "LKDevelopment\\HorizonPrometheusExporter\\Tests\\": "tests" } }, "scripts": { "test": "vendor/bin/phpunit", "test-coverage": "vendor/bin/phpunit --coverage-html coverage" }, "config": { "sort-packages": true }, "extra": { "laravel": { "providers": [ "LKDevelopment\\HorizonPrometheusExporter\\HorizonPrometheusExporterServiceProvider" ] } } } ================================================ FILE: config/.gitkeep ================================================ ================================================ FILE: config/config.php ================================================ 'app', "enabled" => env('HORIZON_PROMETHEUS_EXPORTER_ENABLED', true), /** * You can change the default endpoint to something other than metrics. * Keep in mind that the change needs to be reflected in your Prometheus configuration as well. */ "url" => 'metrics', /** * You can enable or disable or even create own exporters by simply implementing the LKDevelopment\HorizonPrometheusExporter\Contracts\Exporter Contract. * If you want to disable oder enable a Exporter just comment the specific line out. * If you want to add your own Exporter just add the Class Name to this array */ "exporters" => [ \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentMasterSupervisors::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\JobsPerMinute::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentWorkload::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentProcessesPerQueue::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\FailedJobsPerHour::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\HorizonStatus::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\RecentJobs::class ], /** * IP Whitelisting, you may don't want to expose your metrics on the internet so you can add the IP addresses of your Prometheus Server here. */ "ip_whitelist" => [ // Keep empty to allow all IP addresses ], /** * You can change the Middleware which is used for the IP whitelisting. You can add your own, like a token based authentication. */ "middleware" => \LKDevelopment\HorizonPrometheusExporter\Http\Middleware\IPWhitelistingMiddleware::class, /** * Allow storage to be wiped after a render of data in metrics controller */ "wipe_storage_after_render" => false, ]; ================================================ FILE: phpunit.xml.dist ================================================ tests src/ ================================================ FILE: routes/api.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_current_mastersupervisors', 'Number of mastersupervisors' ); } public function collect(): void { $number = count(app(MasterSupervisorRepository::class)->all()); $this->gauge->set($number); } } ================================================ FILE: src/Exporter/CurrentProcessesPerQueue.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_current_processes', 'Current processes of all queues', ['queue'] ); } public function collect(): void { $workloadRepository = app(WorkloadRepository::class); $workloads = collect($workloadRepository->get())->sortBy('name')->values(); $workloads->each(function ($workload) { $this->gauge->set($workload['processes'], [$workload['name']]); }); } } ================================================ FILE: src/Exporter/CurrentWorkload.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_current_workload', 'Current workload of all queues', ['queue'] ); } public function collect(): void { $workloadRepository = app(WorkloadRepository::class); $workloads = collect($workloadRepository->get())->sortBy('name')->values(); $workloads->each(function ($workload) { if (isset($workload['split_queues']) && $workload['split_queues']) { $workload['split_queues']->each(function ($queue) { $this->gauge->set($queue['length'], [$queue['name']]); }); return; } $this->gauge->set($workload['length'], [$workload['name']]); }); } } ================================================ FILE: src/Exporter/FailedJobsPerHour.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_failed_jobs', 'The number of recently failed jobs' ); } public function collect(): void { $this->gauge->set(app(JobRepository::class)->countRecentlyFailed()); } } ================================================ FILE: src/Exporter/HorizonStatus.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_status', 'The status of Horizon, -1 = inactive, 0 = paused, 1 = running' ); } public function collect(): void { $status = -1; if ($masters = app(MasterSupervisorRepository::class)->all()) { $status = collect($masters)->contains(function ($master) { return $master->status === 'paused'; }) ? 0 : 1; } $this->gauge->set($status); } } ================================================ FILE: src/Exporter/JobsPerMinute.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_jobs_per_minute', 'The number of jobs per minute' ); } public function collect(): void { $this->gauge->set(app(MetricsRepository::class)->jobsProcessedPerMinute()); } } ================================================ FILE: src/Exporter/RecentJobs.php ================================================ gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'horizon_recent_jobs', 'The number of recent jobs' ); } public function collect(): void { $this->gauge->set(app(JobRepository::class)->countRecent()); } } ================================================ FILE: src/HorizonPrometheusExporterServiceProvider.php ================================================ app->runningInConsole()) { $this->publishes([ __DIR__ . '/../config/config.php' => config_path('horizon-exporter.php'), ], 'config'); } $this->app->booted(function () { $this->routes(); }); } /** * Register the application services. */ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'horizon-exporter'); } protected function routes(): void { if ($this->app->routesAreCached()) { return; } if (config('horizon-exporter.enabled')) { Route::middleware(config('horizon-exporter.middleware'))->group( __DIR__ . '/../routes/api.php' ); } } } ================================================ FILE: src/Http/Controller/HorizonPrometheusExporterController.php ================================================ render(ExporterRepository::getRegistry()->getMetricFamilySamples()); if(config('horizon-exporter.wipe_storage_after_render', false)) { ExporterRepository::getRegistry()->wipeStorage(); } return new Response($result, Response::HTTP_OK, ["Content-Type" => RenderTextFormat::MIME_TYPE]); } } ================================================ FILE: src/Http/Middleware/IPWhitelistingMiddleware.php ================================================ ip(); if (IpUtils::checkIp($clientIp, config('horizon-exporter.ip_whitelist'))) { return $next($request); } else { abort(403); } } else { return $next($request); } } } ================================================ FILE: src/Repository/ExporterRepository.php ================================================ metrics(self::$registry); $_exporter->collect(); } } /** * @param CollectorRegistry $collectorRegistry */ public static function setRegistry(CollectorRegistry $collectorRegistry): void { self::$registry = $collectorRegistry; } /** * @return CollectorRegistry */ public static function getRegistry() { return self::$registry; } } ================================================ FILE: tests/Http/Controller/HorizonPrometheusExporterControllerTest.php ================================================ metrics(); $expected = <<handle(new Request([], [], [], [], [], ['REMOTE_ADDR' => $requestingIP]), \Closure::fromCallable(function ($next) { return new Response(); }))->getStatusCode(); } catch (HttpException $httpException) { $statusCode = $httpException->getStatusCode(); } self::assertEquals($expectedStatusCode, $statusCode); } public function testCases() { return [ [ "127.0.0.1", // Requesting IP Response::HTTP_OK // Expected Status Code ], [ "127.0.0.2", Response::HTTP_FORBIDDEN ], [ "10.0.0.1", Response::HTTP_OK ], [ "10.0.1.1", Response::HTTP_FORBIDDEN ] ]; } } ================================================ FILE: tests/Repository/ExporterRepositoryTest.php ================================================ getCounter("app", "noop_metric"); self::assertNotNull($counter); } } ================================================ FILE: tests/TestCase.php ================================================ set('horizon-exporter.exporters', [NoopExporter::class]); $app['config']->set('horizon-exporter.ip_whitelist', ["127.0.0.1", "10.0.0.0/24"]); } } ================================================ FILE: tests/Util/NoopExporter.php ================================================ counter = $collectorRegistry->getOrRegisterCounter( "app", 'noop_metric', 'noop metric', ["op"] ); } /** * @inheritDoc */ public function collect(): void { $this->counter->inc(["op" => "noop"]); } }