Installation
composer require birkof/statsd-bundle
Add to config/bundles.php:
return [
// ...
Birkof\StatsdBundle\BirkofStatsdBundle::class => ['all' => true],
];
Configure in config/packages/birkof_statsd.yaml
birkof_statsd:
host: 'localhost'
port: 8125
prefix: 'myapp.'
namespace: 'default'
First Metric
Inject StatsdClientInterface and use:
use Birkof\StatsdBundle\Client\StatsdClientInterface;
class MyController
{
public function __construct(private StatsdClientInterface $statsd) {}
public function index()
{
$this->statsd->increment('user.visits');
$this->statsd->timing('user.visit.duration', 150); // ms
}
}
Verify
Check StatsD UI (e.g., Grafana + Prometheus) or use netcat:
nc -ul 8125
Event-Driven Metrics
Bind metrics to Symfony events (e.g., kernel.request):
# config/packages/birkof_statsd.yaml
birkof_statsd:
listeners:
kernel.request: ['user.visits']
kernel.exception: ['errors.total']
Or programmatically:
$this->statsd->addEventListener('kernel.request', 'user.visits');
Contextual Metrics
Use tags (via StatsdClientInterface):
$this->statsd->increment('user.visits', ['source' => 'web', 'user_id' => 123]);
Timing Critical Paths Measure execution time:
$this->statsd->timing('api.response.time', $this->stopwatch->lap());
Gauge for Dynamic Values Track real-time metrics (e.g., queue size):
$this->statsd->gauge('queue.size', count($queue));
Dependency Injection Prefer constructor injection over service locator:
public function __construct(private StatsdClientInterface $statsd) {}
Configuration Overrides Use environment variables for dynamic config:
birkof_statsd:
host: '%env(STATSD_HOST)%'
port: '%env(int:STATSD_PORT)%'
Async vs. Sync StatsD is UDP by default (async). For reliability, consider:
Metric Naming
Use a consistent prefix (e.g., app.) and avoid special chars:
$this->statsd->increment('app.user.auth.success');
UDP Packet Loss StatsD drops packets silently. Mitigate by:
port (e.g., 8125 is default but may be blocked).statsd Docker container) to buffer metrics.Namespace Collisions
Ensure prefix in config is unique to your app to avoid mixing metrics with others.
Event Listener Order Metrics bound to events fire after the event is dispatched. For pre-event metrics, use a custom listener:
$dispatcher->addListener('kernel.request', function () {
$this->statsd->increment('request.started');
});
Deprecated Methods
Avoid StatsdClient (old class). Use StatsdClientInterface for type safety.
Check Connection Test StatsD connectivity:
telnet localhost 8125
Or use nc to monitor traffic:
nc -ul 8125 | strings
Log Metrics Locally
Temporarily log metrics to monolog for debugging:
birkof_statsd:
debug: true
Common Errors
Connection refused: StatsD server not running.prefix and namespace in config.Custom Metric Types
Extend StatsdClientInterface to add custom methods:
$this->statsd->custom('app.custom.metric', $value);
Dynamic Prefixes Override the prefix per request:
$this->statsd->setPrefix("app.{$request->get('env')}.");
StatsD Backend Swap Replace the underlying client (e.g., for testing):
services:
Birkof\StatsdBundle\Client\StatsdClientInterface: '@test.statsd_client'
Batch Processing For high-volume apps, batch metrics before sending:
$this->statsd->flush(); // Force send buffered metrics
How can I help you explore Laravel packages today?