mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2025-10-02 22:40:31 +00:00
fix: Resolve compilation errors and improve Docker build for metrics
- Fix Rocket response type issues in metrics endpoint - Correct middleware request timer handling - Resolve borrow checker issues with UUID string conversion - Add proper EXTRA_FEATURES support to Dockerfile for optional features - Suppress unused code warnings in proof-of-concept metrics implementation - Update Cargo.lock with resolved dependencies These fixes enable successful compilation of the metrics feature and production-ready Docker image building with: docker build --build-arg EXTRA_FEATURES="enable_metrics" The metrics-enabled image has been successfully built and tested.
This commit is contained in:
parent
3cbe12aea6
commit
8fac5f5060
6 changed files with 54 additions and 78 deletions
|
@ -1,17 +1,20 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
|
||||
use rocket::{
|
||||
http::{ContentType, Status},
|
||||
request::{FromRequest, Outcome, Request},
|
||||
response::{Content, Result},
|
||||
response::{self, content::RawText},
|
||||
Route,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
auth::ClientIp,
|
||||
db::DbConn,
|
||||
error::Error,
|
||||
CONFIG,
|
||||
};
|
||||
|
||||
use log::error;
|
||||
|
||||
// Metrics endpoint routes
|
||||
pub fn routes() -> Vec<Route> {
|
||||
if CONFIG.enable_metrics() {
|
||||
|
@ -46,7 +49,11 @@ impl<'r> FromRequest<'r> for MetricsToken {
|
|||
.headers()
|
||||
.get_one("Authorization")
|
||||
.and_then(|auth| auth.strip_prefix("Bearer "))
|
||||
.or_else(|| request.query_value::<&str>("token").and_then(Result::ok));
|
||||
.or_else(|| {
|
||||
request
|
||||
.query_value::<&str>("token")
|
||||
.and_then(|result| result.ok())
|
||||
});
|
||||
|
||||
match provided_token {
|
||||
Some(token) => {
|
||||
|
@ -84,7 +91,7 @@ fn validate_metrics_token(provided: &str, configured: &str) -> bool {
|
|||
|
||||
/// Prometheus metrics endpoint
|
||||
#[get("/")]
|
||||
async fn get_metrics(_token: MetricsToken, mut conn: DbConn) -> Result<Content<String>, Status> {
|
||||
async fn get_metrics(_token: MetricsToken, mut conn: DbConn) -> Result<RawText<String>, Status> {
|
||||
// Update business metrics from database
|
||||
if let Err(e) = crate::metrics::update_business_metrics(&mut conn).await {
|
||||
error!("Failed to update business metrics: {e}");
|
||||
|
@ -93,7 +100,7 @@ async fn get_metrics(_token: MetricsToken, mut conn: DbConn) -> Result<Content<S
|
|||
|
||||
// Gather all Prometheus metrics
|
||||
match crate::metrics::gather_metrics() {
|
||||
Ok(metrics) => Ok(Content(ContentType::Plain, metrics)),
|
||||
Ok(metrics) => Ok(RawText(metrics)),
|
||||
Err(e) => {
|
||||
error!("Failed to gather metrics: {e}");
|
||||
Err(Status::InternalServerError)
|
||||
|
@ -103,7 +110,7 @@ async fn get_metrics(_token: MetricsToken, mut conn: DbConn) -> Result<Content<S
|
|||
|
||||
/// Health check endpoint that also updates some basic metrics
|
||||
#[cfg(feature = "enable_metrics")]
|
||||
pub async fn update_health_metrics(conn: &mut DbConn) -> Result<(), Error> {
|
||||
pub async fn update_health_metrics(_conn: &mut DbConn) {
|
||||
// Update basic system metrics
|
||||
use std::time::SystemTime;
|
||||
static START_TIME: std::sync::OnceLock<SystemTime> = std::sync::OnceLock::new();
|
||||
|
@ -114,11 +121,7 @@ pub async fn update_health_metrics(conn: &mut DbConn) -> Result<(), Error> {
|
|||
// Update database connection metrics
|
||||
// Note: This is a simplified version - in production you'd want to get actual pool stats
|
||||
crate::metrics::update_db_connections("main", 1, 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "enable_metrics"))]
|
||||
pub async fn update_health_metrics(_conn: &mut DbConn) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
pub async fn update_health_metrics(_conn: &mut DbConn) {}
|
|
@ -1,7 +1,6 @@
|
|||
/// Metrics middleware for automatic HTTP request instrumentation
|
||||
use rocket::{
|
||||
fairing::{Fairing, Info, Kind},
|
||||
http::Method,
|
||||
Data, Request, Response,
|
||||
};
|
||||
use std::time::Instant;
|
||||
|
@ -24,16 +23,15 @@ impl Fairing for MetricsFairing {
|
|||
}
|
||||
|
||||
async fn on_response<'r>(&self, req: &'r Request<'_>, res: &mut Response<'r>) {
|
||||
if let Some(timer) = req.local_cache(|| RequestTimer { start_time: Instant::now() }) {
|
||||
let duration = timer.start_time.elapsed();
|
||||
let method = req.method().as_str();
|
||||
let path = normalize_path(req.uri().path().as_str());
|
||||
let status = res.status().code;
|
||||
let timer = req.local_cache(|| RequestTimer { start_time: Instant::now() });
|
||||
let duration = timer.start_time.elapsed();
|
||||
let method = req.method().as_str();
|
||||
let path = normalize_path(req.uri().path().as_str());
|
||||
let status = res.status().code;
|
||||
|
||||
// Record metrics
|
||||
crate::metrics::increment_http_requests(method, &path, status);
|
||||
crate::metrics::observe_http_request_duration(method, &path, duration.as_secs_f64());
|
||||
}
|
||||
// Record metrics
|
||||
crate::metrics::increment_http_requests(method, &path, status);
|
||||
crate::metrics::observe_http_request_duration(method, &path, duration.as_secs_f64());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
/// Database metrics collection utilities
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
/// Database operation tracker for metrics
|
||||
|
@ -38,7 +40,7 @@ macro_rules! db_metric {
|
|||
}
|
||||
|
||||
/// Track database connection pool statistics
|
||||
pub async fn update_pool_metrics(pool: &crate::db::DbPool) {
|
||||
pub async fn update_pool_metrics(_pool: &crate::db::DbPool) {
|
||||
#[cfg(feature = "enable_metrics")]
|
||||
{
|
||||
// Note: This is a simplified implementation
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
#![allow(dead_code, unused_imports)]
|
||||
|
||||
#[cfg(feature = "enable_metrics")]
|
||||
use once_cell::sync::Lazy;
|
||||
#[cfg(feature = "enable_metrics")]
|
||||
use prometheus::{
|
||||
register_counter_vec, register_gauge_vec, register_histogram_vec, register_int_counter_vec, register_int_gauge_vec,
|
||||
CounterVec, Encoder, GaugeVec, HistogramVec, IntCounterVec, IntGaugeVec, TextEncoder,
|
||||
register_gauge_vec, register_histogram_vec, register_int_counter_vec, register_int_gauge_vec,
|
||||
Encoder, GaugeVec, HistogramVec, IntCounterVec, IntGaugeVec, TextEncoder,
|
||||
};
|
||||
|
||||
#[cfg(feature = "enable_metrics")]
|
||||
|
@ -198,7 +200,13 @@ pub async fn update_business_metrics(conn: &mut DbConn) -> Result<(), crate::err
|
|||
4 => "identity",
|
||||
_ => "unknown",
|
||||
};
|
||||
let org_label = cipher.organization_uuid.as_ref().map(|id| id.as_str()).unwrap_or("personal");
|
||||
let org_id_string;
|
||||
let org_label = if let Some(id) = &cipher.organization_uuid {
|
||||
org_id_string = id.to_string();
|
||||
&org_id_string
|
||||
} else {
|
||||
"personal"
|
||||
};
|
||||
VAULT_ITEMS_TOTAL.with_label_values(&[cipher_type, org_label]).inc();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue