louiswe/laravel-influxdb

InfluxDB 服务提供程序用于 Laravel

1.1.4 2022-04-05 19:28 UTC

This package is auto-updated.

Last update: 2024-09-06 00:53:29 UTC


README

安装

1. 通过 Composer 安装

composer require louiswe/laravel-influxdb

2. 配置

INFLUXDB_HOST = 127.0.0.1
INFLUXDB_PORT = 8086
INFLUXDB_PORT_UDP = 8094
INFLUXDB_USER =
INFLUXDB_SSL = false
INFLUXDB_TOKEN = ""
INFLUXDB_ORGANISATION =
INFLUXDB_DEFAULT_BUCKET =
INFLUXDB_VERIFY_SSL = false
INFLUXDB_TIMEOUT = 1

同时需要在 .env 文件中设置 APP_ENV

要使用 udp,必须安装 telegraf 并激活 socket 监听插件
https://influxdb.org.cn/blog/telegraf-socket-listener-input-plugin/

3. 发布 ServiceProvider

php artisan vendor:publish

ServiceProvider 将设置库,使您可以在项目的任何地方使用它

4. 添加默认标签(可选)

要添加默认标签,请修改 config/influxdb.php

return [
    // ...
    'tags'        => ['environment' => 'development', 'host' => 'server1']
    // ...
];

用法

有一些预定义函数可以使将数据写入 InfluxDB 更加简单,如果您有任何建议如何进一步改进,请随时告诉我。

use InfluxDB2\Point;
use louiswe\InfluxDB\Facades\InfluxDB;

// Increment success or failure measuremens
InfluxDB::increment("login"); // ["success" => 1]
InfluxDB::increment("login", false); // ["failure" => 1]


$tags = ['environment' => 'development', 'server' => 1];
$fields = ['success' => 1];

// Write Point to InfluxDB
$point = new Point('login', $tags, $fields);
InfluxDB::writePoint($point);

// Write directly to InfluxDB
InfluxDB::write('login', $tags, $fields);

此外,您可以直接访问库的influxdb-client-php 方法,例如

use louiswe\InfluxDB\Facades\InfluxDB;

InfluxDB::createQueryApi();
InfluxDB::createUdpWriter();