Skip to content

Commit 2aafc1d

Browse files
committed
Merge pull request #17 from johndodev/tags-influxDb-0.9
POC : Add tags support for influxDb 0.9
2 parents 47effc6 + 9cf4e96 commit 2aafc1d

File tree

4 files changed

+85
-17
lines changed

4 files changed

+85
-17
lines changed

doc/usage.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,15 @@ $client->timing('another.graphite.node', (float) $timing);
1414
$client->set('a.graphite.set', 12);
1515
$client->gauge('a.gauge.node', 8);
1616

17+
// with tags for influxDb 0.9 : add the tags array at the end of each function
18+
$tags = ['foo' => 'bar', 'site' => 'www', 'country' => 'fr'];
19+
$sampleRate = 1;
20+
21+
$client->increment('a.influxDb.node', $sampleRate, $tags);
22+
$client->count('a.influxDb.node', 5, $sampleRate, $tags);
23+
$client->timing('another.influxDb.node', (float) $timing, $sampleRate, $tags);
24+
$client->set('a.influxDb.set', 12, $sampleRate, $tags);
25+
$client->gauge('a.influxDb.node', 8, $sampleRate, $tags);
26+
1727
$client->send(); // Send the metrics to the servers
1828
```

src/M6Web/Component/Statsd/Client.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ public function getServerKey($stats)
133133
* @param string $value value
134134
* @param float $sampleRate sampling rate
135135
* @param string $unit unit
136+
* @param array $tags Tags key => value for influxDb
136137
*
137138
* @return Client
138139
*/
139-
protected function addToSend($stats, $value, $sampleRate, $unit)
140+
protected function addToSend($stats, $value, $sampleRate, $unit, $tags)
140141
{
141142

142143
$message = new MessageEntity(
143-
(string) $stats, (int) $value, (string) $unit, (float) $sampleRate
144+
(string) $stats, (int) $value, (string) $unit, (float) $sampleRate, $tags
144145
);
145146

146147
$queue = [
@@ -175,12 +176,13 @@ protected function buildSampledData()
175176
* @param string $stats The metric to in log timing info for.
176177
* @param int $time The ellapsed time (ms) to log
177178
* @param float|int $sampleRate the rate (0-1) for sampling.
179+
* @param array $tags Tags key => value for influxDb
178180
*
179181
* @return Client
180182
*/
181-
public function timing($stats, $time, $sampleRate = 1.0)
183+
public function timing($stats, $time, $sampleRate = 1.0, $tags = [])
182184
{
183-
$this->addToSend($stats, $time, $sampleRate, 'ms');
185+
$this->addToSend($stats, $time, $sampleRate, 'ms', $tags);
184186

185187
return $this;
186188
}
@@ -190,14 +192,15 @@ public function timing($stats, $time, $sampleRate = 1.0)
190192
*
191193
* @param string $stats The metric(s) to increment.
192194
* @param float $sampleRate SamplingRate
195+
* @param array $tags Tags key => value for influxDb
193196
*
194197
* @internal param $ float|1 $sampleRate the rate (0-1) for sampling.
195198
*
196199
* @return Client
197200
*/
198-
public function increment($stats, $sampleRate = 1.0)
201+
public function increment($stats, $sampleRate = 1.0, $tags = [])
199202
{
200-
$this->count($stats, '1', $sampleRate);
203+
$this->count($stats, '1', $sampleRate, $tags);
201204

202205
return $this;
203206
}
@@ -208,12 +211,13 @@ public function increment($stats, $sampleRate = 1.0)
208211
*
209212
* @param string $stats The metric(s) to decrement.
210213
* @param float|int $sampleRate the rate (0-1) for sampling.
214+
* @param array $tags Tags key => value for influxDb
211215
*
212216
* @return Client
213217
*/
214-
public function decrement($stats, $sampleRate = 1)
218+
public function decrement($stats, $sampleRate = 1, $tags = [])
215219
{
216-
$this->count($stats, '-1', $sampleRate);
220+
$this->count($stats, '-1', $sampleRate, $tags);
217221

218222
return $this;
219223
}
@@ -224,14 +228,15 @@ public function decrement($stats, $sampleRate = 1)
224228
* @param string $stats The metric(s) to count
225229
* @param int $value The count value
226230
* @param float|int $sampleRate the rate (0-1) for sampling.
231+
* @param array $tags Tags key => value for influxDb
227232
*
228233
* @access public
229234
*
230235
* @return Client
231236
*/
232-
public function count($stats, $value, $sampleRate = 1)
237+
public function count($stats, $value, $sampleRate = 1, $tags = [])
233238
{
234-
$this->addToSend($stats, $value, $sampleRate, 'c');
239+
$this->addToSend($stats, $value, $sampleRate, 'c', $tags);
235240

236241
return $this;
237242
}
@@ -242,13 +247,14 @@ public function count($stats, $value, $sampleRate = 1)
242247
* @param string $stats The metric(s) to count
243248
* @param int $value The value
244249
* @param float|int $sampleRate the rate (0-1) for sampling.
250+
* @param array $tags Tags key => value for influxDb
245251
*
246252
* @access public
247253
* @return Client
248254
*/
249-
public function gauge($stats, $value, $sampleRate = 1)
255+
public function gauge($stats, $value, $sampleRate = 1, $tags = [])
250256
{
251-
$this->addToSend($stats, $value, $sampleRate, 'g');
257+
$this->addToSend($stats, $value, $sampleRate, 'g', $tags);
252258

253259
return $this;
254260
}
@@ -259,13 +265,14 @@ public function gauge($stats, $value, $sampleRate = 1)
259265
* @param string $stats The metric(s) to count
260266
* @param int $value The value
261267
* @param float|int $sampleRate the rate (0-1) for sampling.
268+
* @param array $tags Tags key => value for influxDb
262269
*
263270
* @access public
264271
* @return Client
265272
*/
266-
public function set($stats, $value, $sampleRate = 1)
273+
public function set($stats, $value, $sampleRate = 1, $tags = [])
267274
{
268-
$this->addToSend($stats, $value, $sampleRate, 's');
275+
$this->addToSend($stats, $value, $sampleRate, 's', $tags);
269276

270277
return $this;
271278
}

src/M6Web/Component/Statsd/MessageEntity.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,21 @@ class MessageEntity
2929
*/
3030
protected $unit;
3131

32+
/**
33+
* @var array
34+
*/
35+
protected $tags = [];
36+
3237
/**
3338
* @param string $node node
3439
* @param int $value value of the node
3540
* @param string $unit units (ms for timer, c for counting ...)
3641
* @param float $sampleRate sampling rate
42+
* @param array $tags Tags key => value for influxDb
3743
*
3844
* @return MessageEntity
3945
*/
40-
public function __construct($node, $value, $unit = '', $sampleRate = 1.0)
46+
public function __construct($node, $value, $unit = '', $sampleRate = 1.0, $tags = [])
4147
{
4248
$this->node = $node;
4349
$this->value = $value;
@@ -48,6 +54,8 @@ public function __construct($node, $value, $unit = '', $sampleRate = 1.0)
4854
$this->unit = $unit;
4955
}
5056

57+
$this->tags = $tags ?: [];
58+
5159
$this->checkConstructor();
5260
}
5361

@@ -69,6 +77,10 @@ protected function checkConstructor()
6977
if (!is_float($this->sampleRate) or ($this->sampleRate <= 0)) {
7078
throw new Exception('sampleRate has to be a non-zero posivite float');
7179
}
80+
81+
if (!is_array($this->tags)) {
82+
throw new Exception('Tags has to be an array');
83+
}
7284
}
7385

7486
/**
@@ -117,14 +129,40 @@ public function getUnit()
117129
return $this->unit;
118130
}
119131

132+
/**
133+
* @return string Tags formatted for sending
134+
* ex: "server=5,country=fr"
135+
*/
136+
private function getTagsAsString()
137+
{
138+
$tags = array_map(function($k, $v) {
139+
return $k.'='.$v;
140+
}, array_keys($this->tags), $this->tags);
141+
142+
return implode(',', $tags);
143+
}
144+
145+
/**
146+
* @return string the node with tags as string
147+
* ex : node "foo.bar" and tag ["country" => "fr" ] Into "foo.bar,country=fr"
148+
*/
149+
private function getFullNode()
150+
{
151+
if ($this->tags) {
152+
return $this->getNode().','.$this->getTagsAsString();
153+
}
154+
155+
return $this->getNode();
156+
}
157+
120158
/**
121159
* format a statsd message
122160
*
123161
* @return string
124162
*/
125163
public function getStatsdMessage()
126164
{
127-
$message = sprintf('%s:%s|%s', $this->getNode(), $this->getValue(), $this->getUnit());
165+
$message = sprintf('%s:%s|%s', $this->getFullNode(), $this->getValue(), $this->getUnit());
128166
if ($this->useSampleRate()) {
129167
$message .= sprintf('|@%s', $this->getSampleRate());
130168
}

src/M6Web/Component/Tests/Units/MessageEntity.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function testgetStatsdMessage()
5656
->string($messageEntity->getStatsdMessage())
5757
->isEqualTo('raoul.node:1|c|@0.2')
5858
;
59+
60+
// with tags
61+
$this->if($messageEntity = new Statsd\MessageEntity(
62+
'raoul.node', 1, 'c', 0.2, ['foo' => 'bar']))
63+
->then()
64+
->string($messageEntity->getStatsdMessage())
65+
->isEqualTo('raoul.node,foo=bar:1|c|@0.2')
66+
;
5967
}
6068

6169
public function testErrorConstructorStatsdMessage()
@@ -87,5 +95,10 @@ function () {
8795
})
8896
->isInstanceOf('\M6Web\Component\Statsd\Exception');
8997

98+
$this->exception(
99+
function() {
100+
new Statsd\MessageEntity('raoul.node', 1, 'c', 1, 'stringTag');
101+
}
102+
)->isInstanceOf('\M6Web\Component\Statsd\Exception');
90103
}
91-
}
104+
}

0 commit comments

Comments
 (0)