diff --git a/README.md b/README.md index e0e8acb46..a9de6b906 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ $client = new InfluxDB\Client($host, $port); This will create a new client object which you can use to read and write points to InfluxDB. -It's also possible to create a client from a DSN: +It's also possible to create a client from a DSN (Data Source Name): ```php @@ -91,14 +91,14 @@ Writing data is done by providing an array of points to the writePoints method o new Point( 'test_metric', 0.64, - array('host' => 'server01', 'region' => 'us-west'), - array('cpucount' => 10), + ['host' => 'server01', 'region' => 'us-west'], + ['cpucount' => 10], 1435255849 // Time precision has to be set to seconds! ), new Point( 'test_metric', 0.84, - array('host' => 'server01', 'region' => 'us-west'), + ['host' => 'server01', 'region' => 'us-west'], array('cpucount' => 10), 1435255850 // Time precision has to be set to seconds! ) @@ -129,13 +129,38 @@ First, set your InfluxDB host to support incoming UDP sockets: Then, configure the UDP driver in the client: ```php - // set the udp driver + + // set the UDP driver in the client $client->setDriver(new \InfluxDB\Driver\UDP($client->getHost(), 4444)); + $points = [ + new Point( + 'test_metric', + 0.84, + ['host' => 'server01', 'region' => 'us-west'], + ['cpucount' => 10], + exec('date +%s%N') // this will produce a nanosecond timestamp in Linux operating systems + ) + ]; + // now just write your points like you normally would - $result = $database->writePoints($points, Database::PRECISION_SECONDS); + $result = $database->writePoints($points); ``` +Or simply use a DSN (Data Source Name) to send metrics using UDP: + +```php + + // get a database object using a DSN (Data Source Name) + $database = \InfluxDB\Client::fromDSN('udp+influxdb://username:pass@localhost:4444/test123'); + + // write your points + $result = $database->writePoints($points); +``` + +*Note:* It is import to note that precision will be *ignored* when you use UDP. You should always use nanosecond +precision when writing data to InfluxDB using UDP. + #### Timestamp precision It's important to provide the correct precision when adding a timestamp to a Point object. This is because diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 2cb932d1c..faec328cf 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -6,6 +6,7 @@ use InfluxDB\Client\Exception as ClientException; use InfluxDB\Driver\DriverInterface; use InfluxDB\Driver\Guzzle; use InfluxDB\Driver\QueryDriverInterface; +use InfluxDB\Driver\UDP; /** * Class Client @@ -165,10 +166,8 @@ class Client ]); try { - // send the data - $driver->send(); - - return $driver->getResultSet(); + // perform the query and return the resultset + return $driver->query(); } catch (\Exception $e) { throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage())); @@ -200,12 +199,16 @@ class Client /** * Build the client from a dsn - * Example: https+influxdb://username:pass@localhost:8086/databasename', timeout=5 + * Examples: + * + * https+influxdb://username:pass@localhost:8086/databasename + * udp+influxdb://username:pass@localhost:4444/databasename * * @param string $dsn * @param int $timeout * @param bool $verifySSL - * @return Client|Database + * +*@return Client|Database * @throws ClientException */ public static function fromDSN($dsn, $timeout = 0, $verifySSL = false) @@ -217,12 +220,12 @@ class Client $scheme = $schemeInfo[0]; if (isset($schemeInfo[1])) { - $modifier = $schemeInfo[0]; + $modifier = strtolower($schemeInfo[0]); $scheme = $schemeInfo[1]; } if ($scheme != 'influxdb') { - throw new ClientException(sprintf('%s is not a valid scheme', $scheme)); + throw new ClientException($scheme . ' is not a valid scheme'); } $ssl = $modifier === 'https' ? true : false; @@ -238,6 +241,11 @@ class Client $timeout ); + // set the UDP driver when the DSN specifies UDP + if ($modifier == 'udp') { + $client->setDriver(new UDP($connParams['host'], $connParams['port'])); + } + return ($dbName ? $client->selectDB($dbName) : $client); } diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 601ca8ec5..e75300261 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -133,7 +133,7 @@ class Database ]); // send the points to influxDB - $driver->send(implode(PHP_EOL, $payload)); + $driver->write(implode(PHP_EOL, $payload)); return $driver->isSuccess(); diff --git a/src/InfluxDB/Driver/DriverInterface.php b/src/InfluxDB/Driver/DriverInterface.php index cb1eddfbd..5400ba051 100644 --- a/src/InfluxDB/Driver/DriverInterface.php +++ b/src/InfluxDB/Driver/DriverInterface.php @@ -38,7 +38,7 @@ interface DriverInterface * * @return mixed */ - public function send($data = null); + public function write($data = null); /** * Should return if sending the data was successful diff --git a/src/InfluxDB/Driver/Guzzle.php b/src/InfluxDB/Driver/Guzzle.php index a9b746864..d014c1f3b 100644 --- a/src/InfluxDB/Driver/Guzzle.php +++ b/src/InfluxDB/Driver/Guzzle.php @@ -10,6 +10,11 @@ use Guzzle\Http\Message\Response; use GuzzleHttp\Psr7\Request; use InfluxDB\ResultSet; +/** + * Class Guzzle + * + * @package InfluxDB\Driver + */ class Guzzle implements DriverInterface, QueryDriverInterface { @@ -70,11 +75,22 @@ class Guzzle implements DriverInterface, QueryDriverInterface * @throws Exception * @return mixed */ - public function send($data = null) + public function write($data = null) { - $requestObject = new Request($this->parameters['method'], $this->parameters['url'], [], $data); + $this->response = $this->httpClient->post($this->parameters['url'], ['body' => $data]); + } + + /** + * @return ResultSet + */ + public function query() + { + $response = $this->httpClient->get($this->parameters['url']); + + $raw = (string) $response->getBody(); + + return new ResultSet($raw); - $this->response = $this->httpClient->send($requestObject); } /** @@ -86,14 +102,4 @@ class Guzzle implements DriverInterface, QueryDriverInterface { return in_array($this->response->getStatusCode(), ['200', '204']); } - - /** - * @return ResultSet - */ - public function getResultSet() - { - $raw = (string) $this->response->getBody(true); - - return new ResultSet($raw); - } } diff --git a/src/InfluxDB/Driver/QueryDriverInterface.php b/src/InfluxDB/Driver/QueryDriverInterface.php index 13830e83a..0966cecc3 100644 --- a/src/InfluxDB/Driver/QueryDriverInterface.php +++ b/src/InfluxDB/Driver/QueryDriverInterface.php @@ -18,5 +18,5 @@ interface QueryDriverInterface /** * @return ResultSet */ - public function getResultSet(); + public function query(); } \ No newline at end of file diff --git a/src/InfluxDB/Driver/UDP.php b/src/InfluxDB/Driver/UDP.php index 1805841f9..ac2be0ebb 100644 --- a/src/InfluxDB/Driver/UDP.php +++ b/src/InfluxDB/Driver/UDP.php @@ -35,13 +35,7 @@ class UDP implements DriverInterface $this->config['host'] = $host; $this->config['port'] = $port; - // create a socket connection to check if InfluxDB is alive - $socket = fsockopen('udp://' .$host, $port); - if (!$socket) { - throw new Exception('There is no InfluxDB UDP service running on port '. $port); - } - fclose($socket); } /** @@ -71,11 +65,15 @@ class UDP implements DriverInterface * * @return mixed */ - public function send($data = null) + public function write($data = null) { - $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - socket_sendto($socket, $data, strlen($data), 0, $this->config['host'], $this->config['port']); - socket_close($socket); + + $host = sprintf('udp://%s:%d', $this->config['host'], $this->config['port']); + + // stream the data using UDP and suppress any errors + $stream = @stream_socket_client($host); + @stream_socket_sendto($stream, $data); + @fclose($stream); return true; } diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 2b6e8148b..095d534b0 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -54,7 +54,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase } /** - * @return \Guzzle\Http\Client + * @return GuzzleClient */ public static function buildHttpMockClient($body) {