-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add metrics to Block Streamer #579
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e0a7df5
refactor: Move redis specific logic to client
morgsmccauley be85994
refactor: Rename `SERVER_PORT` -> `GRPC_PORT`
morgsmccauley 662928c
feat: Stand up prometheus server
morgsmccauley 75582f3
feat: Expose `LAST_PROCESSED_BLOCK` metric
morgsmccauley a121470
feat: Expose `PUBLISHED_BLOCKS_COUNT` metric
morgsmccauley 8e349d5
feat: Expose `PROCESSED_BLOCKS_COUNT` metric
morgsmccauley 95cbfef
feat: Reset `PUBLISHED_BLOCKS_COUNT` on new stream
morgsmccauley 5609feb
feat: Expose `LOGS_COUNT` metric
morgsmccauley f878633
test: Fix block stream tests
morgsmccauley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use tracing_subscriber::prelude::*; | |
mod block_stream; | ||
mod delta_lake_client; | ||
mod indexer_config; | ||
mod metrics; | ||
mod redis; | ||
mod rules; | ||
mod s3_client; | ||
|
@@ -15,26 +16,36 @@ mod test_utils; | |
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::registry() | ||
.with(tracing_subscriber::fmt::layer()) | ||
.with(metrics::LogCounter) | ||
.with(tracing_subscriber::EnvFilter::from_default_env()) | ||
.init(); | ||
|
||
let redis_url = std::env::var("REDIS_URL").expect("REDIS_URL is not set"); | ||
let server_port = std::env::var("SERVER_PORT").expect("SERVER_PORT is not set"); | ||
let grpc_port = std::env::var("GRPC_PORT").expect("GRPC_PORT is not set"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will also make this change in Terraform |
||
let metrics_port = std::env::var("METRICS_PORT") | ||
.expect("METRICS_PORT is not set") | ||
.parse() | ||
.expect("METRICS_PORT is not a valid number"); | ||
|
||
tracing::info!( | ||
redis_url, | ||
grpc_port, | ||
metrics_port, | ||
"Starting Block Streamer" | ||
); | ||
|
||
tracing::info!("Starting Block Streamer Service..."); | ||
|
||
tracing::info!("Connecting to Redis..."); | ||
let redis_client = std::sync::Arc::new(redis::RedisClient::connect(&redis_url).await?); | ||
|
||
let aws_config = aws_config::from_env().load().await; | ||
let s3_config = aws_sdk_s3::Config::from(&aws_config); | ||
let s3_client = crate::s3_client::S3Client::new(s3_config.clone()); | ||
|
||
tracing::info!("Connecting to Delta Lake..."); | ||
let delta_lake_client = | ||
std::sync::Arc::new(crate::delta_lake_client::DeltaLakeClient::new(s3_client)); | ||
|
||
server::init(&server_port, redis_client, delta_lake_client, s3_config).await?; | ||
tokio::spawn(metrics::init_server(metrics_port).expect("Failed to start metrics server")); | ||
|
||
server::init(&grpc_port, redis_client, delta_lake_client, s3_config).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use actix_web::{get, App, HttpServer, Responder}; | ||
use lazy_static::lazy_static; | ||
use prometheus::{ | ||
register_int_counter_vec, register_int_gauge_vec, Encoder, IntCounterVec, IntGaugeVec, | ||
}; | ||
use tracing_subscriber::layer::Context; | ||
use tracing_subscriber::Layer; | ||
|
||
lazy_static! { | ||
pub static ref LAST_PROCESSED_BLOCK: IntGaugeVec = register_int_gauge_vec!( | ||
"queryapi_block_streamer_last_processed_block", | ||
"Height of last block seen", | ||
&["indexer"] | ||
) | ||
.unwrap(); | ||
pub static ref PROCESSED_BLOCKS_COUNT: IntCounterVec = register_int_counter_vec!( | ||
"queryapi_block_streamer_processed_blocks_count", | ||
"Number of blocks processed by block stream", | ||
&["indexer"] | ||
) | ||
.unwrap(); | ||
pub static ref PUBLISHED_BLOCKS_COUNT: IntCounterVec = register_int_counter_vec!( | ||
"queryapi_block_streamer_published_blocks_count", | ||
"Number of blocks published to redis stream", | ||
&["indexer"] | ||
) | ||
.unwrap(); | ||
pub static ref LOGS_COUNT: IntCounterVec = register_int_counter_vec!( | ||
"queryapi_block_streamer_logs_count", | ||
"Number of messages logged", | ||
&["level"] | ||
) | ||
.unwrap(); | ||
} | ||
|
||
pub struct LogCounter; | ||
|
||
impl<S> Layer<S> for LogCounter | ||
where | ||
S: tracing::Subscriber, | ||
{ | ||
fn on_event(&self, event: &tracing::Event, _ctx: Context<S>) { | ||
LOGS_COUNT | ||
.with_label_values(&[event.metadata().level().as_str()]) | ||
.inc(); | ||
} | ||
} | ||
|
||
#[get("/metrics")] | ||
async fn get_metrics() -> impl Responder { | ||
let mut buffer = Vec::<u8>::new(); | ||
let encoder = prometheus::TextEncoder::new(); | ||
loop { | ||
match encoder.encode(&prometheus::gather(), &mut buffer) { | ||
Ok(_) => break, | ||
Err(err) => { | ||
tracing::error!("Error encoding metrics: {}", err); | ||
} | ||
} | ||
} | ||
String::from_utf8(buffer).unwrap() | ||
} | ||
|
||
pub(crate) fn init_server(port: u16) -> anyhow::Result<actix_web::dev::Server> { | ||
tracing::info!("Starting metrics server on 0.0.0.0:{port}"); | ||
|
||
Ok(HttpServer::new(|| App::new().service(get_metrics)) | ||
.bind(("0.0.0.0", port))? | ||
.disable_signals() | ||
.workers(1) | ||
.run()) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"published" should mean it actually got added to the Redis Stream