babymarkt/influxdb2-bundle

官方 influxdata/influxdb-client-php 客户端包的集成服务包

安装量: 2,631

依赖关系: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放性问题: 0

类型:symfony-bundle

v1.0.0 2022-01-16 16:24 UTC

This package is auto-updated.

Last update: 2024-09-16 22:26:59 UTC


README

将官方 InfluxDB 2.x 客户端 InfluxDB 2.x 客户端 集成到 Symfony Bundle。

Build 1.x codecov Packagist Version License PHP from Packagist PHP from Packagist

注意:请使用此 symfony bundle 与 InfluxDB 2.x 和 InfluxDB 1.8+ 一起使用(请查看客户端仓库中的详细信息)。

安装

您需要通过 composer 需求此库

composer require babymarkt/influxdb2-bundle

如果您正在使用 Symfony Flex,以下操作将自动执行。否则,您必须手动在 bundles.php 中启用此包

// config/bundles.php
return [
    // ...
    Babymarkt\Symfony\Influxdb2Bundle\BabymarktInfluxdb2Bundle::class => ['all' => true],
];

配置

让我们从最小配置开始

babymarkt_influxdb2:
  client:
    connections:
      default:
        url: https://:8086
        bucket: my-bucket
        org: my-org

或者简而言之

babymarkt_influxdb2:
  client:
    url: https://:8086
    bucket: my-bucket
    org: my-org

这为您创建了以下服务

  • 一个 InfluxDb2 客户端 babymarkt_influxdb2.default_client
  • 一个默认的写入 API babymarkt_influxdb2.default_write_api
  • 以及一个默认的查询 API babymarkt_influxdb2.default_query_api

完整配置参考

babymarkt_influxdb2:

  # The clients will be named by connection names.
  client:
    # If not set, the first connection will be taken.
    default_connection: ~
    connections:
      your_client_name:

        # InfluxDB server API url (ex. https://:8086).
        url: ~ # Required

        # Auth token to access your instance.
        token: ~

        # Default destination bucket for writes.
        bucket: ~ # Required

        # Default organization bucket for writes.          
        org: ~ # Required

        # Precision for the unix timestamps within the body line-protocol.
        precision: 'ns'

        # Turn on/off SSL certificate verification. Set to `false` to disable certificate verification.
        verifySSL: true

        # Enable verbose logging of http requests.
        debug: false

        # Log output.
        logFile: ~

        # Default tags.
        tags:
          - first-tag
          - second-tag
          #...

        # The number of seconds to wait while trying to connect to a server. Use 0 to wait indefinitely.
        timeout: 10

        # Pass ~ string to specify an HTTP proxy, or an array to specify different proxies for different protocols.
        proxy: ~

        # GuzzleHttp Client options to allow following redirects.
        allow_redirects:
          max: 5
          strict: ~
          referer: ~
          protocols: [ 'http', 'https' ]

  api:
    write:
      # If not set, the first option_set will be taken.
      default_option_set: ~
      option_sets:
        # The Write-API name
        your-optionset-name:
          # The client connection to use for writes. Defaults to the default_connection.
          connection: ~

          # The Write-API options (see https://github.com/influxdata/influxdb-client-php#writing-data)
          options:
            # (writeType) Type of write SYNCHRONOUS / BATCHING.
            write_type: ~

            # (batchSize) The number of data point to collect in batch.
            batch_size: ~

            # (retryInterval) The number of milliseconds to retry unsuccessful write. The retry interval is "exponentially" used when the InfluxDB server does not specify "Retry-After" header.
            retry_interval: ~

            # (jitterInterval) The number of milliseconds before the data is written increased by a random amount.
            jitter_interval: ~

            # (maxRetries) The number of max retries when write fails.
            max_retries: ~

            # (maxRetryDelay) Maximum delay when retrying write in milliseconds.
            max_retry_delay: ~

            # (maxRetryTime) Maximum total retry timeout in milliseconds.
            max_retry_time: ~

            # (exponentialBase) The base for the exponential retry delay.
            exponential_base: ~

服务使用

客户端

通过类名注入默认客户端

namespace App;
class GenericMetricsWriter {
    public function __construct(protected \InfluxDB2\Client $client) {
        // ...
    }
}

可以通过服务定义注入或从客户端注册表中获取特定客户端

services:
  App\GenericMetricsWriter:
    arguments: [ '@babymarkt_influxdb.your_client_name_client' ]

或通过获取客户端注册表中的客户端

namespace App;
use Babymarkt\Symfony\Influxdb2Bundle\Registry\ClientRegistry;
class GenericMetricsWriter {
    public function __construct(protected ClientRegistry $registry) {
        /** @var \InfluxDB2\Client $client */
        $client = $this->registry->getClient('your_client_name');
        // ...
    }
}

API

同样,您可以获取写入和查询 API

namespace App;
class GenericMetricsWriter {
    // Injects the default Write- and Query-API.
    public function __construct(
        protected \InfluxDB2\WriteApi $writeApi,
        protected \InfluxDB2\QueryApi $queryApi) {
        // ...
    }
}

可以通过服务定义注入或从 API 注册表中获取特定 API

