From a5386187e3f9b44442889920adc0be009881ccd7 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Mon, 4 Dec 2023 15:53:53 +0100 Subject: [PATCH] Enable missing code for prometheus actix-web stats (#4230) * Enable missing code for prometheus actix-web stats * enable middleware conditionally --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 1 + docker/docker-compose.yml | 4 ++-- src/lib.rs | 11 ++++++++--- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f375fa1b..7a25e9a4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -328,6 +328,19 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "actix-web-prom" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f23f332a652836b8f3a6876103c70c9ed436d0e69fa779ab5d7f57b1d5c8d488" +dependencies = [ + "actix-web", + "futures-core", + "pin-project-lite", + "prometheus", + "regex", +] + [[package]] name = "addr2line" version = "0.21.0" @@ -2776,6 +2789,7 @@ dependencies = [ "activitypub_federation", "actix-cors", "actix-web", + "actix-web-prom", "chrono", "clap", "clokwerk", diff --git a/Cargo.toml b/Cargo.toml index 544627596..067eadc0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -191,3 +191,4 @@ chrono = { workspace = true } prometheus = { version = "0.13.3", features = ["process"] } serial_test = { workspace = true } clap = { version = "4.4.10", features = ["derive"] } +actix-web-prom = "0.7.0" diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 9626af84a..fdf6c32c2 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -43,8 +43,8 @@ services: - RUST_LOG="warn,lemmy_server=debug,lemmy_api=debug,lemmy_api_common=debug,lemmy_api_crud=debug,lemmy_apub=debug,lemmy_db_schema=debug,lemmy_db_views=debug,lemmy_db_views_actor=debug,lemmy_db_views_moderator=debug,lemmy_routes=debug,lemmy_utils=debug,lemmy_websocket=debug" - RUST_BACKTRACE=full ports: - # prometheus metrics available at the path /metrics on port 10002 by default - # enable prometheus metrics by setting the CARGO_BUILD_FEATURES build arg above to "prometheus-metrics" + # prometheus metrics can be enabled with the `prometheus` config option. they are available on + # port 10002, path /metrics by default - "10002:10002" volumes: - ./lemmy.hjson:/config/config.hjson:Z diff --git a/src/lib.rs b/src/lib.rs index 28dcaafe6..6edcf1b37 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,13 +16,14 @@ use activitypub_federation::config::{FederationConfig, FederationMiddleware}; use actix_cors::Cors; use actix_web::{ dev::{ServerHandle, ServiceResponse}, - middleware::{self, ErrorHandlerResponse, ErrorHandlers}, + middleware::{self, Condition, ErrorHandlerResponse, ErrorHandlers}, web::Data, App, HttpResponse, HttpServer, Result, }; +use actix_web_prom::PrometheusMetricsBuilder; use clap::{ArgAction, Parser}; use lemmy_api_common::{ context::LemmyContext, @@ -49,6 +50,7 @@ use lemmy_utils::{ response::jsonify_plain_text_errors, settings::{structs::Settings, SETTINGS}, }; +use prometheus::default_registry; use prometheus_metrics::serve_prometheus; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use reqwest_tracing::TracingMiddleware; @@ -271,7 +273,6 @@ fn create_http_server( ) -> Result { // this must come before the HttpServer creation // creates a middleware that populates http metrics for each path, method, and status code - #[cfg(feature = "prometheus-metrics")] let prom_api_metrics = PrometheusMetricsBuilder::new("lemmy_api") .registry(default_registry().clone()) .build() @@ -296,7 +297,11 @@ fn create_http_server( .app_data(Data::new(context.clone())) .app_data(Data::new(rate_limit_cell.clone())) .wrap(FederationMiddleware::new(federation_config.clone())) - .wrap(SessionMiddleware::new(context.clone())); + .wrap(SessionMiddleware::new(context.clone())) + .wrap(Condition::new( + SETTINGS.prometheus.is_some(), + prom_api_metrics.clone(), + )); // The routes app