async fn show_metrics(
    req: Request<Body>,
    context: Arc<PowerMetrics>,
    suffix: String,
) -> Result<Response<Body>, Infallible> {
    trace!("{}", req.uri());
    let mut body = String::new();
    if req.uri().path() == format!("/{}", &suffix) {
        trace!("in metrics !");
        let now = current_system_time_since_epoch();
        let mut last_request = context.last_request.lock().unwrap();
        let mut metric_generator = context.metric_generator.lock().unwrap();
        if now - (*last_request) > Duration::from_secs(2) {
            {
                info!(
                    "{}: Refresh topology",
                    Utc::now().format("%Y-%m-%dT%H:%M:%S")
                );
                metric_generator
                    .topology
                    .proc_tracker
                    .clean_terminated_process_records_vectors();
                metric_generator.topology.refresh();
            }
        }
        *last_request = now;

        info!("{}: Refresh data", Utc::now().format("%Y-%m-%dT%H:%M:%S"));

        metric_generator.gen_all_metrics();

        // Send all data
        for msg in metric_generator.pop_metrics() {
            let mut attributes: Option<&HashMap<String, String>> = None;
            if !msg.attributes.is_empty() {
                attributes = Some(&msg.attributes);
            }

            let value = match msg.metric_value {
                // MetricValueType::IntSigned(value) => event.set_metric_sint64(value),
                // MetricValueType::Float(value) => event.set_metric_f(value),
                MetricValueType::FloatDouble(value) => value.to_string(),
                MetricValueType::IntUnsigned(value) => value.to_string(),
                MetricValueType::Text(ref value) => value.to_string(),
            };
            body = push_metric(
                body,
                msg.description.clone(),
                msg.metric_type.clone(),
                msg.name.clone(),
                format_metric(&msg.name, &value, attributes),
            );
        }
    } else {
        body.push_str(&format!("<a href=\"https://github.com/hubblo-org/scaphandre/\">Scaphandre's</a> prometheus exporter here. Metrics available on <a href=\"/{}\">/{}</a>", suffix, suffix));
    }
    Ok(Response::new(body.into()))
}
/// Handles requests and returns data formated for Prometheus.
Handles requests and returns data formated for Prometheus.
Handles requests and returns data formated for Prometheus .
async fn show_metrics(
    req: Request<Body>,
    context: Arc<PowerMetrics>,
    suffix: String,
) -> Result<Response<Body>, Infallible> {
    trace!("{}", req.uri());
    let mut body = String::new();
    if req.uri().path() == format!("/{}", &suffix) {
        trace!("in metrics !");
        let now = current_system_time_since_epoch();
        let mut last_request = context.last_request.lock().unwrap();
        let mut metric_generator = context.metric_generator.lock().unwrap();
        if now - (*last_request) > Duration::from_secs(2) {
            {
                info!(
                    "{}: Refresh topology",
                    Utc::now().format("%Y-%m-%dT%H:%M:%S")
                );
                metric_generator
                    .topology
                    .proc_tracker
                    .clean_terminated_process_records_vectors();
                metric_generator.topology.refresh();
            }
        }
        *last_request = now;
        info!("{}: Refresh data", Utc::now().format("%Y-%m-%dT%H:%M:%S"));
        metric_generator.gen_all_metrics();
        // Send all data
        for msg in metric_generator.pop_metrics() {
            let mut attributes: Option<&HashMap<String, String>> = None;
            if !msg.attributes.is_empty() {
                attributes = Some(&msg.attributes);
            }
            let value = match msg.metric_value {
                // MetricValueType::IntSigned(value) => event.set_metric_sint64(value),
                // MetricValueType::Float(value) => event.set_metric_f(value),
                MetricValueType::FloatDouble(value) => value.to_string(),
                MetricValueType::IntUnsigned(value) => value.to_string(),
                MetricValueType::Text(ref value) => value.to_string(),
            };
            body = push_metric(
                body,
                msg.description.clone(),
                msg.metric_type.clone(),
                msg.name.clone(),
                format_metric(&msg.name, &value, attributes),
            );
        }
    } else {
        body.push_str(&format!("<a href=\"https://github.com/hubblo-org/scaphandre/\">Scaphandre's</a> prometheus exporter here. Metrics available on <a href=\"/{}\">/{}</a>", suffix, suffix));
    }
    Ok(Response::new(body.into()))
}
async fn show_metrics ( req : Request < Body > , context : Arc < PowerMetrics > , suffix : String , ) -> Result < Response < Body > , Infallible > { trace ! ( " " req uri ( ) ) ; let mut body = String :: new ( ) ; if req . uri ( ) . path ( ) == format ! ( " " suffix ) { trace ! ( " " ) ; let now = current_system_time_since_epoch ( ) ; let mut last_request = context . last_request . lock ( ) . unwrap ( ) ; let mut metric_generator = context . metric_generator . lock ( ) . unwrap ( ) ; if now - ( * last_request ) > Duration :: from_secs ( 2 ) { { info ! ( " " Utc now ( ) format ( " " ) ) ; metric_generator . topology . proc_tracker . clean_terminated_process_records_vectors ( ) ; metric_generator . topology . refresh ( ) ; } } * last_request = now ; info ! ( " " Utc now ( ) format ( " " ) ) ; metric_generator . gen_all_metrics ( ) ; // Send all data for msg in metric_generator . pop_metrics ( ) { let mut attributes : Option < & HashMap < String , String > > = None ; if ! msg . attributes . is_empty ( ) { attributes = Some ( & msg . attributes ) ; } let value = match msg . metric_value { // MetricValueType::IntSigned(value) => event.set_metric_sint64(value), // MetricValueType::Float(value) => event.set_metric_f(value), MetricValueType :: FloatDouble ( value ) => value . to_string ( ) , MetricValueType :: IntUnsigned ( value ) => value . to_string ( ) , MetricValueType :: Text ( ref value ) => value . to_string ( ) , } ; body = push_metric ( body , msg . description . clone ( ) , msg . metric_type . clone ( ) , msg . name . clone ( ) , format_metric ( & msg . name , & value , attributes ) , ) ; } } else { body . push_str ( & format ! ( " \" \" \" \" " suffix suffix ) ) ; } Ok ( Response :: new ( body . into ( ) ) ) }
Handles requests and returns data formated for Prometheus.
Handles requests and returns data formated for Prometheus .
req
Request<Body>
context
Arc<PowerMetrics>
suffix
String
req
Request<Body>
context
Arc<PowerMetrics>
suffix
String