Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vlog): Remove env getters from vlog #1077

Merged
merged 7 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions core/bin/block_reverter/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use anyhow::Context as _;
use clap::{Parser, Subcommand};
use tokio::io::{self, AsyncReadExt};
use zksync_config::{ContractsConfig, DBConfig, ETHClientConfig, ETHSenderConfig, PostgresConfig};
use zksync_config::{
configs::ObservabilityConfig, ContractsConfig, DBConfig, ETHClientConfig, ETHSenderConfig,
PostgresConfig,
};
use zksync_core::block_reverter::{
BlockReverter, BlockReverterEthConfig, BlockReverterFlags, L1ExecutedBatchesRevert,
};
Expand Down Expand Up @@ -68,19 +71,19 @@ enum Command {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();
let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
.with_sentry_url(&sentry_url)
.context("Invalid Sentry URL")?
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();

Expand Down
24 changes: 13 additions & 11 deletions core/bin/contract-verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use anyhow::Context as _;
use futures::{channel::mpsc, executor::block_on, SinkExt, StreamExt};
use prometheus_exporter::PrometheusExporterConfig;
use tokio::sync::watch;
use zksync_config::{configs::PrometheusConfig, ApiConfig, ContractVerifierConfig, PostgresConfig};
use zksync_config::{
configs::{ObservabilityConfig, PrometheusConfig},
ApiConfig, ContractVerifierConfig, PostgresConfig,
};
use zksync_dal::ConnectionPool;
use zksync_env_config::FromEnv;
use zksync_queued_job_processor::JobProcessor;
Expand Down Expand Up @@ -140,24 +143,23 @@ async fn main() -> anyhow::Result<()> {
.await
.unwrap();

#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();

let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;
let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = &sentry_url {
if let Some(sentry_url) = &observability_config.sentry_url {
builder = builder
.with_sentry_url(sentry_url)
.expect("Invalid Sentry URL")
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();

// Report whether sentry is running after the logging subsystem was initialized.
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
tracing::info!("Sentry configured with URL: {sentry_url}");
} else {
tracing::info!("No sentry URL was provided");
Expand Down
1 change: 1 addition & 0 deletions core/bin/external_node/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use zksync_web3_decl::{
namespaces::{EthNamespaceClient, ZksNamespaceClient},
};

pub(crate) mod observability;
#[cfg(test)]
mod tests;

Expand Down
39 changes: 39 additions & 0 deletions core/bin/external_node/src/config/observability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use zksync_config::configs::ObservabilityConfig;

pub fn observability_config_from_env() -> anyhow::Result<ObservabilityConfig> {
// The logic in this method mimics the historical logic of loading observability options
// This is left intact, since some of the existing deployments may rely on the this behavior.
let sentry_url = if let Ok(sentry_url) = std::env::var("MISC_SENTRY_URL") {
if sentry_url == "unset" {
None
} else {
Some(sentry_url)
}
} else {
None
};
let sentry_environment = std::env::var("EN_SENTRY_ENVIRONMENT").ok().or_else(|| {
popzxc marked this conversation as resolved.
Show resolved Hide resolved
let l1_network = std::env::var("CHAIN_ETH_NETWORK").ok();
let l2_network = std::env::var("CHAIN_ETH_ZKSYNC_NETWORK").ok();
match (l1_network, l2_network) {
(Some(l1_network), Some(l2_network)) => {
Some(format!("{} - {}", l1_network, l2_network))
}
_ => None,
}
});
let log_format = if let Ok(log_format) = std::env::var("MISC_LOG_FORMAT") {
if log_format != "plain" && log_format != "json" {
anyhow::bail!("MISC_LOG_FORMAT has an unexpected value {}", log_format);
}
log_format
} else {
"plain".to_string()
};
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved

Ok(ObservabilityConfig {
sentry_url,
sentry_environment,
log_format,
})
}
20 changes: 10 additions & 10 deletions core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod metrics;
const RELEASE_MANIFEST: &str =
std::include_str!("../../../../.github/release-please/manifest.json");

use crate::config::ExternalNodeConfig;
use crate::config::{observability::observability_config_from_env, ExternalNodeConfig};

/// Creates the state keeper configured to work in the external node mode.
#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -402,24 +402,24 @@ async fn main() -> anyhow::Result<()> {
// Initial setup.
let opt = Cli::parse();

#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();
let observability_config =
observability_config_from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = &sentry_url {
if let Some(sentry_url) = &observability_config.sentry_url {
builder = builder
.with_sentry_url(sentry_url)
.expect("Invalid Sentry URL")
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();

// Report whether sentry is running after the logging subsystem was initialized.
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
tracing::info!("Sentry configured with URL: {sentry_url}");
} else {
tracing::info!("No sentry URL was provided");
Expand Down
19 changes: 9 additions & 10 deletions core/bin/merkle_tree_consistency_checker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{path::Path, time::Instant};

use anyhow::Context as _;
use clap::Parser;
use zksync_config::DBConfig;
use zksync_config::{configs::ObservabilityConfig, DBConfig};
use zksync_env_config::FromEnv;
use zksync_merkle_tree::domain::ZkSyncTree;
use zksync_storage::RocksDB;
Expand Down Expand Up @@ -48,19 +48,18 @@ impl Cli {
}

fn main() -> anyhow::Result<()> {
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();

let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;
let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
.with_sentry_url(&sentry_url)
.context("Invalid Sentry URL")?
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();

Expand Down
23 changes: 13 additions & 10 deletions core/bin/snapshots_creator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
use anyhow::Context as _;
use prometheus_exporter::PrometheusExporterConfig;
use tokio::{sync::watch, task::JoinHandle};
use zksync_config::{configs::PrometheusConfig, PostgresConfig, SnapshotsCreatorConfig};
use zksync_config::{
configs::{ObservabilityConfig, PrometheusConfig},
PostgresConfig, SnapshotsCreatorConfig,
};
use zksync_dal::ConnectionPool;
use zksync_env_config::{object_store::SnapshotsObjectStoreConfig, FromEnv};
use zksync_object_store::ObjectStoreFactory;
Expand Down Expand Up @@ -50,23 +53,23 @@ const MIN_CHUNK_COUNT: u64 = 10;
async fn main() -> anyhow::Result<()> {
let (stop_sender, stop_receiver) = watch::channel(false);

tracing::info!("Starting snapshots creator");
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();
let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let prometheus_exporter_task = maybe_enable_prometheus_metrics(stop_receiver).await?;
let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
.with_sentry_url(&sentry_url)
.context("Invalid Sentry URL")?
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();
tracing::info!("Starting snapshots creator");

let object_store_config =
SnapshotsObjectStoreConfig::from_env().context("SnapshotsObjectStoreConfig::from_env()")?;
Expand Down
22 changes: 11 additions & 11 deletions core/bin/zksync_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use zksync_config::{
},
fri_prover_group::FriProverGroupConfig,
house_keeper::HouseKeeperConfig,
FriProofCompressorConfig, FriProverConfig, FriWitnessGeneratorConfig, PrometheusConfig,
ProofDataHandlerConfig, WitnessGeneratorConfig,
FriProofCompressorConfig, FriProverConfig, FriWitnessGeneratorConfig, ObservabilityConfig,
PrometheusConfig, ProofDataHandlerConfig, WitnessGeneratorConfig,
},
ApiConfig, ContractsConfig, DBConfig, ETHClientConfig, ETHSenderConfig, ETHWatchConfig,
GasAdjusterConfig, ObjectStoreConfig, PostgresConfig,
Expand Down Expand Up @@ -68,24 +68,24 @@ impl FromStr for ComponentsToRun {
async fn main() -> anyhow::Result<()> {
let opt = Cli::parse();

#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let log_format = vlog::log_format_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let sentry_url = vlog::sentry_url_from_env();
#[allow(deprecated)] // TODO (QIT-21): Use centralized configuration approach.
let environment = vlog::environment_from_env();
let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = &sentry_url {
if let Some(sentry_url) = &observability_config.sentry_url {
builder = builder
.with_sentry_url(sentry_url)
.expect("Invalid Sentry URL")
.with_sentry_environment(environment);
.with_sentry_environment(observability_config.sentry_environment);
}
let _guard = builder.build();

// Report whether sentry is running after the logging subsystem was initialized.
if let Some(sentry_url) = sentry_url {
if let Some(sentry_url) = observability_config.sentry_url {
tracing::info!("Sentry configured with URL: {sentry_url}");
} else {
tracing::info!("No sentry URL was provided");
Expand Down
2 changes: 2 additions & 0 deletions core/lib/config/src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use self::{
fri_witness_generator::FriWitnessGeneratorConfig,
fri_witness_vector_generator::FriWitnessVectorGeneratorConfig,
object_store::ObjectStoreConfig,
observability::ObservabilityConfig,
proof_data_handler::ProofDataHandlerConfig,
snapshots_creator::SnapshotsCreatorConfig,
utils::PrometheusConfig,
Expand All @@ -37,6 +38,7 @@ pub mod fri_witness_generator;
pub mod fri_witness_vector_generator;
pub mod house_keeper;
pub mod object_store;
pub mod observability;
pub mod proof_data_handler;
pub mod snapshots_creator;
pub mod utils;
Expand Down
12 changes: 12 additions & 0 deletions core/lib/config/src/configs/observability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// Configuration for the essential observability stack, like
/// logging and sentry integration.
#[derive(Debug, Clone)]
pub struct ObservabilityConfig {
/// URL of the Sentry instance to send events to.
pub sentry_url: Option<String>,
/// Name of the environment to use in Sentry.
pub sentry_environment: Option<String>,
/// Format of the logs as expected by the `vlog` crate.
/// Currently must be either `plain` or `json`.
pub log_format: String,
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions core/lib/env_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod fri_witness_generator;
mod fri_witness_vector_generator;
mod house_keeper;
pub mod object_store;
mod observability;
mod proof_data_handler;
mod snapshots_creator;
mod utils;
Expand Down
43 changes: 43 additions & 0 deletions core/lib/env_config/src/observability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use zksync_config::configs::ObservabilityConfig;

use crate::FromEnv;

impl FromEnv for ObservabilityConfig {
fn from_env() -> anyhow::Result<Self> {
// The logic in this method mimics the historical logic of loading observability options
// This is left intact, since some of the existing deployments may rely on the this behavior.
let sentry_url = if let Ok(sentry_url) = std::env::var("MISC_SENTRY_URL") {
if sentry_url == "unset" {
None
} else {
Some(sentry_url)
}
} else {
None
};
let sentry_environment = {
let l1_network = std::env::var("CHAIN_ETH_NETWORK").ok();
let l2_network = std::env::var("CHAIN_ETH_ZKSYNC_NETWORK").ok();
match (l1_network, l2_network) {
(Some(l1_network), Some(l2_network)) => {
Some(format!("{} - {}", l1_network, l2_network))
}
_ => None,
}
};
let log_format = if let Ok(log_format) = std::env::var("MISC_LOG_FORMAT") {
if log_format != "plain" && log_format != "json" {
anyhow::bail!("MISC_LOG_FORMAT has an unexpected value {}", log_format);
}
log_format
} else {
"plain".to_string()
};

Ok(ObservabilityConfig {
sentry_url,
sentry_environment,
log_format,
})
}
}
1 change: 1 addition & 0 deletions core/lib/protobuf_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod fri_witness_generator;
mod fri_witness_vector_generator;
mod house_keeper;
mod object_store;
mod observability;
mod proof_data_handler;
mod snapshots_creator;
mod witness_generator;
Expand Down
Loading