Skip to content

Commit

Permalink
Topic/dogstatsd support (#87)
Browse files Browse the repository at this point in the history
* Added option to configure the sending metrics in DogStatsD format.

Co-authored-by: Oliver THEBAULT <[email protected]>
  • Loading branch information
raing3 and Oliboy50 authored Mar 4, 2022
1 parent 6e27723 commit 2db6800
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ If you want to learn more about our opinion on open source, you can read the [OS

The features available for now are only those we need, but you're welcome to open an issue or pull-request if you need more.

To ensure good code quality, we use our awesome tool "[coke](https://github.com/M6Web/Coke)" to check there is no coding standards violations.
We use [Symfony2 coding standards](https://github.com/M6Web/Symfony2-coding-standard).
To ensure good code quality, we use [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) to check there is no coding standards violations.

To execute coke, you need to install dependencies in dev mode
To execute PHP-CS-Fixer, you need to install dependencies in dev mode
```bash
composer install
```

And you can launch coke
And you can launch php-cs-fixer
```bash
./vendor/bin/coke
./bin/php-cs-fixer fix --dry-run --using-cache=no --verbose --diff
```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/property-access": "^4.4 || ^5.0 || ^6.0",
"symfony/validator": "^4.4 || ^5.0 || ^6.0",
"symfony/yaml": "^4.4 || ^5.0 || ^6.0",
"m6web/statsd": "^1.3"
"m6web/statsd": "^1.4"
},
"require-dev": {
"atoum/atoum": "^3.4 || ^4.0",
Expand Down
8 changes: 5 additions & 3 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ m6_statsd:
port: 1236
clients:
default:
servers: ["default"] # the 'default' client will use only the default server
servers: ["default"] # the 'default' client will use only the default server
message_formatter: "dogstatsd" # format to transmit metrics to the server in 'influxdbstatsd' (default), 'dogstatsd' or a custom service ID which implements \M6Web\Component\Statsd\MessageFormatterMessageFormatterInterface can be used.
swag:
servers: ["serv1", "serv2"] # the 'swag' client will use serv1 OR serv2 to send the data
servers: ["serv1", "serv2"] # the 'swag' client will use serv1 OR serv2 to send the data
mighty:
servers: ["all"] # use all servers configured
shell_patern:
Expand Down Expand Up @@ -296,7 +297,8 @@ use \M6Web\Component\Statsd;
$statsd = new Statsd\Client(
array(
'serv1' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx'),
'serv2' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx'))
'serv2' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx')),
new Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter()
);
$statsd->increment('service.coucougraphite');
// we can also pass a sampling rate, default value is 1
Expand Down
4 changes: 3 additions & 1 deletion src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*/
class Client extends BaseClient
{
/** @var array */
protected $listenedEvents = [];

/** @var PropertyAccessInterface */
/** @var PropertyAccess\PropertyAccessorInterface */
protected $propertyAccessor;

/** @var int|null */
protected $toSendLimit;

/**
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private function addClientsSection($rootNode)
->end()
->end()
->end()
->scalarNode('message_formatter')->defaultValue('influxdbstatsd')->end()
->integerNode('to_send_limit')->min(1)->end()
->end()
->end()
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/M6WebStatsdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ protected function loadClient($container, $alias, array $config, array $servers,
$definition = new Definition('M6Web\Bundle\StatsdBundle\Client\Client');
$definition->setPublic(true);
$definition->addArgument($usedServers);
$definition->addArgument(new Reference(
$container->has('statsdbundle.formatter.'.$config['message_formatter']) ?
'statsdbundle.formatter.'.$config['message_formatter'] :
$config['message_formatter']
));

if (isset($config['to_send_limit'])) {
$definition->addMethodCall('setToSendLimit', [$config['to_send_limit']]);
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ services:
public: false
class: "%property_accessor.class_statsdbundle%"
factory: ['@property_accessor_statsdbundle.factory', 'getPropertyAccessor']

statsdbundle.formatter.dogstatsd:
public: false
class: M6Web\Component\Statsd\MessageFormatter\DogStatsDMessageFormatter

statsdbundle.formatter.influxdbstatsd:
public: false
class: M6Web\Component\Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter
17 changes: 17 additions & 0 deletions src/Tests/Fixtures/message_formatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
m6_statsd:
servers:
default:
address: 'udp://localhost'
port: 1234
clients:
unspecified:
servers: ["default"]
dogstatsd:
servers: ["default"]
message_formatter: dogstatsd
influxdbstatsd:
servers: ["default"]
message_formatter: influxdbstatsd
custom_service:
servers: ["default"]
message_formatter: my.custom.message_formatter
50 changes: 49 additions & 1 deletion src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace M6Web\Bundle\StatsdBundle\Tests\Units\DependencyInjection;

use M6Web\Bundle\StatsdBundle\DependencyInjection\M6WebStatsdExtension as BaseM6WebStatsdExtension;
use M6Web\Component\Statsd\MessageFormatter\DogStatsDMessageFormatter;
use M6Web\Component\Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;

class M6WebStatsdExtension extends \atoum
Expand All @@ -20,6 +24,12 @@ protected function initContainer($resource, $debug = false)
$this->container->registerExtension(new BaseM6WebStatsdExtension());
$this->loadConfiguration($this->container, $resource);
$this->container->setParameter('kernel.debug', $debug);

$this->container->setDefinition(
'my.custom.message_formatter',
new Definition('\mock\M6Web\Component\Statsd\MessageFormatter\MessageFormatterInterface')
);

$this->container->compile();
}

Expand Down Expand Up @@ -59,6 +69,44 @@ public function testBasicConfigurationWithoutKernelDebug()
->isIdenticalTo(false);
}

/**
* @dataProvider messageFormatterConfigDataProvider
*/
public function testMessageFormatterConfig($service, $expectedFormatter)
{
$this->initContainer('message_formatter');

$this
->object($definition = $this
->container
->getDefinition(sprintf('m6_statsd.%s', $service))
)
->array($arguments = $definition->getArguments())
->object($formatterDefinition = $arguments[1])
;

if ($formatterDefinition instanceof Reference) {
// if a service is referenced a single time it will be inlined as an argument (a Definition object).
// if a service is referenced multiple times it will not be inlined (a Reference object).
// normalise to a definition to make assertions easier.
$formatterDefinition = $this->container->getDefinition((string) $formatterDefinition);
}

$this
->string($formatterDefinition->getClass())
->isEqualTo($expectedFormatter);
}

public function messageFormatterConfigDataProvider()
{
return [
['unspecified', InfluxDBStatsDMessageFormatter::class],
['dogstatsd', DogStatsDMessageFormatter::class],
['influxdbstatsd', InfluxDBStatsDMessageFormatter::class],
['custom_service', '\mock\M6Web\Component\Statsd\MessageFormatter\MessageFormatterInterface'],
];
}

/**
* @dataProvider shellPatternConfigDataProvider
*/
Expand All @@ -72,7 +120,7 @@ public function testShellPatternConfig($service, $expectedServers)
->getDefinition(sprintf('m6_statsd.%s', $service))
)
->array($arguments = $definition->getArguments())
->array($servers = array_pop($arguments))
->array($servers = $arguments[0])
;

foreach ($expectedServers as $key => $expectedServer) {
Expand Down

0 comments on commit 2db6800

Please sign in to comment.