corley/influxdb-sdk

将您的应用程序指标发送到InfluxDB

1.0.0 2017-07-01 12:50 UTC

README

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License

将指标发送到InfluxDB并进行查询。

本项目支持InfluxDB API >= 0.9 - 对于InfluxDB v0.8,请检出分支0.3(不再支持)

支持的适配器

  • UDP/IP
  • HTTP(通过GuzzleHTTP版本:~5,~6) - 对于Guzzle 4支持,请检出分支0.9(不再支持)

安装它

只需使用composer

$ composer require corley/influxdb-sdk:~1

或将其添加到您的composer.json文件中

{
  "require": {
    "corley/influxdb-sdk": "~1"
  }
}

使用它

添加新的点

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

或使用InfluxDB直接消息

$client->mark([
    "tags" => [
        "dc" => "eu-west-1",
    ],
    "points" => [
        [
            "measurement" => "instance",
            "fields" => [
                "cpu" => 18.12,
                "free" => 712423,
            ],
        ],
    ]
]);

检索现有点

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

InfluxDB客户端适配器

实际上我们支持两种网络适配器

  • UDP/IP - 为了通过UDP/IP(数据报)发送数据
  • HTTP - 为了使用HTTP消息(面向连接)发送/检索

使用UDP/IP适配器

为了使用UDP/IP适配器,您必须使用PHP编译了sockets扩展。

用法

$reader = ...

$options = new Udp\Options();
$writer = new Udp\Writer($options);

$client = new Client($reader, $writer);

UDP/IP选项集只有hostport属性,您直接在InfluxDB配置中配置数据库和保留策略

使用HTTP适配器

实际上Guzzle用作HTTP客户端库

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

$writer = ...

$options = new Http\Options();
$reader = new Http\Reader($http, $options);

$client = new Client($reader, $writer);

混合读取器和写入器

当然,您可以将Udp\Ip和Http适配器混合使用,以便使用UDP/IP协议写入数据点,但使用HTTP读取信息。

$reader = new Http\Reader($http, $httpOptions);
$writer = new Udp\Writer($udpOptions);
$client = new Client($reader, $writer);

$client->mark(...); // Use UDP/IP support
$client->query("SELECT * FROM my_serie"); // Use HTTP support

或仅使用HTTP

$reader = new Http\Reader($http, $options);
$writer = new Http\Writer($http, $options);
$client = new Client($reader, $writer);

$client->mark(...); // Use HTTP support
$client->query("SELECT * FROM my_serie"); // Use HTTP support

查询InfluxDB

您可以使用查询方法查询时间序列数据库。

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

适配器返回InfluxDB响应的json解码后的主体,类似于

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

如果您希望获得比原始响应更简单的响应,您可以使用corley/influxdb-http-handlers,该工具将原始InfluxDB响应转换为更简单的响应,类似于

array(1) {
  'serie_name' => array(2) {
    [0] => array(4) {
      'time' => string(30) "2015-09-09T20:42:07.927267636Z"
      'value1' => int(1)
      'value2' => int(2)
      'valueS' => string(6) "string"
    }
    [1] => array(4) {
      'time' => string(30) "2015-09-09T20:42:51.332853369Z"
      'value1' => int(2)
      'value2' => int(4)
      'valueS' => string(11) "another-one"
    }
  }
}

全局标签和保留策略

您可以为SDK设置一组默认标签,SDK将添加到您的指标中

$options = new Http\Options();
$options->setTags([
    "env" => "prod",
    "region" => "eu-west-1",
]);

SDK在添加标签时标记所有点。

您可以使用以下方式设置默认保留策略

$options->setRetentionPolicy("myPolicy");

这样,SDK将使用该策略而不是default策略。

代理和InfluxDB

如果您代理InfluxDB,通常您的端点前有一个前缀。

$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"]);

数据类型管理

从InfluxDB版本>=0.9.3开始,整型类型以尾随的i标记。此库支持数据类型,默认情况下,PHP类型映射到influxdb的方式如下

