【亲测免费】 Prometheus FastAPI Instrumentator 安装和配置指南
2026-01-25 04:10:43作者:俞予舒Fleming
1. 项目基础介绍和主要编程语言
项目介绍
Prometheus FastAPI Instrumentator 是一个用于 FastAPI 应用的 Prometheus 指标采集工具。它可以帮助你轻松地将 Prometheus 指标集成到 FastAPI 应用中,以便监控应用的性能和行为。
主要编程语言
该项目主要使用 Python 编程语言。
2. 项目使用的关键技术和框架
关键技术
- Prometheus: 一个开源的系统监控和报警工具包。
- FastAPI: 一个现代、快速(高性能)的 Web 框架,用于构建 APIs,基于 Python 3.7+ 和异步编程。
框架
- FastAPI: 用于构建 Web 应用。
- Prometheus Client Library: 用于在 Python 应用中集成 Prometheus 指标。
3. 项目安装和配置的准备工作和详细安装步骤
准备工作
在开始安装和配置之前,请确保你已经安装了以下软件:
- Python 3.7 或更高版本
- pip(Python 包管理工具)
安装步骤
步骤 1:创建虚拟环境(可选)
为了隔离项目依赖,建议创建一个虚拟环境。
python3 -m venv myenv
source myenv/bin/activate
步骤 2:安装 Prometheus FastAPI Instrumentator
使用 pip 安装 prometheus-fastapi-instrumentator 包。
pip install prometheus-fastapi-instrumentator
步骤 3:创建 FastAPI 应用
创建一个新的 FastAPI 应用文件 main.py,并添加以下代码:
from fastapi import FastAPI
from prometheus_fastapi_instrumentator import Instrumentator
app = FastAPI()
# 初始化 Instrumentator
instrumentator = Instrumentator()
# 在应用启动时进行指标暴露
@app.on_event("startup")
async def startup():
instrumentator.instrument(app).expose(app)
# 示例路由
@app.get("/")
async def root():
return {"message": "Hello World"}
步骤 4:运行 FastAPI 应用
使用以下命令运行 FastAPI 应用:
uvicorn main:app --reload
步骤 5:访问 Prometheus 指标
启动应用后,你可以通过访问 http://127.0.0.1:8000/metrics 来查看 Prometheus 指标。
配置步骤
自定义指标
你可以通过添加自定义指标来扩展 Prometheus 指标采集功能。例如,添加一个计数器来统计特定语言的请求次数:
from prometheus_client import Counter
from prometheus_fastapi_instrumentator import metrics
# 创建自定义指标
http_requested_languages_total = Counter(
"http_requested_languages_total",
"Number of times a certain language has been requested",
labelnames=("langs",)
)
# 添加自定义指标到 Instrumentator
instrumentator.add(
metrics.request_size(
should_include_handler=True,
should_include_method=False,
should_include_status=True,
metric_namespace="a",
metric_subsystem="b",
)
)
配置选项
Instrumentator 提供了多种配置选项,例如忽略特定路由、控制指标暴露等。你可以在创建 Instrumentator 实例时进行配置:
instrumentator = Instrumentator(
should_group_status_codes=False,
should_ignore_untemplated=True,
should_respect_env_var=True,
should_instrument_requests_inprogress=True,
excluded_handlers=["/admin/*", "/metrics"],
env_var_name="ENABLE_METRICS",
inprogress_name="inprogress",
inprogress_labels=True,
)
通过以上步骤,你已经成功安装并配置了 Prometheus FastAPI Instrumentator,并可以开始监控你的 FastAPI 应用了。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0220- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
AntSK基于.Net9 + AntBlazor + SemanticKernel 和KernelMemory 打造的AI知识库/智能体,支持本地离线AI大模型。可以不联网离线运行。支持aspire观测应用数据CSS01
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
27
13
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
626
4.12 K
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.49 K
845
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
930
802
暂无简介
Dart
872
207
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.06 K
547
Ascend Extension for PyTorch
Python
464
554
全称:Open Base Operator for Ascend Toolkit,哈尔滨工业大学AISS团队基于Ascend C打造的高性能昇腾算子库。
C++
45
47
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.25 K
100
昇腾LLM分布式训练框架
Python
137
160