Skip to content

Monitoring

The monitoring system provides metrics collection and system observability.

Overview

The monitoring infrastructure includes:

  • System metrics collection (memory, CPU, server load)
  • Application metrics recording
  • Performance timing tracking
  • Metrics endpoint for external monitoring tools

MetricsService

The MetricsService is responsible for collecting and reporting metrics.

Recording Metrics

php
use App\Service\Monitoring\MetricsService;

class MyService
{
    public function __construct(
        private readonly MetricsService $metricsService
    ) {
    }

    public function doSomething(): void
    {
        $startTime = microtime(true);

        // Your logic here

        $duration = (microtime(true) - $startTime) * 1000;
        $this->metricsService->recordTiming('my_service.operation', $duration);
    }
}

Available Methods

recordMetric(string $name, float $value, array $tags = []): void

Records a generic metric with optional tags.

php
$this->metricsService->recordMetric('api.requests', 1, [
    'endpoint' => '/api/knowledge',
    'method' => 'GET',
]);

incrementCounter(string $name, array $tags = []): void

Increments a counter metric.

php
$this->metricsService->incrementCounter('cache.hits', [
    'cache_type' => 'redis',
]);

recordTiming(string $name, float $milliseconds, array $tags = []): void

Records a timing metric in milliseconds.

php
$this->metricsService->recordTiming('database.query', 45.67, [
    'query_type' => 'select',
]);

getSystemMetrics(): array

Returns current system metrics.

php
$metrics = $this->metricsService->getSystemMetrics();
// Returns:
// [
//   'memory' => [...],
//   'php' => [...],
//   'server' => [...]
// ]

System Metrics

The following system metrics are automatically collected:

Memory Metrics

  • usage: Current memory usage in bytes
  • peak: Peak memory usage in bytes
  • limit: Memory limit from php.ini

PHP Metrics

  • version: PHP version
  • sapi: Server API (cli, fpm, etc.)

Server Metrics

  • load: Server load averages (1min, 5min, 15min)
  • uptime: Server uptime in seconds

Metrics Endpoint

Access metrics via the health check endpoint:

bash
GET /api/health/metrics

Response:

json
{
  "system": {
    "memory": {
      "usage": 12345678,
      "peak": 23456789,
      "limit": "256M"
    },
    "php": {
      "version": "8.2.0",
      "sapi": "fpm"
    },
    "server": {
      "load": {
        "1min": 0.5,
        "5min": 0.3,
        "15min": 0.2
      },
      "uptime": 86400
    }
  },
  "application": [
    {
      "name": "api.requests",
      "value": 1,
      "tags": {
        "endpoint": "/api/knowledge",
        "method": "GET"
      },
      "timestamp": 1234567890
    }
  ],
  "timestamp": "2024-01-01T00:00:00+00:00"
}

Integration with External Tools

Prometheus

To integrate with Prometheus, you can create a custom exporter that reads from the metrics endpoint:

yaml
scrape_configs:
  - job_name: 'yappa-backend'
    metrics_path: '/api/health/metrics'
    static_configs:
      - targets: ['backend:8000']

Grafana

Create dashboards using the metrics data:

  1. Add Prometheus as a data source
  2. Create panels for key metrics:
    • Memory usage over time
    • Request rates
    • Response times
    • Error rates

CloudWatch

For AWS deployments, metrics can be pushed to CloudWatch:

php
// Example CloudWatch integration
$cloudWatch = new CloudWatchClient([...]);

$metrics = $this->metricsService->getMetrics();
foreach ($metrics as $metric) {
    $cloudWatch->putMetricData([
        'Namespace' => 'YappaBackend',
        'MetricData' => [
            [
                'MetricName' => $metric['name'],
                'Value' => $metric['value'],
                'Timestamp' => $metric['timestamp'],
            ],
        ],
    ]);
}

Best Practices

Metric Naming

Use a hierarchical naming convention:

  • service.operation.metric_type
  • Examples:
    • api.knowledge.request_count
    • database.query.duration
    • cache.redis.hit_rate

Tags

Use tags for dimensional data:

php
$this->metricsService->recordMetric('api.request', 1, [
    'endpoint' => '/api/knowledge',
    'method' => 'GET',
    'status' => 200,
]);

Performance Considerations

  • Metrics collection should be lightweight
  • Avoid blocking operations in metric recording
  • Consider sampling for high-frequency metrics
  • Clear metrics periodically to prevent memory issues
php
// Clear metrics after reporting
$this->metricsService->clearMetrics();

Monitoring Checklist

  • [ ] Record key business metrics (requests, errors, etc.)
  • [ ] Track performance metrics (response times, query durations)
  • [ ] Monitor resource usage (memory, CPU)
  • [ ] Set up alerts for critical thresholds
  • [ ] Create dashboards for visualization
  • [ ] Document custom metrics
  • [ ] Review and optimize metric collection overhead