Skip to content

Commit 2db6800

Browse files
raing3Oliboy50
andauthored
Topic/dogstatsd support (#87)
* Added option to configure the sending metrics in DogStatsD format. Co-authored-by: Oliver THEBAULT <Oliboy50@users.noreply.github.com>
1 parent 6e27723 commit 2db6800

9 files changed

Lines changed: 93 additions & 11 deletions

File tree

CONTRIBUTING.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ If you want to learn more about our opinion on open source, you can read the [OS
88

99
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.
1010

11-
To ensure good code quality, we use our awesome tool "[coke](https://github.com/M6Web/Coke)" to check there is no coding standards violations.
12-
We use [Symfony2 coding standards](https://github.com/M6Web/Symfony2-coding-standard).
11+
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.
1312

14-
To execute coke, you need to install dependencies in dev mode
13+
To execute PHP-CS-Fixer, you need to install dependencies in dev mode
1514
```bash
1615
composer install
1716
```
1817

19-
And you can launch coke
18+
And you can launch php-cs-fixer
2019
```bash
21-
./vendor/bin/coke
20+
./bin/php-cs-fixer fix --dry-run --using-cache=no --verbose --diff
2221
```
2322

2423
## Testing

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"symfony/property-access": "^4.4 || ^5.0 || ^6.0",
2626
"symfony/validator": "^4.4 || ^5.0 || ^6.0",
2727
"symfony/yaml": "^4.4 || ^5.0 || ^6.0",
28-
"m6web/statsd": "^1.3"
28+
"m6web/statsd": "^1.4"
2929
},
3030
"require-dev": {
3131
"atoum/atoum": "^3.4 || ^4.0",

doc/usage.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ m6_statsd:
1919
port: 1236
2020
clients:
2121
default:
22-
servers: ["default"] # the 'default' client will use only the default server
22+
servers: ["default"] # the 'default' client will use only the default server
23+
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.
2324
swag:
24-
servers: ["serv1", "serv2"] # the 'swag' client will use serv1 OR serv2 to send the data
25+
servers: ["serv1", "serv2"] # the 'swag' client will use serv1 OR serv2 to send the data
2526
mighty:
2627
servers: ["all"] # use all servers configured
2728
shell_patern:
@@ -296,7 +297,8 @@ use \M6Web\Component\Statsd;
296297
$statsd = new Statsd\Client(
297298
array(
298299
'serv1' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx'),
299-
'serv2' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx'))
300+
'serv2' => array('address' => 'udp://xx.xx.xx.xx', 'port' => 'xx')),
301+
new Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter()
300302
);
301303
$statsd->increment('service.coucougraphite');
302304
// we can also pass a sampling rate, default value is 1

src/Client/Client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
*/
1212
class Client extends BaseClient
1313
{
14+
/** @var array */
1415
protected $listenedEvents = [];
1516

16-
/** @var PropertyAccessInterface */
17+
/** @var PropertyAccess\PropertyAccessorInterface */
1718
protected $propertyAccessor;
1819

20+
/** @var int|null */
1921
protected $toSendLimit;
2022

2123
/**

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private function addClientsSection($rootNode)
8888
->end()
8989
->end()
9090
->end()
91+
->scalarNode('message_formatter')->defaultValue('influxdbstatsd')->end()
9192
->integerNode('to_send_limit')->min(1)->end()
9293
->end()
9394
->end()

src/DependencyInjection/M6WebStatsdExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ protected function loadClient($container, $alias, array $config, array $servers,
155155
$definition = new Definition('M6Web\Bundle\StatsdBundle\Client\Client');
156156
$definition->setPublic(true);
157157
$definition->addArgument($usedServers);
158+
$definition->addArgument(new Reference(
159+
$container->has('statsdbundle.formatter.'.$config['message_formatter']) ?
160+
'statsdbundle.formatter.'.$config['message_formatter'] :
161+
$config['message_formatter']
162+
));
158163

159164
if (isset($config['to_send_limit'])) {
160165
$definition->addMethodCall('setToSendLimit', [$config['to_send_limit']]);

src/Resources/config/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ services:
1212
public: false
1313
class: "%property_accessor.class_statsdbundle%"
1414
factory: ['@property_accessor_statsdbundle.factory', 'getPropertyAccessor']
15+
16+
statsdbundle.formatter.dogstatsd:
17+
public: false
18+
class: M6Web\Component\Statsd\MessageFormatter\DogStatsDMessageFormatter
19+
20+
statsdbundle.formatter.influxdbstatsd:
21+
public: false
22+
class: M6Web\Component\Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
m6_statsd:
2+
servers:
3+
default:
4+
address: 'udp://localhost'
5+
port: 1234
6+
clients:
7+
unspecified:
8+
servers: ["default"]
9+
dogstatsd:
10+
servers: ["default"]
11+
message_formatter: dogstatsd
12+
influxdbstatsd:
13+
servers: ["default"]
14+
message_formatter: influxdbstatsd
15+
custom_service:
16+
servers: ["default"]
17+
message_formatter: my.custom.message_formatter

src/Tests/Units/DependencyInjection/M6WebStatsdExtension.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
namespace M6Web\Bundle\StatsdBundle\Tests\Units\DependencyInjection;
44

55
use M6Web\Bundle\StatsdBundle\DependencyInjection\M6WebStatsdExtension as BaseM6WebStatsdExtension;
6+
use M6Web\Component\Statsd\MessageFormatter\DogStatsDMessageFormatter;
7+
use M6Web\Component\Statsd\MessageFormatter\InfluxDBStatsDMessageFormatter;
68
use Symfony\Component\Config\FileLocator;
79
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Definition;
811
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
12+
use Symfony\Component\DependencyInjection\Reference;
913
use Symfony\Component\EventDispatcher\EventDispatcher;
1014

1115
class M6WebStatsdExtension extends \atoum
@@ -20,6 +24,12 @@ protected function initContainer($resource, $debug = false)
2024
$this->container->registerExtension(new BaseM6WebStatsdExtension());
2125
$this->loadConfiguration($this->container, $resource);
2226
$this->container->setParameter('kernel.debug', $debug);
27+
28+
$this->container->setDefinition(
29+
'my.custom.message_formatter',
30+
new Definition('\mock\M6Web\Component\Statsd\MessageFormatter\MessageFormatterInterface')
31+
);
32+
2333
$this->container->compile();
2434
}
2535

@@ -59,6 +69,44 @@ public function testBasicConfigurationWithoutKernelDebug()
5969
->isIdenticalTo(false);
6070
}
6171

72+
/**
73+
* @dataProvider messageFormatterConfigDataProvider
74+
*/
75+
public function testMessageFormatterConfig($service, $expectedFormatter)
76+
{
77+
$this->initContainer('message_formatter');
78+
79+
$this
80+
->object($definition = $this
81+
->container
82+
->getDefinition(sprintf('m6_statsd.%s', $service))
83+
)
84+
->array($arguments = $definition->getArguments())
85+
->object($formatterDefinition = $arguments[1])
86+
;
87+
88+
if ($formatterDefinition instanceof Reference) {
89+
// if a service is referenced a single time it will be inlined as an argument (a Definition object).
90+
// if a service is referenced multiple times it will not be inlined (a Reference object).
91+
// normalise to a definition to make assertions easier.
92+
$formatterDefinition = $this->container->getDefinition((string) $formatterDefinition);
93+
}
94+
95+
$this
96+
->string($formatterDefinition->getClass())
97+
->isEqualTo($expectedFormatter);
98+
}
99+
100+
public function messageFormatterConfigDataProvider()
101+
{
102+
return [
103+
['unspecified', InfluxDBStatsDMessageFormatter::class],
104+
['dogstatsd', DogStatsDMessageFormatter::class],
105+
['influxdbstatsd', InfluxDBStatsDMessageFormatter::class],
106+
['custom_service', '\mock\M6Web\Component\Statsd\MessageFormatter\MessageFormatterInterface'],
107+
];
108+
}
109+
62110
/**
63111
* @dataProvider shellPatternConfigDataProvider
64112
*/
@@ -72,7 +120,7 @@ public function testShellPatternConfig($service, $expectedServers)
72120
->getDefinition(sprintf('m6_statsd.%s', $service))
73121
)
74122
->array($arguments = $definition->getArguments())
75-
->array($servers = array_pop($arguments))
123+
->array($servers = $arguments[0])
76124
;
77125

78126
foreach ($expectedServers as $key => $expectedServer) {

0 commit comments

Comments
 (0)