Skip to content

Commit

Permalink
Merge branch 'main' into feat/1413-plain-text-component
Browse files Browse the repository at this point in the history
  • Loading branch information
LynxLynxx authored Jan 15, 2025
2 parents 472536a + e169a08 commit d537267
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 76 deletions.
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod cli;
mod db;
mod jinja;
mod logger;
mod metrics;
mod service;
mod settings;
mod utils;
Expand Down
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/metrics/chain_follower.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Metrics related to Chain Follower analytics.
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/metrics/chain_indexer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Metrics related to Chain Indexer analytics.
60 changes: 60 additions & 0 deletions catalyst-gateway/bin/src/metrics/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Metrics related to endpoint analytics.
use std::sync::LazyLock;

use prometheus::{register_histogram_vec, register_int_counter_vec, HistogramVec, IntCounterVec};

/// Labels for the metrics
const METRIC_LABELS: [&str; 3] = ["endpoint", "method", "status_code"];
/// Labels for the client metrics
const CLIENT_METRIC_LABELS: [&str; 2] = ["client", "status_code"];

// Prometheus Metrics maintained by the service

/// HTTP Request duration histogram.
pub(crate) static HTTP_REQ_DURATION_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
register_histogram_vec!(
"http_request_duration_ms",
"Duration of HTTP requests in milliseconds",
&METRIC_LABELS
)
.unwrap()
});

/// HTTP Request CPU Time histogram.
pub(crate) static HTTP_REQ_CPU_TIME_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
register_histogram_vec!(
"http_request_cpu_time_ms",
"CPU Time of HTTP requests in milliseconds",
&METRIC_LABELS
)
.unwrap()
});

// No Tacho implemented to enable this.
// static ref HTTP_REQUEST_RATE: GaugeVec = register_gauge_vec!(
// "http_request_rate",
// "Rate of HTTP requests per second",
// &METRIC_LABELS
// )
// .unwrap();

/// HTTP Request count histogram.
pub(crate) static HTTP_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!(
"http_request_count",
"Number of HTTP requests",
&METRIC_LABELS
)
.unwrap()
});

/// Client Request Count histogram.
pub(crate) static CLIENT_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
register_int_counter_vec!(
"client_request_count",
"Number of HTTP requests per client",
&CLIENT_METRIC_LABELS
)
.unwrap()
});
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/metrics/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! Metrics related to memory analytics.
18 changes: 18 additions & 0 deletions catalyst-gateway/bin/src/metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! This module contains submodules related to metrics report and analytics.
use prometheus::{default_registry, Registry};

pub(crate) mod chain_follower;
pub(crate) mod chain_indexer;
pub(crate) mod endpoint;
pub(crate) mod memory;

/// Initialize Prometheus metrics.
///
/// ## Returns
///
/// Returns the default prometheus registry.
#[must_use]
pub(crate) fn init_prometheus() -> Registry {
default_registry().clone()
}
3 changes: 2 additions & 1 deletion catalyst-gateway/bin/src/service/poem_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use poem::{
};

use crate::{
metrics::init_prometheus,
service::{
api::mk_api,
docs::{docs, favicon},
utilities::{
catch_panic::{set_panic_hook, ServicePanicHandler},
middleware::tracing_mw::{init_prometheus, Tracing},
middleware::tracing_mw::Tracing,
},
},
settings::Settings,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Full Tracing and metrics middleware.
use std::{sync::LazyLock, time::Instant};
use std::time::Instant;

use cpu_time::ProcessTime; // ThreadTime doesn't work.
use poem::{
Expand All @@ -8,74 +9,17 @@ use poem::{
Endpoint, Error, FromRequest, IntoResponse, Middleware, PathPattern, Request, Response, Result,
};
use poem_openapi::OperationId;
use prometheus::{
default_registry, register_histogram_vec, register_int_counter_vec, HistogramVec,
IntCounterVec, Registry,
};
use tracing::{error, field, Instrument, Level, Span};
use ulid::Ulid;
use uuid::Uuid;

use crate::{settings::Settings, utils::blake2b_hash::generate_uuid_string_from_data};

/// Labels for the metrics
const METRIC_LABELS: [&str; 3] = ["endpoint", "method", "status_code"];
/// Labels for the client metrics
const CLIENT_METRIC_LABELS: [&str; 2] = ["client", "status_code"];

// Prometheus Metrics maintained by the service

/// HTTP Request duration histogram.
static HTTP_REQ_DURATION_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
#[allow(clippy::ignored_unit_patterns)]
register_histogram_vec!(
"http_request_duration_ms",
"Duration of HTTP requests in milliseconds",
&METRIC_LABELS
)
.unwrap()
});

/// HTTP Request CPU Time histogram.
static HTTP_REQ_CPU_TIME_MS: LazyLock<HistogramVec> = LazyLock::new(|| {
#[allow(clippy::ignored_unit_patterns)]
register_histogram_vec!(
"http_request_cpu_time_ms",
"CPU Time of HTTP requests in milliseconds",
&METRIC_LABELS
)
.unwrap()
});

// No Tacho implemented to enable this.
// static ref HTTP_REQUEST_RATE: GaugeVec = register_gauge_vec!(
// "http_request_rate",
// "Rate of HTTP requests per second",
// &METRIC_LABELS
// )
// .unwrap();

/// HTTP Request count histogram.
static HTTP_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
#[allow(clippy::ignored_unit_patterns)]
register_int_counter_vec!(
"http_request_count",
"Number of HTTP requests",
&METRIC_LABELS
)
.unwrap()
});

/// Client Request Count histogram.
static CLIENT_REQUEST_COUNT: LazyLock<IntCounterVec> = LazyLock::new(|| {
#[allow(clippy::ignored_unit_patterns)]
register_int_counter_vec!(
"client_request_count",
"Number of HTTP requests per client",
&CLIENT_METRIC_LABELS
)
.unwrap()
});
use crate::{
metrics::endpoint::{
CLIENT_REQUEST_COUNT, HTTP_REQUEST_COUNT, HTTP_REQ_CPU_TIME_MS, HTTP_REQ_DURATION_MS,
},
settings::Settings,
utils::blake2b_hash::generate_uuid_string_from_data,
};

// Currently no way to get these values. TODO.
// Panic Request Count histogram.
Expand Down Expand Up @@ -389,13 +333,3 @@ impl<E: Endpoint> Endpoint for TracingEndpoint<E> {
response
}
}

/// Initialize Prometheus metrics.
///
/// ## Returns
///
/// Returns the default prometheus registry.
#[must_use]
pub(crate) fn init_prometheus() -> Registry {
default_registry().clone()
}

0 comments on commit d537267

Please sign in to comment.