mirror of
https://github.com/alexgo-io/stacks-puppet-node.git
synced 2026-04-24 03:45:38 +08:00
Re-wire prometheus integration
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -72,3 +72,4 @@ node_modules
|
||||
tarpaulin-report.html
|
||||
osxcross
|
||||
osxcross*.tar.xz
|
||||
testnet/stacks-node/target
|
||||
|
||||
@@ -52,6 +52,7 @@ lazy_static = "1.4.0"
|
||||
url = "2.1.0"
|
||||
percent-encoding = "2.1.0"
|
||||
sha2 = "0.8.0"
|
||||
prometheus = { version = "0.9", optional = true }
|
||||
|
||||
[dependencies.serde_json]
|
||||
version = "1.0"
|
||||
@@ -84,6 +85,7 @@ criterion = "0.3"
|
||||
[features]
|
||||
developer-mode = []
|
||||
default = ["developer-mode"]
|
||||
monitoring = ["prometheus"]
|
||||
|
||||
[target.'cfg(all(target_arch = "x86_64", not(target_env = "msvc")))'.dependencies]
|
||||
sha2-asm = "0.5.3"
|
||||
|
||||
@@ -48,6 +48,9 @@ extern crate percent_encoding;
|
||||
#[macro_use]
|
||||
extern crate assert_json_diff;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
#[macro_use] pub extern crate prometheus;
|
||||
|
||||
#[macro_use]
|
||||
pub mod util;
|
||||
|
||||
@@ -65,6 +68,9 @@ pub mod vm;
|
||||
|
||||
pub mod clarity;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
pub mod monitoring;
|
||||
|
||||
// set via _compile-time_ envars
|
||||
const GIT_BRANCH: Option<&'static str> = option_env!("GIT_BRANCH");
|
||||
const GIT_COMMIT: Option<&'static str> = option_env!("GIT_COMMIT");
|
||||
|
||||
@@ -1,125 +1,75 @@
|
||||
use async_std::net::{TcpListener, TcpStream};
|
||||
use async_std::prelude::*;
|
||||
use async_std::task;
|
||||
use prometheus::{IntCounter, Encoder, Gauge, HistogramVec, TextEncoder};
|
||||
use http_types::{
|
||||
Response,
|
||||
StatusCode,
|
||||
Body,
|
||||
};
|
||||
use prometheus::{IntCounter, Gauge, HistogramVec, TextEncoder};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref RPC_CALL_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_rpc_requests_total",
|
||||
"Total number of RPC requests made.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref P2P_CONTROL_PLAN_MSG_SENT_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_p2p_control_plan_msg_sent_total",
|
||||
"Total number of messages sent to p2p control plan.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref P2P_CONTROL_PLAN_MSG_RECEIVED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_p2p_control_plan_msg_received_total",
|
||||
"Total number of messages received from p2p control plan.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref P2P_DATA_PLAN_MSG_SENT_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_p2p_data_plan_msg_sent_total",
|
||||
"Total number of messages sent to p2p data plan.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref P2P_DATA_PLAN_MSG_RECEIVED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_p2p_data_plan_msg_received_total",
|
||||
"Total number of messages received from p2p data plan.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref TXS_RECEIVED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_transactions_received_total",
|
||||
"Total number of transactions received and relayed.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref BTC_BLOCKS_RECEIVED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_btc_blocks_received_total",
|
||||
"Total number of blocks processed from the burnchain.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref BTC_OPS_SENT_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_btc_ops_sent_total",
|
||||
"Total number of ops (key registrations, block commits, user burn supports) submitted to the burnchain.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref STX_BLOCKS_PROCESSED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_stx_blocks_processed_total",
|
||||
"Total number of stacks blocks processed.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref STX_BLOCKS_MINED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_stx_blocks_mined_total",
|
||||
"Total number of stacks blocks mined by node.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref WARNING_EMITTED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_warning_emitted_total",
|
||||
"Total number of warning logs emitted by node.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
)).unwrap();
|
||||
|
||||
pub static ref ERRORS_EMITTED_COUNTER: IntCounter = register_int_counter!(opts!(
|
||||
"stacks_node_errors_emitted_total",
|
||||
"Total number of error logs emitted by node.",
|
||||
labels! {"handler" => "all",}
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub fn start_serving_prometheus_metrics(prometheus_bind: String) {
|
||||
let addr = prometheus_bind.clone();
|
||||
|
||||
async_std::task::block_on(async {
|
||||
let listener = TcpListener::bind(addr).await.expect("todo(ludo)");
|
||||
let addr = format!("http://{}", listener.local_addr().expect("todo(ludo)"));
|
||||
println!("Prometheus server listening on {}", addr);
|
||||
|
||||
let mut incoming = listener.incoming();
|
||||
while let Some(stream) = incoming.next().await {
|
||||
let stream = stream.expect("todo(ludo)");
|
||||
let addr = addr.clone();
|
||||
|
||||
task::spawn(async {
|
||||
if let Err(err) = accept(addr, stream).await {
|
||||
eprintln!("{}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
|
||||
println!("starting new connection from {}", stream.peer_addr()?);
|
||||
async_h1::accept(&addr, stream.clone(), |mut req| async {
|
||||
RPC_CALL_COUNTER.inc();
|
||||
|
||||
let encoder = TextEncoder::new();
|
||||
let metric_families = prometheus::gather();
|
||||
let mut buffer = vec![];
|
||||
encoder.encode(&metric_families, &mut buffer).unwrap();
|
||||
|
||||
let mut response = Response::new(StatusCode::Ok);
|
||||
response.append_header("Content-Type", encoder.format_type()).expect("Unable to set headers");
|
||||
response.set_body(Body::from(buffer));
|
||||
|
||||
Ok(response)
|
||||
}).await?;
|
||||
Ok(())
|
||||
)).unwrap();
|
||||
}
|
||||
@@ -18,7 +18,6 @@ async-h1 = "1.1"
|
||||
async-std = { version = "<1.6", features = ["attributes"] }
|
||||
http-types = "1.0"
|
||||
base64 = "0.12.0"
|
||||
prometheus = { version = "0.9", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
warp = "0.2"
|
||||
@@ -30,5 +29,5 @@ name = "stacks-node"
|
||||
path = "src/main.rs"
|
||||
|
||||
[features]
|
||||
monitoring = ["prometheus"]
|
||||
monitoring = ["stacks/monitoring"]
|
||||
default = []
|
||||
@@ -43,7 +43,7 @@ use stacks::util::secp256k1::Secp256k1PublicKey;
|
||||
use stacks::util::sleep_ms;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
use crate::run_loop::monitoring::{BTC_BLOCKS_RECEIVED_COUNTER, BTC_OPS_SENT_COUNTER};
|
||||
use stacks::monitoring::{BTC_BLOCKS_RECEIVED_COUNTER, BTC_OPS_SENT_COUNTER};
|
||||
|
||||
pub struct BitcoinRegtestController {
|
||||
config: Config,
|
||||
|
||||
@@ -6,11 +6,11 @@ extern crate serde;
|
||||
#[macro_use] extern crate serde_json;
|
||||
#[macro_use] extern crate stacks;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
#[macro_use] extern crate prometheus;
|
||||
|
||||
pub use stacks::util;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
pub mod monitoring;
|
||||
|
||||
pub mod run_loop;
|
||||
pub mod keychain;
|
||||
pub mod node;
|
||||
|
||||
51
testnet/stacks-node/src/monitoring.rs
Normal file
51
testnet/stacks-node/src/monitoring.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use async_std::net::{TcpListener, TcpStream};
|
||||
use async_std::prelude::*;
|
||||
use async_std::task;
|
||||
use stacks::prometheus::{Encoder, TextEncoder, gather};
|
||||
|
||||
pub use stacks::monitoring::{ERRORS_EMITTED_COUNTER, WARNING_EMITTED_COUNTER};
|
||||
|
||||
use http_types::{
|
||||
Response,
|
||||
StatusCode,
|
||||
Body,
|
||||
};
|
||||
|
||||
pub fn start_serving_prometheus_metrics(prometheus_bind: String) {
|
||||
let addr = prometheus_bind.clone();
|
||||
|
||||
async_std::task::block_on(async {
|
||||
let listener = TcpListener::bind(addr).await.expect("todo(ludo)");
|
||||
let addr = format!("http://{}", listener.local_addr().expect("todo(ludo)"));
|
||||
println!("Prometheus server listening on {}", addr);
|
||||
|
||||
let mut incoming = listener.incoming();
|
||||
while let Some(stream) = incoming.next().await {
|
||||
let stream = stream.expect("todo(ludo)");
|
||||
let addr = addr.clone();
|
||||
|
||||
task::spawn(async {
|
||||
if let Err(err) = accept(addr, stream).await {
|
||||
eprintln!("{}", err);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn accept(addr: String, stream: TcpStream) -> http_types::Result<()> {
|
||||
println!("starting new connection from {}", stream.peer_addr()?);
|
||||
async_h1::accept(&addr, stream.clone(), |mut req| async {
|
||||
let encoder = TextEncoder::new();
|
||||
let metric_families = gather();
|
||||
let mut buffer = vec![];
|
||||
encoder.encode(&metric_families, &mut buffer).unwrap();
|
||||
|
||||
let mut response = Response::new(StatusCode::Ok);
|
||||
response.append_header("Content-Type", encoder.format_type()).expect("Unable to set headers");
|
||||
response.set_body(Body::from(buffer));
|
||||
|
||||
Ok(response)
|
||||
}).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -49,7 +49,7 @@ use stacks::core::FIRST_BURNCHAIN_BLOCK_HASH;
|
||||
use stacks::vm::costs::ExecutionCost;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
use crate::run_loop::monitoring::{
|
||||
use crate::stacks::monitoring::{
|
||||
STX_BLOCKS_PROCESSED_COUNTER,
|
||||
STX_BLOCKS_MINED_COUNTER,
|
||||
TXS_RECEIVED_COUNTER};
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
pub mod helium;
|
||||
pub mod neon;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
pub mod monitoring;
|
||||
|
||||
use crate::{BurnchainController, BurnchainTip, ChainTip, Tenure};
|
||||
|
||||
use stacks::chainstate::stacks::{TransactionAuth, TransactionSpendingCondition, TransactionPayload};
|
||||
|
||||
@@ -12,7 +12,7 @@ use stacks::burnchains::bitcoin::{BitcoinNetworkType,
|
||||
use super::RunLoopCallbacks;
|
||||
|
||||
#[cfg(feature = "monitoring")]
|
||||
use super::monitoring::start_serving_prometheus_metrics;
|
||||
use crate::monitoring::start_serving_prometheus_metrics;
|
||||
|
||||
/// Coordinating a node running in neon mode.
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user