yproximite / influxdb-bundle
官方 influxdb/influxdb-php 客户端的服务集成包
v4.0.0
2022-05-10 12:32 UTC
Requires
- php: ^8.0
- influxdb/influxdb-php: ^1.2
- symfony/console: ^5.4 || ^6.0
- symfony/framework-bundle: ^5.4 || ^6.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.1
- phpspec/prophecy: ^1.10
- phpspec/prophecy-phpunit: ^2.0
- symfony/form: ^5.4 || ^6.0
- symfony/phpunit-bridge: ^5.4 || ^6.0
Suggests
- symfony/form: Needed for form types usage
- symfony/proxy-manager-bridge: Needed to lazy-load connection services
This package is not auto-updated.
Last update: 2024-09-25 23:03:55 UTC
README
Fork 自 Algatux/influxdb-bundle,似乎不再维护
⚠ 如果 Algatux/influxdb-bundle 更新,则可删除 ⚠
官方 influxdb/influxdb-php 客户端的服务集成包
安装
首先,您需要通过 composer 需求此库
composer require yproximite/influxdb-bundle
然后,在 AppKernel
类上启用此包
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Yproximite\InfluxDbBundle\InfluxDbBundle(), ); // ... return $bundles }
配置
以下是配置参考
influx_db: # If not defined, the first connection will be taken. default_connection: ~ connections: # Prototype name: # Your InfluxDB host address host: ~ # Required # Your InfluxDB database name database: ~ # Required # Set it to true to activate the UDP connection udp: false # Set it to true to enable SSL over HTTP (required for Influx Cloud) ssl: false # Set it to true to activate the ssl verification ssl_verification: false udp_port: 4444 http_port: 8086 username: '' password: '' # Setup timeout or connection timeout (seconds) for your requests timeout: 0.0 connect_timeout: 0.0 # Set it to false to disable the event listener configuration listener_enabled: true # Simple override for the default event listener class (constructor args and methods must match) listener_class: Yproximite\InfluxDbBundle\Events\Listeners\InfluxDbEventListener
如果只需要配置一个连接,这可以简化为以下内容
influx_db: # Your InfluxDB host address host: ~ # Required # Your InfluxDB database name database: ~ # Required # Set it to true to activate the UDP connection udp: false # Set it to true to enable SSL over HTTP (required for Influx Cloud) ssl: false # Set it to true to activate the ssl verification ssl_verification: false udp_port: 4444 http_port: 8086 username: '' password: '' # Setup timeout or connection timeout (seconds) for your requests timeout: 0.0 connect_timeout: 0.0 # Set it to false to disable the event listener configuration listener_enabled: true # Simple override for the default event listener class (constructor args and methods must match) listener_class: Yproximite\InfluxDbBundle\Events\Listeners\InfluxDbEventListener
服务
您可以通过 UDP 或 HTTP 直接访问 InfluxDB\Database
这些服务
$httpDatabase = $this->get('yproximite_influx_db.connection.http'); // Default HTTP connection $udpDatabase = $this->get('yproximite_influx_db.connection.udp'); // Default UDP connection // Same as before. $httpDatabase = $this->get('yproximite_influx_db.connection.default.http'); $udpDatabase = $this->get('yproximite_influx_db.connection.default.udp');
您也可以通过注册表来获取它们
$database = $this->get('yproximite_influx_db.connection_registry')->getDefaultHttpConnection(); $database = $this->get('yproximite_influx_db.connection_registry')->getDefaultUdpConnection(); // Same as before. $database = $this->get('yproximite_influx_db.connection_registry')->getHttpConnection('default'); $database = $this->get('yproximite_influx_db.connection_registry')->getUdpConnection('default');
通过事件将数据发送到 influx db
假设这个集合要发送
$time = new \DateTime(); $points = [new Point( 'test_metric', // name of the measurement 0.64, // the measurement value ['host' => 'server01', 'region' => 'italy'], // optional tags ['cpucount' => rand(1,100), 'memory' => memory_get_usage(true)], // optional additional fields $time->getTimestamp() )];
根据选择的写入协议发送事件实例
// UDP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new UdpEvent($points, Database::PRECISION_SECONDS)); // HTTP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new HttpEvent($points, Database::PRECISION_SECONDS));
或者,如果您更喜欢延迟事件
// Deferred Events // Collect your measurements during the request and make only one write to influxdb. // Deferred events are catched and "stored". Than on the kernel.terminate event one write per // event type and precision will be fired. // UDP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new DeferredUdpEvent($points, Database::PRECISION_SECONDS)); // HTTP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new DeferredHttpEvent($points, Database::PRECISION_SECONDS));
如果您想要写入非默认的连接,您必须指定它
// UDP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new UdpEvent($points, Database::PRECISION_SECONDS, 'other_connection')); // HTTP $container ->get('event_dispatcher') ->dispatch(InfluxDbEvent::NAME, new HttpEvent($points, Database::PRECISION_SECONDS, 'other_connection'));
命令
提供了一些命令
yproximite:influx:database:create
:创建数据库。yproximite:influx:database:drop
:删除数据库。
要获取更多信息,请运行
./app/console help <command>
表单类型
此包提供了一些预定义的表单类型。它们很有用,但不是必需的。
如果您想使用它们,您必须需求 symfony/form
包。
每个类的文档块中都有它们各自的描述。以下是一个简短的用法示例
$form ->add('measurement', MeasurementType::class, [ 'connection' => 'default' // Optional: The connection you want to use. ]) ->add('fields', FieldKeyType::class, [ 'measurement' => 'cpu', // The concerned measurement. 'multiple' => true, // Parent type is ChoiceType. You can use parent option like multiple. ]) ->add('tags', TagKeyType::class, [ 'measurement' => 'cpu', 'exclude_host' => false, // True by default. Excludes the 'host' choice value. 'multiple' => true, ]) ->add('tag_value', TagValueType::class, [ 'measurement' => 'disk', 'tag_key' => 'fstype', // The related tag key. ]) ;
自定义事件监听器
为了使其更灵活,您可以选择覆盖或完全禁用默认的事件监听器并实现自己的。
这在例如您想在实际数据库调用周围添加额外的日志记录或错误处理时很有用。
贡献
如果您发现错误或建议新功能,请随意通过打开 pull request 来贡献。如果您喜欢 docker,此仓库提供了包含一些脚本的 dev 环境,以准备和使用它。您只需要在系统上安装 docker 和 docker-compose。
make setup # will build the needed containers and setup the project
make start # will start the needed containers and put you inside the php-cli container
make test # will launch the test suite
注意:所有这些脚本都旨在在容器外使用。