Generates Metrics Collector for PHP 8.4. Creates MetricsCollectorInterface, Counter/Gauge/Histogram wrappers, PrometheusMetricsCollector, MetricsMiddleware for RED metrics. Includes unit tests.
From accnpx claudepluginhub dykyi-roman/awesome-claude-code --plugin accThis skill uses the workspace's default tool permissions.
references/examples.mdreferences/templates.mdCreates metrics collection infrastructure for application observability and monitoring.
| Scenario | Example |
|---|---|
| Request monitoring | Track request rate, errors, duration (RED) |
| Business metrics | Count orders, revenue, active users |
| Performance profiling | Histogram of response times |
| Health dashboards | Expose /metrics for Prometheus scraping |
/metrics endpoint exposing Prometheus formattext/plain with all registered metricsPath: src/Infrastructure/Metrics/
MetricsCollectorInterface.php — Metrics contractCounter.php — Counter metric wrapperGauge.php — Gauge metric wrapperHistogram.php — Histogram metric wrapperPath: src/Infrastructure/Metrics/
PrometheusMetricsCollector.php — Prometheus adapterNullMetricsCollector.php — Null Object for testingPath: src/Infrastructure/Metrics/
MetricsMiddleware.php — PSR-15 RED metrics middlewareMetricsAction.php — /metrics endpoint actionNullMetricsCollectorTest.php — Null object behavior testsMetricsMiddlewareTest.php — Middleware metrics tests| Component | Path |
|---|---|
| All Classes | src/Infrastructure/Metrics/ |
| Unit Tests | tests/Unit/Infrastructure/Metrics/ |
| Component | Pattern | Example |
|---|---|---|
| Interface | MetricsCollectorInterface | MetricsCollectorInterface |
| Counter | Counter | Counter |
| Gauge | Gauge | Gauge |
| Histogram | Histogram | Histogram |
| Prometheus | PrometheusMetricsCollector | PrometheusMetricsCollector |
| Null Object | NullMetricsCollector | NullMetricsCollector |
| Middleware | MetricsMiddleware | MetricsMiddleware |
| Action | MetricsAction | MetricsAction |
| Test | {ClassName}Test | MetricsMiddlewareTest |
interface MetricsCollectorInterface
{
/** @param array<string, string> $labels */
public function increment(string $name, array $labels = [], float $value = 1.0): void;
/** @param array<string, string> $labels */
public function gauge(string $name, float $value, array $labels = []): void;
/** @param array<string, string> $labels */
public function histogram(string $name, float $value, array $labels = []): void;
}
final readonly class MetricsMiddleware implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface;
}
final readonly class NullMetricsCollector implements MetricsCollectorInterface
{
public function increment(string $name, array $labels = [], float $value = 1.0): void {}
public function gauge(string $name, float $value, array $labels = []): void {}
public function histogram(string $name, float $value, array $labels = []): void {}
}
// Middleware auto-collects RED metrics
$middleware = new MetricsMiddleware($collector);
// Manual metrics in use cases
$collector->increment('orders_created_total', ['channel' => 'web']);
$collector->gauge('active_connections', $pool->activeCount());
$collector->histogram('payment_duration_seconds', $elapsed, ['gateway' => 'stripe']);
// Expose via /metrics endpoint
$app->get('/metrics', new MetricsAction($collector));
Rate ──→ http_requests_total{method, path, status}
Errors ──→ http_requests_total{status=~"5.."}
Duration ──→ http_request_duration_seconds{method, path}
| Anti-pattern | Problem | Solution |
|---|---|---|
| High cardinality labels | Prometheus storage explosion | Limit label values, no user IDs |
| No null implementation | Cannot disable metrics in tests | NullMetricsCollector |
| Inline metric names | Typos, inconsistency | Define constants or enum |
| Missing error metrics | Cannot calculate error rate | Track status codes |
| No histogram buckets | Default buckets don't fit use case | Configure per metric |
| Metrics in domain layer | Infrastructure leak | Keep in infrastructure only |
For complete PHP templates and examples, see:
references/templates.md — MetricsCollectorInterface, Counter, Gauge, Histogram, Prometheus, Middleware templatesreferences/examples.md — Integration examples and testsProvides UI/UX resources: 50+ styles, color palettes, font pairings, guidelines, charts for web/mobile across React, Next.js, Vue, Svelte, Tailwind, React Native, Flutter. Aids planning, building, reviewing interfaces.