Skip to content

Commit

Permalink
Add a RdKafka caster to Var-Dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Feb 4, 2020
1 parent 49b8570 commit 953b8b5
Show file tree
Hide file tree
Showing 4 changed files with 497 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* added `RdKafka` support

4.4.0
-----

Expand Down
187 changes: 187 additions & 0 deletions Caster/RdKafkaCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Caster;

use RdKafka;
use RdKafka\Conf;
use RdKafka\Exception as RdKafkaException;
use RdKafka\KafkaConsumer;
use RdKafka\Message;
use RdKafka\Metadata\Broker as BrokerMetadata;
use RdKafka\Metadata\Collection as CollectionMetadata;
use RdKafka\Metadata\Partition as PartitionMetadata;
use RdKafka\Metadata\Topic as TopicMetadata;
use RdKafka\Topic;
use RdKafka\TopicConf;
use RdKafka\TopicPartition;
use Symfony\Component\VarDumper\Cloner\Stub;

/**
* Casts RdKafka related classes to array representation.
*
* @author Romain Neutron <[email protected]>
*/
class RdKafkaCaster
{
public static function castKafkaConsumer(KafkaConsumer $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

try {
$assignment = $c->getAssignment();
} catch (RdKafkaException $e) {
$assignment = [];
}

$a += [
$prefix.'subscription' => $c->getSubscription(),
$prefix.'assignment' => $assignment,
];

$a += self::extractMetadata($c);

return $a;
}

public static function castTopic(Topic $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'name' => $c->getName(),
];

return $a;
}

public static function castTopicPartition(TopicPartition $c, array $a)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'offset' => $c->getOffset(),
$prefix.'partition' => $c->getPartition(),
$prefix.'topic' => $c->getTopic(),
];

return $a;
}

public static function castMessage(Message $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'errstr' => $c->errstr(),
];

return $a;
}

public static function castConf(Conf $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

foreach ($c->dump() as $key => $value) {
$a[$prefix.$key] = $value;
}

return $a;
}

public static function castTopicConf(TopicConf $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

foreach ($c->dump() as $key => $value) {
$a[$prefix.$key] = $value;
}

return $a;
}

public static function castRdKafka(\RdKafka $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'out_q_len' => $c->getOutQLen(),
];

$a += self::extractMetadata($c);

return $a;
}

public static function castCollectionMetadata(CollectionMetadata $c, array $a, Stub $stub, $isNested)
{
$a += iterator_to_array($c);

return $a;
}

public static function castTopicMetadata(TopicMetadata $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'name' => $c->getTopic(),
$prefix.'partitions' => $c->getPartitions(),
];

return $a;
}

public static function castPartitionMetadata(PartitionMetadata $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'id' => $c->getId(),
$prefix.'err' => $c->getErr(),
$prefix.'leader' => $c->getLeader(),
];

return $a;
}

public static function castBrokerMetadata(BrokerMetadata $c, array $a, Stub $stub, $isNested)
{
$prefix = Caster::PREFIX_VIRTUAL;

$a += [
$prefix.'id' => $c->getId(),
$prefix.'host' => $c->getHost(),
$prefix.'port' => $c->getPort(),
];

return $a;
}

private static function extractMetadata($c)
{
$prefix = Caster::PREFIX_VIRTUAL;

try {
$m = $c->getMetadata(true, null, 500);
} catch (RdKafkaException $e) {
return [];
}

return [
$prefix.'orig_broker_id' => $m->getOrigBrokerId(),
$prefix.'orig_broker_name' => $m->getOrigBrokerName(),
$prefix.'brokers' => $m->getBrokers(),
$prefix.'topics' => $m->getTopics(),
];
}
}
12 changes: 12 additions & 0 deletions Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ abstract class AbstractCloner implements ClonerInterface
':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
':xml' => ['Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'],

'RdKafka' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castRdKafka'],
'RdKafka\Conf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castConf'],
'RdKafka\KafkaConsumer' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castKafkaConsumer'],
'RdKafka\Metadata\Broker' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castBrokerMetadata'],
'RdKafka\Metadata\Collection' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castCollectionMetadata'],
'RdKafka\Metadata\Partition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castPartitionMetadata'],
'RdKafka\Metadata\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicMetadata'],
'RdKafka\Message' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castMessage'],
'RdKafka\Topic' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopic'],
'RdKafka\TopicPartition' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicPartition'],
'RdKafka\TopicConf' => ['Symfony\Component\VarDumper\Caster\RdKafkaCaster', 'castTopicConf'],
];

protected $maxItems = 2500;
Expand Down
Loading

0 comments on commit 953b8b5

Please sign in to comment.