Code Monkey home page Code Monkey logo

rocket_prometheus's Introduction

Rocket Prometheus

Build Status docs.rs crates.io

Prometheus instrumentation for Rocket applications.

Usage

Add this crate to your Cargo.toml alongside Rocket 0.5.0:

[dependencies]
rocket = "0.5.0"
rocket_prometheus = "0.10.0"

Then attach and mount a PrometheusMetrics instance to your Rocket app:

use rocket_prometheus::PrometheusMetrics;

#[rocket::launch]
fn launch() -> _ {
    let prometheus = PrometheusMetrics::new();
    rocket::build()
        .attach(prometheus.clone())
        .mount("/metrics", prometheus)

This will expose metrics like this at the /metrics endpoint of your application:

$ curl localhost:8000/metrics
# HELP rocket_http_requests_duration_seconds HTTP request duration in seconds for all requests
# TYPE rocket_http_requests_duration_seconds histogram
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.005"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.01"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.025"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.05"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.1"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.25"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="0.5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="1"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="2.5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="5"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="10"} 2
rocket_http_requests_duration_seconds_bucket{endpoint="/metrics",method="GET",status="200",le="+Inf"} 2
rocket_http_requests_duration_seconds_sum{endpoint="/metrics",method="GET",status="200"} 0.0011045669999999999
rocket_http_requests_duration_seconds_count{endpoint="/metrics",method="GET",status="200"} 2
# HELP rocket_http_requests_total Total number of HTTP requests
# TYPE rocket_http_requests_total counter
rocket_http_requests_total{endpoint="/metrics",method="GET",status="200"} 2

Metrics

By default this crate tracks two metrics:

  • rocket_http_requests_total (labels: endpoint, method, status): the total number of HTTP requests handled by Rocket.
  • rocket_http_requests_duration_seconds (labels: endpoint, method, status): the request duration for all HTTP requests handled by Rocket.

The 'rocket' prefix of these metrics can be changed by setting the ROCKET_PROMETHEUS_NAMESPACE environment variable.

Custom Metrics

Further metrics can be tracked by registering them with the registry of the PrometheusMetrics instance:

#[macro_use]
use once_cell::sync::Lazy;
use rocket::{get, launch, routes};
use rocket_prometheus::{
    prometheus::{opts, IntCounterVec},
    PrometheusMetrics,
};

static NAME_COUNTER: Lazy<IntCounterVec> = Lazy::new(|| {
    IntCounterVec::new(opts!("name_counter", "Count of names"), &["name"])
        .expect("Could not create NAME_COUNTER")
});

#[get("/hello/<name>")]
pub fn hello(name: &str) -> String {
    NAME_COUNTER.with_label_values(&[name]).inc();
    format!("Hello, {}!", name)
}

#[launch]
fn launch() -> _ {
    let prometheus = PrometheusMetrics::new();
    prometheus
        .registry()
        .register(Box::new(NAME_COUNTER.clone()))
        .unwrap();
    rocket::build()
        .attach(prometheus.clone())
        .mount("/", routes![hello])
        .mount("/metrics", prometheus)
}

rocket_prometheus's People

Contributors

dependabot-preview[bot] avatar ivashchenkoserhii avatar sd2k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rocket_prometheus's Issues

/metrics endpoint is resolving but no metrics are displayed

I'm using version = "0.10.0-rc.3" and my rocket webserver will resolve /metrics but there is no data returned, even though I have sent several GET AND POST requests to my server.

rocket::build()
.attach(AdHoc::config::())
.manage(pool)
.manage(replier)
.manage(prometheus.clone())
.manage(amqp_pool)
.attach(rocket_governor::LimitHeaderGen::default())
.register("/", catchers!(rocket_governor_catcher)) // catches limit errors and produces a 429 error code
.mount("/ref", routes![refs::get,refs::patch, refs::delete, refs::post, refs::get_all])
.mount("/device", routes![device::get,device::patch, device::delete, device::post, device::get_all])
.mount("/library", routes![library::get,library::patch, library::delete, library::post, library::get_all, library::link_ref_item_to_library, library::get_ref_item_in_library])
.mount("/session", routes![session::get,session::patch, session::delete, session::post, session::get_all])
.mount("/project", routes![project::get,project::patch, project::delete, project::post, project::get_all,project::link_library_to_project, project::get_library_in_project,project::match_detections])
.mount("/metrics", prometheus)
.launch().await?;

prepare rocket v0.5

it does not build with current master of rocket ;)

error[E0277]: the trait bound PrometheusMetrics: Fairing is not satisfied
--> src/main.rs:38:17
|
38 | .attach(prometheus.clone())
| ^^^^^^^^^^^^^^^^^^ the trait Fairing is not implemented for PrometheusMetrics
error[E0277]: the trait bound Vec<Route>: From<PrometheusMetrics> is not satisfied
--> src/main.rs:44:10
|
44 | .mount("/metrics", prometheus)
| ^^^^^ the trait From<PrometheusMetrics> is not implemented for Vec<Route>
|
= help: the following implementations were found:
<Vec<T> as From<&[T]>>
<Vec<T> as From<&mut [T]>>
<Vec<T> as From<BinaryHeap<T>>>
<Vec<T> as From<Box<[T]>>>
and 6 others
= note: required because of the requirements on the impl of Into<Vec<Route>> for PrometheusMetrics

sorry, i know this issue is a really early for a unstable rocket version

Update to current version

Hey!
Cool plugin! It works like a charm. Unfortunately tools like Grafana dont support old Prometheus versions as it seems.

The current API specification is prometheus 1.8 I believe. It would be nice if you would implement instant queries (i.e., Prometheus 2.14 specification) such that Grafana works :)

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.