cors / prometheus

CORS - Prometheus

1.1.0 2024-02-21 15:50 UTC

This package is auto-updated.

Last update: 2024-09-21 17:30:06 UTC


README

CORS.gmbh

CORS Pimcore Prometheus

将 Pimcore 指标暴露给 Prometheus。这包括以下指标

  • 已安装包及其版本
  • Pimcore 网站
  • 表及其行数
  • Symfony Messenger 处理的消息数

安装

  1. 安装并启用该包
composer require cors/prometheus
bin/console pimcore:bundle:enable CORSPrometheusBundle
  1. 配置路由
# app/routes.yaml

_cors_prometheus:
  resource: "@CORSPrometheusBundle/Resources/config/routing.yaml"

请确保不要将 /metrics 路由公开访问!我们通过 nginx 配置来实现这一点

location ~* /metrics {
  deny all;
  return 403;
}

您还可以添加一个使用密钥保护路由的事件监听器

<?php

declare(strict_types=1);

namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

final class MetricsListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => 'request',
        ];
    }

    public function request(KernelEvent $event): void
    {
        if ('cors_prometheus' === $event->getRequest()->attributes->get('_route')) {
            if ($event->getRequest()->query->get('apiKey') !== 'your-super-secret-key') {
                throw new NotFoundHttpException('Access denied');
            }
        }
    }
}

存储

同时,请确保您有某种类型的存储来临时存储指标。我们使用 Redis 实例来实现。

Prometheus\Storage\Adapter:
    factory: [ 'CORS\Bundle\PrometheusBundle\StorageFactory', 'create' ]
    arguments:
        $dsn: 'redis://redis:6379'