Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/inline tags for all #55

Open
wants to merge 23 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"okitsu/zabbix-sender": "*@dev",
"symfony/config": "~2.3",
"symfony/dependency-injection": "~2.3",
"symfony/http-kernel": "~2.3"
"symfony/http-kernel": "~2.3",
"phpunit/phpunit": "4.5.0"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Bundle\MetricsBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private function createCollector($type, $config)
return $definition;
case 'logger':
case 'null':
case 'memory':
return $definition;
case 'prometheus':
$definition->replaceArgument(0, new Reference($config['prometheus_collector_registry']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<argument /> <!-- sender, set by the extension -->
<argument /> <!-- host, set by the extension -->
</service>
<service id="beberlei_metrics.collector_proto.memory" class="Beberlei\Metrics\Collector\InMemory" abstract="true">
</service>
</services>

</container>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Bundle\MetricsBundle\Tests\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function testWithGraphite()
}

/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* The source has to be specified to use a Librato
*/
public function testWithLibratoAndInvalidConfiguration()
Expand Down Expand Up @@ -206,7 +207,7 @@ public function testWithTelegraf()
$this->assertSame(1234, $this->getProperty($collector, 'port'));
$this->assertSame('application.com.symfony.', $this->getProperty($collector, 'prefix'));

$this->assertEquals(',string_tag=first_value,int_tag=123', $this->getProperty($collector, 'tags'));
$this->assertEquals($expectedTags, $this->getProperty($collector, 'tags'));
}

public function testWithZabbix()
Expand Down Expand Up @@ -330,6 +331,19 @@ public function testWithPrometheus()
$this->assertSame('', $this->getProperty($collector, 'namespace'));
}

public function testWithInMemory()
{
$container = $this->createContainer(array(
'collectors' => array(
'memory' => array(
'type' => 'memory',
),
),
));
$collector = $container->get('beberlei_metrics.collector.memory');
$this->assertInstanceOf('Beberlei\Metrics\Collector\InMemory', $collector);
}

