PHP code example of imiphp / imi-influxdb

1. Go to this page and download the library: Download imiphp/imi-influxdb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imiphp / imi-influxdb example snippets


[
    'influxDB'  => [
        'clients'   => [
            // default 是连接名称,可以随意更改
            'default'   => [
                'host'              => '127.0.0.1', // 主机名
                'port'              => 8086, // 端口
                'username'          => '', // 用户名
                'password'          => '', // 密码
                'defaultDatabase'   => '', // 默认数据库名
                'ssl'               => false, // 是否启用 SSL
                'verifySSL'         => false, // 是否验证 SSL 证书
                'timeout'           => 0, // 超时时间
                'connectTimeout'    => 0, // 连接超时时间
                'path'              =>'/', // 请求路径前缀
                'createDatabase'    => true, // 当数据库不存在时,自动创建数据库
            ],
        ],
        'default'   => 'default', // 默认连接名
    ],
]

use Imi\InfluxDB\InfluxDB;

$client = InfluxDB::getClient(); // 获取默认客户端
$client = InfluxDB::getClient('default'); // 获取指定名称客户端

use Imi\InfluxDB\InfluxDB;

$db = InfluxDB::getDatabase(); // 获取默认数据库名的对象
$db = InfluxDB::getDatabase('dbname'); // 获取指定数据库名的对象
$db = InfluxDB::getDatabase(null, 'default'); // 指定客户端名称

$db = InfluxDB::getDatabase();
$db->query(); // SQL 查询
$db->writePoints(); // 写入数据

[
    'MeterRegistry' => [
        'driver'  => \Imi\InfluxDB\Meter\InfluxDBMeterRegistry::class,
        'options' => [
            'database'   => null, // 使用的数据库名,可以设为null使用连接中配置的数据库名
            'clientName' => null, // 连接客户端名称,可以设为null使用默认客户端名称
            'batch'      => 1000, // 单次推送的记录数量
            'interval'   => 0, // 推送时间周期,单位:秒,默认为0不启用推送,如希望监控生效,请设置一个合理的数值。
            // 所有标签如设为空字符串或 null 则忽略该标签
            'resultTag' => 'result', // 标签名-结果
            'exceptionTag' => 'exception', // 标签名-异常类名
            'instanceTag' => 'instance', // 标签名-实例
            'workerTag' => 'worker', // 标签名-WorkerId
            'instance' => 'imi', // 当前实例名称,每个实例要设置不同
        ],
    ],
]

// 连接配置一定要设置这几项
[
    'port'              => 6041,
    'path'              => '/influxdb/v1/',
    'createDatabase'    => false,
    'username'          => 'root',
    'password'          => 'taosdata',
]

use Imi\Meter\Annotation\Gauged;
use Imi\Meter\Annotation\Histogram;
use Imi\Meter\Annotation\Summary;
use Imi\Meter\Annotation\Timed;
use Imi\Meter\Enum\TimeUnit;

/**
 * @Gauged(name="test_memory_usage", description="memory usage", tags={"workerId"="{returnValue.workerId}"}, value="{returnValue.memory}")
 */
public function recordMemoryUsage(): array
{
    return [
        'workerId' => Worker::getWorkerId(),
        'memory'   => memory_get_usage(),
    ];
}

/**
 * @Timed(name="test_timed", description="memory usage", baseTimeUnit=TimeUnit::MILLI_SECONDS)
 */
public function testTimed(): int
{
    $ms = mt_rand(10, 1000);
    usleep($ms * 1000);

    return $ms;
}

/**
 * @Timed(name="test_timed_histogram", description="memory usage", baseTimeUnit=TimeUnit::MILLI_SECONDS, options={"histogram"=true})
 */
public function testTimedHistogram(): int
{
    $ms = mt_rand(10, 1000);
    usleep($ms * 1000);

    return $ms;
}

/**
 * @Histogram(name="test_histogram", baseTimeUnit=TimeUnit::MILLI_SECONDS)
 */
public function testHistogram(): int
{
    return mt_rand(10, 1000);
}

/**
 * @Summary(name="test_summary", baseTimeUnit=TimeUnit::MILLI_SECONDS)
 */
public function testSummary(): int
{
    return mt_rand(10, 1000);
}

use \Imi\Meter\Facade\MeterRegistry;
use \Imi\Meter\Enum\TimeUnit;

$description = '我是描述';
$tags = ['result' => 'success'];

// counter
MeterRegistry::getDriverInstance()->counter('testCounterManual', $tags, $description)->increment();

// gauge
MeterRegistry::getDriverInstance()->gauge('testGaugeManual', $tags, $description)->record(114514);

// timer
$timer = MeterRegistry::getDriverInstance()->timer('testTimedManual', $tags, $description, TimeUnit::MILLI_SECONDS);
$timerSample = $timer->start();
usleep(mt_rand(10, 1000) * 1000);
$timerSample->stop($timer);

// timer Histogram
$timer = MeterRegistry::getDriverInstance()->timer('testTimedHistogramManual', $tags, $description, TimeUnit::MILLI_SECONDS, [
    'histogram' => true,
]);
$timerSample = $timer->start();
usleep(mt_rand(10, 1000) * 1000); // 你的耗时代码
$timerSample->stop($timer);

// Histogram
$value = 114514;
MeterRegistry::getDriverInstance()->histogram('testHistogramManual', $tags, $description)->record($value);

// Summary
$value = 114514;
MeterRegistry::getDriverInstance()->summary('testHistogramManual', $tags, $description)->record($value);