Skip to content

Commit

Permalink
feat: add telemetry to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjorgedev committed Jan 22, 2025
1 parent 9fcd3e1 commit 3a008f0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"ai",
"rag"
],
"version": "0.1.4",
"homepage": "https://github.com/upstash/vector-php",
"license": "MIT",
"authors": [
Expand Down
39 changes: 39 additions & 0 deletions src/Telemetry/SdkTelemetryReporter.php
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');
}
}
}
6 changes: 3 additions & 3 deletions src/Transporter/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Upstash\Vector\Contracts\TransporterInterface;
use Upstash\Vector\Telemetry\SdkTelemetryReporter;

/**
* @internal
Expand Down Expand Up @@ -91,8 +92,7 @@ private function transformResponse(ResponseInterface $response): TransporterResp

private function addTelemetryHeaders(RequestInterface|MessageInterface $psr17Request): RequestInterface|MessageInterface
{
// TODO: Add PHP and Package Version
return $psr17Request
->withHeader('Upstash-Telemetry-Runtime', 'php');
return (new SdkTelemetryReporter)
->appendHeaders($psr17Request);
}
}
29 changes: 29 additions & 0 deletions tests/Unit/Telemetry/SdkTelemetryReporterTest.php
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);
}
}

0 comments on commit 3a008f0

Please sign in to comment.