Skip to content
This repository was archived by the owner on Apr 4, 2022. It is now read-only.

Commit 3cc52a9

Browse files
author
toni.lopez
committed
NOJIRA: Adding a list of stats and then send all together.
1 parent 1551ea0 commit 3cc52a9

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ $configuration->setHost($config['host'])
3838

3939
$statsClient = new Statsd\Client($configuration);
4040

41-
// send stats
42-
$statsClient->sendStat(
41+
// add stats (you can also add an array of stats with addStats())
42+
$statsClient->addStat(
4343
new Statsd\Domain\Stat(
4444
'endpoints.' . $path, // that will be your stat namespace
4545
$executionTime, // calculate it in microseconds
4646
Statsd\Domain\Stat::TIME_MS
4747
)
4848
);
4949

50+
// send them
51+
$statsClient->sendStats();
52+
5053
```
5154

5255
You can use TIME_MS, COUNT, GAUGE or SET (ms, c, g, s) as type of stats.

src/Statsd/Client.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,66 @@
2020
class Client
2121
{
2222
/**
23-
* @var string
23+
* @var Configuration
2424
*/
25-
private $namespace;
25+
private $configuration;
2626

2727
/**
28-
* @var Resource
28+
* @var Logger|null
2929
*/
30-
private $socket;
30+
private $logger;
3131

3232
/**
33-
* @var Logger|null
33+
* @var array
3434
*/
35-
private $logger;
35+
private $stats = array();
3636

3737
/**
3838
* @param Configuration $configuration
3939
* @param Logger|null $logger
4040
*/
4141
public function __construct(Configuration $configuration, $logger = null)
4242
{
43-
$this->namespace = $configuration->getNamespace();
44-
45-
$socketUrl = sprintf('udp://' . $configuration->getHost());
46-
$this->socket = fsockopen($socketUrl, $configuration->getPort());
47-
43+
$this->configuration = $configuration;
4844
$this->logger = $logger;
4945
}
5046

5147
/**
5248
* @param Stat $stat
5349
*/
54-
public function sendStat(Stat $stat)
50+
public function addStat(Stat $stat)
5551
{
56-
$msg = sprintf(
57-
"%s:%s|%s",
58-
$this->namespace . '.' . $stat->getNamespace(),
59-
$stat->getValue(),
60-
$stat->getType()
61-
);
62-
63-
if (null !== $this->logger) {
64-
$this->logger->info('Sending metrics: ' . $msg);
52+
$this->stats[] = $stat;
53+
}
54+
55+
/**
56+
* @param array $stats
57+
*/
58+
public function addStats(array $stats)
59+
{
60+
foreach ($stats as $stat) {
61+
$this->addStat($stat);
62+
}
63+
}
64+
65+
public function sendStats()
66+
{
67+
$namespace = $this->configuration->getNamespace();
68+
69+
$socketUrl = sprintf('udp://' . $this->configuration->getHost());
70+
$socket = fsockopen($socketUrl, $this->configuration->getPort());
71+
72+
foreach ($this->stats as $key => $stat) {
73+
$msg = $namespace . '.' . (string) $stat;
74+
75+
if (null !== $this->logger) {
76+
$this->logger->info('Sending metrics: ' . $msg);
77+
}
78+
79+
fwrite($socket, $msg);
80+
unset($this->stats[$key]);
6581
}
6682

67-
fwrite($this->socket, $msg);
83+
fclose($socket);
6884
}
6985
}

src/Statsd/Domain/Stat.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,12 @@ public function getValue()
114114
{
115115
return $this->value;
116116
}
117+
118+
/**
119+
* @return string
120+
*/
121+
public function __toString()
122+
{
123+
return sprintf('%s:%s|%s', $this->namespace, $this->value, $this->type);
124+
}
117125
}

tests/Statsd/Tests/Domain/StatTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,14 @@ public function testGetDefaultType()
159159
'Unexpected default type from stat.'
160160
);
161161
}
162+
163+
public function testToString()
164+
{
165+
$stat = new Stat('name.space', 23.4, Stat::TIME_MS);
166+
$this->assertSame(
167+
(string) $stat,
168+
'name.space:23.4|ms',
169+
'Unexpected string cast of the stat.'
170+
);
171+
}
162172
}

0 commit comments

Comments
 (0)