Skip to content

Commit 94fe0c0

Browse files
author
Harry Bragg
authored
support dd env vars (#33)
1 parent 64c6dbd commit 94fe0c0

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

src/Client.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function __toString()
238238
*/
239239
public function configure(array $options = [])
240240
{
241-
$setOption = function ($name, $type = null) use ($options) {
241+
$setOption = function ($name, $type = null, $default = false) use ($options) {
242242
if (isset($options[$name])) {
243243
if (!is_null($type) && (gettype($options[$name]) != $type)) {
244244
throw new ConfigurationException($this->instanceId, sprintf(
@@ -249,17 +249,23 @@ public function configure(array $options = [])
249249
));
250250
}
251251
$this->{$name} = $options[$name];
252+
} elseif ($default) {
253+
$this->{$name} = $default;
252254
}
253255
};
254256

255-
$setOption('host', 'string');
256-
$setOption('port');
257+
$setOption('host', 'string', getenv('DD_AGENT_HOST'));
258+
$setOption('port', null, getenv('DD_DOGSTATSD_PORT'));
257259
$setOption('namespace', 'string');
258260
$setOption('timeout');
259261
$setOption('onError', 'string');
260262
$setOption('dataDog', 'boolean');
261263
$setOption('tags', 'array');
262264

265+
if (getenv('DD_ENTITY_ID')) {
266+
$this->tags['dd.internal.entity_id'] = getenv('DD_ENTITY_ID');
267+
}
268+
263269
if (isset($options['tagProcessors']) && is_array($options['tagProcessors'])) {
264270
foreach ($options['tagProcessors'] as $tagProcessor) {
265271
if (!is_callable($tagProcessor)) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* This file is part of graze/dog-statsd
4+
*
5+
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md
11+
* @link https://github.com/graze/dog-statsd
12+
*/
13+
14+
namespace Graze\DogStatsD\Test\Unit;
15+
16+
use Graze\DogStatsD\Test\TestCase;
17+
18+
class EnvConfigurationTest extends TestCase
19+
{
20+
public function tearDown()
21+
{
22+
putenv('DD_AGENT_HOST=');
23+
putenv('DD_DOGSTATSD_PORT=');
24+
putenv('DD_ENTITY_ID=');
25+
}
26+
27+
public function testHost()
28+
{
29+
putenv('DD_AGENT_HOST=127.0.0.1');
30+
$this->client->configure();
31+
32+
$this->assertEquals('127.0.0.1', $this->client->getHost());
33+
}
34+
35+
public function testPort()
36+
{
37+
putenv('DD_DOGSTATSD_PORT=12434');
38+
$this->client->configure();
39+
40+
$this->assertEquals(12434, $this->client->getPort());
41+
}
42+
43+
/**
44+
* @expectedException \Graze\DogStatsD\Exception\ConfigurationException
45+
* @expectedExceptionMessage Option: Port is invalid or is out of range
46+
*/
47+
public function testLargePortWillThrowAnException()
48+
{
49+
putenv('DD_DOGSTATSD_PORT=65536');
50+
$this->client->configure();
51+
}
52+
53+
/**
54+
* @expectedException \Graze\DogStatsD\Exception\ConfigurationException
55+
* @expectedExceptionMessage Option: Port is invalid or is out of range
56+
*/
57+
public function testStringPortWillThrowAnException()
58+
{
59+
putenv('DD_DOGSTATSD_PORT=not-integer');
60+
$this->client->configure();
61+
}
62+
63+
public function testTags()
64+
{
65+
putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8');
66+
$this->client->configure();
67+
68+
$this->client->gauge('test_metric', 456);
69+
$this->assertEquals('test_metric:456|g|#dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage());
70+
}
71+
72+
public function testTagsAppended()
73+
{
74+
putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8');
75+
$this->client->configure([
76+
'tags' => ['key' => 'value'],
77+
]);
78+
79+
$this->client->gauge('test_metric', 456);
80+
$this->assertEquals('test_metric:456|g|#key:value,dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage());
81+
}
82+
}

0 commit comments

Comments
 (0)