{"id":19756830,"url":"https://github.com/imiphp/imi-influxdb","last_synced_at":"2025-09-05T19:41:10.775Z","repository":{"id":62096558,"uuid":"557739195","full_name":"imiphp/imi-influxdb","owner":"imiphp","description":null,"archived":false,"fork":false,"pushed_at":"2022-10-28T06:59:23.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.1","last_synced_at":"2024-05-23T01:31:15.241Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imiphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-26T08:00:53.000Z","updated_at":"2023-11-11T11:07:23.000Z","dependencies_parsed_at":"2022-10-26T10:16:18.452Z","dependency_job_id":null,"html_url":"https://github.com/imiphp/imi-influxdb","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-influxdb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-influxdb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-influxdb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-influxdb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imiphp","download_url":"https://codeload.github.com/imiphp/imi-influxdb/tar.gz/refs/heads/2.1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241086904,"owners_count":19907357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T03:17:06.378Z","updated_at":"2025-02-28T01:53:06.902Z","avatar_url":"https://github.com/imiphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# imi-influxdb\n\n[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-influxdb.svg)](https://packagist.org/packages/imiphp/imi-influxdb)\n[![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/)\n[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.8.0-brightgreen.svg)](https://github.com/swoole/swoole-src)\n[![imi License](https://img.shields.io/badge/license-MulanPSL%202.0-brightgreen.svg)](https://github.com/imiphp/imi-influxdb/blob/2.1/LICENSE)\n\n## 介绍\n\n此项目是 imi 框架的 InfluxDB 组件。\n\n自动引入 `yurunsoft/influxdb-orm`，可以直接使用该 ORM 功能。\n\n**支持的功能：**\n\n* InfluxDB 连接管理\n* [influxdb-orm](https://github.com/Yurunsoft/influxdb-orm)\n* 服务指标监控\n\n\u003e 正在开发中，随时可能修改，请勿用于生产环境！\n\n## 安装\n\n`composer require imiphp/imi-influxdb:~2.1.0`\n\n## 使用说明\n\n### InfluxDB 连接管理\n\n#### 配置连接\n\n`config.php`：\n\n```php\n[\n    'influxDB'  =\u003e [\n        'clients'   =\u003e [\n            // default 是连接名称，可以随意更改\n            'default'   =\u003e [\n                'host'              =\u003e '127.0.0.1', // 主机名\n                'port'              =\u003e 8086, // 端口\n                'username'          =\u003e '', // 用户名\n                'password'          =\u003e '', // 密码\n                'defaultDatabase'   =\u003e '', // 默认数据库名\n                'ssl'               =\u003e false, // 是否启用 SSL\n                'verifySSL'         =\u003e false, // 是否验证 SSL 证书\n                'timeout'           =\u003e 0, // 超时时间\n                'connectTimeout'    =\u003e 0, // 连接超时时间\n                'path'              =\u003e'/', // 请求路径前缀\n                'createDatabase'    =\u003e true, // 当数据库不存在时，自动创建数据库\n            ],\n        ],\n        'default'   =\u003e 'default', // 默认连接名\n    ],\n]\n```\n\n#### 获取客户端对象\n\n```php\nuse Imi\\InfluxDB\\InfluxDB;\n\n$client = InfluxDB::getClient(); // 获取默认客户端\n$client = InfluxDB::getClient('default'); // 获取指定名称客户端\n```\n\n#### 获取数据库对象\n\n```php\nuse Imi\\InfluxDB\\InfluxDB;\n\n$db = InfluxDB::getDatabase(); // 获取默认数据库名的对象\n$db = InfluxDB::getDatabase('dbname'); // 获取指定数据库名的对象\n$db = InfluxDB::getDatabase(null, 'default'); // 指定客户端名称\n```\n\n#### 使用数据库对象\n\n```php\n$db = InfluxDB::getDatabase();\n$db-\u003equery(); // SQL 查询\n$db-\u003ewritePoints(); // 写入数据\n```\n\n\u003e 详细用法请参考：\u003chttps://github.com/influxdata/influxdb-php\u003e\n\n#### InfluxDB ORM\n\n详细用法请参考：\u003chttps://github.com/Yurunsoft/influxdb-orm\u003e\n\n#### SQL 查询\n\n#### 写入\n\n### 服务指标监控\n\n仅支持 Swoole、Workerman。\n\n支持 TDengine InfluxDB 协议写入。\n\n#### 安装所需组件\n\n`composer require imiphp/imi-meter:~2.1.0`\n\n#### 配置\n\n**配置监控指标：**\n\n`@app.beans`：\n\n```php\n[\n    'MeterRegistry' =\u003e [\n        'driver'  =\u003e \\Imi\\InfluxDB\\Meter\\InfluxDBMeterRegistry::class,\n        'options' =\u003e [\n            'database'   =\u003e null, // 使用的数据库名，可以设为null使用连接中配置的数据库名\n            'clientName' =\u003e null, // 连接客户端名称，可以设为null使用默认客户端名称\n            'batch'      =\u003e 1000, // 单次推送的记录数量\n            'interval'   =\u003e 0, // 推送时间周期，单位：秒，默认为0不启用推送，如希望监控生效，请设置一个合理的数值。\n            // 所有标签如设为空字符串或 null 则忽略该标签\n            'resultTag' =\u003e 'result', // 标签名-结果\n            'exceptionTag' =\u003e 'exception', // 标签名-异常类名\n            'instanceTag' =\u003e 'instance', // 标签名-实例\n            'workerTag' =\u003e 'worker', // 标签名-WorkerId\n            'instance' =\u003e 'imi', // 当前实例名称，每个实例要设置不同\n        ],\n    ],\n]\n```\n\n**TDengine InfluxDB 所需修改的配置：**\n\n```php\n// 连接配置一定要设置这几项\n[\n    'port'              =\u003e 6041,\n    'path'              =\u003e '/influxdb/v1/',\n    'createDatabase'    =\u003e false,\n    'username'          =\u003e 'root',\n    'password'          =\u003e 'taosdata',\n]\n```\n\n#### 使用\n\n##### 注解\n\n###### @Counted\n\n类名：`\\Imi\\Meter\\Annotation\\Counted`\n\n计数统计，适合只累加，不减少的统计数据类型。\n\n例如：访问次数统计。\n\n| 参数名 | 类型 | 默认值  | 描述 |\n| ------ | ------ | ------ | ------ |\n| name | `string` | `imi.counted` | 指标名称 |\n| recordFailuresOnly | `false` | `bool` | 是否只在抛出异常时记录 |\n| tags | `array` | `[]` | 标签，键值数组 |\n| description | `string` |  | 描述 |\n| options | `array` | `[]` | 额外参数，每个驱动不同 |\n\n###### @Gauged\n\n类名：`\\Imi\\Meter\\Annotation\\Gauged`\n\n适合数字有上下波动的统计。\n\n例如：CPU 占用率统计。\n\n| 参数名 | 类型 | 默认值  | 描述 |\n| ------ | ------ | ------ | ------ |\n| name | `string` | `imi.counted` | 指标名称 |\n| recordFailuresOnly | `false` | `bool` | 是否只在抛出异常时记录 |\n| tags | `array` | `[]` | 标签，键值数组 |\n| description | `string` |  | 描述 |\n| value | `string\\|float` | `{returnValue}` | 写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值 |\n| operation | `int` | `\\Imi\\Meter\\Enum\\GaugeOperation::SET` | 操作类型。设置`GaugeOperation::SET`；增加`GaugeOperation::INCREMENT`；减少`GaugeOperation::DECREMENT` |\n| options | `array` | `[]` | 额外参数，每个驱动不同 |\n\n###### @Timed\n\n类名：`\\Imi\\Meter\\Annotation\\Timed`\n\n耗时统计。\n\n例如：方法执行耗时\n\n| 参数名 | 类型 | 默认值  | 描述 |\n| ------ | ------ | ------ | ------ |\n| name | `string` | `imi.counted` | 指标名称 |\n| tags | `array` | `[]` | 标签，键值数组 |\n| description | `string` |  | 描述 |\n| baseTimeUnit | `int` | `\\Imi\\Meter\\Enum\\TimeUnit::NANO_SECOND` | 基础时间单位，默认纳秒，可以使用 `\\Imi\\Meter\\Enum\\TimeUnit::XXX` 常量设置。 |\n| options | `array` | `[]` | 额外参数，每个驱动不同 |\n\n###### @Histogram\n\n类名：`\\Imi\\Meter\\Annotation\\Histogram`\n\n柱状图，一般人用不懂，如无特殊需求可以无视。\n\n| 参数名 | 类型 | 默认值  | 描述 |\n| ------ | ------ | ------ | ------ |\n| name | `string` | `imi.counted` | 指标名称 |\n| tags | `array` | `[]` | 标签，键值数组 |\n| description | `string` |  | 描述 |\n| buckets | `array` | `[]` | 桶，例如：`[100, 500, 1000]` |\n| baseTimeUnit | `int` | `\\Imi\\Meter\\Enum\\TimeUnit::NANO_SECOND` | 基础时间单位，默认纳秒，可以使用 `\\Imi\\Meter\\Enum\\TimeUnit::XXX` 常量设置。 |\n| value | `string\\|float` | `{returnValue}` | 写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值 |\n| options | `array` | `[]` | 额外参数，每个驱动不同 |\n\n###### @Summary\n\n类名：`\\Imi\\Meter\\Annotation\\Summary`\n\n采样点分位图，一般人用不懂，如无特殊需求可以无视。\n\n| 参数名 | 类型 | 默认值  | 描述 |\n| ------ | ------ | ------ | ------ |\n| name | `string` | `imi.counted` | 指标名称 |\n| tags | `array` | `[]` | 标签，键值数组 |\n| description | `string` |  | 描述 |\n| percentile | `array` | `[]` | 百分位数，例如：`[0.01, 0.5, 0.99]` |\n| baseTimeUnit | `int` | `\\Imi\\Meter\\Enum\\TimeUnit::NANO_SECOND` | 基础时间单位，默认纳秒，可以使用 `\\Imi\\Meter\\Enum\\TimeUnit::XXX` 常量设置。 |\n| value | `string\\|float` | `{returnValue}` | 写入的值；`{returnValue}` 表示方法返回值；`{returnValue.xxx}` 表示方法返回值的属性值；`{params.0}` 表示方法参数值；`{params.0.xxx}` 表示方法参数值的属性值；也可以是固定的 `float` 值 |\n| options | `array` | `[]` | 额外参数，每个驱动不同 |\n\n**代码示例：**\n\n```php\nuse Imi\\Meter\\Annotation\\Gauged;\nuse Imi\\Meter\\Annotation\\Histogram;\nuse Imi\\Meter\\Annotation\\Summary;\nuse Imi\\Meter\\Annotation\\Timed;\nuse Imi\\Meter\\Enum\\TimeUnit;\n\n/**\n * @Gauged(name=\"test_memory_usage\", description=\"memory usage\", tags={\"workerId\"=\"{returnValue.workerId}\"}, value=\"{returnValue.memory}\")\n */\npublic function recordMemoryUsage(): array\n{\n    return [\n        'workerId' =\u003e Worker::getWorkerId(),\n        'memory'   =\u003e memory_get_usage(),\n    ];\n}\n\n/**\n * @Timed(name=\"test_timed\", description=\"memory usage\", baseTimeUnit=TimeUnit::MILLI_SECONDS)\n */\npublic function testTimed(): int\n{\n    $ms = mt_rand(10, 1000);\n    usleep($ms * 1000);\n\n    return $ms;\n}\n\n/**\n * @Timed(name=\"test_timed_histogram\", description=\"memory usage\", baseTimeUnit=TimeUnit::MILLI_SECONDS, options={\"histogram\"=true})\n */\npublic function testTimedHistogram(): int\n{\n    $ms = mt_rand(10, 1000);\n    usleep($ms * 1000);\n\n    return $ms;\n}\n\n/**\n * @Histogram(name=\"test_histogram\", baseTimeUnit=TimeUnit::MILLI_SECONDS)\n */\npublic function testHistogram(): int\n{\n    return mt_rand(10, 1000);\n}\n\n/**\n * @Summary(name=\"test_summary\", baseTimeUnit=TimeUnit::MILLI_SECONDS)\n */\npublic function testSummary(): int\n{\n    return mt_rand(10, 1000);\n}\n```\n\n##### 手动操作\n\n```php\nuse \\Imi\\Meter\\Facade\\MeterRegistry;\nuse \\Imi\\Meter\\Enum\\TimeUnit;\n\n$description = '我是描述';\n$tags = ['result' =\u003e 'success'];\n\n// counter\nMeterRegistry::getDriverInstance()-\u003ecounter('testCounterManual', $tags, $description)-\u003eincrement();\n\n// gauge\nMeterRegistry::getDriverInstance()-\u003egauge('testGaugeManual', $tags, $description)-\u003erecord(114514);\n\n// timer\n$timer = MeterRegistry::getDriverInstance()-\u003etimer('testTimedManual', $tags, $description, TimeUnit::MILLI_SECONDS);\n$timerSample = $timer-\u003estart();\nusleep(mt_rand(10, 1000) * 1000);\n$timerSample-\u003estop($timer);\n\n// timer Histogram\n$timer = MeterRegistry::getDriverInstance()-\u003etimer('testTimedHistogramManual', $tags, $description, TimeUnit::MILLI_SECONDS, [\n    'histogram' =\u003e true,\n]);\n$timerSample = $timer-\u003estart();\nusleep(mt_rand(10, 1000) * 1000); // 你的耗时代码\n$timerSample-\u003estop($timer);\n\n// Histogram\n$value = 114514;\nMeterRegistry::getDriverInstance()-\u003ehistogram('testHistogramManual', $tags, $description)-\u003erecord($value);\n\n// Summary\n$value = 114514;\nMeterRegistry::getDriverInstance()-\u003esummary('testHistogramManual', $tags, $description)-\u003erecord($value);\n```\n\n## 免费技术支持\n\nQQ群：17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png \"点击加群\")](https://jq.qq.com/?_wv=1027\u0026k=5wXf4Zq)，如有问题会有人解答和修复。\n\n## 运行环境\n\n* [PHP](https://php.net/) \u003e= 7.4\n* [Composer](https://getcomposer.org/) \u003e= 2.0\n* [Swoole](https://www.swoole.com/) \u003e= 4.8.0\n* [imi](https://www.imiphp.com/) \u003e= 2.1\n\n## 版权信息\n\n`imi-influxdb` 遵循 MulanPSL-2.0 开源协议发布，并提供免费使用。\n\n## 捐赠\n\n\u003cimg src=\"https://cdn.jsdelivr.net/gh/imiphp/imi@2.1/res/pay.png\"/\u003e\n\n开源不求盈利，多少都是心意，生活不易，随缘随缘……\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-influxdb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimiphp%2Fimi-influxdb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-influxdb/lists"}