public function testWithPrometheusAndWithNamespace()
{
$expectedNamespace = 'some_namespace';
Expand Down
1 change: 1 addition & 0 deletions src/Beberlei/Metrics/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Beberlei/Metrics/Collector/DoctrineDBAL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

use Doctrine\DBAL\Connection;
Expand Down
16 changes: 15 additions & 1 deletion src/Beberlei/Metrics/Collector/DogStatsD.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

class DogStatsD implements Collector, InlineTaggableGaugeableCollector
class DogStatsD implements Collector, TaggableCollector, TaggableGaugeableCollector, InlineTaggableGaugeableCollector
{
/** @var string */
private $host;
Expand All @@ -26,6 +27,9 @@ class DogStatsD implements Collector, InlineTaggableGaugeableCollector
/** @var array */
private $data;

/** @var array */
private $tags = array();

/**
* @param string $host
* @param string $port
Expand Down Expand Up @@ -105,6 +109,14 @@ public function flush()
$this->data = array();
}

/**
* {@inheritdoc}
*/
public function setTags($tags)
{
$this->tags = $tags;
}

/**
* Given a key/value map of metric tags, builds them into a
* DogStatsD tag string and returns the string.
Expand All @@ -117,6 +129,8 @@ private function buildTagString($tags)
{
$results = array();

$tags = array_merge($this->tags, $tags);

foreach ($tags as $key => $value) {
$results[] = sprintf('%s:%s', $key, $value);
}
Expand Down
1 change: 1 addition & 0 deletions src/Beberlei/Metrics/Collector/GaugeableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

interface GaugeableCollector
Expand Down
1 change: 1 addition & 0 deletions src/Beberlei/Metrics/Collector/Graphite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

use Exception;
Expand Down
154 changes: 154 additions & 0 deletions src/Beberlei/Metrics/Collector/InMemory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* Beberlei Metrics.
*
* LICENSE
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

/**
* Stores metrics in memory.
* Useful for testing and with any custom persistence mechanisms.
*/
class InMemory implements Collector, GaugeableCollector
{
/** @var int[] */
private $incrementData = [];
/** @var int[] */
private $gaugeData = [];
/** @var int[] */
private $timingData = [];

/**
* Updates a counter by some arbitrary amount.
*
* @param string $variable
* @param int $value The amount to increment the counter by
*/
public function measure($variable, $value)
{
if (!isset($this->incrementData[$variable])) {
$this->incrementData[$variable] = 0;
}
$this->incrementData[$variable] += $value;
}

/**
* Increments a counter.
*
* @param string $variable
*/
public function increment($variable)
{
$this->measure($variable, 1);
}

/**
* Decrements a counter.
*
* @param string $variable
*/
public function decrement($variable)
{
$this->measure($variable, -1);
}

/**
* Records a timing.
*
* @param string $variable
* @param int $time The duration of the timing in milliseconds
*/
public function timing($variable, $time)
{
if (!isset($this->timingData[$variable])) {
$this->timingData[$variable] = 0;
}
$this->timingData[$variable] = $time;
}

/**
* Sends the metrics to the adapter backend.
*/
public function flush()
{
$this->timingData = [];
$this->gaugeData = [];
$this->incrementData = [];
}

/**
* Updates a gauge by an arbitrary amount.
*
* @param string $variable
* @param int $value
*/
public function gauge($variable, $value)
{
$sign = substr($value, 0, 1);

if (in_array($sign, ['-', '+'])) {
$this->gaugeIncrement($variable, (int) $value);

return;
}

$this->gaugeData[$variable] = $value;
}

/**
* Returns current value of incremented/decremented/measured variable.
*
* @param string $variable
*
* @return int
*/
public function getMeasure($variable)
{
return isset($this->incrementData[$variable]) ? $this->incrementData[$variable] : 0;
}

/**
* Returns current value of gauged variable.
*
* @param string $variable
*
* @return int
*/
public function getGauge($variable)
{
return isset($this->gaugeData[$variable]) ? $this->gaugeData[$variable] : 0;
}

/**
* Returns current value of timed variable.
*
* @param string $variable
*
* @return int
*/
public function getTiming($variable)
{
return isset($this->timingData[$variable]) ? $this->timingData[$variable] : 0;
}

/**
* @param string $variable
* @param int $value
*/
private function gaugeIncrement($variable, $value)
{
if (!isset($this->gaugeData[$variable])) {
$this->gaugeData[$variable] = 0;
}

$this->gaugeData[$variable] += $value;
}
}
19 changes: 10 additions & 9 deletions src/Beberlei/Metrics/Collector/InfluxDB.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

use InfluxDB\Client;
Expand All @@ -36,33 +37,33 @@ public function __construct(Client $client)
/**
* {@inheritdoc}
*/
public function increment($variable)
public function increment($variable, $tags = array())
{
$this->data[] = array($variable, 1);
$this->data[] = array($variable, 1, $tags);
}

/**
* {@inheritdoc}
*/
public function decrement($variable)
public function decrement($variable, $tags = array())
{
$this->data[] = array($variable, -1);
$this->data[] = array($variable, -1, $tags);
}

/**
* {@inheritdoc}
*/
public function timing($variable, $time)
public function timing($variable, $time, $tags = array())
{
$this->data[] = array($variable, $time);
$this->data[] = array($variable, $time, $tags);
}

/**
* {@inheritdoc}
*/
public function measure($variable, $value)
public function measure($variable, $value, $tags = array())
{
$this->data[] = array($variable, $value);
$this->data[] = array($variable, $value, $tags);
}

/**
Expand All @@ -78,7 +79,7 @@ public function flush()
'fields' => array('value' => $data[1]),
),
),
'tags' => $this->tags,
'tags' => array_merge($this->tags, $data[2]),
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

/**
* InlineTaggableGaugeableCollector interface.
* @deprecated Replaced by TaggableCollector + TaggableGaugeableCollector
*/
interface InlineTaggableGaugeableCollector
{
Expand Down
1 change: 1 addition & 0 deletions src/Beberlei/Metrics/Collector/Librato.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

namespace Beberlei\Metrics\Collector;

use Buzz\Browser;
Expand Down
Loading