1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::{
    graphql_metrics::GRAPHQL_METRICS,
    p2p_metrics::P2P_METRICS,
    txpool_metrics::TXPOOL_METRICS,
};
use axum::{
    body::Body,
    response::{
        IntoResponse,
        Response,
    },
};
use libp2p_prom_client::encoding::text::encode as libp2p_encode;
use prometheus_client::encoding::text::encode;

pub fn encode_metrics_response() -> impl IntoResponse {
    // encode libp2p metrics using older prometheus
    let mut libp2p_bytes = Vec::<u8>::new();
    if let Some(value) = P2P_METRICS.gossip_sub_registry.get() {
        if libp2p_encode(&mut libp2p_bytes, value).is_err() {
            return error_body()
        }
    }
    if libp2p_encode(&mut libp2p_bytes, &P2P_METRICS.peer_metrics).is_err() {
        return error_body()
    }

    let mut encoded = String::from_utf8_lossy(&libp2p_bytes).into_owned();

    // encode the rest of the fuel-core metrics using latest prometheus
    if encode(&mut encoded, &TXPOOL_METRICS.registry).is_err() {
        return error_body()
    }

    if encode(&mut encoded, &GRAPHQL_METRICS.registry).is_err() {
        return error_body()
    }

    Response::builder()
        .status(200)
        .body(Body::from(encoded))
        .unwrap()
}

fn error_body() -> Response<Body> {
    Response::builder()
        .status(503)
        .body(Body::from(""))
        .unwrap()
}