# Symfony

```bash
composer require watchtower15/symfony
```

Flex registers the bundle in `config/bundles.php` from its Composer type. Without Flex, add `WatchTower15\Symfony\WatchTower15Bundle::class => ['all' => true]` to `config/bundles.php`. Create `config/packages/watchtower15.yaml` yourself (below) and add the variables you use to `.env`. The bundle needs Symfony 6.4, 7.4 or 8.x (8.x on PHP 8.4 or later). Then check the setup:

```bash
php bin/console watchtower15:test
```

## Configuration

Every key is optional. The config root is `watchtower15`.

```yaml
watchtower15:
    api_key: '%env(default::WT15_API_KEY)%'
    metrics:
        token: '%env(default::WT15_METRICS_TOKEN)%'
        store: '%env(default::WT15_METRICS_STORE)%'   # redis://redis:6379 for a fleet; unset: a file under var/
        prefix: shop                                   # required with redis:// and apcu://: one per application
        allowed:                                       # nothing else is sent
            orders.shipped: count
            queue.depth: level
    logs:
        token: '%env(default::WT15_LOGS_TOKEN)%'
```

| Key | Meaning |
| --- | --- |
| `api_key` | The API key, for `WatchTower15\Client` |
| `api_url`, `ingest_url` | Base URLs; unset is the SDK’s default |
| `metrics.token`, `logs.token` | The ingest tokens |
| `metrics.enabled` | `true` by default |
| `metrics.store`, `metrics.prefix` | The metrics store and its prefix |
| `timeout`, `connect_timeout`, `max_retries`, `app_info` | Passed to the application’s client |
| `http_client` | A service id; default the bundle’s own `watchtower15.http_client`; `null` uses the SDK’s cURL transport |
| `metrics.allowed` | A map of metric key to `count` or `level` |
| `metrics.allow_any` | `true` sends any metric key (default `false`) |
| `metrics.heartbeat` | A metric key the flusher adds once a minute |
| `metrics.flush_after_response` | `auto`, `always` or `never` |
| `metrics.schedule` | `true` registers the Scheduler message |

Use `%env(default::NAME)%` so that an unset or empty variable is read as “not set”. A key that is wrong fails `cache:clear`, naming it; a value from the environment cannot be checked then and is refused at run time instead.

`WatchTower15\Client`, `WatchTower15\Metrics\Aggregator`, `Flusher` and `BucketStore` are autowirable:

```php
public function __construct(private readonly \WatchTower15\Client $wt15, private readonly \WatchTower15\Metrics\Aggregator $metrics) {}
// …
$this->metrics->increment('orders.shipped');
$page = $this->wt15->servers->list(['health' => ['critical']]);
```

A bad API key throws to the code that asks for the `Client`; the metrics pipe and the logs handler have clients of their own that never throw: a bad value is counted and the buckets are kept.

## The HTTP client

The bundle sends through its own `watchtower15.http_client`, a Symfony HTTP client that never follows redirects and is traced by the profiler. It is not your application’s `http_client`. In a development profile the profiler stores each traced request’s headers, `Authorization` included: do not share profiles.

## The metrics store

`metrics.store` is a DSN:

| DSN | Shared by |
| --- | --- |
| unset: a file under `var/watchtower15` | FPM and the CLI on one host |
| `file:///absolute/folder` | one host |
| `redis://[user:password@]host[:port][/db]`, `rediss://…`, `redis:///path/to.sock` | a fleet |
| `apcu://` | one SAPI only |
| `memory://` | one process (console commands and tests) |

Redis needs `ext-redis` 6.3 or later, or `predis/predis`, and one primary: Redis Cluster, `RedisArray` and Sentinel are refused. `metrics.prefix` is required with `redis://` and `apcu://`, one per application. APCu is per SAPI, so it is flushed from requests only. A Messenger worker keeps the default file store, flushed by the Scheduler or cron. A store the bundle refuses disables the metrics pipe with its reason and throws nothing.

## Flushing

-   **After the response.** `flush_after_response: auto` flushes on `kernel.terminate` where the response is already finished (PHP-FPM, FrankenPHP, LiteSpeed), and on `console.terminate` of every command but `watchtower15:flush`. One flush a minute runs per shared store.
    
-   **Every minute.** With `symfony/scheduler` the bundle registers the schedule `watchtower15`; run it as the web server’s user: `php bin/console messenger:consume scheduler_watchtower15`. Without the Scheduler, put this in the web server’s user’s crontab on each host:
    
    ```plaintext
    * * * * * cd /app && php bin/console watchtower15:flush --if-due
    ```
    

Run cron jobs and workers as the same user as PHP-FPM, or they cannot write each other’s store files.

## Logs

With MonologBundle (and Monolog 3), the service `watchtower15.monolog_handler` is the SDK’s handler. Put it inside a `buffer` handler and exclude the channel `watchtower15`:

```yaml
monolog:
    handlers:
        watchtower15:
            type: buffer
            handler: watchtower15_send
            level: warning
            buffer_size: 100
            flush_on_overflow: true
            channels: ['!watchtower15']
        watchtower15_send:
            type: service
            id: watchtower15.monolog_handler
            nested: true
```

The push runs after the response. A Messenger worker, or a worker-mode runtime such as FrankenPHP’s, sends the buffer after each unit of work. See [logs](https://developer.watchtower15.com/sdks/php/logs/).

## Commands

-   `php bin/console watchtower15:test` checks each configured credential (the API key with `me()`, the metrics token with the source’s listing, the logs token by its format) and prints the store, the allow-list, the flush paths and the log handler. Exit 2 when nothing is configured or a value is malformed, 1 when a check failed, else 0.
-   `php bin/console watchtower15:flush` sends the closed minute buckets now. `--if-due` takes the same minute claim as the Scheduler and prints “Not due” when another process holds it: this is the cron line’s form.

Source: https://developer.watchtower15.com/sdks/php/symfony/
