Skip to content

Commit

Permalink
chore: add more sentry tags
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Apr 14, 2019
1 parent e452c64 commit dfd5c8a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
7 changes: 7 additions & 0 deletions pow/src/cuckoo.rs
Expand Up @@ -5,6 +5,7 @@ use hash::blake2b_256;
use serde::{de, Deserialize as SerdeDeserialize};
use serde_derive::Deserialize;
use std::collections::HashMap;
use std::fmt;

// Cuckatoo proofs take the form of a length 42 off-by-1-cycle in a bipartite graph with
// 2^N+2^N nodes and 2^N edges, with N ranging from 10 up to 64.
Expand All @@ -19,6 +20,12 @@ pub struct CuckooParams {
cycle_length: u32,
}

impl fmt::Display for CuckooParams {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.edge_bits, self.cycle_length)
}
}

fn validate_cycle_length<'de, D>(d: D) -> Result<u32, D::Error>
where
D: de::Deserializer<'de>,
Expand Down
10 changes: 10 additions & 0 deletions pow/src/lib.rs
Expand Up @@ -4,6 +4,7 @@ use ckb_core::header::{BlockNumber, Header, RawHeader, Seal};
use hash::blake2b_256;
use numext_fixed_hash::H256;
use serde_derive::Deserialize;
use std::fmt;
use std::sync::Arc;

mod cuckoo;
Expand All @@ -19,6 +20,15 @@ pub enum Pow {
Cuckoo(CuckooParams),
}

impl fmt::Display for Pow {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Pow::Dummy => write!(f, "Dummy"),
Pow::Cuckoo(params) => write!(f, "Cuckoo{}", params),
}
}
}

impl Pow {
pub fn engine(&self) -> Arc<dyn PowEngine> {
match *self {
Expand Down
49 changes: 45 additions & 4 deletions src/setup/mod.rs
Expand Up @@ -12,13 +12,15 @@ use ckb_chain_spec::{consensus::Consensus, ChainSpec};
use ckb_instrument::Format;
use ckb_resource::ResourceLocator;
use clap::{value_t, ArgMatches};
use log::info;
use logger::LoggerInitGuard;
use std::path::PathBuf;

pub struct Setup {
subcommand_name: String,
resource_locator: ResourceLocator,
config: AppConfig,
is_sentry_enabled: bool,
}

pub struct SetupGuard {
Expand All @@ -45,18 +47,36 @@ impl Setup {
return Err(ExitCode::Config);
}

let is_sentry_enabled = is_daemon(&subcommand_name) && config.sentry().is_enabled();

Ok(Setup {
subcommand_name: subcommand_name.to_string(),
resource_locator,
config,
is_sentry_enabled,
})
}

pub fn setup_app(&self) -> Result<SetupGuard, ExitCode> {
let logger_guard = logger::init(self.config.logger().clone())?;
let sentry_guard = if is_daemon(&self.subcommand_name) {
Some(self.config.sentry().init())

let sentry_guard = if self.is_sentry_enabled {
let sentry_config = self.config.sentry();

info!(target: "sentry", "**Notice**: \
The ckb process will send stack trace to sentry on Rust panics. \
This is enabled by default before mainnet, which can be opted out by setting \
the option `dsn` to empty in the config file. The DSN is now {}", sentry_config.dsn);

let guard = sentry_config.init();

sentry::configure_scope(|scope| {
scope.set_tag("subcommand", &self.subcommand_name);
});

Some(guard)
} else {
info!(target: "sentry", "sentry is disabled");
None
};

Expand Down Expand Up @@ -141,11 +161,32 @@ impl Setup {
}

fn chain_spec(&self) -> Result<ChainSpec, ExitCode> {
self.config.chain_spec(&self.resource_locator)
let result = self.config.chain_spec(&self.resource_locator);

if let Ok(spec) = &result {
if self.is_sentry_enabled {
sentry::configure_scope(|scope| {
scope.set_tag("spec.name", &spec.name);
scope.set_tag("spec.pow", &spec.pow);
});
}
}

result
}

fn consensus(&self) -> Result<Consensus, ExitCode> {
consensus_from_spec(&self.chain_spec()?)
let result = consensus_from_spec(&self.chain_spec()?);

if let Ok(consensus) = &result {
if self.is_sentry_enabled {
sentry::configure_scope(|scope| {
scope.set_tag("genesis", consensus.genesis_block.header().hash());
});
}
}

result
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/setup/sentry_config.rs
@@ -1,5 +1,4 @@
use build_info::{get_version, Version};
use log::info;
use serde_derive::Deserialize;

#[derive(Clone, Debug, PartialEq, Deserialize)]
Expand All @@ -18,17 +17,15 @@ impl SentryConfig {
});

sentry::integrations::panic::register_panic_handler();
info!(target: "sentry", "**Notice**: \
The ckb process will send stack trace to sentry on Rust panics. \
This is enabled by default before mainnet, which can be opted out by setting \
the option `dsn` to empty in the config file. The DSN is now {}", self.dsn);
} else {
info!(target: "sentry", "sentry is disabled");
}

guard
}

pub fn is_enabled(&self) -> bool {
self.dsn.parse::<sentry::internals::Dsn>().is_ok()
}

fn build_sentry_client_options(&self, version: &Version) -> sentry::ClientOptions {
sentry::ClientOptions {
dsn: self.dsn.parse().ok(),
Expand Down

0 comments on commit dfd5c8a

Please sign in to comment.