
A Python port of the Rust autometrics-rs library
Metrics are a powerful and cost-efficient tool for understanding the health and performance of your code in production. But it's hard to decide what metrics to track and even harder to write queries to understand the data.
Autometrics provides a decorator that makes it trivial to instrument any function with the most useful metrics: request rate, error rate, and latency. It standardizes these metrics and then generates powerful Prometheus queries based on your function details to help you quickly identify and debug issues in production.
See Why Autometrics? for more details on the ideas behind autometrics.
@autometrics decorator instruments any function or class method to track the
most useful metricsopentelemetry or prometheus)autometrics to your project's dependencies:pip install autometrics
@autometrics decoratorfrom autometrics import autometrics @autometrics def my_function(): # ...
init function:from autometrics import init init(tracker="prometheus", service_name="my-service")
# This example uses FastAPI, but you can use any web framework from fastapi import FastAPI, Response from prometheus_client import generate_latest # Set up a metrics endpoint for Prometheus to scrape # `generate_latest` returns metrics data in the Prometheus text format @app.get("/metrics") def metrics(): return Response(generate_latest())
# Replace `8080` with the port that your app runs on am start :8080
autometrics-pyfrom autometrics import autometrics @autometrics def sayHello: return "hello"
To show tooltips over decorated functions in VSCode, with links to Prometheus queries, try installing the VSCode extension.
Note: We cannot support tooltips without a VSCode extension due to behavior of the static analyzer used in VSCode.
You can also track the number of concurrent calls to a function by using the track_concurrency argument: @autometrics(track_concurrency=True).
Note: Concurrency tracking is only supported when you set with the environment variable
AUTOMETRICS_TRACKER=prometheus.
To access the PromQL queries for your decorated functions, run help(yourfunction) or print(yourfunction.__doc__).
For these queries to work, include a
.envfile in your project with your prometheus endpointPROMETHEUS_URL=your endpoint. If this is not defined, the default endpoint will behttp://localhost:9090/
Autometrics provides Grafana dashboards that will work for any project instrumented with the library.
Autometrics makes it easy to add intelligent alerting to your code, in order to catch increases in the error rate or latency across multiple functions.
from autometrics import autometrics from autometrics.objectives import Objective, ObjectiveLatency, ObjectivePercentile # Create an objective for a high success rate # Here, we want our API to have a success rate of 99.9% API_SLO_HIGH_SUCCESS = Objective( "My API SLO for High Success Rate (99.9%)", success_rate=ObjectivePercentile.P99_9, ) @autometrics(objective=API_SLO_HIGH_SUCCESS) def api_handler(): # ...
The library uses the concept of Service-Level Objectives (SLOs) to define the acceptable error rate and latency for groups of functions. Alerts will fire depending on the SLOs you set.
Not sure what SLOs are? Check out our docs for an introduction.
In order to receive alerts, you need to add a special set of rules to your Prometheus setup. These are configured automatically when you use the Autometrics CLI to run Prometheus.
Already running Prometheus yourself? Read about how to load the autometrics alerting rules into Prometheus here.
Once the alerting rules are in Prometheus, you're ready to go.
To use autometrics SLOs and alerts, create one or multiple Objectives based on the function(s) success rate and/or latency, as shown above.
The Objective can be passed as an argument to the autometrics decorator, which will include the given function in that objective.
The example above used a success rate objective. (I.e., we wanted to be alerted when the error rate started to increase.)
You can also create an objective for the latency of your functions like so:
from autometrics import autometrics from autometrics.objectives import Objective, ObjectiveLatency, ObjectivePercentile # Create an objective for low latency # - Functions with this objective should have a 99th percentile latency of less than 250ms API_SLO_LOW_LATENCY = Objective( "My API SLO for Low Latency (99th percentile < 250ms)", latency=(ObjectiveLatency.Ms250, ObjectivePercentile.P99), ) @autometrics(objective=API_SLO_LOW_LATENCY) def api_handler(): # ...
caller LabelAutometrics keeps track of instrumented functions that call each other. So, if you have a function get_users that calls another function db.query, then the metrics for latter will include a label caller="get_users".
This allows you to drill down into the metrics for functions that are called by your instrumented functions, provided both of those functions are decorated with @autometrics.
In the example above, this means that you could investigate the latency of the database queries that get_users makes, which is rather useful.
Autometrics makes use of a number of environment variables to configure its behavior. All of them are also configurable with keyword arguments to the init function.
tracker - Configure the package that autometrics will use to produce metrics. Default is opentelemetry, but you can also use prometheus. Look in pyproject.toml for the corresponding versions of packages that will be used.histogram_buckets - Configure the buckets used for latency histograms. Default is [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0].enable_exemplars - Enable exemplar collection. Default is

免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供 多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能 电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号