Skip to content

Commit

Permalink
add metric for state size
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Oct 13, 2024
1 parent f8a97c3 commit cdf2659
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,7 @@ dependencies = [
name = "itp-enclave-metrics"
version = "0.9.0"
dependencies = [
"itp-types",
"parity-scale-codec",
"sgx_tstd",
"substrate-fixed",
Expand Down
2 changes: 1 addition & 1 deletion core-primitives/enclave-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ edition = "2021"

[dependencies]
# sgx
itp-types = { path = "../types", default-features = false }
sgx_tstd = { branch = "master", git = "https://github.com/apache/teaclave-sgx-sdk.git", optional = true }

# no-std dependencies
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full"] }
substrate-fixed = { default-features = false, git = "https://github.com/encointer/substrate-fixed", tag = "v0.5.9" }
Expand Down
2 changes: 2 additions & 0 deletions core-primitives/enclave-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate sgx_tstd as std;

use codec::{Decode, Encode};
use core::time::Duration;
use itp_types::ShardIdentifier;
use std::string::String;
use substrate_fixed::types::U32F32;

Expand All @@ -44,6 +45,7 @@ pub enum EnclaveMetric {
StfStateUpdateExecutionDuration(Duration),
StfStateUpdateExecutedCallsSuccessfulCount(u32),
StfStateUpdateExecutedCallsFailedCount(u32),
StfStateSizeSet(ShardIdentifier, u32),
StfRuntimeTotalIssuanceSet(f64),
StfRuntimeParentchainIntegriteeProcessedBlockNumberSet(u32),
StfRuntimeParentchainTargetAProcessedBlockNumberSet(u32),
Expand Down
2 changes: 2 additions & 0 deletions core-primitives/stf-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ where
error!("on_finalize failed: {:?}", e);
});

let state_size_bytes = state.size();
let runtime_metrics = gather_runtime_metrics(&state);

let propsing_duration = duration_now() - started_at;
Expand All @@ -366,6 +367,7 @@ where
successful_call_count as u32,
),
EnclaveMetric::StfStateUpdateExecutedCallsFailedCount(failed_call_count as u32),
EnclaveMetric::StfStateSizeSet(*shard, state_size_bytes as u32),
EnclaveMetric::StfRuntimeTotalIssuanceSet(runtime_metrics.total_issuance),
EnclaveMetric::StfRuntimeParentchainIntegriteeProcessedBlockNumberSet(
runtime_metrics.parentchain_integritee_processed_block_number,
Expand Down
7 changes: 7 additions & 0 deletions core-primitives/substrate-sgx/externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ pub trait SgxExternalitiesTrait {

fn get(&self, k: &[u8]) -> Option<&Vec<u8>>;

/// get the state size in encoded bytes
fn size(&self) -> usize;

fn contains_key(&self, k: &[u8]) -> bool;

/// Get the next key in state after the given one (excluded) in lexicographic order.
Expand Down Expand Up @@ -162,6 +165,10 @@ where
self.state.get(key)
}

fn size(&self) -> usize {
self.state.encoded_size()
}

fn contains_key(&self, key: &[u8]) -> bool {
self.state.contains_key(key)
}
Expand Down
1 change: 1 addition & 0 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,7 @@ dependencies = [
name = "itp-enclave-metrics"
version = "0.9.0"
dependencies = [
"itp-types",
"parity-scale-codec",
"sgx_tstd",
"substrate-fixed",
Expand Down
13 changes: 10 additions & 3 deletions service/src/prometheus_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
error::{Error, ServiceResult},
};
use async_trait::async_trait;
use base58::ToBase58;
use codec::{Decode, Encode};
#[cfg(feature = "attesteer")]
use core::time::Duration;
Expand All @@ -40,11 +41,11 @@ use lazy_static::lazy_static;
use log::*;
use prometheus::{
proto::MetricFamily, register_gauge, register_histogram, register_histogram_vec,
register_int_counter, register_int_gauge, Gauge, Histogram, HistogramOpts, HistogramVec,
IntCounter, IntGauge,
register_int_counter, register_int_gauge, register_int_gauge_vec, Gauge, Histogram,
HistogramOpts, HistogramVec, IntCounter, IntGauge, IntGaugeVec,
};
use serde::{Deserialize, Serialize};
use std::{net::SocketAddr, sync::Arc};
use std::{fmt::Debug, net::SocketAddr, sync::Arc};
use warp::{Filter, Rejection, Reply};

const DURATION_HISTOGRAM_BUCKETS: [f64; 10] =
Expand Down Expand Up @@ -87,6 +88,9 @@ lazy_static! {
register_histogram!(HistogramOpts::new("integritee_worker_enclave_stf_state_update_executed_calls_failed_count", "Enclave STF: how many calls have failed during execution per update proposal")
.buckets(COUNT_HISTOGRAM_BUCKETS.into()))
.unwrap();
static ref ENCLAVE_STF_STATE_SIZE: IntGaugeVec =
register_int_gauge_vec!("integritee_worker_enclave_stf_state_size_bytes", "Enclave STF state size in Bytes", &["shard"])
.unwrap();
static ref ENCLAVE_STF_RUNTIME_TOTAL_ISSUANCE: Gauge =
register_gauge!("integritee_worker_enclave_stf_runtime_total_issuance", "Enclave stf total issuance assuming its native token")
.unwrap();
Expand Down Expand Up @@ -224,6 +228,9 @@ impl ReceiveEnclaveMetrics for EnclaveMetricsReceiver {
ENCLAVE_STF_STATE_UPDATE_EXECUTED_CALLS_SUCCESSFUL_COUNT.observe(count.into()),
EnclaveMetric::StfStateUpdateExecutedCallsFailedCount(count) =>
ENCLAVE_STF_STATE_UPDATE_EXECUTED_CALLS_FAILED_COUNT.observe(count.into()),
EnclaveMetric::StfStateSizeSet(shard, bytes) => ENCLAVE_STF_STATE_SIZE
.with_label_values([shard.0.to_base58().as_str()].as_slice())
.set(bytes as i64),
EnclaveMetric::StfRuntimeTotalIssuanceSet(balance) =>
ENCLAVE_STF_RUNTIME_TOTAL_ISSUANCE.set(balance),
EnclaveMetric::StfRuntimeParentchainIntegriteeProcessedBlockNumberSet(bn) =>
Expand Down

0 comments on commit cdf2659

Please sign in to comment.