## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require api-insight/metrics-bundle
config/bundles.php:
ApiInsight\ApiInsightBundle::class => ['all' => true],
config/packages/api_insight.yaml:
api_insight:
enabled: true
storage: memory
config/routes.yaml:
api_insight:
resource: '@ApiInsightBundle/Resources/config/routes.yaml'
php bin/console cache:clear
Access metrics immediately via:
curl http://your-api.com/metrics
Verify real-time API call tracking (e.g., total_calls, error_rate) for all routes.
Automatic Metrics Collection:
200, 404).avg_duration, min_duration, max_duration).{
"routes": {
"api_users_get": {
"total_calls": 120,
"avg_duration": 0.056,
"status_codes": {"200": 118, "404": 2}
}
}
}
Time-Based Metrics:
/metrics/time:
curl http://your-api.com/metrics/time?period=hour&route=api_users_get
total_calls).avg_duration trends).Conditional Monitoring:
api_insight:
enabled: "%env(bool:API_INSIGHT_ENABLED)%" # Set to `false` in `.env.test`
matcher to ignore specific routes (e.g., health checks) by extending the bundle’s event subscriber.Integration with CI/CD:
curl -X POST http://your-api.com/metrics/reset -H "X-API-Insight-Token: $TOKEN"
error_rate in post-deploy metrics.Memory Storage Limitation:
Route Naming Conflicts:
api_users_get) are unique. Duplicate names may cause metric aggregation issues.php bin/console debug:router | grep api_
Performance Overhead:
curl http://your-api.com/metrics | jq '.global.avg_duration'
total_calls on a single route.Authentication Bypass:
auth.enabled: true, ensure the X-API-Insight-Token header is included in all requests to /metrics. Test with:
curl -I http://your-api.com/metrics # Should return 401 if misconfigured
Verify Bundle Activation:
watch -n 1 'curl http://your-api.com/metrics | jq .global.total_calls'
enabled: true in config.matcher exclusions in the bundle’s event subscriber.Log Collection Issues:
ApiInsightEventListener:
framework:
profiler:
only_exceptions: false
Time-Based Metrics Quirks:
period parameter in /metrics/time defaults to day. For granular data, specify:
curl http://your-api.com/metrics/time?period=minute
year) may return empty data if no calls were made.Custom Metrics:
ApiInsightEvent to log business-specific metrics (e.g., cart abandonment rate):
// src/EventListener/CustomMetricsListener.php
use ApiInsight\ApiInsightBundle\Event\ApiInsightEvent;
class CustomMetricsListener {
public function onApiInsight(ApiInsightEvent $event) {
if ($event->getRouteName() === 'api_cart_checkout') {
$event->addCustomMetric('abandoned_carts', $this->isCartAbandoned());
}
}
}
services.yaml:
services:
App\EventListener\CustomMetricsListener:
tags:
- { name: kernel.event_listener, event: api_insight.collect, method: onApiInsight }
Storage Backend:
MemoryStorage by implementing ApiInsight\ApiInsightBundle\Storage\StorageInterface and configure via:
api_insight:
storage: custom_service_id
Alerting:
/metrics endpoint to trigger alerts (e.g., via Symfony Messenger or external tools like PagerDuty):
curl http://your-api.com/metrics | jq '.global.error_rate > 5' # Example: Alert if >5%
Prometheus Integration (Pro):
api_insight:
prometheus:
enabled: true
/metrics/prometheus (if supported in future versions).
---
How can I help you explore Laravel packages today?