From 94fe0c0d649384f13527eff556136726f6c9803e Mon Sep 17 00:00:00 2001 From: Harry Bragg Date: Mon, 29 Jul 2019 17:45:12 +0100 Subject: [PATCH] support dd env vars (#33) --- src/Client.php | 12 +++-- tests/unit/EnvConfigurationTest.php | 82 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 tests/unit/EnvConfigurationTest.php diff --git a/src/Client.php b/src/Client.php index c551160..895ed79 100644 --- a/src/Client.php +++ b/src/Client.php @@ -238,7 +238,7 @@ public function __toString() */ public function configure(array $options = []) { - $setOption = function ($name, $type = null) use ($options) { + $setOption = function ($name, $type = null, $default = false) use ($options) { if (isset($options[$name])) { if (!is_null($type) && (gettype($options[$name]) != $type)) { throw new ConfigurationException($this->instanceId, sprintf( @@ -249,17 +249,23 @@ public function configure(array $options = []) )); } $this->{$name} = $options[$name]; + } elseif ($default) { + $this->{$name} = $default; } }; - $setOption('host', 'string'); - $setOption('port'); + $setOption('host', 'string', getenv('DD_AGENT_HOST')); + $setOption('port', null, getenv('DD_DOGSTATSD_PORT')); $setOption('namespace', 'string'); $setOption('timeout'); $setOption('onError', 'string'); $setOption('dataDog', 'boolean'); $setOption('tags', 'array'); + if (getenv('DD_ENTITY_ID')) { + $this->tags['dd.internal.entity_id'] = getenv('DD_ENTITY_ID'); + } + if (isset($options['tagProcessors']) && is_array($options['tagProcessors'])) { foreach ($options['tagProcessors'] as $tagProcessor) { if (!is_callable($tagProcessor)) { diff --git a/tests/unit/EnvConfigurationTest.php b/tests/unit/EnvConfigurationTest.php new file mode 100644 index 0000000..8ba7b91 --- /dev/null +++ b/tests/unit/EnvConfigurationTest.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license https://github.com/graze/dog-statsd/blob/master/LICENSE.md + * @link https://github.com/graze/dog-statsd + */ + +namespace Graze\DogStatsD\Test\Unit; + +use Graze\DogStatsD\Test\TestCase; + +class EnvConfigurationTest extends TestCase +{ + public function tearDown() + { + putenv('DD_AGENT_HOST='); + putenv('DD_DOGSTATSD_PORT='); + putenv('DD_ENTITY_ID='); + } + + public function testHost() + { + putenv('DD_AGENT_HOST=127.0.0.1'); + $this->client->configure(); + + $this->assertEquals('127.0.0.1', $this->client->getHost()); + } + + public function testPort() + { + putenv('DD_DOGSTATSD_PORT=12434'); + $this->client->configure(); + + $this->assertEquals(12434, $this->client->getPort()); + } + + /** + * @expectedException \Graze\DogStatsD\Exception\ConfigurationException + * @expectedExceptionMessage Option: Port is invalid or is out of range + */ + public function testLargePortWillThrowAnException() + { + putenv('DD_DOGSTATSD_PORT=65536'); + $this->client->configure(); + } + + /** + * @expectedException \Graze\DogStatsD\Exception\ConfigurationException + * @expectedExceptionMessage Option: Port is invalid or is out of range + */ + public function testStringPortWillThrowAnException() + { + putenv('DD_DOGSTATSD_PORT=not-integer'); + $this->client->configure(); + } + + public function testTags() + { + putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8'); + $this->client->configure(); + + $this->client->gauge('test_metric', 456); + $this->assertEquals('test_metric:456|g|#dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage()); + } + + public function testTagsAppended() + { + putenv('DD_ENTITY_ID=f87dsf7dsf9s7d9f8'); + $this->client->configure([ + 'tags' => ['key' => 'value'], + ]); + + $this->client->gauge('test_metric', 456); + $this->assertEquals('test_metric:456|g|#key:value,dd.internal.entity_id:f87dsf7dsf9s7d9f8', $this->client->getLastMessage()); + } +}