Skip to content

Commit

Permalink
fix: collector
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 7, 2024
1 parent 5cb895b commit f6ebddf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Telemetry/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Collector
private $config;
private $client;

public function __construct(string $id, string $apiKey)
public function __construct(string $id, string $apiKey, Client $http)
{
$this->client = new Client();
$this->client = $http;
$this->data = [];
$this->config = [
'id' => $id,
Expand All @@ -26,9 +26,13 @@ public function setParams($params)
$this->params = $params;
}

public static function create($config)
public static function create($config, Client $http)
{
$collector = new Collector($config['id'], $config['api_key']);
if (!isset($config['id']) || !isset($config['api_key'])) {
throw new \InvalidArgumentException('The id and api_key parameters are required.');
}

$collector = new Collector($config['id'], $config['api_key'], $http);

$collector->start();

Expand Down Expand Up @@ -71,6 +75,10 @@ private function start()

private function sendBeacon($body)
{
if ($this->params == null || !isset($this->params['endpoint'])) {
return;
}

$url = $this->params['endpoint'] . '?api-key=' . $this->config['api_key'];

$this->client->requestAsync('POST', $url, [
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
test('basic fulltext search', function () {
// Create a mock handler and queue a response.
$mock = new MockHandler([
// initial request to get the collect URL
new Response(200, [], json_encode([
'collectUrl' => 'mock-url',
'deploymentID' => 'mock-deployment-id',
'index' => 'mock-index',
])),
// search request
new Response(200, [], json_encode([
'hits' => [['id' => 2]],
'elapsed' => 0.2,
'count' => 1,
])),
// telemetry data collection
new Response(200, [], json_encode([
'message' => 'Telemetry data collected successfully',
])),
]);

$handlerStack = HandlerStack::create($mock);
Expand Down

0 comments on commit f6ebddf

Please sign in to comment.