feat: add prometheus instrumentation
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2022-12-25 13:14:00 +05:30
parent 158ec03aab
commit 58eef6b3fa
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 35 additions and 0 deletions

27
Cargo.lock generated
View file

@ -208,6 +208,18 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
] ]
[[package]]
name = "actix-web-prom"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9df3127d20a5d01c9fc9aceb969a38d31a6767e1b48a54d55a8f56c769a84923"
dependencies = [
"actix-web",
"futures-core",
"pin-project-lite",
"prometheus",
]
[[package]] [[package]]
name = "adler" name = "adler"
version = "1.0.2" version = "1.0.2"
@ -415,6 +427,7 @@ dependencies = [
"actix-web", "actix-web",
"actix-web-codegen-const-routes", "actix-web-codegen-const-routes",
"actix-web-httpauth", "actix-web-httpauth",
"actix-web-prom",
"base64", "base64",
"clap", "clap",
"config", "config",
@ -1408,6 +1421,20 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "prometheus"
version = "0.13.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c"
dependencies = [
"cfg-if",
"fnv",
"lazy_static",
"memchr",
"parking_lot 0.12.1",
"thiserror",
]
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"

View file

@ -12,6 +12,7 @@ build = "build.rs"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-prom = "0.6.0"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] } futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.17" log = "0.4.17"

View file

@ -19,6 +19,7 @@ use std::env;
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use actix_web::web::JsonConfig; use actix_web::web::JsonConfig;
use actix_web::{error::InternalError, middleware, App, HttpServer}; use actix_web::{error::InternalError, middleware, App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use log::info; use log::info;
@ -112,6 +113,11 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
let ip = settings.server.get_ip(); let ip = settings.server.get_ip();
println!("Starting server on: http://{ip}"); println!("Starting server on: http://{ip}");
let prometheus = PrometheusMetricsBuilder::new("api")
.endpoint("/metrics")
.build()
.unwrap();
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
@ -124,6 +130,7 @@ async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> {
middleware::TrailingSlash::Trim, middleware::TrailingSlash::Trim,
)) ))
.app_data(get_json_err()) .app_data(get_json_err())
.wrap(prometheus.clone())
.configure(routes::services) .configure(routes::services)
}) })
.bind(ip)? .bind(ip)?