Skip to content

Commit

Permalink
feat(zksync_cli): Health checkpoint improvements (#3193)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->
Add three new components to node's healthcheck:
- General (i.e., version, last migration)
- State Keeper 
- Eth Sender

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zkstack dev fmt` and `zkstack dev
lint`.

---------

Co-authored-by: Danil <[email protected]>
  • Loading branch information
manuelmauro and Deniallugo authored Nov 26, 2024
1 parent 193c855 commit 440fe8d
Show file tree
Hide file tree
Showing 42 changed files with 756 additions and 241 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ members = [
# Test infrastructure
"core/tests/loadnext",
"core/tests/vm-benchmark",
"core/lib/bin_metadata",
]
resolver = "2"

Expand Down Expand Up @@ -277,6 +278,7 @@ zksync_health_check = { version = "0.1.0", path = "core/lib/health_check" }
zksync_l1_contract_interface = { version = "0.1.0", path = "core/lib/l1_contract_interface" }
zksync_mempool = { version = "0.1.0", path = "core/lib/mempool" }
zksync_merkle_tree = { version = "0.1.0", path = "core/lib/merkle_tree" }
zksync_bin_metadata = { version = "0.1.0", path = "core/lib/bin_metadata" }
zksync_mini_merkle_tree = { version = "0.1.0", path = "core/lib/mini_merkle_tree" }
zksync_object_store = { version = "0.1.0", path = "core/lib/object_store" }
zksync_protobuf_config = { version = "0.1.0", path = "core/lib/protobuf_config" }
Expand Down
3 changes: 2 additions & 1 deletion core/bin/external_node/src/metrics/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zksync_node_framework::{
implementations::resources::pools::{MasterPool, PoolResource},
FromContext, IntoContext, StopReceiver, Task, TaskId, WiringError, WiringLayer,
};
use zksync_shared_metrics::rustc::RUST_METRICS;
use zksync_shared_metrics::{GIT_METRICS, RUST_METRICS};
use zksync_types::{L1ChainId, L2ChainId, SLChainId};

use super::EN_METRICS;
Expand Down Expand Up @@ -40,6 +40,7 @@ impl WiringLayer for ExternalNodeMetricsLayer {

async fn wire(self, input: Self::Input) -> Result<Self::Output, WiringError> {
RUST_METRICS.initialize();
GIT_METRICS.initialize();
EN_METRICS.observe_config(
self.l1_chain_id,
self.sl_chain_id,
Expand Down
8 changes: 4 additions & 4 deletions core/bin/external_node/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use zksync_node_framework::{
NodeStorageInitializerLayer,
},
pools_layer::PoolsLayerBuilder,
postgres_metrics::PostgresMetricsLayer,
postgres::PostgresLayer,
prometheus_exporter::PrometheusExporterLayer,
pruning::PruningLayer,
query_eth_client::QueryEthClientLayer,
Expand Down Expand Up @@ -125,8 +125,8 @@ impl ExternalNodeBuilder {
Ok(self)
}

fn add_postgres_metrics_layer(mut self) -> anyhow::Result<Self> {
self.node.add_layer(PostgresMetricsLayer);
fn add_postgres_layer(mut self) -> anyhow::Result<Self> {
self.node.add_layer(PostgresLayer);
Ok(self)
}

Expand Down Expand Up @@ -582,7 +582,7 @@ impl ExternalNodeBuilder {
// so until we have a dedicated component for "auxiliary" tasks,
// it's responsible for things like metrics.
self = self
.add_postgres_metrics_layer()?
.add_postgres_layer()?
.add_external_node_metrics_layer()?;
// We assign the storage initialization to the core, as it's considered to be
// the "main" component.
Expand Down
10 changes: 4 additions & 6 deletions core/bin/zksync_server/src/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use zksync_node_framework::{
object_store::ObjectStoreLayer,
pk_signing_eth_client::PKSigningEthClientLayer,
pools_layer::PoolsLayerBuilder,
postgres_metrics::PostgresMetricsLayer,
postgres::PostgresLayer,
prometheus_exporter::PrometheusExporterLayer,
proof_data_handler::ProofDataHandlerLayer,
query_eth_client::QueryEthClientLayer,
Expand Down Expand Up @@ -138,8 +138,8 @@ impl MainNodeBuilder {
Ok(self)
}

fn add_postgres_metrics_layer(mut self) -> anyhow::Result<Self> {
self.node.add_layer(PostgresMetricsLayer);
fn add_postgres_layer(mut self) -> anyhow::Result<Self> {
self.node.add_layer(PostgresLayer);
Ok(self)
}

Expand Down Expand Up @@ -760,9 +760,7 @@ impl MainNodeBuilder {
self = self.add_eth_tx_manager_layer()?;
}
Component::Housekeeper => {
self = self
.add_house_keeper_layer()?
.add_postgres_metrics_layer()?;
self = self.add_house_keeper_layer()?.add_postgres_layer()?;
}
Component::ProofDataHandler => {
self = self.add_proof_data_handler_layer()?;
Expand Down
18 changes: 18 additions & 0 deletions core/lib/bin_metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "zksync_bin_metadata"
version.workspace = true
edition.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true

[dependencies]
serde.workspace = true
vise.workspace = true
tracing.workspace = true

[build-dependencies]
rustc_version.workspace = true
75 changes: 75 additions & 0 deletions core/lib/bin_metadata/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! Build script for the external node binary.
use std::{
env, fs,
io::{self, Write},
path::Path,
process::Command,
};

use rustc_version::{Channel, LlvmVersion};

fn print_binary_meta(out: &mut impl Write) -> io::Result<()> {
let rustc_meta = rustc_version::version_meta().expect("Failed obtaining rustc metadata");

writeln!(
out,
"pub const RUST_METADATA: RustMetadata = RustMetadata {{ \
version: {semver:?}, \
commit_hash: {commit_hash:?}, \
commit_date: {commit_date:?}, \
channel: {channel:?}, \
host: {host:?}, \
llvm: {llvm:?}, \
}};
pub const GIT_METADATA: GitMetadata = GitMetadata {{ \
branch: {git_branch:?}, \
revision: {git_revision:?} \
}};",
semver = rustc_meta.semver.to_string(),
commit_hash = rustc_meta.commit_hash,
commit_date = rustc_meta.commit_date,
channel = match rustc_meta.channel {
Channel::Dev => "dev",
Channel::Beta => "beta",
Channel::Nightly => "nightly",
Channel::Stable => "stable",
},
host = rustc_meta.host,
llvm = rustc_meta.llvm_version.as_ref().map(LlvmVersion::to_string),
git_branch = git_branch(),
git_revision = git_revision()
)
}

/// Outputs the current git branch as a string literal.
pub fn git_branch() -> Option<String> {
run_cmd_opt("git", &["rev-parse", "--abbrev-ref", "HEAD"])
}

/// Outputs the current git commit hash as a string literal.
pub fn git_revision() -> Option<String> {
run_cmd_opt("git", &["rev-parse", "--short", "HEAD"])
}

fn run_cmd_opt(cmd: &str, args: &[&str]) -> Option<String> {
let output = Command::new(cmd).args(args).output().ok()?;
if output.status.success() {
String::from_utf8(output.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
}

fn main() {
let out_dir = env::var("OUT_DIR").expect("`OUT_DIR` env var not set for build script");
let metadata_module_path = Path::new(&out_dir).join("metadata_values.rs");
let metadata_module =
fs::File::create(metadata_module_path).expect("cannot create metadata module");
let mut metadata_module = io::BufWriter::new(metadata_module);

print_binary_meta(&mut metadata_module).expect("failed printing binary metadata");
}
68 changes: 68 additions & 0 deletions core/lib/bin_metadata/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use serde::Serialize;
use vise::{EncodeLabelSet, Info, Metrics};

use self::values::{GIT_METADATA, RUST_METADATA};

pub mod values {
use super::{GitMetadata, RustMetadata};

include!(concat!(env!("OUT_DIR"), "/metadata_values.rs"));
}

pub const BIN_METADATA: BinMetadata = BinMetadata {
rust: RUST_METADATA,
git: GIT_METADATA,
};

/// Metadata of the compiled binary.
#[derive(Debug, Serialize)]
pub struct BinMetadata {
pub rust: RustMetadata,
pub git: GitMetadata,
}

/// Rust metadata of the compiled binary.
#[derive(Debug, EncodeLabelSet, Serialize)]
pub struct RustMetadata {
pub version: &'static str,
pub commit_hash: Option<&'static str>,
pub commit_date: Option<&'static str>,
pub channel: &'static str,
pub host: &'static str,
pub llvm: Option<&'static str>,
}

/// Git metadata of the compiled binary.
#[derive(Debug, EncodeLabelSet, Serialize)]
pub struct GitMetadata {
pub branch: Option<&'static str>,
pub revision: Option<&'static str>,
}

#[derive(Debug, Metrics)]
#[metrics(prefix = "rust")]
pub struct RustMetrics {
/// General information about the compiled binary.
info: Info<RustMetadata>,
}

impl RustMetrics {
pub fn initialize(&self) {
tracing::info!("Rust metadata for this binary: {RUST_METADATA:?}");
self.info.set(RUST_METADATA).ok();
}
}

#[derive(Debug, Metrics)]
#[metrics(prefix = "git_info")]
pub struct GitMetrics {
/// General information about the compiled binary.
info: Info<GitMetadata>,
}

impl GitMetrics {
pub fn initialize(&self) {
tracing::info!("Git metadata for this binary: {GIT_METADATA:?}");
self.info.set(GIT_METADATA).ok();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 440fe8d

Please sign in to comment.