-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fcd3e1
commit 3a008f0
Showing
4 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Upstash\Vector\Telemetry; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class SdkTelemetryReporter | ||
{ | ||
public function appendHeaders(RequestInterface $request): RequestInterface | ||
{ | ||
return $request | ||
->withHeader('Upstash-Telemetry-Runtime', $this->getRuntime()) | ||
->withHeader('Upstash-Telemetry-Package', $this->getPackageVersion()); | ||
} | ||
|
||
public function getRuntime(): string | ||
{ | ||
try { | ||
return sprintf('php%s', phpversion()); | ||
} catch (Throwable $e) { | ||
return 'php'; | ||
} | ||
} | ||
|
||
public function getPackageVersion(): string | ||
{ | ||
try { | ||
$version = json_decode(file_get_contents(__DIR__.'/../../composer.json'), true); | ||
|
||
return sprintf('vector-php@%s', $version['version']); | ||
} catch (Throwable $e) { | ||
return sprintf('vector-php@unknown'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Upstash\Vector\Tests\Unit\Telemetry; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Upstash\Vector\Telemetry\SdkTelemetryReporter; | ||
|
||
class SdkTelemetryReporterTest extends TestCase | ||
{ | ||
public function test_runtime_is_correct(): void | ||
{ | ||
$reporter = new SdkTelemetryReporter; | ||
$version = phpversion(); | ||
|
||
$runtime = $reporter->getRuntime(); | ||
|
||
$this->assertSame("php$version", $runtime); | ||
} | ||
|
||
public function test_package_version_is_correct(): void | ||
{ | ||
$reporter = new SdkTelemetryReporter; | ||
$version = json_decode(file_get_contents(__DIR__.'/../../../composer.json'), true)['version']; | ||
|
||
$packageVersion = $reporter->getPackageVersion(); | ||
|
||
$this->assertSame("vector-php@$version", $packageVersion); | ||
} | ||
} |