Skip to content

Commit

Permalink
Merge pull request #288 from ergoplatform/i286-oracle-total-count-metric
Browse files Browse the repository at this point in the history
Add oracle_total_count metric to Prometheus and REST API
  • Loading branch information
greenhat authored Jun 15, 2023
2 parents 89d8ede + 5e68f26 commit 72d7cb6
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 39 deletions.
2 changes: 1 addition & 1 deletion core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn pool_status_sync(oracle_pool: Arc<OraclePool>) -> Result<Json<serde_json::Val
let pool_box_height = pool_box.get_box().creation_height;
let epoch_end_height = pool_box_height + epoch_length.0 as u32;
let pool_health = pool_health_sync(oracle_pool)?;
let active_oracle_count = pool_health.details.active_oracles.len();
let active_oracle_count = pool_health.details.active_oracle_boxes.len();
let json = Json(json!({
"latest_pool_datapoint": pool_box.rate(),
"latest_pool_box_height": pool_box_height,
Expand Down
1 change: 1 addition & 0 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod serde;
mod spec_token;
mod state;
mod templates;
mod util;
mod wallet;

#[cfg(test)]
Expand Down
18 changes: 15 additions & 3 deletions core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ static ACTIVE_ORACLE_COUNT: Lazy<IntGauge> = Lazy::new(|| {
m
});

static TOTAL_ORACLE_COUNT: Lazy<IntGauge> = Lazy::new(|| {
let m = IntGauge::with_opts(
Opts::new("total_oracle_count", "The total number of oracle tokens")
.namespace("ergo")
.subsystem("oracle"),
)
.unwrap();
prometheus::register(Box::new(m.clone())).expect("Failed to register");
m
});

static REQUIRED_ORACLE_COUNT: Lazy<IntGauge> = Lazy::new(|| {
let m = IntGauge::with_opts(
Opts::new(
Expand Down Expand Up @@ -214,22 +225,23 @@ fn update_pool_health(pool_health: &PoolHealth) {
CURRENT_HEIGHT.set(pool_health.details.current_height.into());
EPOCH_LENGTH.set(pool_health.details.epoch_length.into());
POOL_IS_HEALTHY.set(pool_health.status as i64);
for oracle in &pool_health.details.all_oracles {
for oracle in &pool_health.details.all_oracle_boxes {
let box_type = oracle.box_height.label_name();
let box_height = oracle.box_height.oracle_box_height().into();
ALL_ORACLE_BOX_HEIGHT
.with_label_values(&[box_type, &oracle.address.to_base58()])
.set(box_height);
}
for oracle in &pool_health.details.active_oracles {
for oracle in &pool_health.details.active_oracle_boxes {
let box_type = oracle.box_height.label_name();
let box_height = oracle.box_height.oracle_box_height().into();
ACTIVE_ORACLE_BOX_HEIGHT
.with_label_values(&[box_type, &oracle.address.to_base58()])
.set(box_height);
}
ACTIVE_ORACLE_COUNT.set(pool_health.details.active_oracles.len() as i64);
ACTIVE_ORACLE_COUNT.set(pool_health.details.active_oracle_boxes.len() as i64);
REQUIRED_ORACLE_COUNT.set(pool_health.details.min_data_points.into());
TOTAL_ORACLE_COUNT.set(pool_health.details.total_oracle_token_count as i64);
}

fn update_oracle_health(oracle_health: &OracleHealth) {
Expand Down
11 changes: 7 additions & 4 deletions core/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ pub struct PoolHealthDetails {
pub pool_box_height: BlockHeight,
pub current_height: BlockHeight,
pub epoch_length: EpochLength,
pub all_oracles: Vec<OracleDetails>,
pub active_oracles: Vec<OracleDetails>,
pub all_oracle_boxes: Vec<OracleDetails>,
pub active_oracle_boxes: Vec<OracleDetails>,
pub min_data_points: MinDatapoints,
pub total_oracle_token_count: u64,
}

#[derive(Debug, Clone, serde::Serialize)]
Expand Down Expand Up @@ -65,6 +66,7 @@ pub fn check_pool_health(
let acceptable_pool_box_delay_blocks = 3;
let is_healthy =
pool_box_height >= current_height - epoch_length - acceptable_pool_box_delay_blocks;
let total_oracle_token_count = oracle_pool.get_total_oracle_token_count()?;
let all_oracles = get_all_oracle_boxes(oracle_pool, network_prefix)?;
let active_oracles = get_active_oracle_boxes(&all_oracles, pool_box_height);
Ok(PoolHealth {
Expand All @@ -77,13 +79,14 @@ pub fn check_pool_health(
pool_box_height,
current_height,
epoch_length,
all_oracles,
active_oracles,
all_oracle_boxes: all_oracles,
active_oracle_boxes: active_oracles,
min_data_points: pool_conf
.refresh_box_wrapper_inputs
.contract_inputs
.contract_parameters()
.min_data_points(),
total_oracle_token_count,
},
})
}
Expand Down
21 changes: 20 additions & 1 deletion core/src/oracle_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::pool_config::POOL_CONFIG;
use crate::scans::{GenericTokenScan, NodeScanRegistry, ScanError, ScanGetBoxes};
use crate::spec_token::{
BallotTokenId, BuybackTokenId, OracleTokenId, PoolTokenId, RefreshTokenId, RewardTokenId,
UpdateTokenId,
TokenIdKind, UpdateTokenId,
};
use crate::util::get_token_count;
use anyhow::Error;

use ergo_lib::ergotree_ir::mir::constant::TryExtractFromError;
Expand Down Expand Up @@ -310,6 +311,24 @@ impl OraclePool {
.as_ref()
.map(|b| b as &dyn BuybackBoxSource)
}

pub fn get_total_oracle_token_count(&self) -> Result<u64> {
Ok(self
.oracle_datapoint_scan
.scan
.get_boxes()?
.into_iter()
.map(|b| {
get_token_count(
b,
self.oracle_datapoint_scan
.oracle_box_wrapper_inputs
.oracle_token_id
.token_id(),
)
})
.sum::<u64>())
}
}

impl PoolBoxSource for PoolBoxScan {
Expand Down
15 changes: 15 additions & 0 deletions core/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox;
use ergo_lib::ergotree_ir::chain::token::TokenId;
use ergo_lib::wallet::box_selector::ErgoBoxAssets;

pub fn get_token_count(b: ErgoBox, token_id: TokenId) -> u64 {
let mut count = 0;
if let Some(tokens) = b.tokens() {
for token in tokens {
if token.token_id == token_id {
count += token.amount.as_u64();
}
}
}
count
}
119 changes: 89 additions & 30 deletions scripts/grafana_dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
"id": 34,
"options": {
"colorMode": "value",
"graphMode": "area",
"graphMode": "none",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
Expand Down Expand Up @@ -247,6 +247,10 @@
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
Expand All @@ -259,6 +263,61 @@
"x": 8,
"y": 1
},
"id": 36,
"options": {
"colorMode": "value",
"graphMode": "none",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"text": {},
"textMode": "auto"
},
"pluginVersion": "7.5.11",
"targets": [
{
"exemplar": true,
"expr": "ergo_oracle_total_oracle_count",
"interval": "",
"legendFormat": "",
"refId": "A"
}
],
"title": "Total oracle tokens minted",
"type": "stat"
},
{
"datasource": null,
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 5,
"w": 4,
"x": 11,
"y": 1
},
"id": 18,
"options": {
"colorMode": "value",
Expand Down Expand Up @@ -311,9 +370,9 @@
},
"gridPos": {
"h": 5,
"w": 4,
"x": 11,
"y": 1
"w": 5,
"x": 0,
"y": 6
},
"id": 16,
"options": {
Expand Down Expand Up @@ -368,9 +427,9 @@
},
"gridPos": {
"h": 5,
"w": 4,
"x": 15,
"y": 1
"w": 5,
"x": 5,
"y": 6
},
"id": 12,
"options": {
Expand Down Expand Up @@ -427,9 +486,9 @@
},
"gridPos": {
"h": 5,
"w": 4,
"x": 19,
"y": 1
"w": 5,
"x": 10,
"y": 6
},
"id": 26,
"options": {
Expand Down Expand Up @@ -475,10 +534,10 @@
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 17,
"w": 23,
"h": 15,
"w": 15,
"x": 0,
"y": 6
"y": 11
},
"hiddenSeries": false,
"id": 30,
Expand Down Expand Up @@ -565,7 +624,7 @@
"h": 1,
"w": 24,
"x": 0,
"y": 23
"y": 26
},
"id": 22,
"panels": [],
Expand Down Expand Up @@ -614,10 +673,10 @@
"overrides": []
},
"gridPos": {
"h": 7,
"w": 5,
"h": 5,
"w": 3,
"x": 0,
"y": 24
"y": 27
},
"id": 10,
"options": {
Expand Down Expand Up @@ -671,10 +730,10 @@
"overrides": []
},
"gridPos": {
"h": 7,
"w": 6,
"x": 5,
"y": 24
"h": 5,
"w": 3,
"x": 3,
"y": 27
},
"id": 20,
"options": {
Expand Down Expand Up @@ -722,10 +781,10 @@
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 15,
"w": 11,
"x": 11,
"y": 24
"h": 13,
"w": 9,
"x": 6,
"y": 27
},
"hiddenSeries": false,
"id": 2,
Expand Down Expand Up @@ -844,10 +903,10 @@
"overrides": []
},
"gridPos": {
"h": 8,
"w": 11,
"h": 7,
"w": 6,
"x": 0,
"y": 31
"y": 32
},
"id": 14,
"options": {
Expand Down Expand Up @@ -889,12 +948,12 @@
"list": []
},
"time": {
"from": "now-1h",
"from": "now-3h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "Ergo oracle",
"uid": "ORWMfHl4k",
"version": 33
"version": 35
}

0 comments on commit 72d7cb6

Please sign in to comment.