并且结果映射将是

$client->mark("serie", [
    "value" => 12,  // Marked as int64
    "elem" => 12.4, // Marked as float64
]);

强制数据类型

如果您想确保类型被正确解析,您可以在发送操作期间直接强制它

$client->mark("serie", [
    "value"  => new IntType(12),  // Marked as int64
    "elem"   => new FloatType(12.4), // Marked as float64
    "status" => new BoolType(true), // Marked as boolean
    "line"   => new StringType("12w"), // Marked as string
]);

查询构建器

对查询构建器感兴趣吗?

https://github.com/corley/dbal-influxdb

感谢Doctrine DBAL(抽象层),您可以使用查询构建器

$qb = $conn->createQueryBuilder();

$qb->select("*")
    ->from("cpu_load_short")
    ->where("time = ?")
    ->setParameter(0, 1434055562000000000);

$data = $qb->execute();
foreach ($data->fetchAll() as $element) {
    // Use your element
}
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array(
    'dbname' => 'mydb',
    'user' => 'root',
    'password' => 'root',
    'host' => 'localhost',
    'port' => 8086,
    "driverClass" => "Corley\\DBAL\\Driver\\InfluxDB",
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);

数据库操作和自定义查询

InfluxDB\Client类本身不支持任何数据库操作。这意味着您没有创建新数据库或列出实际数据库等的辅助函数。感谢InfluxDB\Manager,您可以使用预设的查询和创建自己的个人查询,该查询将在Influxdb实例上运行。

创建管理器

Manager实例非常简单,您必须使用客户端实例创建它。

$manager = new Manager($client); // InfluxDB\Client instance

附加新查询

管理器允许通过辅助方法addQuery附加新查询。

$manager->addQuery("getExceptionsInMinutes", function($minutes) {
    return "SELECT * FROM app_exceptions WHERE time > now() - {$minutes}m";
});

$manager->getExceptionsInMinutes(10); // The callable name

如您所见,您必须标记匿名函数并使用管理器重用它。

为了收集和重用自定义查询,您可以定义查询对象

class GetExceptionsInMinutes
{
    public function __invoke($minutes)
    {
        return "SELECT * FROM app_exceptions WHERE time > now() - {$minutes}m";
    }

    public function __toString()
    {
        return "getExceptionsInMinutes";
    }
}

$manager->addQuery(new GetExceptionsInMinutes());

$manager->getExceptionsInMinutes(10); //Use the query

如你所见,有效的查询命令应该是通过__invoke方法可调用的,并且也应该通过__toString方法可序列化为字符串

现有查询

本项目提供了一套预设的有效查询

  • 创建新的数据库 InfluxDB\Query\CreateDatabase
  • 删除现有数据库 InfluxDB\Query\DeleteDatabase
  • 列出现有数据库 InfluxDB\Query\GetDatabases
$manager->addQuery(new CreateDatabase());
$manager->addQuery(new DeleteDatabase());
$manager->addQuery(new GetDatabases());

常见问题解答

为PHP添加套接字支持

为了验证您是否有sockets扩展,只需发出一个

php -m | grep sockets

如果您没有sockets扩展,您可以选择以下两种方式之一

  • 重新编译PHP,并添加--enable-sockets标志
  • 或者,只需从PHP源代码中提取并编译sockets扩展。
  1. 这里下载与您当前使用的PHP版本相关的源代码
  2. 进入ext/sockets目录
  3. 发出命令:phpize && ./configure && make -j && sudo make install
  4. extension=sockets.so添加到您的php.ini文件中

Guzzle 4支持

我们放弃了Guzzle 4的支持,但我们测试了它作为HTTP适配器在PHP 7.0及以下版本中的工作情况,与本项目版本0.9.3稳定兼容。

如果您需要Guzzle 4作为HTTP适配器,请为0.9.3版本要求

compose require corley/influxdb-sdk:0.9.*