# CakePHP

```bash
composer require watchtower15/cakephp
```

The plugin installer registers the plugin from its Composer type. Without it, load it in `src/Application.php`:

```php
public function bootstrap(): void
{
    parent::bootstrap();
    $this->addPlugin(\WatchTower15\Cake\CakePlugin::class);
}
```

Then check the setup with `bin/cake watchtower15 test`. It needs CakePHP 5.1 or later on PHP 8.3 or later.

## Configuration

Every key is optional. Credentials come from the environment (`WT15_API_KEY`, `WT15_METRICS_TOKEN`, `WT15_LOGS_TOKEN`, and `WT15_API_URL`, `WT15_INGEST_URL`) unless the `WatchTower15` key of `Configure` names them, which wins.

```php
'WatchTower15' => [
    'metrics' => [
        'cache' => 'redis',                    // a Cache config on the Redis engine, shared by the fleet
        'prefix' => 'shop',                    // required with 'cache': one per application
        'allowed' => ['orders.shipped' => 'count', 'queue.depth' => 'level'],
    ],
],
'Log' => [
    'watchtower15' => [
        'className' => \WatchTower15\Cake\Log\WatchTower15Log::class,
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
    ],
],
```

| Key | Meaning |
| --- | --- |
| `apiKey`, `apiUrl`, `ingestUrl` | The API key, and the two base URLs |
| `timeout`, `connectTimeout`, `maxRetries`, `appInfo` | Passed to the application’s client |
| `httpClient` | `true` (default) sends through CakePHP’s HTTP client; `false` uses the SDK’s cURL transport |
| `metrics.token`, `metrics.enabled` | The metrics token, and the switch for the pipe |
| `metrics.cache`, `metrics.prefix` | A Cache config name for the store, and the application’s prefix |
| `metrics.store` | `file:///absolute/folder` or `memory://`; unset is a folder under `TMP` |
| `metrics.allowed`, `metrics.allowAny` | The allow-list of metric keys and their types, or `true` for any key (not with `metrics.cache`) |
| `metrics.heartbeat` | A metric key the flusher adds once a minute |
| `metrics.flushAfterResponse` | `auto`, `always` or `never` |
| `logs.token` | The logs token |

A value of the wrong type, or a key the plugin does not know, is reported by `bin/cake watchtower15 test`, naming it.

`WatchTower15\Client`, `WatchTower15\Metrics\Aggregator`, `Flusher` and `BucketStore` are in the application’s container, so a controller or command asks for them in its constructor:

```php
public function __construct(private readonly \WatchTower15\Client $wt15, private readonly \WatchTower15\Metrics\Aggregator $metrics) {}
// …
$this->metrics->increment('orders.shipped');
```

## The metrics store

The store is the SDK’s, on one of your Cache configs. A store that is not a Redis, Memcached or APCu config is refused, and the plugin reads the keys you allow (`metrics.allowed` and the heartbeat), because CakePHP’s cache cannot list keys.

| Store | Shared by |
| --- | --- |
| unset: a file under `TMP/watchtower15` | PHP-FPM and the CLI on one host |
| `metrics.store` `file:///absolute/folder` | one host |
| `metrics.cache` on a Redis config | a fleet |
| `metrics.cache` on a Memcached config | a fleet (not tested by the SDK’s own suite) |
| `metrics.cache` on an APCu config | one SAPI only |
| `metrics.store` `memory://` | one process (console commands and tests) |

`metrics.prefix` is required with `metrics.cache`, one per application. Keys live under `wt15_<the config's prefix><metrics.prefix>:`, so a clear of your application’s cache leaves them alone. APCu is per SAPI: the CLI cannot see what PHP-FPM wrote.

## Flushing

-   **After the response.** `flushAfterResponse: auto` flushes on `Server.terminate` where the response is already finished (PHP-FPM, FrankenPHP), and on `Command.afterExecute` of every command but `watchtower15 flush`. One flush a minute runs per shared store.
    
-   **Every minute.** In the web server’s user’s crontab on each host:
    
    ```plaintext
    * * * * * cd /app && bin/cake watchtower15 flush --if-due
    ```
    

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

## Logs

Configure the engine under `Log` as above. It sends one push per request, after the response where PHP-FPM allows it, and never sends the SDK’s own warnings back to WatchTower15. See [logs](https://developer.watchtower15.com/sdks/php/logs/).

## Commands

-   `bin/cake 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 engines. Exit 2 when nothing is configured or a value is malformed, 1 when a check failed, else 0.
-   `bin/cake watchtower15 flush` sends the closed minute buckets now. `--if-due` prints “Not due” when another process holds the minute: this is the cron line’s form.

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