Skip to content

Commit

Permalink
Merge branch 'main' into feat/put-doc-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jan 15, 2025
2 parents f320366 + d1fdfcf commit 8be1bb7
Show file tree
Hide file tree
Showing 45 changed files with 1,802 additions and 124 deletions.
2 changes: 1 addition & 1 deletion .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ lovelace
lovelaces
LTRB
Lynxx
LynxLynxx
mdlint
metadatum
metadatums
Expand Down Expand Up @@ -259,7 +260,6 @@ rustflags
rustfmt
rustls
rxdart
ryszard-schossler
saibatizoku
Schemathesis
Scripthash
Expand Down
4 changes: 2 additions & 2 deletions catalyst-gateway/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ rust_decimal = { version = "1.36.0", features = [
"serde-with-float",
"db-tokio-postgres",
] }
poem = { version = "3.1.3", features = ["embed", "prometheus", "compression"] }
poem-openapi = { version = "5.1.2", features = [
poem = { version = "3.1.6", features = ["embed", "prometheus", "compression"] }
poem-openapi = { version = "5.1.5", features = [
"openapi-explorer",
"rapidoc",
"redoc",
Expand Down
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ use crate::{
#[derive(ApiResponse)]
#[allow(dead_code)]
pub(crate) enum Responses {
/// ## OK
///
/// The Document that was requested.
#[oai(status = 200)]
Ok(Cbor),
/// ## Not Found
///
/// The document could not be found.
#[oai(status = 404)]
NotFound,
Expand Down
44 changes: 37 additions & 7 deletions catalyst-gateway/bin/src/service/api/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ use poem_openapi::{
payload::Json,
OpenApi,
};
use put_document::MAXIMUM_DOCUMENT_SIZE;
use post_document_index_query::query_filter::DocumentIndexQueryFilterBody;
use put_document::{bad_put_request::PutDocumentBadRequest, MAXIMUM_DOCUMENT_SIZE};

use crate::service::{
common::{
auth::none_or_rbac::NoneOrRBAC,
objects::document::bad_put_request::PutDocumentBadRequest,
tags::ApiTags,
types::{generic::uuidv7::UUIDv7, payload::cbor::Cbor},
types::{
generic::{
query::pagination::{Limit, Page},
uuidv7::UUIDv7,
},
payload::cbor::Cbor,
},
},
utilities::middleware::schema_validation::schema_version_validation,
};

mod get_document;
mod post_document_index_query;
mod put_document;

/// Cardano Follower API Endpoints
Expand Down Expand Up @@ -76,12 +83,35 @@ impl DocumentApi {
match cbor.into_bytes_with_limit(MAXIMUM_DOCUMENT_SIZE).await {
Ok(doc_bytes) => put_document::endpoint(doc_bytes).await,
Err(ReadBodyError::PayloadTooLarge) => put_document::Responses::PayloadTooLarge.into(),
Err(_err) => {
put_document::Responses::BadRequest(Json(PutDocumentBadRequest::new(
"Failed to read document from the request",
)))
Err(e) => {
put_document::Responses::BadRequest(Json(PutDocumentBadRequest::new(format!(
"Failed to read document from the request, err: {e}"
))))
.into()
},
}
}

/// Post A Signed Document Index Query.
///
/// This endpoint produces a summary of signed documents that meet the criteria
/// defined in the request body.
///
/// It does not return the actual documents, just an index of the document identifiers
/// which allows the documents to be retrieved by the `GET document` endpoint.
#[oai(
path = "/draft/document/index",
method = "post",
operation_id = "postDocument",
transform = "schema_version_validation"
)]
async fn post_document(
&self, /// The Query Filter Specification
query: Json<DocumentIndexQueryFilterBody>,
page: Query<Option<Page>>, limit: Query<Option<Limit>>,
/// Authorization required.
_auth: NoneOrRBAC,
) -> post_document_index_query::AllResponses {
post_document_index_query::endpoint(query.0 .0, page.0, limit.0).await
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! Document Index Query
use poem_openapi::{payload::Json, ApiResponse, Object};
use query_filter::DocumentIndexQueryFilter;
use response::DocumentIndexListDocumented;

use super::{Limit, Page};
use crate::service::common::responses::WithErrorResponses;

pub(crate) mod query_filter;
pub(crate) mod response;

/// Endpoint responses.
#[derive(ApiResponse)]
#[allow(dead_code)]
pub(crate) enum Responses {
/// ## OK
///
/// The Index of documents which match the query filter.
#[oai(status = 200)]
Ok(Json<DocumentIndexListDocumented>),
/// ## Not Found
///
/// No documents were found which match the query filter.
#[oai(status = 404)]
NotFound,
}

/// All responses.
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// Update user schema
#[derive(Debug, Object, Clone, Eq, PartialEq)]
pub(crate) struct QueryDocumentIndex {
/// Name
name: Option<String>,
}

/// # POST `/document/index`
#[allow(clippy::unused_async, clippy::no_effect_underscore_binding)]
pub(crate) async fn endpoint(
filter: DocumentIndexQueryFilter, page: Option<Page>, limit: Option<Limit>,
) -> AllResponses {
let _filter = filter;
let _page = page;
let _limit = limit;

// We return this when the filter results in no documents found.
Responses::NotFound.into()
}
Loading

0 comments on commit 8be1bb7

Please sign in to comment.