Skip to content
Merged
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
29 changes: 19 additions & 10 deletions packages/core/src/Core/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,24 @@ public function captureTransaction(Transaction $transaction): void
}
}

/**
* Mints an event id in the only shape ingest accepts: the server validates
* the field as uuid4, so a random UUID with the version and variant bits
* set is what makes an event deduplicable rather than rejected with 400.
*
* It is public because the id is not optional for anyone assembling an
* ingest payload by hand instead of going through this client - see
* DuckBugProvider::ensureEventId().
*/
public static function generateEventId(): string
{
$bytes = random_bytes(16);
$bytes[6] = \chr((\ord($bytes[6]) & 0x0F) | 0x40);
$bytes[8] = \chr((\ord($bytes[8]) & 0x3F) | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}

/**
* @param array<string, mixed> $context
* @return array<string, mixed>
Expand Down Expand Up @@ -344,7 +362,7 @@ private function buildBasePayload(array $context): array
$requestContext = $this->pond->getContext();

return [
'eventId' => $this->generateEventId(),
'eventId' => self::generateEventId(),
'platform' => $metadata['platform'],
'release' => $metadata['release'],
'environment' => $metadata['environment'],
Expand Down Expand Up @@ -593,15 +611,6 @@ private function isLevelEnabled(ProviderSetup $setup, string $rawLevel): bool
}
}

private function generateEventId(): string
{
$bytes = random_bytes(16);
$bytes[6] = \chr((\ord($bytes[6]) & 0x0F) | 0x40);
$bytes[8] = \chr((\ord($bytes[8]) & 0x3F) | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}

private function generateTraceId(): string
{
return bin2hex(random_bytes(16));
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ private function execute(string $url, string $body, int $attempts): TransportRes
* deduplicates on the event id with a Postgres primary key and ON CONFLICT
* DO NOTHING, with no expiry, on the single and the batch route alike.
*
* That guarantee is only as strong as the id. It has to be supplied by the
* caller: when a payload reaches ingest without an "eventId", the server
* mints a fresh one per request and a retry does store the event twice.
* Client::generateEventId() sets a UUIDv4 on every event it builds, so the
* normal path is safe. The server validates it as uuid4, so a non-UUID
* idempotency key is rejected with 400 rather than honoured.
* That guarantee is only as strong as the id, and the id is optional to
* ingest: a payload that arrives without an "eventId" gets a fresh one
* minted per request, and a retry then stores the event twice. Nothing
* reaches this client in that state - Client sets a UUIDv4 on every event
* it builds, and DuckBugProvider::ensureEventId() fills the field for
* anything else, including a caller driving captureEvent() directly. The
* server validates it as uuid4, so a non-UUID idempotency key is rejected
* with 400 rather than honoured.
*
* 408 is retried because it is the edge timing out the request body
* (nginx client_body_timeout and friends), never a verdict on the payload;
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/Providers/DuckBugProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DuckBug\Providers;

use DuckBug\Core\Client;
use DuckBug\Core\Event;
use DuckBug\Core\FlushableProvider;
use DuckBug\Core\Provider;
Expand Down Expand Up @@ -222,6 +223,45 @@ private function preparePayload(array $payload): ?array
$payload = $this->stripNullValues($processed);
}

return $this->ensureEventId($payload);
}

/**
* Guarantees that every payload leaving this provider carries an
* idempotency key. Ingest deduplicates on "eventId" - a Postgres primary
* key with ON CONFLICT DO NOTHING, no expiry, on the single and the batch
* route alike - but only for a payload that has one: the field is optional
* there, and without it the server mints a fresh id per request, so the
* retries in HttpClient would store the same event once per attempt.
*
* preparePayload() is the last point every payload passes through before
* the transport, and this runs as its final act: after beforeSend and after
* the null strip, because a hook that dropped or blanked the field would
* otherwise reopen the gap. The "nothing to send" check stays above, so no
* event is born out of an eventId alone.
*
* Client sets the id on everything it builds, so this is a no-op on the
* normal path; it is a caller driving the provider straight through
* captureEvent() that would otherwise reach ingest unidentified.
*
* A usable id the caller supplied always wins. Anything else - absent,
* blank, or not a string - is not an id ingest would honour (it validates
* uuid4 and answers 400 otherwise), so it is replaced rather than passed
* through to be rejected.
*
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
private function ensureEventId(array $payload): array
{
/** @var mixed $existing */
$existing = $payload['eventId'] ?? null;
if (\is_string($existing) && trim($existing) !== '') {
return $payload;
}

$payload['eventId'] = Client::generateEventId();

return $payload;
}

