Skip to content

Commit

Permalink
Merge pull request #58 from mempool/junderw/add-metrics-rest-api
Browse files Browse the repository at this point in the history
Add metrics for REST response times
  • Loading branch information
wiz authored Mar 6, 2024
2 parents 48fe0aa + 53894cc commit 94b9222
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bin/electrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn run_server(config: Arc<Config>) -> Result<()> {
));

// TODO: configuration for which servers to start
let rest_server = rest::start(Arc::clone(&config), Arc::clone(&query));
let rest_server = rest::start(Arc::clone(&config), Arc::clone(&query), &metrics);
let electrum_server = ElectrumRPC::start(Arc::clone(&config), Arc::clone(&query), &metrics);

if let Some(ref precache_file) = config.precache_scripts {
Expand Down
20 changes: 17 additions & 3 deletions src/rest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::chain::{address, BlockHash, Network, OutPoint, Script, Transaction, TxIn, TxOut, Txid};
use crate::config::{Config, VERSION_STRING};
use crate::errors;
use crate::metrics::Metrics;
use crate::new_index::{compute_script_hash, Query, SpendingInput, Utxo};
use crate::util::{
create_socket, electrum_merkle, extract_tx_prevouts, full_hash, get_innerscripts, get_tx_fee,
Expand All @@ -17,6 +18,7 @@ use bitcoin::hashes::Error as HashError;
use hex::{self, FromHexError};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Response, Server, StatusCode};
use prometheus::{HistogramOpts, HistogramVec};
use tokio::sync::oneshot;

use hyperlocal::UnixServerExt;
Expand Down Expand Up @@ -552,7 +554,12 @@ fn prepare_txs(
}

#[tokio::main]
async fn run_server(config: Arc<Config>, query: Arc<Query>, rx: oneshot::Receiver<()>) {
async fn run_server(
config: Arc<Config>,
query: Arc<Query>,
rx: oneshot::Receiver<()>,
metric: HistogramVec,
) {
let addr = &config.http_addr;
let socket_file = &config.http_socket_file;

Expand All @@ -562,11 +569,13 @@ async fn run_server(config: Arc<Config>, query: Arc<Query>, rx: oneshot::Receive
let make_service_fn_inn = || {
let query = Arc::clone(&query);
let config = Arc::clone(&config);
let metric = metric.clone();

async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
let query = Arc::clone(&query);
let config = Arc::clone(&config);
let timer = metric.with_label_values(&["all_methods"]).start_timer();

async move {
let method = req.method().clone();
Expand All @@ -587,6 +596,7 @@ async fn run_server(config: Arc<Config>, query: Arc<Query>, rx: oneshot::Receive
resp.headers_mut()
.insert("Access-Control-Allow-Origin", origins.parse().unwrap());
}
timer.observe_duration();
Ok::<_, hyper::Error>(resp)
}
}))
Expand Down Expand Up @@ -633,13 +643,17 @@ async fn run_server(config: Arc<Config>, query: Arc<Query>, rx: oneshot::Receive
}
}

pub fn start(config: Arc<Config>, query: Arc<Query>) -> Handle {
pub fn start(config: Arc<Config>, query: Arc<Query>, metrics: &Metrics) -> Handle {
let (tx, rx) = oneshot::channel::<()>();
let response_timer = metrics.histogram_vec(
HistogramOpts::new("electrs_rest_api", "Electrs REST API response timings"),
&["method"],
);

Handle {
tx,
thread: crate::util::spawn_thread("rest-server", move || {
run_server(config, query, rx);
run_server(config, query, rx, response_timer);
}),
}
}
Expand Down

0 comments on commit 94b9222

Please sign in to comment.