3slab/tweedegolf-prometheus-bundle
Symfony bundle integrating the Tweede Golf Prometheus client. Configure a /metrics endpoint, choose a storage adapter, and define counters/gauges/histograms via YAML collectors. Update metrics through the CollectorRegistry service in your app.
Install the Bundle
composer require tweedegolf/prometheus-bundle
Register the Bundle
Add to config/bundles.php (Symfony 4+):
return [
// ...
TweedeGolf\PrometheusBundle\TweedeGolfPrometheusBundle::class => ['all' => true],
];
Expose Metrics Endpoint
Add to config/routes.yaml:
tweede_golf_prometheus:
resource: "@TweedeGolfPrometheusBundle/Resources/config/routing.yml"
prefix: /
Verify Endpoint
Access /metrics in your browser or via curl to confirm the Prometheus endpoint is live.
Define a Collector
Configure in config/packages/tweede_golf_prometheus.yaml:
tweede_golf_prometheus:
collectors:
http_requests:
counter:
labels: [method, endpoint]
help: "Number of HTTP requests by method and endpoint"
Increment Metrics in Code
use TweedeGolf\PrometheusClient\CollectorRegistry;
public function someAction(Request $request, CollectorRegistry $registry)
{
$metric = $registry->getCounter('http_requests');
$metric->inc([$request->getMethod(), $request->getPathInfo()]);
}
Define Metrics in Config
Use collectors to declare counters, gauges, or histograms with labels and help text.
Example:
tweede_golf_prometheus:
collectors:
database_queries:
histogram:
labels: [query_type]
help: "Database query execution time (seconds)"
buckets: [0.1, 0.5, 1, 2.5, 5]
Inject and Use CollectorRegistry
Autowire the service in controllers/services:
public function logQueryTime(CollectorRegistry $registry, float $duration, string $type)
{
$histogram = $registry->getHistogram('database_queries');
$histogram->observe([$type], $duration);
}
Leverage Symfony Events
Attach collectors to events (e.g., kernel.request) for automatic metrics:
services:
app.prometheus.request_collector:
class: App\Service\Prometheus\RequestCollector
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
- { name: tweede_golf_prometheus.collector }
Dynamic Collectors
Register collectors programmatically via CollectorRegistry:
$registry->createGauge('dynamic_gauge', 'Dynamic gauge example', ['label1']);
Custom Storage Adapters
Override the default ApcuAdapter for production (e.g., Redis):
tweede_golf_prometheus:
storage_adapter_service: app.prometheus.redis_adapter
Define the service:
services:
app.prometheus.redis_adapter:
class: App\Prometheus\RedisAdapter
arguments: ['@redis']
public: true
Integration with Monolog Log metrics to Prometheus via Monolog handlers (requires custom handler).
Deprecated Symfony Features
services:
TweedeGolf\PrometheusClient\CollectorRegistry: ~
Storage Adapter Limitations
ApcuAdapter is memory-bound and unsuitable for clustered environments.Label Cardinality Explosion
user_id) can bloat metrics.endpoint, status_code).Metric Naming Conflicts
requests counters) will overwrite each other.api_requests, cli_requests).Verify Metrics Endpoint
/metrics for 404 or malformed output.metrics_path is correctly configured and the route is exposed.Missing Metrics
CollectorRegistry service is properly injected.dump($registry->getCollectors());
Storage Adapter Issues
APCu not installed) will break metrics.pecl install apcu
Custom Collectors
Implement TweedeGolf\PrometheusClient\Collector\CollectorInterface:
class CustomCollector implements CollectorInterface
{
public function collect(): array { /* ... */ }
}
Register with:
tags:
- { name: tweede_golf_prometheus.collector }
Modify Metrics Mid-Flight
Use CollectorRegistry to update metrics dynamically:
$gauge = $registry->getGauge('memory_usage');
$gauge->set(memory_get_usage(true) / 1024 / 1024); // MB
Prometheus Relabeling Extend the bundle to support relabeling rules (requires custom middleware).
response_times:
histogram:
buckets: [0.05, 0.1, 0.25, 0.5, 1, 2.5, 5]
active_users), not accumulators.promtool to validate metrics:
docker run --rm prom/prometheus tools/promtool check metrics
How can I help you explore Laravel packages today?