Expand Down
29 changes: 19 additions & 10 deletions src/Core/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,24 @@ public function captureTransaction(Transaction $transaction): void
}
}

/**
* Mints an event id in the only shape ingest accepts: the server validates
* the field as uuid4, so a random UUID with the version and variant bits
* set is what makes an event deduplicable rather than rejected with 400.
*
* It is public because the id is not optional for anyone assembling an
* ingest payload by hand instead of going through this client - see
* DuckBugProvider::ensureEventId().
*/
public static function generateEventId(): string
{
$bytes = random_bytes(16);
$bytes[6] = \chr((\ord($bytes[6]) & 0x0F) | 0x40);
$bytes[8] = \chr((\ord($bytes[8]) & 0x3F) | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}

/**
* @param array<string, mixed> $context
* @return array<string, mixed>
Expand Down Expand Up @@ -344,7 +362,7 @@ private function buildBasePayload(array $context): array
$requestContext = $this->pond->getContext();

return [
'eventId' => $this->generateEventId(),
'eventId' => self::generateEventId(),
'platform' => $metadata['platform'],
'release' => $metadata['release'],
'environment' => $metadata['environment'],
Expand Down Expand Up @@ -593,15 +611,6 @@ private function isLevelEnabled(ProviderSetup $setup, string $rawLevel): bool
}
}

private function generateEventId(): string
{
$bytes = random_bytes(16);
$bytes[6] = \chr((\ord($bytes[6]) & 0x0F) | 0x40);
$bytes[8] = \chr((\ord($bytes[8]) & 0x3F) | 0x80);

return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
}

private function generateTraceId(): string
{
return bin2hex(random_bytes(16));
Expand Down
14 changes: 8 additions & 6 deletions src/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ private function execute(string $url, string $body, int $attempts): TransportRes
* deduplicates on the event id with a Postgres primary key and ON CONFLICT
* DO NOTHING, with no expiry, on the single and the batch route alike.
*
* That guarantee is only as strong as the id. It has to be supplied by the
* caller: when a payload reaches ingest without an "eventId", the server
* mints a fresh one per request and a retry does store the event twice.
* Client::generateEventId() sets a UUIDv4 on every event it builds, so the
* normal path is safe. The server validates it as uuid4, so a non-UUID
* idempotency key is rejected with 400 rather than honoured.
* That guarantee is only as strong as the id, and the id is optional to
* ingest: a payload that arrives without an "eventId" gets a fresh one
* minted per request, and a retry then stores the event twice. Nothing
* reaches this client in that state - Client sets a UUIDv4 on every event
* it builds, and DuckBugProvider::ensureEventId() fills the field for
* anything else, including a caller driving captureEvent() directly. The
* server validates it as uuid4, so a non-UUID idempotency key is rejected
* with 400 rather than honoured.
*
* 408 is retried because it is the edge timing out the request body
* (nginx client_body_timeout and friends), never a verdict on the payload;
Expand Down
40 changes: 40 additions & 0 deletions src/Providers/DuckBugProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DuckBug\Providers;

use DuckBug\Core\Client;
use DuckBug\Core\Event;
use DuckBug\Core\FlushableProvider;
use DuckBug\Core\Provider;
Expand Down Expand Up @@ -222,6 +223,45 @@ private function preparePayload(array $payload): ?array
$payload = $this->stripNullValues($processed);
}

return $this->ensureEventId($payload);
}

/**
* Guarantees that every payload leaving this provider carries an
* idempotency key. Ingest deduplicates on "eventId" - a Postgres primary
* key with ON CONFLICT DO NOTHING, no expiry, on the single and the batch
* route alike - but only for a payload that has one: the field is optional
* there, and without it the server mints a fresh id per request, so the
* retries in HttpClient would store the same event once per attempt.
*
* preparePayload() is the last point every payload passes through before
* the transport, and this runs as its final act: after beforeSend and after
* the null strip, because a hook that dropped or blanked the field would
* otherwise reopen the gap. The "nothing to send" check stays above, so no
* event is born out of an eventId alone.
*
* Client sets the id on everything it builds, so this is a no-op on the
* normal path; it is a caller driving the provider straight through
* captureEvent() that would otherwise reach ingest unidentified.
*
* A usable id the caller supplied always wins. Anything else - absent,
* blank, or not a string - is not an id ingest would honour (it validates
* uuid4 and answers 400 otherwise), so it is replaced rather than passed
* through to be rejected.
*
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
private function ensureEventId(array $payload): array
{
/** @var mixed $existing */
$existing = $payload['eventId'] ?? null;
if (\is_string($existing) && trim($existing) !== '') {
return $payload;
}

$payload['eventId'] = Client::generateEventId();

return $payload;
}

Expand Down
Loading
Loading