Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ use Utopia\Usage\Usage;
use Utopia\Usage\Adapter\ClickHouse;

// Configuration is fixed at construction. The adapter is fully stateless —
// the tenant is passed explicitly on every call (see below).
$adapter = new ClickHouse(
// the tenant is passed explicitly on every call (see below). The adapter is
// the entry point; there is no separate facade to wrap it in.
$usage = new ClickHouse(
host: 'clickhouse-server',
username: 'default',
password: '',
Expand All @@ -48,7 +49,6 @@ $adapter = new ClickHouse(
sharedTables: true,
);

$usage = new Usage($adapter);
$usage->setup(); // Creates events, gauges, and daily MV tables
```

Expand All @@ -57,19 +57,17 @@ $usage->setup(); // Creates events, gauges, and daily MV tables
```php
<?php

use Utopia\Usage\Usage;
use Utopia\Usage\Adapter\Database as DatabaseAdapter;

$adapter = new DatabaseAdapter($database); // Utopia\Database\Database instance
$usage = new Usage($adapter);
$usage = new DatabaseAdapter($database); // Utopia\Database\Database instance
$usage->setup();
```

## Multi-tenancy

`Usage` is stateless: every query/mutation takes the tenant as its first
The adapter is stateless: every query/mutation takes the tenant as its first
argument, and `addBatch` carries a `tenant` on each metric row (so one batch can
span tenants). This makes a single `Usage` instance safe to share across
span tenants). This makes a single adapter instance safe to share across
tenants and coroutines.

```php
Expand All @@ -81,8 +79,8 @@ $usage->addBatch([
```

Callers that only ever touch one tenant can bind it once with the `Tenant`
decorator, which forwards to `Usage` with the tenant pre-filled (and stamps it
onto every `addBatch` row):
decorator, which forwards to the adapter with the tenant pre-filled (and stamps
it onto every `addBatch` row):

```php
use Utopia\Usage\Tenant;
Expand Down Expand Up @@ -355,7 +353,7 @@ $usage->purge('project_123', [], Usage::TYPE_GAUGE);

### Creating Custom Adapters

Extend `Utopia\Usage\Adapter` and implement:
Extend `Utopia\Usage\Usage` and implement:

- `getName()`, `setup()`, `healthCheck()`
- `addBatch(array $metrics, string $type, int $batchSize): bool` (each metric carries its own `tenant`)
Expand Down
4 changes: 1 addition & 3 deletions src/Usage/Accumulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public function collect(string $tenant, string $metric, int $value, string $type
if ($value < 0) {
throw new \InvalidArgumentException('Value cannot be negative');
}
if ($type !== Usage::TYPE_EVENT && $type !== Usage::TYPE_GAUGE) {
throw new \InvalidArgumentException("Invalid metric type '{$type}'. Allowed: " . Usage::TYPE_EVENT . ', ' . Usage::TYPE_GAUGE);
}
Usage::assertType($type);

// Hash the full identity so distinct (tenant, metric, type, tags)
// tuples never collide on the key — a raw `:`-join would let
Expand Down
174 changes: 0 additions & 174 deletions src/Usage/Adapter.php

This file was deleted.

Loading
Loading