Skip to content

Commit

Permalink
graph: refactor GasCounter for trackiing gas metrics, add a new env f…
Browse files Browse the repository at this point in the history
…or gas metrics
  • Loading branch information
incrypto32 committed Nov 7, 2023
1 parent 7f0137d commit 5fa9c3f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/src/subgraph/instance_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::BTreeSet;

use crate::subgraph::runner::SubgraphRunner;
use graph::blockchain::block_stream::BlockStreamMetrics;
use graph::blockchain::{Blockchain, BlockchainKind, DataSource, NodeCapabilities,TriggerFilter};
use graph::blockchain::{Blockchain, BlockchainKind, DataSource, NodeCapabilities};
use graph::components::metrics::gas::GasMetrics;
use graph::components::subgraph::ProofOfIndexingVersion;
use graph::data::subgraph::{UnresolvedSubgraphManifest, SPEC_VERSION_0_0_6};
Expand Down
12 changes: 6 additions & 6 deletions graph/src/components/metrics/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ impl GasMetrics {
subgraph_id.as_str(),
&["method"],
)
.unwrap_or_else(|_| {
.unwrap_or_else(|err| {
panic!(
"Failed to register deployment_gas prometheus counter for {}",
subgraph_id
"Failed to register deployment_gas prometheus counter for {}: {}",
subgraph_id, err
)
});

Expand All @@ -32,10 +32,10 @@ impl GasMetrics {
subgraph_id.as_str(),
&["method"],
)
.unwrap_or_else(|_| {
.unwrap_or_else(|err| {
panic!(
"Failed to register deployment_op_count prometheus counter for {}",
subgraph_id
"Failed to register deployment_op_count prometheus counter for {}: {}",
subgraph_id, err
)
});

Expand Down
7 changes: 7 additions & 0 deletions graph/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ pub struct EnvVars {
pub subgraph_settings: Option<String>,
/// Whether to prefer substreams blocks streams over firehose when available.
pub prefer_substreams_block_streams: bool,
/// Set by the flag `GRAPH_ENABLE_GAS_METRICS`. Whether to enable
/// gas metrics. Off by default.
pub enable_gas_metrics: bool,
}

impl EnvVars {
Expand Down Expand Up @@ -236,6 +239,7 @@ impl EnvVars {
reorg_threshold: inner.reorg_threshold,
subgraph_settings: inner.subgraph_settings,
prefer_substreams_block_streams: inner.prefer_substreams_block_streams,
enable_gas_metrics: inner.enable_gas_metrics.0,
})
}

Expand Down Expand Up @@ -361,6 +365,9 @@ struct Inner {
default = "false"
)]
prefer_substreams_block_streams: bool,

#[envconfig(from = "GRAPH_ENABLE_GAS_METRICS", default = "false")]
enable_gas_metrics: EnvVarBoolean,
}

#[derive(Clone, Debug)]
Expand Down
25 changes: 15 additions & 10 deletions graph/src/runtime/gas/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ impl Display for Gas {
}

#[derive(Clone)]
pub struct GasCounter(Arc<AtomicU64>, GasMetrics);
pub struct GasCounter {
counter: Arc<AtomicU64>,
metrics: GasMetrics,
}

impl CheapClone for GasCounter {}

impl GasCounter {
/// Alias of [`Default::default`].
pub fn new(gas_metrics: GasMetrics) -> Self {
pub fn new(metrics: GasMetrics) -> Self {
Self {
0: Arc::new(AtomicU64::new(0)),
1: gas_metrics,
counter: Arc::new(AtomicU64::new(0)),
metrics,
}
}

Expand All @@ -98,13 +100,16 @@ impl GasCounter {
) -> Result<(), DeterministicHostError> {
amount += costs::HOST_EXPORT_GAS;

if let Some(method) = method {
self.1.track_gas(method, amount.0);
self.1.track_operations(method, 1);
// If gas metrics are enabled, track the gas used
if ENV_VARS.enable_gas_metrics {
if let Some(method) = method {
self.metrics.track_gas(method, amount.0);
self.metrics.track_operations(method, 1);
}
}

let old = self
.0
.counter
.fetch_update(SeqCst, SeqCst, |v| Some(v.saturating_add(amount.0)))
.unwrap();
let new = old.saturating_add(amount.0);
Expand All @@ -131,6 +136,6 @@ impl GasCounter {
}

pub fn get(&self) -> Gas {
Gas(self.0.load(SeqCst))
Gas(self.counter.load(SeqCst))
}
}

0 comments on commit 5fa9c3f

Please sign in to comment.