Skip to content

Commit

Permalink
[pyth-rng] A few small improvements (#1114)
Browse files Browse the repository at this point in the history
* more stuff

* build

* cleanup

* Update pyth-rng/src/command/generate.rs

Co-authored-by: Reisen <Reisen@users.noreply.github.com>

* Update pyth-rng/src/command/generate.rs

Co-authored-by: Reisen <Reisen@users.noreply.github.com>

* Update pyth-rng/src/command/generate.rs

Co-authored-by: Reisen <Reisen@users.noreply.github.com>

* tracing

---------

Co-authored-by: Reisen <Reisen@users.noreply.github.com>
  • Loading branch information
jayantk and Reisen authored Oct 20, 2023
1 parent efcfae3 commit d096bfa
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 62 deletions.
135 changes: 131 additions & 4 deletions pyth-rng/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyth-rng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clap = { version = "4.4.6", features = ["derive", "cargo", "env"] }
ethabi = "18.0.0"
ethers = "2.0.10"
hex = "0.4.3"
prometheus-client = { version = "0.21.2" }
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "2.0.0", features = ["strum"] }
rand = "0.8.5"
reqwest = { version = "0.11.22", features = ["json", "blocking"] }
Expand All @@ -23,5 +24,7 @@ serde_yaml = "0.9.25"
sha3 = "0.10.8"
tokio = { version = "1.33.0", features = ["full"] }
tower-http = { version = "0.4.0", features = ["cors"] }
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
utoipa = { version = "3.4.0", features = ["axum_extras"] }
utoipa-swagger-ui = { version = "3.1.4", features = ["axum"] }
50 changes: 50 additions & 0 deletions pyth-rng/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,44 @@ use {
},
},
ethers::core::types::Address,
prometheus_client::{
encoding::EncodeLabelSet,
metrics::{
counter::Counter,
family::Family,
},
registry::Registry,
},
std::{
collections::HashMap,
sync::Arc,
},
tokio::sync::RwLock,
};
pub use {
chain_ids::*,
index::*,
live::*,
metrics::*,
ready::*,
revelation::*,
};

mod chain_ids;
mod index;
mod live;
mod metrics;
mod ready;
mod revelation;

pub type ChainId = String;

#[derive(Clone)]
pub struct ApiState {
pub chains: Arc<HashMap<ChainId, BlockchainState>>,

/// Prometheus metrics
pub metrics: Arc<Metrics>,
}

/// The state of the randomness service for a single blockchain.
Expand All @@ -43,6 +61,38 @@ pub struct BlockchainState {
pub provider_address: Address,
}

pub struct Metrics {
pub registry: RwLock<Registry>,
// TODO: track useful metrics. this counter is just a placeholder to get things set up.
pub request_counter: Family<Label, Counter>,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
pub struct Label {
value: String,
}

impl Metrics {
pub fn new() -> Self {
let mut metrics_registry = Registry::default();
let http_requests = Family::<Label, Counter>::default();

// Register the metric family with the registry.
metrics_registry.register(
// With the metric name.
"http_requests",
// And the metric help text.
"Number of HTTP requests received",
http_requests.clone(),
);

Metrics {
registry: RwLock::new(metrics_registry),
request_counter: http_requests,
}
}
}

pub enum RestError {
/// The caller passed a sequence number that isn't within the supported range
InvalidSequenceNumber,
Expand Down
11 changes: 11 additions & 0 deletions pyth-rng/src/api/live.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use axum::{
http::StatusCode,
response::{
IntoResponse,
Response,
},
};

pub async fn live() -> Response {
(StatusCode::OK, "OK").into_response()
}
20 changes: 20 additions & 0 deletions pyth-rng/src/api/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Exposing prometheus metrics via HTTP in openmetrics format.

use {
axum::{
extract::State,
response::IntoResponse,
},
prometheus_client::encoding::text::encode,
};

pub async fn metrics(State(state): State<crate::api::ApiState>) -> impl IntoResponse {
let registry = state.metrics.registry.read().await;
let mut buffer = String::new();

// Should not fail if the metrics are valid and there is memory available
// to write to the buffer.
encode(&mut buffer, &registry).unwrap();

buffer
}
13 changes: 13 additions & 0 deletions pyth-rng/src/api/ready.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use axum::{
http::StatusCode,
response::{
IntoResponse,
Response,
},
};

pub async fn ready() -> Response {
// TODO: are there useful checks here? At the moment, everything important (specifically hash
// chain computation) occurs synchronously on startup.
(StatusCode::OK, "OK").into_response()
}
Loading

0 comments on commit d096bfa

Please sign in to comment.