2015-06-10 15:54:06 +02:00
2014-09-11 15:04:12 +02:00
2015-06-10 15:54:06 +02:00
2015-03-24 08:30:55 +01:00
2014-09-11 09:50:43 +02:00
2015-06-10 15:54:06 +02:00
2015-05-18 20:51:43 +02:00

InfluxDB PHP SDK

SensioLabsInsight

  • Master: Build Status
  • Develop: Build Status

Send metrics to InfluxDB and query for any data.

For InfluxDB v0.8 checkout branch 0.3

Install it

Just use composer

php composer.phar require corley/influxdb-sdk:dev-master

Or place it in your require section

{
  "require": {
    // ...
    "corley/influxdb-sdk": "dev-master"
  }
}

Add new points:

$client->mark("app.search", [
    "key" => "this is my search"
]);

Or use InfluxDB direct messages

$client->mark([
    "database" => "mydb",
    "tags" => [
        "dc" => "eu-west-1",
    ],
    "points" => [
        [
            "name" => "vm-serie",
            "fields" => [
                "cpu" => 18.12,
                "free" => 712423,
            ],
        ],
    ]
]);

Retrieve existing points:

$results = $client->query("select * from app.search");

InfluxDB client adapters

Actually we supports two adapters

  • UDP/IP - in order to send data via UDP (datagram)
  • HTTP JSON - in order to send/retrieve using HTTP (connection oriented)

Using UDP/IP Adapter

In order to use the UDP/IP adapter your must have PHP compiled with the sockets extension.

To verify if you have the sockets extension just issue a:

php -m | grep sockets

If you don't have the sockets extension, you can proceed in two ways:

  • Recompile your PHP whith the --enable-sockets flag
  • Or just compile the sockets extension extracting it from the PHP source.
    1. Download the source relative to the PHP version that you on from here
    2. Enter in the ext/sockets directory
    3. Issue a phpize && ./configure && make -j && sudo make install
    4. Add extension=sockets.so to your php.ini

Usage

$options = new Options();
$adapter = new UdpAdapter($options);

$client = new Client();
$client->setAdapter($adapter);

Using HTTP Adapters

Actually Guzzle is used as HTTP client library

<?php
$http = new \GuzzleHttp\Client();

$options = new Options();
$adapter = new HttpAdapter($http, $options);

$client = new Client();
$client->setAdapter($adapter);

Create your client with the factory method

Effectively the client creation is not so simple, for that reason you can you the factory method provided with the library.

$options = [
    "adapter" => [
        "name" => "InfluxDB\\Adapter\\GuzzleAdapter",
        "options" => [
            // guzzle options
        ],
    ],
    "options" => [
        "host" => "my.influx.domain.tld",
        "db" => "mydb",
    ]
];
$client = \InfluxDB\ClientFactory::create($options);

$client->mark("error.404", ["page" => "/a-missing-page"]);

Of course you can always use the DiC or your service manager in order to create a valid client instance.

Query InfluxDB

You can query the time series database using the query method.

$influx->query('select * from "mine"');

You can query the database only if the adapter is queryable (implements QueryableInterface), actually HttpAdapter.

The adapter returns the json decoded body of the InfluxDB response, something like:

array(1) {
  'results' =>
  array(1) {
    [0] =>
    array(1) {
      'series' =>
      array(1) {
        ...
      }
    }
  }
}

Database operations

You can create, list or destroy databases using dedicated methods

$client->getDatabases(); // list all databases
$client->createDatabase("my.name"); // create a new database with name "my.name"
$client->deleteDatabase("my.name"); // delete an existing database with name "my.name"

Actually only queryable adapters can handle databases (implements the QueryableInterface)

Benchmarks

Adapters

The impact using UDP or HTTP adapters

Corley\Benchmarks\InfluxDB\AdapterEvent
    Method Name                Iterations    Average Time      Ops/second
    ------------------------  ------------  --------------    -------------
    sendDataUsingHttpAdapter: [1,000     ] [0.0098177127838] [101.85672]
    sendDataUsingUdpAdapter : [1,000     ] [0.0000694372654] [14,401.48880]
S
Description
No description provided
Readme
446 MiB
Languages
PHP 85.1%
JavaScript 6.6%
Blade 4.3%
CSS 1.8%
Python 1.1%
Other 1%