Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 55f7f98

Browse files
committed
Added examples to readme
1 parent 523178f commit 55f7f98

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3130
composer 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

src/Client/StatsD.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,28 @@ class StatsD extends AbstractClient implements Client
3030
* @param string $dsn statsd connection dsn
3131
*/
3232
public function __construct($dsn)
33+
{
34+
$this->client = new StatsDClient();
35+
$this->configure($dsn);
36+
}
37+
38+
/**
39+
* @param string $dsn
40+
*/
41+
protected function configure($dsn)
3342
{
3443
$url = (array)parse_url($dsn);
3544

3645
$params = parse_str(empty($url['query']) ? '' : $url['query']);
3746
$options = [
3847
'host' => empty($url['host']) ? 'localhost' : '',
3948
'port' => empty($url['port']) ? $url['port'] : 8125,
40-
'namespace' => empty($params['ns']) ? '' : $params['ns'],
49+
'namespace' => empty($url['path']) ? '' : trim($url['path'], '/'),
4150
'timeout' => empty($params['timeout']) ? null : (float)$params['timeout'],
4251
'throwConnectionExceptions' => empty($params['error']) ? true : (bool)$params['error'],
4352
];
4453

45-
$this->client = (new StatsDClient())->configure($options);
54+
$this->client->configure($options);
4655
}
4756

4857
/**

0 commit comments

Comments
 (0)