Closed cybershang closed 1 month ago
- There are conflicts in three files in the pull request, they need to be resolved.
- test case is missing.
The Windows test case has failed.
The Windows test case has failed.
The metric load average
captured by sysinfo is not available on Windows platform.
In system_metric.rs test case, skip the assert of load average on Windows platform.
// load average is not available on Windows
if cfg!(target_os = "windows") {
gauge_map.remove("load_average");
}
Instrument RustyVault with Prometheus
Design
To monitor the system's performance effectively, I applied both the USE and RED methods for metrics collection in RustyVault.
USE Method (Utilization, Saturation, Errors):
Track resource utilization and detect bottlenecks. Metrics related to system resources have been added to ensure the system's health is continuously monitored:
CPU Utilization: Measures the percentage of CPU usage by the RustyVault service.
Memory Utilization: Tracks memory usage, including total, free, and cached memory.
Disk I/O Saturation: Monitors disk read/write speed and detects potential bottlenecks.
Network I/O Saturation: Tracks the amount of data sent and received.
RED Method (Rate, Errors, Duration)
Track the behavior of requests within the application:
Rate: We implemented requests_total to track the rate of requests coming into the system. This allows us to monitor the overall throughput.
Errors: The errors_total counter tracks the number of failed requests and helps monitor the system's error rate.
Duration: Using request_duration_seconds, we measure the time taken to process each request, enabling us to analyze latency and potential performance issues.
Implemented Metrics
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
<Gauge, AtomicU64>
struct HttpLabel {path:String, method:MetricsMethod, status:u16}
Family<HttpLabel, Counter>
Family<HttpLabel, Histogram>
Changes
Dependency Imported
MetricsManager Implementation:
MetricsManager
inmanager.rs
to store Prometheus Registry, system metrics (system_metrics
), and HTTP API metrics (http_metrics
).metrics_manager
into the server insrc/cli/command/server.rs
by inserting it intoapp_data
.Implemented metrics_handler:
init_metrics_service
in metrics.rs, Sets up the /metrics service by configuring a route in the ServiceConfig. Associates the /metrics route withmetrics_handler
to handle GET requests and respond with Prometheus metrics in text format.System Metrics Collection:
SystemMetrics
struct insystem_metrics.rs
to gather CPU, memory, load, and disk metrics using thesysinfo
crate.collect_metrics
function to collect and store system information.start_collecting
method inserver.block_on
to periodically collect system metrics.HTTP Middleware:
MetricsMiddleware
inmiddleware.rs
as a function middleware to capture HTTP request metrics.src/cli/command/server.rs
to apply the middleware using.wrap(from_fn(metrics_middleware))
.MetricsMethod
enum, trackingGET
,POST
,PUT
,DELETE
, and categorizing others asOTHER
.HTTP Metrics:
HttpMetrics
struct inhttp_metrics.rs
to handle HTTP request counting and duration observation.requests
counter andhistogram
for request durations.increment_request_count
andobserve_duration
for tracking requests and their durations, labeled by HTTP method and path.Testing Steps
curl
to visithttp://localhost:<PORT>/metrics
./login
and/register
.requests_total
andrequest_duration_seconds
increment appropriately.errors_total
increments accordingly./metrics
endpoint to the Prometheus configuration.