Tongsuo-Project / RustyVault

A rusted vault that can do many awesome secrets management stuff...
Apache License 2.0
264 stars 21 forks source link

Instrument RustyVault with Prometheus #76

Closed cybershang closed 1 month ago

cybershang commented 2 months ago

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.

Implemented Metrics

Changes

  1. Dependency Imported

    • prometheus-client = "0.22.3"
    • tokio = "1.40.0"
    • sysinfo = "0.31.4"
  2. MetricsManager Implementation:

    • Implemented MetricsManager in manager.rs to store Prometheus Registry, system metrics (system_metrics), and HTTP API metrics (http_metrics).
    • Integrated metrics_manager into the server in src/cli/command/server.rs by inserting it into app_data.
  3. Implemented metrics_handler:

    • Implemented init_metrics_service in metrics.rs, Sets up the /metrics service by configuring a route in the ServiceConfig. Associates the /metrics route with metrics_handler to handle GET requests and respond with Prometheus metrics in text format.
  4. System Metrics Collection:

    • Implemented SystemMetrics struct in system_metrics.rs to gather CPU, memory, load, and disk metrics using the sysinfo crate.
    • Added collect_metrics function to collect and store system information.
    • Launched the start_collecting method in server.block_on to periodically collect system metrics.
  5. HTTP Middleware:

    • Implemented MetricsMiddleware in middleware.rs as a function middleware to capture HTTP request metrics.
    • Configured the HTTP server in src/cli/command/server.rs to apply the middleware using .wrap(from_fn(metrics_middleware)).
    • Transformed Actix-web's HTTP methods into a custom MetricsMethod enum, tracking GET, POST, PUT, DELETE, and categorizing others as OTHER.
    • Recorded request duration by logging start and end timestamps for each request.
  6. HTTP Metrics:

    • Created HttpMetrics struct in http_metrics.rs to handle HTTP request counting and duration observation.
    • Registered two Prometheus metrics: requests counter and histogram for request durations.
    • Added methods increment_request_count and observe_duration for tracking requests and their durations, labeled by HTTP method and path.

Testing Steps

  1. Start RustyVault Service:
    • Ensure that Prometheus integration is enabled in the configuration.
  2. Access Metrics Endpoint:
    • Open a browser or use curl to visit http://localhost:<PORT>/metrics.
    • Verify that Prometheus metrics are correctly displayed.
  3. Trigger Various Requests:
    • Successful Requests:
      • Send valid requests to endpoints like /login and /register.
      • Confirm that requests_total and request_duration_seconds increment appropriately.
    • Failed Requests:
      • Send invalid or malformed requests to induce errors.
      • Check that errors_total increments accordingly.
  4. Integrate with Prometheus Server:
    • Add RustyVault's /metrics endpoint to the Prometheus configuration.
  5. Using Grafana Dashboard:
    • Use a Grafana dashboard to visualize the collected metrics and demonstrate the data.

image

CLAassistant commented 2 months ago

CLA assistant check
All committers have signed the CLA.

wa5i commented 2 months ago
  1. There are conflicts in three files in the pull request, they need to be resolved.
  2. test case is missing.
cybershang commented 1 month ago
  1. There are conflicts in three files in the pull request, they need to be resolved.
  2. test case is missing.
  1. Conflicts resolved
wa5i commented 1 month ago

The Windows test case has failed.

cybershang commented 1 month ago

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");
        }