- How do I integrate domnikl/statsd with Laravel’s HTTP middleware to track response times?
- Use Laravel’s middleware to wrap request/response cycles. Inject the StatsD client via the constructor and emit a timer metric (e.g., `response_time`) with tags like `status_code` and `route`. Example: `StatsD::timer('laravel.http.response_time', ['status' => $response->status()])`.
- Does this package support Laravel’s queue job metrics (e.g., execution time, failures)?
- Yes. Hook into Laravel’s `dispatch()` or `failed()` events. Use the StatsD client to log timers for job duration (e.g., `job.execution_time`) and counters for failures (e.g., `job.failed`). Example: `StatsD::increment('job.failed', ['queue' => $job->queue])`.
- What Laravel versions and PHP versions are officially supported by domnikl/statsd?
- The package is tested for Laravel 8.0+ and PHP 8.0+. For Laravel 7.x, use the `^3.0` branch, but PHP 7.4+ is recommended. Check the [GitHub releases](https://github.com/domnikl/statsd-php/releases) for version-specific notes.
- How can I mock StatsD in PHPUnit tests to avoid hitting a real StatsD server?
- Use PHPUnit’s mocking to replace the StatsD client with a test double. Example: `$this->partialMock(StatsD::class, ['send']);` and assert calls to `increment()`, `timer()`, etc. Avoid real network calls in unit tests.
- What’s the best way to handle StatsD server downtime in production? Should I log metrics locally?
- Configure a fallback transport (e.g., TCP or local file logging) via the StatsD client’s `setTransport()` method. For critical metrics, implement a circuit breaker pattern to buffer metrics in memory or a queue until StatsD is available.
- Can I use domnikl/statsd with Prometheus instead of Graphite or Datadog?
- Yes. Use the `prometheus/statsd_exporter` to scrape StatsD metrics and expose them to Prometheus. Configure the StatsD client to send metrics to the exporter’s StatsD-compatible endpoint (default port: `9125`).
- How do I batch metrics to reduce UDP packet overhead in high-traffic Laravel apps?
- Enable batching by setting a delay (e.g., `StatsD::setBatchDelay(100)`) to group metrics into fewer UDP packets. Adjust the delay based on your throughput—shorter delays reduce latency but increase packet volume.
- Are there alternatives to domnikl/statsd for Laravel if I need Prometheus-native metrics?
- For Prometheus-native metrics, consider `prometheus/client_php` (direct scraping) or `spatie/laravel-prometheus`. These avoid StatsD’s aggregation layer and push metrics directly to Prometheus, which may be simpler for some use cases.
- How do I tag metrics consistently across Laravel services (e.g., `service:api`, `environment:prod`)?
- Define a naming convention in your Laravel config (e.g., `config/statsd.php`) and use helper methods or traits to enforce tags. Example: `StatsD::increment('laravel.events.user.created', ['service' => 'auth', 'env' => app()->environment()])`.
- Does domnikl/statsd support custom metric types like sets or histograms beyond counters/timers/gauges?
- The package primarily supports counters, timers, and gauges (StatsD’s core types). For histograms or sets, use a StatsD-compatible backend like Prometheus (via `prometheus/statsd_exporter`) or extend the client to emit custom formats, though this requires backend support.