Struct rocket_prometheus::PrometheusMetrics
source · [−]pub struct PrometheusMetrics { /* private fields */ }
Expand description
Fairing and Handler implementing request instrumentation.
By default this 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.
Usage
Simply attach and mount a PrometheusMetrics
instance to your Rocket
app as for a normal fairing / handler:
use rocket_prometheus::PrometheusMetrics;
#[rocket::launch]
fn launch() -> _ {
let prometheus = PrometheusMetrics::new();
rocket::build()
.attach(prometheus.clone())
.mount("/metrics", prometheus)
}
Metrics will then be available on the “/metrics” endpoint:
$ curl localhost:8000/metrics
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
rocket_http_requests_total{endpoint="/metrics",method="GET",status="200"} 2
Implementations
sourceimpl PrometheusMetrics
impl PrometheusMetrics
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new PrometheusMetrics
.
sourcepub fn with_registry(registry: Registry) -> Self
pub fn with_registry(registry: Registry) -> Self
Create a new PrometheusMetrics
with a custom Registry
.
sourcepub fn with_default_registry() -> Self
pub fn with_default_registry() -> Self
Create a new PrometheusMetrics
using the default Prometheus Registry
.
This will cause the fairing to include metrics created by the various
prometheus
macros, e.g. register_int_counter
.
sourcepub const fn registry(&self) -> &Registry
pub const fn registry(&self) -> &Registry
Get the registry used by this fairing to track additional metrics.
You can use this to register further metrics,
causing them to be exposed along with the default metrics
on the PrometheusMetrics
handler.
Note that the http_requests_total
and http_requests_duration_seconds
metrics
are not included in this registry.
use once_cell::sync::Lazy;
use prometheus::{opts, IntCounter};
use rocket_prometheus::PrometheusMetrics;
static MY_COUNTER: Lazy<IntCounter> = Lazy::new(|| {
IntCounter::new("my_counter", "A counter I use a lot")
.expect("Could not create counter")
});
let prometheus = PrometheusMetrics::new();
prometheus.registry().register(Box::new(MY_COUNTER.clone()));
Trait Implementations
sourceimpl Clone for PrometheusMetrics
impl Clone for PrometheusMetrics
sourcefn clone(&self) -> PrometheusMetrics
fn clone(&self) -> PrometheusMetrics
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Default for PrometheusMetrics
impl Default for PrometheusMetrics
sourceimpl Fairing for PrometheusMetrics
impl Fairing for PrometheusMetrics
sourcefn on_request<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request<'life2>,
__arg2: &'life3 mut Data<'life4>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
fn on_request<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request<'life2>,
__arg2: &'life3 mut Data<'life4>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
The request callback. Read more
sourcefn on_response<'r, 'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: &'r Request<'life1>,
response: &'life2 mut Response<'r>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn on_response<'r, 'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: &'r Request<'life1>,
response: &'life2 mut Response<'r>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
The response callback. Read more
sourcefn on_ignite<'life0, 'async_trait>(
&'life0 self,
rocket: Rocket<Build>
) -> Pin<Box<dyn Future<Output = Result<Rocket<Build>, Rocket<Build>>> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
Self: 'async_trait,
fn on_ignite<'life0, 'async_trait>(
&'life0 self,
rocket: Rocket<Build>
) -> Pin<Box<dyn Future<Output = Result<Rocket<Build>, Rocket<Build>>> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
Self: 'async_trait,
The ignite callback. Returns Ok
if ignition should proceed and Err
if ignition and launch should be aborted. Read more
sourceimpl From<PrometheusMetrics> for Vec<Route>
impl From<PrometheusMetrics> for Vec<Route>
sourcefn from(other: PrometheusMetrics) -> Self
fn from(other: PrometheusMetrics) -> Self
Performs the conversion.
sourceimpl Handler for PrometheusMetrics
impl Handler for PrometheusMetrics
sourcefn handle<'r, 'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'r Request<'life1>,
__arg2: Data<'r>
) -> Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn handle<'r, 'life0, 'life1, 'async_trait>(
&'life0 self,
req: &'r Request<'life1>,
__arg2: Data<'r>
) -> Pin<Box<dyn Future<Output = Outcome<'r>> + Send + 'async_trait>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Called by Rocket when a Request
with its associated Data
should be
handled by this handler. Read more
Auto Trait Implementations
impl !RefUnwindSafe for PrometheusMetrics
impl Send for PrometheusMetrics
impl Sync for PrometheusMetrics
impl Unpin for PrometheusMetrics
impl !UnwindSafe for PrometheusMetrics
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Cloneable for T where
T: Handler + Clone,
impl<T> Cloneable for T where
T: Handler + Clone,
pub fn clone_handler(&self) -> Box<dyn Handler + 'static, Global>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
pub fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
pub fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more