diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3f1382a..95f5880 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/composer.json b/composer.json index 811a20d..fce98b2 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/doc/usage.md b/doc/usage.md index 02e00c0..8571712 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -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: @@ -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 diff --git a/src/Client/Client.php b/src/Client/Client.php index e137521..9c2fe7a 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -11,11 +11,13 @@ */ class Client extends BaseClient { + /** @var array */ protected $listenedEvents = []; - /** @var PropertyAccessInterface */ + /** @var PropertyAccess\PropertyAccessorInterface */ protected $propertyAccessor; + /** @var int|null */ protected $toSendLimit; /** diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5846290..e50cda9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/DependencyInjection/M6WebStatsdExtension.php b/src/DependencyInjection/M6WebStatsdExtension.php index ab37133..1c76893 100644 --- a/src/DependencyInjection/M6WebStatsdExtension.php +++ b/src/DependencyInjection/M6WebStatsdExtension.php @@ -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']]); diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 7b2d8f6..5a86c32 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -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 diff --git a/src/Tests/Fixtures/message_formatter.yml b/src/Tests/Fixtures/message_formatter.yml new file mode 100644 index 0000000..18e1cd9 --- /dev/null +++ b/src/Tests/Fixtures/message_formatter.yml @@ -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 diff --git a/src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php b/src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php index 1de752e..f274335 100644 --- a/src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php +++ b/src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php @@ -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 @@ -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(); } @@ -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 */ @@ -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) {