-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
Kafka
reporter for zipkin (#6069)
- Loading branch information
1 parent
02f9b01
commit bdc7deb
Showing
6 changed files
with
172 additions
and
11 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
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,92 @@ | ||
<?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; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Throwable; | ||
use Zipkin\Recording\Span; | ||
use Zipkin\Reporter; | ||
use Zipkin\Reporters\JsonV2Serializer; | ||
use Zipkin\Reporters\SpanSerializer; | ||
|
||
use function sprintf; | ||
|
||
class Kafka implements Reporter | ||
{ | ||
private Producer $producer; | ||
|
||
private string $topic; | ||
|
||
private LoggerInterface $logger; | ||
|
||
private SpanSerializer $serializer; | ||
|
||
public function __construct( | ||
array $options = [], | ||
Producer $producer = null, | ||
LoggerInterface $logger = null, | ||
SpanSerializer $serializer = null | ||
) { | ||
$this->topic = $options['topic'] ?? 'zipkin'; | ||
$this->serializer = $serializer ?? new JsonV2Serializer(); | ||
$this->logger = $logger ?? new NullLogger(); | ||
$this->producer = $producer ?? $this->createProducer($options); | ||
} | ||
|
||
/** | ||
* @param array|Span[] $spans | ||
*/ | ||
public function report(array $spans): void | ||
{ | ||
if (empty($spans)) { | ||
return; | ||
} | ||
|
||
try { | ||
$this->producer->send( | ||
$this->topic, | ||
$this->serializer->serialize($spans), | ||
uniqid('', true) | ||
); | ||
} catch (Throwable $e) { | ||
$this->logger->error(sprintf('failed to report spans: %s', $e->getMessage())); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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 RuntimeException; | ||
use Zipkin\Reporter; | ||
|
||
use function Hyperf\Support\make; | ||
|
||
class ReporterFactory | ||
{ | ||
public function make(array $option = []): Reporter | ||
{ | ||
$class = $option['class'] ?? ''; | ||
$constructor = $option['constructor'] ?? []; | ||
|
||
if (! class_exists($class)) { | ||
throw new RuntimeException(sprintf('Class %s is not exists.', $class)); | ||
} | ||
|
||
if (! is_a($class, Reporter::class, true)) { | ||
throw new RuntimeException('Unsupported reporter.'); | ||
} | ||
|
||
return make($class, $constructor); | ||
} | ||
} |
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