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

Commit 69af686

Browse files
committed
Added statsd client
1 parent 5ec78ce commit 69af686

File tree

10 files changed

+335
-185
lines changed

10 files changed

+335
-185
lines changed

src/Client/Base.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace HelloFresh\Stats\Client;
4+
5+
6+
use HelloFresh\Stats\Bucket;
7+
use HelloFresh\Stats\Bucket\MetricOperation;
8+
use HelloFresh\Stats\Client;
9+
use HelloFresh\Stats\HTTPMetricAlterCallback;
10+
use HelloFresh\Stats\Incrementer;
11+
use HelloFresh\Stats\State;
12+
use HelloFresh\Stats\Timer;
13+
use Psr\Http\Message\RequestInterface;
14+
15+
abstract class Base implements Client
16+
{
17+
/** @var string */
18+
protected $httpRequestSection;
19+
/** @var HTTPMetricAlterCallback */
20+
protected $httpMetricAlterCallback;
21+
22+
/**
23+
* @return Incrementer
24+
*/
25+
abstract protected function getIncrementer();
26+
27+
/**
28+
* @return State
29+
*/
30+
abstract protected function getState();
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function trackRequest(RequestInterface $request, Timer $timer, $success)
36+
{
37+
$bucket = new Bucket\HTTPRequest(
38+
$this->httpRequestSection, $request, $success, $this->getHTTPMetricAlterCallback()
39+
);
40+
$incrementer = $this->getIncrementer();
41+
42+
$timer->finish($bucket->metric());
43+
$incrementer->incrementAll($bucket);
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function trackOperation($section, MetricOperation $operation, $success, Timer $timer = null, $n = 1)
52+
{
53+
$bucket = new Bucket\Plain($section, $operation, $success);
54+
$incrementer = $this->getIncrementer();
55+
56+
if (null !== $timer) {
57+
$timer->finish($bucket->metricWithSuffix());
58+
}
59+
$incrementer->incrementAll($bucket, $n);
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function trackMetric($section, MetricOperation $operation, Timer $timer = null, $n = 1)
68+
{
69+
$bucket = new Bucket\Plain($section, $operation, true);
70+
$incrementer = $this->getIncrementer();
71+
72+
if (null !== $timer) {
73+
$timer->finish($bucket->metricWithSuffix());
74+
}
75+
$incrementer->increment($bucket->metric());
76+
$incrementer->increment($bucket->metricTotal());
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* @inheritdoc
83+
*/
84+
public function trackState($section, MetricOperation $operation, $value)
85+
{
86+
$bucket = new Bucket\Plain($section, $operation, true);
87+
$state = $this->getState();
88+
89+
$state->set($bucket->metric(), $value);
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* @inheritdoc
96+
*/
97+
public function setHTTPMetricAlterCallback(HTTPMetricAlterCallback $callback)
98+
{
99+
$this->httpMetricAlterCallback = $callback;
100+
return $this;
101+
}
102+
103+
/**
104+
* @inheritdoc
105+
*/
106+
public function getHTTPMetricAlterCallback()
107+
{
108+
return $this->httpMetricAlterCallback;
109+
}
110+
111+
/**
112+
* @inheritdoc
113+
*/
114+
public function setHTTPRequestSection($section)
115+
{
116+
$this->httpRequestSection = $section;
117+
return $this;
118+
}
119+
120+
/**
121+
* @inheritdoc
122+
*/
123+
public function resetHTTPRequestSection()
124+
{
125+
return $this->setHTTPRequestSection(Bucket::DEFAULT_HTTP_REQUEST_SECTION);
126+
}
127+
}

src/Client/Log.php

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,16 @@
33
namespace HelloFresh\Stats\Client;
44

55

6-
use HelloFresh\Stats\Bucket;
7-
use HelloFresh\Stats\Bucket\MetricOperation;
86
use HelloFresh\Stats\Client;
9-
use HelloFresh\Stats\HTTPMetricAlterCallback;
107
use HelloFresh\Stats\Incrementer;
118
use HelloFresh\Stats\State;
129
use HelloFresh\Stats\Timer;
13-
use Psr\Http\Message\RequestInterface;
1410
use Psr\Log\LoggerInterface;
1511

16-
class Log implements Client
12+
class Log extends Base implements Client
1713
{
1814
/** @var LoggerInterface */
1915
protected $logger;
20-
/** @var string */
21-
protected $httpRequestSection;
22-
/** @var HTTPMetricAlterCallback */
23-
protected $httpMetricAlterCallback;
2416

2517
/**
2618
* Log constructor.
@@ -44,96 +36,16 @@ public function buildTimer()
4436
/**
4537
* @inheritdoc
4638
*/
47-
public function trackRequest(RequestInterface $request, Timer $timer, $success)
39+
protected function getIncrementer()
4840
{
49-
$bucket = new Bucket\HTTPRequest(
50-
$this->httpRequestSection, $request, $success, $this->getHTTPMetricAlterCallback()
51-
);
52-
$incrementer = new Incrementer\Log($this->logger);
53-
54-
$timer->finish($bucket->metric());
55-
$incrementer->incrementAll($bucket);
56-
57-
return $this;
58-
}
59-
60-
/**
61-
* @inheritdoc
62-
*/
63-
public function trackOperation($section, MetricOperation $operation, $success, Timer $timer = null, $n = 1)
64-
{
65-
$bucket = new Bucket\Plain($section, $operation, $success);
66-
$incrementer = new Incrementer\Log($this->logger);
67-
68-
if (null !== $timer) {
69-
$timer->finish($bucket->metricWithSuffix());
70-
}
71-
$incrementer->incrementAll($bucket, $n);
72-
73-
return $this;
74-
}
75-
76-
/**
77-
* @inheritdoc
78-
*/
79-
public function trackMetric($section, MetricOperation $operation, Timer $timer = null, $n = 1)
80-
{
81-
$bucket = new Bucket\Plain($section, $operation, true);
82-
$incrementer = new Incrementer\Log($this->logger);
83-
84-
if (null !== $timer) {
85-
$timer->finish($bucket->metricWithSuffix());
86-
}
87-
$incrementer->increment($bucket->metric());
88-
$incrementer->increment($bucket->metricTotal());
89-
90-
return $this;
91-
}
92-
93-
/**
94-
* @inheritdoc
95-
*/
96-
public function trackState($section, MetricOperation $operation, $value)
97-
{
98-
$bucket = new Bucket\Plain($section, $operation, true);
99-
$state = new State\Log($this->logger);
100-
101-
$state->set($bucket->metric(), $value);
102-
103-
return $this;
104-
}
105-
106-
/**
107-
* @inheritdoc
108-
*/
109-
public function setHTTPMetricAlterCallback(HTTPMetricAlterCallback $callback)
110-
{
111-
$this->httpMetricAlterCallback = $callback;
112-
return $this;
113-
}
114-
115-
/**
116-
* @inheritdoc
117-
*/
118-
public function getHTTPMetricAlterCallback()
119-
{
120-
return $this->httpMetricAlterCallback;
121-
}
122-
123-
/**
124-
* @inheritdoc
125-
*/
126-
public function setHTTPRequestSection($section)
127-
{
128-
$this->httpRequestSection = $section;
129-
return $this;
41+
return new Incrementer\Log($this->logger);
13042
}
13143

13244
/**
13345
* @inheritdoc
13446
*/
135-
public function resetHTTPRequestSection()
47+
protected function getState()
13648
{
137-
return $this->setHTTPRequestSection(Bucket::DEFAULT_HTTP_REQUEST_SECTION);
49+
return new State\Log($this->logger);
13850
}
13951
}

src/Client/NoOp.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Memory;
10+
11+
class NoOp extends Base implements Client
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function buildTimer()
17+
{
18+
return new Memory();
19+
}
20+
21+
/**
22+
* @return Incrementer
23+
*/
24+
protected function getIncrementer()
25+
{
26+
return new Incrementer\NoOp();
27+
}
28+
29+
/**
30+
* @return State
31+
*/
32+
protected function getState()
33+
{
34+
return new State\NoOp();
35+
}
36+
}

src/Client/NoOpClient.php

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)