renoki-co/laravel-exporter-contracts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

renoki-co/laravel-exporter-contracts

Composer 安装命令:

composer require renoki-co/laravel-exporter-contracts

包简介

Base contracts implementation for Prometheus exports in Laravel.

README 文档

README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Base contracts implementation for Prometheus exports in Laravel.

🤝 Supporting

If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦

🚀 Installation

You can install the package via composer:

composer require renoki-co/laravel-exporter-contracts

Publish the config:

$ php artisan vendor:publish --provider="RenokiCo\LaravelExporter\LaravelExporterServiceProvider" --tag="config"

🙌 Usage

All you have to do is to create a \RenokiCo\LaravelExporter\Metric class that defines how the values will update on each Prometheus call to scrap, and the definition of the collector.

By default, metrics are available on the /exporter/group/metrics endpoint and you can point Prometheus towards it for scraping. (i.e. http://localhost/exporter/group/metrics)

You can choose one of the following classes to extend:

  • \RenokiCo\LaravelExporter\GaugeMetric for gauges
  • \RenokiCo\LaravelExporter\CounterMetric for counters

For example, you can define gauges for users:

use RenokiCo\LaravelExporter\GaugeMetric;

class DatabaseUsers extends GaugeMetric
{
    /**
     * Get the metric help.
     *
     * @return string
     */
    protected function help(): string
    {
        return 'Get the total amount of users.';
    }

    /**
     * Perform the update call on the collector.
     * Optional, as some metrics can be modified somewhere else.
     *
     * @return void
     */
    public function update(): void
    {
        $this->set(User::count());
    }
}

In your AppServiceProvider's boot() method, register your metric:

use RenokiCo\LaravelExporter\Exporter;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Exporter::register(DatabaseUsers::class);
    }

    // ...
}

You don't need to set the values. When the Prometheus scraper will make the request for metrics, the gauge will automatically be set.

Labelling

You may label data by setting default values (i.e. server name, static data, etc.) and on-update.

class DatabaseRecords extends GaugeMetric
{
    /**
     * Define the default labels with their values.
     *
     * @return array
     */
    protected function defaultLabels(): array
    {
        return [
            'static_label' => 'static-value',
        ];
    }

    /**
     * Get the metric allowed labels.
     *
     * @return array
     */
    protected function allowedLabels(): array
    {
        return [
            'model',
            'static_label',
        ];
    }

    /**
     * Perform the update call on the collector.
     * Optional, as some metrics can be modified somewhere else.
     *
     * @return void
     */
    public function update(): void
    {
        $models = [
            User::class,
            Post::class,
            Team::class,
        ];

        foreach ($models as $model) {
            $this->set(
                value: $model::count(),
                labels: ['model' => $model,
            ]);
        }
    }
}

Grouping

If you wish to have separate endpoints for different metrics, consider specifying it in the $showsOnGroup property:

class CustomMetrics extends Metric
{
    /**
     * The group this metric gets shown into.
     *
     * @var string|null
     */
    public static $showsOnGroup = 'metrics-reloaded';

    // ...
}

Under the hood, Laravel Exporter registers a route that allows you to scrape any group:

Route::get('/exporter/group/{group?}', ...);

To scrape this specifc metric (and other metrics that are associated with this group), the endpoint is /exporter/group/metrics-reloaded (i.e. http://localhost/exporter/group/metrics-reloaded).

Sending string responses

In some cases you may want to export the string-alike, Prometheus-formatted response without using the internal metrics.

To do so, use the exportResponse function. The function will return the response directly instead of relying on the Metric class and subsequent class registrations via the Exporter::register() function.

use RenokiCo\LaravelExporter\Exporter;

Exporter::exportResponse(function () {
    return <<<'PROM'
    # TYPE openswoole_max_conn gauge
    openswoole_max_conn 256
    # TYPE openswoole_coroutine_num gauge
    openswoole_coroutine_num 1
    PROM;
);

When accessing the /metrics endpoint, the response will be the one declared earlier.

For custom groups, pass a second parameter with the group name:

use RenokiCo\LaravelExporter\Exporter;

Exporter::exportResponse(..., 'metrics-reloaded');

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits

renoki-co/laravel-exporter-contracts 适用场景与选型建议

renoki-co/laravel-exporter-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 174.34k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「Metrics」 「laravel」 「exporter」 「prometheus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 renoki-co/laravel-exporter-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 renoki-co/laravel-exporter-contracts 我们能提供哪些服务?
定制开发 / 二次开发

基于 renoki-co/laravel-exporter-contracts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 174.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 14
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-09-27