@@ -23,12 +23,69 @@ dashboards to track activity and problems.
2323 * ` noop ` for environments that do not require any stats gathering
2424* Fixed metric sections count for all metrics to allow easy monitoring/alerting setup in ` grafana `
2525* Easy to build HTTP requests metrics - timing and count
26- * Generalise or modify HTTP Requests metric - e.g. skip ID part
2726
2827## Installation
2928
3029``` sh
3130composer require hellofresh/stats-php
3231```
3332
34- TBD
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+ $logClient = Factory::build('log://', $logger);
54+
55+ $noopClient = Factory::build('noop://', $logger);
56+
57+ $memoryClient = Factory::build('memory://', $logger);
58+
59+ $statsClient = Factory::build(getenv('STATS_DSN'), $logger);
60+ ```
61+
62+ ### Count metrics manually
63+
64+ ``` php
65+ <?php
66+
67+ use HelloFresh\Stats\Bucket\MetricOperation;
68+ use HelloFresh\Stats\Factory;
69+
70+ $statsClient = Factory::build(getenv('STATS_DSN'), $logger);
71+
72+ $section = 'ordering';
73+ $timer = $statsClient->buildTimer()->start();
74+ $operation = new MetricOperation(['orders', 'order', 'create']);
75+
76+ try {
77+ OrdersService::create(...);
78+ $statsClient->trackOperation($section, $operation, true, $timer);
79+ } catch (\Exception $e) {
80+ $statsClient->trackOperation($section, $operation, false, $timer);
81+ }
82+
83+ $statsClient->trackMetric('requests', $operation);
84+
85+ $ordersInTheLast24Hours = OrdersService::count(60 * 60 * 24);
86+ $statsClient->trackState($section, $operation, $ordersInTheLast24Hours);
87+ ```
88+
89+ ## TODO
90+
91+ * [ ] Generalise or modify HTTP Requests metric - e.g. skip ID part
0 commit comments