-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sc-4232 Added prometheus metrics for latest processed block per addre…
…ss + small code fixes (#11) * Added prometheus metrics into challenger * sc-4232 Added prometheus metrics for latest processed block per address + small code fixes * sc-4232 Code optimizations + linter fixes * sc-4232 Fix tests
- Loading branch information
1 parent
3ab6f29
commit 7789a4d
Showing
8 changed files
with
1,327 additions
and
779 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use eyre::{Context, Result}; | ||
use lazy_static::lazy_static; | ||
use prometheus::{IntCounterVec, IntGaugeVec, Opts, Registry}; | ||
|
||
lazy_static! { | ||
pub static ref REGISTRY: Registry = | ||
Registry::new_custom(Some(String::from("challenger")), None) | ||
.expect("registry can be created"); | ||
pub static ref ERRORS_COUNTER: IntCounterVec = IntCounterVec::new( | ||
Opts::new("errors_total", "Challenger Errors Counter"), | ||
&["address", "from", "error"] | ||
) | ||
.expect("metric can be created"); | ||
pub static ref CHALLENGE_COUNTER: IntCounterVec = IntCounterVec::new( | ||
Opts::new("challenges_total", "Number of challenges made"), | ||
&["address", "from", "tx"] | ||
) | ||
.expect("metric can be created"); | ||
pub static ref LAST_SCANNED_BLOCK_GAUGE: IntGaugeVec = IntGaugeVec::new( | ||
Opts::new("last_scanned_block", "Last scanned block"), | ||
&["address", "from"] | ||
) | ||
.expect("metric can be created"); | ||
} | ||
|
||
/// `register_custom_metrics` registers custom metrics to the registry. | ||
/// It have to be called before you plan to serve `/metrics` route. | ||
pub fn register_custom_metrics() { | ||
REGISTRY | ||
.register(Box::new(ERRORS_COUNTER.clone())) | ||
.expect("collector can be registered"); | ||
|
||
REGISTRY | ||
.register(Box::new(CHALLENGE_COUNTER.clone())) | ||
.expect("collector can be registered"); | ||
|
||
REGISTRY | ||
.register(Box::new(LAST_SCANNED_BLOCK_GAUGE.clone())) | ||
.expect("collector can be registered"); | ||
} | ||
|
||
pub fn as_encoded_string() -> Result<String> { | ||
use prometheus::Encoder; | ||
let encoder = prometheus::TextEncoder::new(); | ||
|
||
// Collect and encode custom metrics from `REGISTRY` | ||
let mut buffer = Vec::new(); | ||
encoder | ||
.encode(®ISTRY.gather(), &mut buffer) | ||
.wrap_err("Failed to encode REGISTRY metrics")?; | ||
|
||
let mut res = String::from_utf8(buffer.clone()) | ||
.wrap_err("Failed to convert REGISTRY metrics from utf8")?; | ||
buffer.clear(); | ||
|
||
// Collect and encode prometheus metrics from `prometheus::gather()` | ||
let mut buffer = Vec::new(); | ||
encoder | ||
.encode(&prometheus::gather(), &mut buffer) | ||
.wrap_err("Failed to encode prometheus metrics")?; | ||
|
||
let res_custom = String::from_utf8(buffer.clone()) | ||
.wrap_err("Failed to convert prometheus metrics from utf8")?; | ||
buffer.clear(); | ||
|
||
res.push_str(&res_custom); | ||
Ok(res) | ||
} |
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