#rocket #metrics-collection #client #endpoints #capability #service #valensas

valensas-rocket

A library that provides a Rocket client with the capability of metric collection for endpoints

4 releases

0.1.3 Aug 21, 2023
0.1.2 Jul 13, 2023
0.1.1 Jul 12, 2023
0.1.0 Jul 12, 2023

#15 in #metrics-collection

Download history 2/week @ 2024-02-22 2/week @ 2024-02-29 5/week @ 2024-03-07 15/week @ 2024-03-14 10/week @ 2024-03-28 6/week @ 2024-04-04 5/week @ 2024-04-11 53/week @ 2024-04-18

74 downloads per month

MIT license

8KB
97 lines

Valensas Rocket - Rocket Client Library

This library provides a Rocket client with the capability of metric collection for endpoints. It allows you to create and configure a Rocket server instance to interact with web services.

Usage

To use this library, you need to create an instance of valensas_rocket::client::client_service::Client and run spawn_rocket() method to launch the application.

For detailed information visit: https://docs.rs/valensas-rocket

Installation

Add the following to your Cargo.toml file:

[dependencies]
valensas-rocket = "0.1.2"

lib.rs:

Rocket Client Library

This library provides a Rocket client with metric collection for endpoints. It allows you to create and configure a Rocket server instance to interact with web services.

Example Usage

use valensas_actuator::metrics::{ArcRwLockPrometheus, PrometheusMetrics};
use valensas_rocket::client::client_params::ClientParams;
use valensas_rocket::client::client_service::Client;
use rocket::{get, routes, Route};
use std::sync::{Arc, RwLock};

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[tokio::main]
async fn main() {
    // Create a vector of routes (endpoints)
    let routes: Vec<Route> = routes![index];

    // Optional: Create a Prometheus metrics object
    // For further information about actuator library, visit: https://crates.io/crates/valensas-actuator
    let prometheus = Arc::new(RwLock::new(PrometheusMetrics::new("your_namespace")));
    let prometheus_fairing = ArcRwLockPrometheus::new(prometheus.clone());

    // Create a new Rocket client
    let client = Client::new(
        ClientParams {
            ip_addr: "127.0.0.1".to_string(),
            port: "8000".to_string(),
        },
        routes,
        Some(prometheus_fairing), // Pass the Prometheus object
    )
        // Optional: Set a managed state
        //.set_manage(SomeState)
        // Optional: Set a fairing
        //.set_fairing(SomeFairing)
        .spawn_rocket();

    rocket::tokio::task::spawn(client.await)
        .await
        .unwrap()
        .unwrap();
    println!("Rocket server launched successfully!");
}

The example demonstrates how to create a Rocket client using the library. It sets up a single route ("/") with a handler function (index). Optionally, it creates a Prometheus metrics object, sets a managed state (SomeState), and attaches a fairing (SomeFairing). The client is then launched using the spawn_rocket function, and the Prometheus metrics can be accessed if available (For further information about metrics, visit: valensas-actuator). Finally, the client awaits completion and prints a success message.

Dependencies

~17–53MB
~862K SLoC