Skip to content

Commit 38a6db4

Browse files
Rename status counters for clarity and add total_received
Rename total_accepted → total_buffered (only counts reports that passed the pause check), add total_received (every 202 response), and update total_dropped to received − forwarded so the full picture is visible via /status.
1 parent 4853beb commit 38a6db4

5 files changed

Lines changed: 46 additions & 22 deletions

File tree

CLAUDE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ k6 run loadtest/loadtest.js
7070
kill %1
7171
```
7272

73-
`--test` mode uses `NullUpstream` — payloads are accepted and flushed through the full pipeline but no HTTP leaves the process. Stats (accepted, forwarded, buffered, memory) are printed every 5 seconds.
73+
`--test` mode uses `NullUpstream` — payloads are accepted and flushed through the full pipeline but no HTTP leaves the process. Stats (received, buffered, forwarded, pending, memory) are printed every 5 seconds.
74+
75+
The `/status` endpoint exposes lifetime counters: `total_received` (all 202 responses), `total_buffered` (passed the pause check), `total_forwarded` (sent upstream successfully), and `total_dropped` (received minus forwarded).
7476

7577
The k6 script ramps from 1→200 VUs over 80 seconds, posting realistic error payloads to `/v1/errors`. Pass `DAEMON_URL` and `API_KEY` env vars to customize.
7678

loadtest/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ k6 run loadtest/loadtest.js
2525
## What `--test` mode does
2626

2727
- Replaces the real upstream with a null upstream that instantly returns `204`
28-
- Prints periodic stats to stdout: accepted/forwarded counts, buffer size, memory usage
28+
- Prints periodic stats to stdout: received/buffered/forwarded counts, pending buffer size, memory usage
2929
- The full buffer and flush pipeline still runs — the only difference is no HTTP leaves the process
3030

3131
## Goals

src/Ingest.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class Ingest
2626

2727
protected QuotaState $quotaState;
2828

29-
protected int $totalAccepted = 0;
29+
protected int $totalReceived = 0;
30+
31+
protected int $totalBuffered = 0;
3032

3133
protected int $totalForwarded = 0;
3234

@@ -64,6 +66,8 @@ public function isShuttingDown(): bool
6466
*/
6567
public function accept(string $apiKey, string $type, array $payload): void
6668
{
69+
$this->totalReceived++;
70+
6771
if ($this->shuttingDown) {
6872
return;
6973
}
@@ -74,7 +78,7 @@ public function accept(string $apiKey, string $type, array $payload): void
7478
return;
7579
}
7680

77-
$this->totalAccepted++;
81+
$this->totalBuffered++;
7882

7983
$this->output->debug('payload accepted', [
8084
'api_key' => $apiKey,
@@ -146,7 +150,13 @@ public function shutdown(?callable $onDrained = null): void
146150
}
147151

148152
/**
149-
* @return array{keys: array<string, array<string, array{buffered: int, paused: bool, retry_after: string|null, last_429_reason: string|null}>>|object}
153+
* @return array{
154+
* total_received: int,
155+
* total_buffered: int,
156+
* total_forwarded: int,
157+
* total_dropped: int,
158+
* keys: array<string, array<string, array{buffered: int, paused: bool, retry_after: string|null, last_429_reason: string|null}>>|object
159+
* }
150160
*/
151161
public function status(): array
152162
{
@@ -156,7 +166,12 @@ public function status(): array
156166
...$this->quotaState->keys(),
157167
]);
158168

159-
$status = [];
169+
$status = [
170+
'total_received' => $this->totalReceived,
171+
'total_buffered' => $this->totalBuffered,
172+
'total_forwarded' => $this->totalForwarded,
173+
'total_dropped' => $this->totalReceived - $this->totalForwarded,
174+
];
160175

161176
foreach ($keys as $apiKey) {
162177
foreach (QuotaState::ENTITY_TYPES as $type) {
@@ -171,35 +186,41 @@ public function status(): array
171186
}
172187
}
173188

174-
return $status === [] ? ['keys' => new \stdClass] : $status;
189+
if (! isset($status['keys'])) {
190+
$status['keys'] = new \stdClass;
191+
}
192+
193+
return $status;
175194
}
176195

177196
/**
178197
* @return array{
179-
* accepted: int,
180-
* forwarded: int,
198+
* received: int,
181199
* buffered: int,
182-
* buffered_bytes: int,
200+
* forwarded: int,
201+
* pending: int,
202+
* pending_bytes: int,
183203
* in_flight: int
184204
* }
185205
*/
186206
public function stats(): array
187207
{
188-
$buffered = 0;
189-
$bufferedBytes = 0;
208+
$pending = 0;
209+
$pendingBytes = 0;
190210

191211
foreach ($this->buffers as $typedBuffers) {
192212
foreach ($typedBuffers as $buffer) {
193-
$buffered += $buffer->count();
194-
$bufferedBytes += $buffer->bufferedBytes();
213+
$pending += $buffer->count();
214+
$pendingBytes += $buffer->bufferedBytes();
195215
}
196216
}
197217

198218
return [
199-
'accepted' => $this->totalAccepted,
219+
'received' => $this->totalReceived,
220+
'buffered' => $this->totalBuffered,
200221
'forwarded' => $this->totalForwarded,
201-
'buffered' => $buffered,
202-
'buffered_bytes' => $bufferedBytes,
222+
'pending' => $pending,
223+
'pending_bytes' => $pendingBytes,
203224
'in_flight' => $this->inFlight,
204225
];
205226
}

src/daemon.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@
9494
$loop->addPeriodicTimer(5.0, function () use ($ingest, $output): void {
9595
$stats = $ingest->stats();
9696
$output->info('stats', [
97-
'accepted' => $stats['accepted'],
98-
'forwarded' => $stats['forwarded'],
97+
'received' => $stats['received'],
9998
'buffered' => $stats['buffered'],
100-
'buffered_bytes' => $stats['buffered_bytes'],
99+
'forwarded' => $stats['forwarded'],
100+
'pending' => $stats['pending'],
101+
'pending_bytes' => $stats['pending_bytes'],
101102
'in_flight' => $stats['in_flight'],
102103
'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 2),
103104
]);

tests/Feature/ServerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
expect($healthResponse->getStatusCode())->toBe(200)
2020
->and(json_decode((string) $healthResponse->getBody(), true))->toBe(['status' => 'ok'])
2121
->and($statusResponse->getStatusCode())->toBe(200)
22-
->and(json_decode((string) $statusResponse->getBody(), true))->toBe(['keys' => []]);
22+
->and(json_decode((string) $statusResponse->getBody(), true))->toBe(['total_received' => 0, 'total_buffered' => 0, 'total_forwarded' => 0, 'total_dropped' => 0, 'keys' => []]);
2323
});
2424

2525
it('validates incoming requests', function () {
@@ -144,7 +144,7 @@
144144
->and($testResponse->getHeaderLine('Content-Type'))->toContain('text/plain')
145145
->and($testResponse->getHeaderLine('Retry-After'))->toBe('60')
146146
->and((string) $testResponse->getBody())->toBe('Trace quota exceeded')
147-
->and(json_decode((string) $statusResponse->getBody(), true))->toBe(['keys' => []]);
147+
->and(json_decode((string) $statusResponse->getBody(), true))->toBe(['total_received' => 0, 'total_buffered' => 0, 'total_forwarded' => 0, 'total_dropped' => 0, 'keys' => []]);
148148
});
149149

150150
it('returns validation and rejection responses for test payloads', function () {

0 commit comments

Comments
 (0)