services:
  App\GenericMetricsWriter:
    arguments:
      - '@babymarkt_influxdb.your_name_write_api'
      - '@babymarkt_influxdb.your_name_query_api'

或从 API 注册表获取

namespace App;
use Babymarkt\Symfony\Influxdb2Bundle\Registry\ApiRegistry;
class GenericMetricsWriter {
    public function __construct(protected ApiRegistry $registry) {
        /** @var \InfluxDB2\WriteApi $writeAPi */
        $writeAPi = $this->registry->getWriteApi('your_name');
        /** @var \InfluxDB2\QueryApi $queryApi */
        $queryApi = $this->registry->getQueryApi('your_name');
        // ...
    }
}

额外的 InfluxDB2 API

官方 InfluxDB2 客户端库提供了许多额外的 API 服务。尽管没有为这些服务定义 Symfony 服务,但可以通过客户端实例在任何时候获取它们,无需进一步配置。

以下是一个关于 ReadyService 的示例,该服务返回 InfluxDB2 实例的状态

namespace App;
use InfluxDB2\Service\ReadyService;
class GenericMetricsWriter {
    public function __construct(protected \InfluxDB2\Client $client) {
        /** @var ReadyService $readyService */
        $readyService = $this->client->createService(ReadyService::class);
        $ready = $readyService->getReady();
        echo $ready->getStatus(); // => "ready"
    }
}

有关更多信息,请参阅 InfluxDB2 的 API 文档

控制台命令

此包包含一些控制台命令,用于通过 InfluxDB2 API 管理实体。所有命令都有 --client|-c 选项,用于选择要使用的 InfluxDB2 客户端。

babymarkt_influxdb:setup

为新实例设置初始用户、组织和个人数据存储桶。

Options:
  -c, --client[=CLIENT]      The client to use. [default: "default"]
      --user[=USER]          Initial username
      --password[=PASSWORD]  Initial user password
      --org[=ORG]            Initial organisation name
      --token[=TOKEN]        Initial admin token
      --bucket[=BUCKET]      Initial bucket
      --duration[=DURATION]  Initial bucket duration [default: 0]

babymarkt_influxdb:ping

检查 InfluxDB 实例的状态和版本。

Options:
  -c, --client[=CLIENT]      The client to use. [default: "default"]

babymarkt_influxdb:ready

获取实例启动时的就绪状态。

Options:
  -c, --client[=CLIENT]      The client to use. [default: "default"]

babymarkt_influxdb:buckets:list

列出实例的所有可用数据存储桶。

Options:
  -c, --client[=CLIENT]      The client to use. [default: "default"]

babymarkt_influxdb:buckets:retrieve

提供有关数据存储桶的所有信息。

Arguments:
  bucket                 The bucket name or ID.

Options:
  -c, --client[=CLIENT]  The client to use. [default: "default"]

babymarkt_influxdb:buckets:create

创建新的数据存储桶。

Options:
  -c, --client[=CLIENT]            The client to use. [default: "default"]
      --name[=NAME]                The bucket name.
      --description[=DESCRIPTION]  The bucket description.
      --org[=ORG]                  The organization name or id.
      --duration[=DURATION]        The duration in seconds for how long data will be kept in the database. 0 means infinite. [default: 0]
      --schema-type[=SCHEMA-TYPE]  The schema type. Allowed values are "implicit" or "explicit". [default: "implicit"]

babymarkt_influxdb:buckets:update

更新现有数据存储桶。

Arguments:
  bucket                           The bucket name or ID to update.

Options:
  -c, --client[=CLIENT]            The client to use. [default: "default"]
      --name[=NAME]                The bucket name.
      --description[=DESCRIPTION]  The bucket description.
      --duration[=DURATION]        The duration in seconds for how long data will be kept in the database. 0 means infinite. [default: 0]

babymarkt_influxdb:buckets:delete

删除现有数据存储桶。

Arguments:
  bucket                 The bucket name or id.

Options:
  -c, --client[=CLIENT]  The client to use. [default: "default"]

babymarkt_influxdb:orgs:list

列出实例的所有可用组织。

Options:
  -c, --client[=CLIENT]      The client to use. [default: "default"]

babymarkt_influxdb:orgs:retrieve

提供有关组织的所有信息。

Arguments:
  org                    The organization name or ID.

Options:
  -c, --client[=CLIENT]  The client to use. [default: "default"]

babymarkt_influxdb:orgs:create

创建新的组织。

Options:
  -c, --client[=CLIENT]            The client to use. [default: "default"]
      --name[=NAME]                The organization name.
      --description[=DESCRIPTION]  The organization description.

babymarkt_influxdb:orgs:update

更新现有组织。

Arguments:
  org                              The organization name or ID.

Options:
  -c, --client[=CLIENT]            The client to use. [default: "default"]
      --name[=NAME]                The organization name.
      --description[=DESCRIPTION]  The organization description.

babymarkt_influxdb:orgs:delete

删除现有组织。

Arguments:
  org                    The organization name or ID.

Options:
  -c, --client[=CLIENT]  The client to use. [default: "default"]

贡献

欢迎在 GitHub 上提交错误报告和拉取请求 https://github.com/Baby-Markt/influxdb2-bundle

许可证

此包根据 MIT 许可证 的条款作为开源软件提供。