2个版本
0.1.1 | 2021年9月1日 |
---|---|
0.1.0 | 2021年9月1日 |
#660 in 调试
在 hesione_macros 中使用
38KB
691 行
赫西奥内
Prometheus客户端
概述
已经有几个针对Rust的Prometheus客户端,但我决定再做一个。Rust有很多语言特性,我觉得现有的实现并没有真正利用这些特性。在赫西奥内中,利用特质使度量存储类型可插拔,并存在一个派生宏,可以轻松定义度量并生成它们的输出,等等。
此外,我的GitLab实例已启用GitHub和GitLab.com账户登录,所以如果您想打开问题或贡献代码,请随时这么做!
特性
- Rust相关
- 简化度量定义的代码生成
- 易于使用
- 经过良好测试
- 处理度量
- Prometheus相关
- 计数器
- 仪表
- 直方图
- 摘要
示例
use std::sync::{
Arc,
atomic::{Ordering, AtomicUsize},
};
use once_cell::sync::Lazy;
use hesione::{Hesione, Counter, labels};
/// The collection of metrics used by your program
#[derive(Hesione)]
struct Metrics {
/// The amount of HTTP requests
///
/// Extra documentation that won't show up in Prometheus' help text can go
/// here. For example, I'll take this moment to mention that `AtomicUsize`
/// is just an arbitrary choice here. If you'd like to use another type to
/// store the value in, this is possible (with the stipulation that the type
/// you want implements the `hesione::Value` trait).
http_reqs: Counter<AtomicUsize>,
}
/// A globally usable instance of the above structure
///
/// This isn't strictly necessary, although it will likely make instrumentation
/// much easier.
static METRICS: Lazy<Metrics> = Lazy::new(Metrics::default);
// Instrument your code by importing METRICS and modifying its fields:
METRICS.http_reqs.inc(Ordering::SeqCst, None);
// You can also add labels when accessing a metric:
let labels = labels! {
"status_code": 404.to_string(),
}.unwrap();
METRICS.http_reqs.inc(Ordering::SeqCst, Some(labels));
// Call this method in your `/metrics` HTTP endpoint to generate the output:
let actual = METRICS.to_prometheus_lines(Ordering::SeqCst, None);
// This is what the output will look like for this example:
let expected = concat!(
// The help text comes from the first line of the doc comments
"# HELP http_reqs The amount of HTTP requests\n",
// Type hints are dictated by the struct's field type
"# TYPE http_reqs counter\n",
// Metric names are generated by the struct's field name
"http_reqs 1\n",
"http_reqs{status_code=\"404\"} 1\n",
"\n",
);
assert_eq!(expected, actual);
贡献代码
确保在第一次克隆后运行 .hooks/install.sh
脚本。合并后钩子会重新运行此脚本以应用对钩子的更改(如果有的话)。预提交钩子会运行格式化程序、代码检查器(在拒绝警告模式下)和测试套件。如果由于某些原因(可能是因为您已经运行过并不想再次自动运行,或者正在提交与代码无关的事情)不想运行严格模式的代码检查器或测试,可以将 SKIP_TESTS_PLEASE
环境变量设置为任何值。
许可
本项目可根据以下任一许可进行许可:
-
Apache许可证第2版 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
-
MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任您选择。
依赖
~4–6MB
~109K SLoC