Skip to main content

为 NoneBot 和其他插件提供 Prometheus 监控支持

Project description

nonebot

Nonebot Plugin Prometheus

✨ NoneBot Prometheus 监控插件 ✨

license pypi

✨功能

  • 自动挂载 /metrics 路径,提供 Prometheus 监控数据
  • 为其他插件提供统一的数据上报接口
  • 新增:支持通过对话查询指标数据
  • 新增:提供通用指标查询接口,支持任意已注册指标的查询

📊支持统计的指标

  • Bot 在线状态
  • Bot 掉线次数
  • Bot 发送和接受消息次数
  • Matcher 执行次数
  • Matcher 执行耗时分布

♿官方提供 Grafana 面板

23060

图片

📦 安装

  • 使用 nb-cli
nb plugin install nonebot-plugin-prometheus
  • 使用 uv
uv add nonebot-plugin-prometheus
  • 使用 poetry
poetry add nonebot-plugin-prometheus
  • 使用 pip
pip install nonebot-plugin-prometheus

🔌接入方式

先在插件代码最前面声明依赖

from nonebot import require

require("nonebot_plugin_prometheus")

然后可以从插件导入相关指标对象使用,详情请参考 Prometheus Python Client 官方文档

from nonebot_plugin_prometheus import Gauge, Counter, Histogram, Summary

# Request counter
request_counter = Counter(
    "request_counter", "The number of requests"
)
request_counter.inc()

Tips

为了统计 matcher 运行时间,本插件会自动在 Matcher.state 中插入 _prometheus_start_time 字段。

🔧配置

# 是否开启 Prometheus 插件
PROMETHEUS_ENABLE=true
# Prometheus 挂载地址
PROMETHEUS_METRICS_PATH=/metrics

Note

使用插件需要支持 ASGI 的驱动器,例如 fastapi

💬对话查询功能

本插件现在支持通过对话命令查询指标数据,方便在聊天中快速查看监控信息。

基础命令

# 查看系统概览
/metrics

# 查看机器人状态
/metrics status

# 查看消息统计
/metrics messages

# 查看匹配器统计
/metrics matchers

# 查看系统指标
/metrics system

# 查看运行时间
/metrics uptime

# 查看帮助
/metrics help

通用指标查询

新增:支持查询任意已注册的 Prometheus 指标:

# 查询特定指标
/metrics query nonebot_received_messages

# 带标签过滤的查询
/metrics query nonebot_received_messages{bot_id="123456"}

# 列出所有可用指标
/metrics list

# 搜索包含关键字的指标
/metrics search matcher

查询示例

# 查看接收消息总数
/metrics query nonebot_received_messages

# 查看特定机器人的消息统计
/metrics query nonebot_received_messages{bot_id="72075954", adapter_name="Telegram"}

# 查看匹配器执行情况
/metrics query nonebot_matcher_calls_total

# 搜索所有与匹配器相关的指标
/metrics search matcher

输出格式

查询结果会以格式化的方式显示,包含:

  • 指标类型和描述
  • 标签信息
  • 指标值
  • 适当的单位格式化(如 K、M、B 等)

🔧开发者接口

除了对话查询,本插件还提供了编程接口供其他插件使用:

from nonebot_plugin_prometheus.registry import (
    get_metrics,           # 获取所有指标
    get_metrics_by_name,   # 按名称获取指标
    get_metric_values,     # 获取指标值
    list_all_metrics,      # 列出所有指标
    search_metrics,        # 搜索指标
    parse_metric_filter,   # 解析查询字符串
)

# 示例:获取特定指标的值
values = get_metric_values("nonebot_received_messages", {"bot_id": "123456"})
for labels, value in values:
    print(f"Labels: {labels}, Value: {value}")

📝TODO

  • 提供快速上手 docker compose 文件

相关仓库

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_prometheus-0.4.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_prometheus-0.4.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_prometheus-0.4.1.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_prometheus-0.4.1.tar.gz
Algorithm Hash digest
SHA256 113a23f076d490ab75b7f9851bc1911de4fc6aaf79600f2f0e759ce7ef6666db
MD5 2329209939e8bbfa90d636b73cad5cd5
BLAKE2b-256 1acb306c93f4ce36c083c17196aa84a51ad2f31e96ad0d0963a23842d71843f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_prometheus-0.4.1.tar.gz:

Publisher: release.yml on suyiiyii/nonebot-plugin-prometheus

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nonebot_plugin_prometheus-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_prometheus-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c53797ac30cc22f8e54942990100678c823f90fc73051fef3c6c501a9409f029
MD5 57f1af70ef36d78de4e5eb432e1317c6
BLAKE2b-256 d678ee801769f82aff50213f2ea17cddff469348876b0dc40b841e1315c477f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for nonebot_plugin_prometheus-0.4.1-py3-none-any.whl:

Publisher: release.yml on suyiiyii/nonebot-plugin-prometheus

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page