Enable missing code for prometheus actix-web stats (#4230)

* Enable missing code for prometheus actix-web stats

* enable middleware conditionally
This commit is contained in:
Nutomic 2023-12-04 15:53:53 +01:00 committed by GitHub
parent 3f79eacb53
commit a5386187e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 5 deletions

14
Cargo.lock generated
View File

@ -328,6 +328,19 @@ dependencies = [
"pin-project-lite", "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]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.21.0" version = "0.21.0"
@ -2776,6 +2789,7 @@ dependencies = [
"activitypub_federation", "activitypub_federation",
"actix-cors", "actix-cors",
"actix-web", "actix-web",
"actix-web-prom",
"chrono", "chrono",
"clap", "clap",
"clokwerk", "clokwerk",

View File

@ -191,3 +191,4 @@ chrono = { workspace = true }
prometheus = { version = "0.13.3", features = ["process"] } prometheus = { version = "0.13.3", features = ["process"] }
serial_test = { workspace = true } serial_test = { workspace = true }
clap = { version = "4.4.10", features = ["derive"] } clap = { version = "4.4.10", features = ["derive"] }
actix-web-prom = "0.7.0"

View File

@ -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_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 - RUST_BACKTRACE=full
ports: ports:
# prometheus metrics available at the path /metrics on port 10002 by default # prometheus metrics can be enabled with the `prometheus` config option. they are available on
# enable prometheus metrics by setting the CARGO_BUILD_FEATURES build arg above to "prometheus-metrics" # port 10002, path /metrics by default
- "10002:10002" - "10002:10002"
volumes: volumes:
- ./lemmy.hjson:/config/config.hjson:Z - ./lemmy.hjson:/config/config.hjson:Z

View File

@ -16,13 +16,14 @@ use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use actix_cors::Cors; use actix_cors::Cors;
use actix_web::{ use actix_web::{
dev::{ServerHandle, ServiceResponse}, dev::{ServerHandle, ServiceResponse},
middleware::{self, ErrorHandlerResponse, ErrorHandlers}, middleware::{self, Condition, ErrorHandlerResponse, ErrorHandlers},
web::Data, web::Data,
App, App,
HttpResponse, HttpResponse,
HttpServer, HttpServer,
Result, Result,
}; };
use actix_web_prom::PrometheusMetricsBuilder;
use clap::{ArgAction, Parser}; use clap::{ArgAction, Parser};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
@ -49,6 +50,7 @@ use lemmy_utils::{
response::jsonify_plain_text_errors, response::jsonify_plain_text_errors,
settings::{structs::Settings, SETTINGS}, settings::{structs::Settings, SETTINGS},
}; };
use prometheus::default_registry;
use prometheus_metrics::serve_prometheus; use prometheus_metrics::serve_prometheus;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_tracing::TracingMiddleware; use reqwest_tracing::TracingMiddleware;
@ -271,7 +273,6 @@ fn create_http_server(
) -> Result<ServerHandle, LemmyError> { ) -> Result<ServerHandle, LemmyError> {
// this must come before the HttpServer creation // this must come before the HttpServer creation
// creates a middleware that populates http metrics for each path, method, and status code // 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") let prom_api_metrics = PrometheusMetricsBuilder::new("lemmy_api")
.registry(default_registry().clone()) .registry(default_registry().clone())
.build() .build()
@ -296,7 +297,11 @@ fn create_http_server(
.app_data(Data::new(context.clone())) .app_data(Data::new(context.clone()))
.app_data(Data::new(rate_limit_cell.clone())) .app_data(Data::new(rate_limit_cell.clone()))
.wrap(FederationMiddleware::new(federation_config.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 // The routes
app app