From da239ecf8ed39b6c8c4c1f6fd759e7e9cc59ce12 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sun, 21 Jun 2015 17:33:46 +0200 Subject: [PATCH 1/5] Allow URL prefixes This should fixes #34 --- .travis.yml | 4 ++++ scripts/nginx_proxy.conf | 25 +++++++++++++++++++++++++ src/Adapter/GuzzleAdapter.php | 10 ++++++---- src/Options.php | 14 ++++++++++++++ tests/ClientTest.php | 19 +++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 scripts/nginx_proxy.conf diff --git a/.travis.yml b/.travis.yml index af9b34c6d..72b9027c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,10 @@ before_install: - sudo cp ./scripts/influxdb_conf.toml /etc/opt/influxdb/influxdb.conf - travis_retry sudo service influxdb restart - sudo service influxdb status + - sudo apt-get update + - sudo apt-get install -y nginx + - sudo cp scripts/nginx_proxy.conf /etc/nginx/conf.d/proxy.conf + - travis_retry sudo service nginx restart before_script: - composer selfupdate diff --git a/scripts/nginx_proxy.conf b/scripts/nginx_proxy.conf new file mode 100644 index 000000000..f92d10a30 --- /dev/null +++ b/scripts/nginx_proxy.conf @@ -0,0 +1,25 @@ +server { + listen 9000 default_server; + listen [::]:9000 default_server ipv6only=on; + + root /usr/share/nginx/html; + index index.html index.htm; + + server_name localhost; + + location / { + try_files $uri $uri/ =404; + } + + location /influxdb { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + + rewrite ^/influxdb/?(.*) /$1 break; + + proxy_pass http://localhost:8086; + proxy_redirect off; + } +} diff --git a/src/Adapter/GuzzleAdapter.php b/src/Adapter/GuzzleAdapter.php index d55352101..35a3a6db4 100644 --- a/src/Adapter/GuzzleAdapter.php +++ b/src/Adapter/GuzzleAdapter.php @@ -54,20 +54,22 @@ class GuzzleAdapter extends AdapterAbstract implements QueryableInterface protected function getHttpSeriesEndpoint() { return sprintf( - "%s://%s:%d/write", + "%s://%s:%d%s/write", $this->getOptions()->getProtocol(), $this->getOptions()->getHost(), - $this->getOptions()->getPort() + $this->getOptions()->getPort(), + $this->getOptions()->getPrefix() ); } protected function getHttpQueryEndpoint($name = false) { $url = sprintf( - "%s://%s:%d/query", + "%s://%s:%d%s/query", $this->getOptions()->getProtocol(), $this->getOptions()->getHost(), - $this->getOptions()->getPort() + $this->getOptions()->getPort(), + $this->getOptions()->getPrefix() ); return $url; diff --git a/src/Options.php b/src/Options.php index c472c5235..15b1581f2 100644 --- a/src/Options.php +++ b/src/Options.php @@ -23,6 +23,8 @@ class Options private $tags; + private $prefix; + public function __construct() { $this->setHost("localhost"); @@ -30,11 +32,23 @@ class Options $this->setUsername("root"); $this->setPassword("root"); $this->setProtocol("http"); + $this->setPrefix(""); $this->setRetentionPolicy("default"); $this->setTags([]); } + public function getPrefix() + { + return $this->prefix; + } + + public function setPrefix($prefix) + { + $this->prefix = $prefix; + return $this; + } + public function getTags() { return $this->tags; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index ad5367b2e..cad9201bf 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -64,6 +64,25 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]); } + /** + * @group tcp + * @group proxy + */ + public function testGuzzleHttpApiWorksCorrectlyWithProxies() + { + $this->options->setHost("localhost"); + $this->options->setPort(9000); + $this->options->setPrefix("/influxdb"); + $this->object->mark("tcp.test", ["mark" => "element"]); + + sleep(2); + + $body = $this->object->query("select * from \"tcp.test\""); + $this->assertCount(1, $body["results"][0]["series"][0]["values"]); + $this->assertEquals("mark", $body["results"][0]["series"][0]["columns"][1]); + $this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]); + } + /** * @group tcp */ From a323409dbcd3f5b37f735f4c55c1621c08697636 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sun, 21 Jun 2015 17:46:09 +0200 Subject: [PATCH 2/5] Added proxies in our doc file --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index bd9a47b19..b38953782 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,20 @@ $options->setRetentionPolicy("myPolicy"); In that way the SDK use that policy instead of `default` policy. +## Proxies and InfluxDB + +If you proxy your InfluxDB typically you have a prefix in your endpoints. + +``` +$option->setHost("proxy.influxdb.tld"); +$option->setPort(80); +$option->setPrefix("/influxdb"); // your prefix is: /influxdb + +// final url will be: http://proxy.influxdb.tld:80/influxdb/write + +$client->mark("serie", ["data" => "my-data"]); +``` + ## Benchmarks Simple benchmarks executed on a Sony Vaio T13 (SVT1311C5E) From 27c19c9e4b0528712e52769e60d1571c85a7aa69 Mon Sep 17 00:00:00 2001 From: Evan Darwin Date: Sat, 20 Jun 2015 13:07:40 -0700 Subject: [PATCH 3/5] Fix issue #27 test Conflicts: tests/ClientTest.php --- tests/ClientTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index cad9201bf..f44a2b1a0 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -378,6 +378,29 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]); } + /** + * Test that we handle socket problems correctly in the UDP + * adapter, and that they don't inturrupt the user's application. + * + * @group udp + */ + public function testReplicateIssue27() + { + $options = new \InfluxDB\Options(); + + // Configure options + $options->setHost('172.16.1.182'); + $options->setPort(4444); + $options->setDatabase('...'); + $options->setUsername('root'); + $options->setPassword('root'); + + $httpAdapter = new \InfluxDB\Adapter\UdpAdapter($options); + + $client = new \InfluxDB\Client($httpAdapter); + $client->mark("udp.test", ["mark" => "element"]); + } + public function testListActiveDatabses() { $databases = $this->object->getDatabases(); From c6e7b744c309f5862b4bffe0236c72b3b868fd90 Mon Sep 17 00:00:00 2001 From: Evan Darwin Date: Sat, 20 Jun 2015 13:03:09 -0700 Subject: [PATCH 4/5] Silently handle socket errors in the UdpAdapter (closes #27), add testcase Conflicts: tests/ClientTest.php --- src/Adapter/UdpAdapter.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Adapter/UdpAdapter.php b/src/Adapter/UdpAdapter.php index e497b13b7..2f9930087 100644 --- a/src/Adapter/UdpAdapter.php +++ b/src/Adapter/UdpAdapter.php @@ -15,9 +15,18 @@ final class UdpAdapter extends AdapterAbstract public function write($message) { + // Create a handler in order to handle the 'Host is down' message + set_error_handler(function() { + // Suppress the error, this is the UDP adapter and if we can't send + // it then we shouldn't inturrupt their application. + }); + $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_sendto($socket, $message, strlen($message), 0, $this->getOptions()->getHost(), $this->getOptions()->getPort()); socket_close($socket); + + // Remove our error handler. + restore_error_handler(); } private function serialize(array $message) From 8e57dd7eb22e231bbbbc417055db272e60e7b76b Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sun, 21 Jun 2015 17:58:29 +0200 Subject: [PATCH 5/5] Bumped version 0.5.0 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 0bfccb080..8f0916f76 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.5 +0.5.0