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

Commit 19f753f

Browse files
committed
Added Memory backend
1 parent 687d5ea commit 19f753f

File tree

12 files changed

+230
-5
lines changed

12 files changed

+230
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dashboards to track activity and problems.
1919
* Several stats backends:
2020
* `log` for development environment
2121
* `statsd` for production
22+
* `memory` for testing purpose, to track stats operations in unit tests
2223
* `noop` for environments that do not require any stats gathering
2324
* Fixed metric sections count for all metrics to allow easy monitoring/alerting setup in `grafana`
2425
* Easy to build HTTP requests metrics - timing and count

src/Client/Memory.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats\Client;
7+
use HelloFresh\Stats\Incrementer;
8+
use HelloFresh\Stats\State;
9+
use HelloFresh\Stats\Timer;
10+
11+
class Memory extends AbstractClient implements Client
12+
{
13+
/** @var Incrementer\Memory */
14+
protected $incrementer;
15+
/** @var State\Memory */
16+
protected $state;
17+
/** @var Timer\Memory[] */
18+
protected $timers;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function getIncrementer()
24+
{
25+
if (null === $this->incrementer) {
26+
$this->incrementer = new Incrementer\Memory();
27+
}
28+
return $this->incrementer;
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
protected function getState()
35+
{
36+
if (null === $this->state) {
37+
$this->state = new State\Memory();
38+
}
39+
return $this->state;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function buildTimer()
46+
{
47+
$timer = new Timer\Memory();
48+
$this->timers[] = $timer;
49+
return $timer;
50+
}
51+
52+
/**
53+
* @return Timer\Memory[]
54+
*/
55+
public function getTimers()
56+
{
57+
return $this->timers;
58+
}
59+
60+
/**
61+
* Returns all stored metrics array.
62+
* Key - metric name, value - metric value.
63+
*
64+
* @return array
65+
*/
66+
public function getIncrementedMetrics()
67+
{
68+
return $this->getIncrementer()->getMetrics();
69+
}
70+
71+
/**
72+
* Returns all stored states array.
73+
* Key - metric name, value - metric value.
74+
*
75+
* @return array
76+
*/
77+
public function getStateMetrics()
78+
{
79+
return $this->getState()->getMetrics();
80+
}
81+
}

src/Factory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
use HelloFresh\Stats\Client\Log;
7+
use HelloFresh\Stats\Client\Memory;
78
use HelloFresh\Stats\Client\NoOp;
89
use HelloFresh\Stats\Client\StatsD;
910
use Psr\Log\LoggerInterface;
@@ -13,6 +14,7 @@ class Factory
1314
const STATSD = 'statsd';
1415
const LOG = 'log';
1516
const NOOP = 'noop';
17+
const MEMORY = 'memory';
1618

1719
/**
1820
* Builds Stats Client instance.
@@ -34,6 +36,9 @@ public static function build($dsn, LoggerInterface $logger)
3436

3537
case static::NOOP:
3638
return new NoOp();
39+
40+
case static::MEMORY:
41+
return new Memory();
3742
}
3843

3944
throw new \RuntimeException('Unknown client type');

src/Incrementer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Incrementer
1111
* @param string $metric
1212
* @param int $n
1313
*
14-
* @return void
14+
* @return self
1515
*/
1616
public function increment($metric, $n = 1);
1717

src/Incrementer/AbstractIncrementer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ abstract public function increment($metric, $n = 1);
1818
*/
1919
public function incrementAll(Bucket $bucket, $n = 1)
2020
{
21-
$this->increment($bucket->metric(), $n);
22-
$this->increment($bucket->metricWithSuffix(), $n);
23-
$this->increment($bucket->metricTotal(), $n);
24-
$this->increment($bucket->metricTotalWithSuffix(), $n);
21+
$this->increment($bucket->metric(), $n)
22+
->increment($bucket->metricWithSuffix(), $n)
23+
->increment($bucket->metricTotal(), $n)
24+
->increment($bucket->metricTotalWithSuffix(), $n);
2525
}
2626
}

src/Incrementer/Log.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public function __construct($logger)
2727
public function increment($metric, $n = 1)
2828
{
2929
$this->logger->debug('Stats counter increment', ['metric' => $metric, 'n' => $n]);
30+
return $this;
3031
}
3132
}

src/Incrementer/Memory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Incrementer;
4+
5+
6+
use HelloFresh\Stats\Incrementer;
7+
8+
class Memory extends AbstractIncrementer implements Incrementer
9+
{
10+
protected $metrics = [];
11+
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function increment($metric, $n = 1)
16+
{
17+
$this->metrics[$metric] = empty($this->metrics[$metric]) ? $n : $this->metrics[$metric] + $n;
18+
return $this;
19+
}
20+
21+
/**
22+
* Returns all stored metrics array.
23+
* Key - metric name, value - metric value.
24+
*
25+
* @return array
26+
*/
27+
public function getMetrics()
28+
{
29+
return $this->metrics;
30+
}
31+
}

src/Incrementer/NoOp.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ class NoOp extends AbstractIncrementer implements Incrementer
1212
*/
1313
public function increment($metric, $n = 1)
1414
{
15+
return $this;
1516
}
1617
}

src/Incrementer/StatsD.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public function __construct(Client $client)
2727
public function increment($metric, $n = 1)
2828
{
2929
$this->client->increment($metric, $n);
30+
return $this;
3031
}
3132
}

src/State/Memory.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\State;
4+
5+
6+
use HelloFresh\Stats\State;
7+
8+
class Memory implements State
9+
{
10+
protected $metrics = [];
11+
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function set($metric, $state)
16+
{
17+
$this->metrics[$metric] = $state;
18+
}
19+
20+
/**
21+
* Returns all stored states array.
22+
* Key - metric name, value - metric value.
23+
*
24+
* @return array
25+
*/
26+
public function getMetrics()
27+
{
28+
return $this->metrics;
29+
}
30+
}

0 commit comments

Comments
 (0)