|
| 1 | +<p align="center"> |
| 2 | + <a href="https://hellofresh.com"> |
| 3 | + <img width="120" src="https://www.hellofresh.de/images/hellofresh/press/HelloFresh_Logo.png"> |
| 4 | + </a> |
| 5 | +</p> |
| 6 | + |
| 7 | +# hellofresh/stats-php |
| 8 | + |
| 9 | +[](https://travis-ci.org/hellofresh/stats-php) |
| 10 | +[](https://codecov.io/gh/hellofresh/stats-php) |
| 11 | + |
| 12 | +> Generic Stats library written in PHP |
| 13 | +
|
| 14 | +This is generic stats library that we at HelloFresh use in our projects to collect services' stats and then create monitoring |
| 15 | +dashboards to track activity and problems. |
| 16 | + |
| 17 | +## Key Features |
| 18 | + |
| 19 | +* Several stats backends: |
| 20 | + * `log` for development environment |
| 21 | + * `statsd` for production |
| 22 | + * `memory` for testing purpose, to track stats operations in unit tests |
| 23 | + * `noop` for environments that do not require any stats gathering |
| 24 | +* Fixed metric sections count for all metrics to allow easy monitoring/alerting setup in `grafana` |
| 25 | +* Easy to build HTTP requests metrics - timing and count |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +```sh |
| 30 | +composer require hellofresh/stats-php |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +### Instance creation |
| 36 | + |
| 37 | +Connection DSN has the following format: `<type>://<connection params>/<connection path>?<connection options>`. |
| 38 | + |
| 39 | +* `<type>` - one of supported backends: `log`, `statsd`, `memory`, `noop` |
| 40 | +* `<connection params>` - used for `statsd` backend only, to defining host and port |
| 41 | +* `<connection path>` - used for `statsd` backend only, to define prefix/namespace |
| 42 | +* `<connection options>` - user for `statsd` backend only: |
| 43 | + * `timeout` - statsd request timeout in seconds, if not set `ini_get('default_socket_timeout')` is used |
| 44 | + * `error` - throw connection error exception, default value is `true` |
| 45 | + |
| 46 | +```php |
| 47 | +<?php |
| 48 | + |
| 49 | +use HelloFresh\Stats\Factory; |
| 50 | + |
| 51 | +$statsdClient = Factory::build('statsd://statsd-host:8125/prefix?timeout=2.5&error=1', $logger); |
| 52 | + |
| 53 | +// php parse_url does not support url with only schema part set |
| 54 | +$logClient = Factory::build('log://log', $logger); |
| 55 | + |
| 56 | +// php parse_url does not support url with only schema part set |
| 57 | +$noopClient = Factory::build('noop://noop', $logger); |
| 58 | + |
| 59 | +// php parse_url does not support url with only schema part set |
| 60 | +$memoryClient = Factory::build('memory://memory', $logger); |
| 61 | + |
| 62 | +// php parse_url does not support url with only schema part set |
| 63 | +$statsClient = Factory::build(getenv('STATS_DSN'), $logger); |
| 64 | +``` |
| 65 | + |
| 66 | +### Count metrics manually |
| 67 | + |
| 68 | +```php |
| 69 | +<?php |
| 70 | + |
| 71 | +use HelloFresh\Stats\Bucket\MetricOperation; |
| 72 | +use HelloFresh\Stats\Factory; |
| 73 | + |
| 74 | +$statsClient = Factory::build(getenv('STATS_DSN'), $logger); |
| 75 | + |
| 76 | +$section = 'ordering'; |
| 77 | +$timer = $statsClient->buildTimer()->start(); |
| 78 | +$operation = new MetricOperation(['orders', 'order', 'create']); |
| 79 | + |
| 80 | +try { |
| 81 | + OrdersService::create(...); |
| 82 | + $statsClient->trackOperation($section, $operation, true, $timer); |
| 83 | +} catch (\Exception $e) { |
| 84 | + $statsClient->trackOperation($section, $operation, false, $timer); |
| 85 | +} |
| 86 | + |
| 87 | +$statsClient->trackMetric('requests', $operation); |
| 88 | + |
| 89 | +$ordersInTheLast24Hours = OrdersService::count(60 * 60 * 24); |
| 90 | +$statsClient->trackState($section, $operation, $ordersInTheLast24Hours); |
| 91 | +``` |
| 92 | + |
| 93 | +## TODO |
| 94 | + |
| 95 | +* [ ] Generalise or modify HTTP Requests metric - e.g. skip ID part |
0 commit comments