#metrics #dashboard #web-server #poem #zero-config #metrics-rs

metrics-dashboard

使用 metrics-rs 的零配置仪表板

6 个版本

0.2.1 2024 年 5 月 29 日
0.2.0 2024 年 1 月 25 日
0.1.3 2023 年 12 月 12 日

#469 in HTTP 服务器

每月下载 45
用于 atm0s 媒体服务器

MIT 许可证

130KB
921

metrics-dashboard-rs

Crates.io Docs.rs CI

本包为 metric-rs 包提供简单的自动生成仪表板。

截图

如何使用

  • 运行 cargo add metrics-dashboard
  • 将其包含到 poem 网络服务器中,如下所示
use std::time::Duration;

use metrics_dashboard::build_dashboard_route;
use metrics::{describe_counter, increment_counter};
use poem::{
    get, handler, listener::TcpListener, middleware::Tracing, web::Path, EndpointExt, Route, Server,
};

#[handler]
fn hello(Path(name): Path<String>) -> String {
    format!("hello: {name}")
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "poem=debug");
    }
    tracing_subscriber::fmt::init();

    let dashboard_options = DashboardOptions {
        custom_charts: vec![
            ChartType::Line {
                metrics: vec![
                    "demo_live_time".to_string(),
                    "demo_live_time_max".to_string(),
                ],
                desc: Some("Demo metric line".to_string()),
            }
        ],
        include_default: true,
    };

    let app = Route::new()
        .at("/hello/:name", get(hello))
        .nest("/dashboard/", build_dashboard_route())
        .with(Tracing);

    tokio::spawn(async move {
        describe_counter!("demo_metric1", "Demo metric1");
        loop {
            tokio::time::sleep(Duration::from_secs(1)).await;
            increment_counter!("demo_metric1");
        }
    });

    tokio::spawn(async move {
        describe_counter!("demo_metric2", "Demo metric2");
        loop {
            tokio::time::sleep(Duration::from_secs(1)).await;
            increment_counter!("demo_metric2");
        }
    });

    Server::new(TcpListener::bind("0.0.0.0:3000"))
        .name("hello-world")
        .run(app)
        .await
}

许可证

许可协议为 (LICENSE-MIThttp://opensource.org/licenses/MIT)

贡献

除非你明确声明,否则任何有意提交以包含在本作品中的贡献,如 MIT 许可证中定义,不附加任何额外条款或条件。

CONTRIBUTING.md

依赖关系

~18–48MB
~756K SLoC