-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
bdc7deb
commit 2fbef18
Showing
6 changed files
with
139 additions
and
77 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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Tracer\Adapter\Reporter; | ||
|
||
use Hyperf\Guzzle\ClientFactory as GuzzleClientFactory; | ||
use RuntimeException; | ||
use Zipkin\Reporters\Http\ClientFactory; | ||
|
||
class HttpClientFactory implements ClientFactory | ||
{ | ||
public function __construct(private GuzzleClientFactory $guzzleClientFactory) | ||
{ | ||
} | ||
|
||
public function build(array $options): callable | ||
{ | ||
return function (string $payload) use ($options): void { | ||
$url = $options['endpoint_url']; | ||
unset($options['endpoint_url']); | ||
$client = $this->guzzleClientFactory->create($options); | ||
$additionalHeaders = $options['headers'] ?? []; | ||
$requiredHeaders = [ | ||
'Content-Type' => 'application/json', | ||
'Content-Length' => strlen($payload), | ||
'b3' => '0', | ||
]; | ||
$headers = array_merge($additionalHeaders, $requiredHeaders); | ||
$response = $client->post($url, [ | ||
'body' => $payload, | ||
'headers' => $headers, | ||
// If 'no_aspect' option is true, then the HttpClientAspect will not modify the client options. | ||
'no_aspect' => true, | ||
]); | ||
$statusCode = $response->getStatusCode(); | ||
if ($statusCode !== 202) { | ||
throw new RuntimeException( | ||
sprintf('Reporting of spans failed, status code %d', $statusCode) | ||
); | ||
} | ||
}; | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Tracer\Adapter\Reporter; | ||
|
||
use longlang\phpkafka\Producer\Producer; | ||
use longlang\phpkafka\Producer\ProducerConfig; | ||
|
||
class KafkaClientFactory | ||
{ | ||
private ?Producer $producer = null; | ||
|
||
public function build(array $options): callable | ||
{ | ||
$this->producer ??= $this->createProducer($options); | ||
|
||
return function (string $payload) use ($options): void { | ||
$this->producer->send( | ||
$options['topic'] ?? 'zipkin', | ||
$payload, | ||
uniqid('', true) | ||
); | ||
}; | ||
} | ||
|
||
private function createProducer(array $options): Producer | ||
{ | ||
$options = array_replace([ | ||
'bootstrap_servers' => '127.0.0.1:9092', | ||
'acks' => -1, | ||
'connect_timeout' => 1, | ||
'send_timeout' => 1, | ||
], $options); | ||
$config = new ProducerConfig(); | ||
|
||
$config->setBootstrapServer($options['bootstrap_servers']); | ||
$config->setUpdateBrokers(true); | ||
if (is_int($options['acks'])) { | ||
$config->setAcks($options['acks']); | ||
} | ||
if (is_float($options['connect_timeout'])) { | ||
$config->setConnectTimeout($options['connect_timeout']); | ||
} | ||
if (is_float($options['send_timeout'])) { | ||
$config->setSendTimeout($options['send_timeout']); | ||
} | ||
|
||
return new Producer($config); | ||
} | ||
} |
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