algatux / influxdb-bundle
官方influxdb/influxdb-php客户端的捆绑服务集成
3.0.0
2024-02-20 16:44 UTC
Requires
- php: ^7.2 || ^8.0
- influxdb/influxdb-php: ^1.2
- symfony/console: ^4.4 || ^5.4 || ^6.4
- symfony/framework-bundle: ^4.4 || ^5.4 || ^6.4
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.1
- phpspec/prophecy: ^1.10
- symfony/form: ^4.4 || ^5.4 || ^6.4
Suggests
- symfony/form: Needed for form types usage
- symfony/proxy-manager-bridge: Needed to lazy-load connection services
This package is auto-updated.
Last update: 2024-09-21 10:52:38 UTC
README
官方influxdb/influxdb-php客户端的捆绑服务集成
安装
首先,您需要通过composer要求此库
composer require algatux/influxdb-bundle
然后,在AppKernel
类上启用捆绑服务
// app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Algatux\InfluxDbBundle\AlgatuxInfluxDbBundle(), ); // ... return $bundles }
配置
以下是配置参考
algatux_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: Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener
如果您只有一个连接需要配置,这可以简化为以下内容
algatux_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: Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener
服务
您可以通过UDP或HTTP直接访问InfluxDB\Database
服务
$httpDatabase = $this->get('algatux_influx_db.connection.http'); // Default HTTP connection $udpDatabase = $this->get('algatux_influx_db.connection.udp'); // Default UDP connection // Same as before. $httpDatabase = $this->get('algatux_influx_db.connection.default.http'); $udpDatabase = $this->get('algatux_influx_db.connection.default.udp');
您也可以通过注册表获取它们
$database = $this->get('algatux_influx_db.connection_registry')->getDefaultHttpConnection(); $database = $this->get('algatux_influx_db.connection_registry')->getDefaultUdpConnection(); // Same as before. $database = $this->get('algatux_influx_db.connection_registry')->getHttpConnection('default'); $database = $this->get('algatux_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'));
命令
提供了一些命令
algatux:influx:database:create
:创建数据库。algatux: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. ]) ;
自定义事件监听器
为了使其更加灵活,您可以覆盖或甚至完全禁用默认事件监听器并实现自己的。
这在需要添加额外的日志记录或错误处理在真正的数据库调用周围时非常有用。
贡献
如果您发现错误或建议新功能,请随时通过打开拉取请求进行贡献。如果您喜欢Docker,此存储库提供了一个带有一些脚本的开发环境,用于准备和使用它。您只需要在系统上安装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
注意:所有这些脚本都旨在在容器外部使用。