pub struct RequestMetrics { /* private fields */ }
Available on crate feature metrics only.
Expand description

Request metrics tracking

Examples

use actix_web::{dev, http, web, App, HttpRequest, HttpServer};
use actix_web_opentelemetry::{
    PrometheusMetricsHandler,
    RequestMetricsBuilder,
    RequestTracing,
};
use opentelemetry::{
    global,
    sdk::{
        export::metrics::aggregation,
        metrics::{controllers, processors, selectors},
        propagation::TraceContextPropagator,
    },
};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Request metrics middleware
    let meter = global::meter("actix_web");
    let request_metrics = RequestMetricsBuilder::new().build(meter);

    // Prometheus request metrics handler
    let controller = controllers::basic(
        processors::factory(
            selectors::simple::histogram([1.0, 2.0, 5.0, 10.0, 20.0, 50.0]),
            aggregation::cumulative_temporality_selector(),
        )
        .with_memory(true),
    )
    .build();
    let exporter = opentelemetry_prometheus::exporter(controller).init();
    let metrics_handler = PrometheusMetricsHandler::new(exporter);

    // Run actix server, metrics are now available at http://localhost:8080/metrics
    HttpServer::new(move || {
        App::new()
            .wrap(RequestTracing::new())
            .wrap(request_metrics.clone())
            .route("/metrics", web::get().to(metrics_handler.clone()))
    })
    .bind("localhost:8080")?
    .run()
    .await
}

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Responses produced by the service.
Errors produced by the service.
The TransformService value created by this factory
Errors produced while building a transform service.
The future response value.
Creates and returns a new Transform component, asynchronously

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more