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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function telemetry(Request $request, ?int $userId = null): Request
return (new TelemetryRequest($request, new TelemetryHeader))
->userId($userId)
->ip()
->trackId()
->traceId()
->getRequest();
}

Expand Down Expand Up @@ -63,7 +63,7 @@ function telemetry(Request $request, ?int $userId = null): Request
return (new TelemetryRequest($request, new TelemetryHeader))
->userId($userId)
->ip()
->trackId()
->traceId()
->custom('Some-Header', fn (Request $request) => 1234
->getRequest();
}
Expand Down Expand Up @@ -91,7 +91,7 @@ use DragonCode\Telemetry\TelemetryHeader;
return new TelemetryHeader(
userId: 'Some-User-Id',
ip: 'Some-IP',
trackId: 'Some-Track-Id',
traceId: 'Some-Trace-Id',
);
```

Expand Down
2 changes: 1 addition & 1 deletion src/TelemetryHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
public function __construct(
public string $userId = 'X-Telemetry-User-Id',
public string $ip = 'X-Telemetry-Ip',
public string $trackId = 'X-Telemetry-Track-Id',
public string $traceId = 'X-Telemetry-Trace-Id',
) {}
}
10 changes: 5 additions & 5 deletions src/TelemetryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public function getIp(): string
return $this->get('HTTP_X_REAL_IP') ?: $this->request->getClientIp();
}

public function trackId(?string $id = null): static
public function traceId(?string $id = null): static
{
$id ??= $this->getTrackId();
$id ??= $this->getTraceId();

$this->set($this->header->trackId, $id);
$this->set($this->header->traceId, $id);

return $this;
}

public function getTrackId(): string
public function getTraceId(): string
{
if ($id = $this->get($this->header->trackId)) {
if ($id = $this->get($this->header->traceId)) {
return $id;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

expect($header->userId)->toBe('X-Telemetry-User-Id')
->and($header->ip)->toBe('X-Telemetry-Ip')
->and($header->trackId)->toBe('X-Telemetry-Track-Id');
->and($header->traceId)->toBe('X-Telemetry-Trace-Id');
});

it('accepts custom header names', function () {
$header = new TelemetryHeader(
userId : 'Some-User-Id',
ip : 'Some-IP',
trackId: 'Some-Track-Id',
traceId: 'Some-Trace-Id',
);

expect($header->userId)->toBe('Some-User-Id')
->and($header->ip)->toBe('Some-IP')
->and($header->trackId)->toBe('Some-Track-Id');
->and($header->traceId)->toBe('Some-Trace-Id');
});
22 changes: 11 additions & 11 deletions tests/Unit/Request/TrackIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
use DragonCode\Telemetry\TelemetryRequest;
use Ramsey\Uuid\Uuid;

it('gets or sets track id, generating a UUID v4 when absent', function () {
it('gets or sets trace id, generating a UUID v4 when absent', function () {
$header = new TelemetryHeader;

// 1) If header exists, return it
$request = makeRequest([$header->trackId => 'track-123']);
$request = makeRequest([$header->traceId => 'trace-123']);
$telemetry = new TelemetryRequest($request, $header);
expect($telemetry->getTrackId())->toBe('track-123');
expect($telemetry->getTraceId())->toBe('trace-123');

// 2) When absent, generate UUID v4
$request = makeRequest();
$telemetry = new TelemetryRequest($request, $header);
$generated = $telemetry->getTrackId();
$generated = $telemetry->getTraceId();
expect(Uuid::isValid($generated))->toBeTrue()
->and(Uuid::fromString($generated)->getVersion())->toBe(4);

// 3) trackId() without param sets header using getTrackId()
$telemetry->trackId();
expect($request->headers->has($header->trackId))->toBeTrue()
->and(Uuid::isValid($request->headers->get($header->trackId)))->toBeTrue();
// 3) traceId() without param sets header using getTraceId()
$telemetry->traceId();
expect($request->headers->has($header->traceId))->toBeTrue()
->and(Uuid::isValid($request->headers->get($header->traceId)))->toBeTrue();

// 4) trackId() with explicit value sets header
$telemetry->trackId('manual-id');
expect($request->headers->get($header->trackId))->toBe('manual-id');
// 4) traceId() with explicit value sets header
$telemetry->traceId('manual-id');
expect($request->headers->get($header->traceId))->toBe('manual-id');
});