zf-statsd
is a ZF2 module for automating monitoring/profiling tasks within a Zend Framework 2 with StatsD.
Run the following composer
command:
$ composer require "corentin-larose/zf-statsd:dev-master"
Alternately, manually add the following to your composer.json
, in the require
section:
"require": {
"corentin-larose/zf-statsd": "dev-master"
}
And then run composer update
to ensure the module is installed.
Finally, add the module name to your project's config/application.config.php
under the modules
key:
return array(
/* ... */
'modules' => array(
/* ... */
'ZF\Statsd',
),
/* ... */
);
As a rule of thumb, avoid as much as possible using anonymous functions since it prevents you from caching your configuration.
The top-level configuration key for user configuration of this module is zf-statsd
.
The config/module.config.php
file contains a self-explanative example of configuration.
The enable
key is utilized for enabling/disabling the statsd module at run time.
If you no longer need this module, rather consider removing the module from the application.config.php
list.
Example:
'zf-statsd' => array(
/* ... */
'enable' => true, // Statsd module is enabled.
/* ... */
),
The memory_pattern
key is utilized to configure the metric name pattern for the memory related data.
All token surrounded by %
will be replaced with the corresponding value (see the available token list below).
You can remove, add and sort tokens at your will.
Example:
'zf-statsd' => array(
/* ... */
'memory_pattern' => '%hostname%.%controller%.%http-method%.%http-code%.%response-content-type%.%mvc-event%.memory',
/* ... */
),
The timer_pattern
key is utilized to configure the metric name pattern for the time related data.
All token surrounded by %
will be replaced with the corresponding value (see the available token list below).
You can remove, add and sort tokens at your will.
Example:
'zf-statsd' => array(
/* ... */
'timer_pattern' => '%hostname%.%controller%.%http-method%.%http-code%.%response-content-type%.%mvc-event%.duration',
/* ... */
),
Will be replaced with the value returned by hostname()
function.
Will be replaced with the value returned by Zend\Mvc\MvcEvent::getRouteMatch()->getParam('controller')
method.
Will be replaced with the value returned by Zend\Http\Request::getMethod()
method.
Will be replaced with the value returned by Zend\Http\Response::getStatusCode()
method.
Will be replaced with the value returned by Zend\Http\Response::getHeaders()->get('content-type')->getFieldValue()
method.
Will be replaced with the value returned by Zend\Mvc\MvcEvent::getName()
method.
Callback used to override the case of the metric names, must be a valid PHP callback.
Example:
'zf-statsd' => array(
/* ... */
'metric_tokens_callback' => 'strtolower',
/* ... */
),
Whether to override a .
found in a string used in the metric name.
If true, .
will be replaced with the value of the replace_special_chars_with
key.
Example:
'zf-statsd' => array(
/* ... */
'replace_dots_in_tokens' => true,
/* ... */
),
Char used to replace all special chars encountererd within a string used in the metric name.
Example:
'zf-statsd' => array(
/* ... */
'replace_special_chars_with' => '-',
/* ... */
),
Statsd host and port.
Example:
'zf-statsd' => array(
/* ... */
'statsd' => array(
'host' => '127.0.0.1',
'port' => '8125',
),
/* ... */
),
This listener is attached twice on each Zend\Mvc\Application
event, once with the very low priority of -10000
to collect metrics before event propagation, once with the very high priority of 10000
to collect metrics after event propagation.
Install NodeJs an NPM:
apt-get install nodejs npm
Clone StatsD
git clone https://github.com/etsy/statsd.git
Create a config
cd statsd && cp exampleConfig.js config.js
Edit config.js
{
port: 8125
, backends: [ "./backends/console" ]
}
Start StatsD
node stats.js ./config.js