Skip to content

Commit

Permalink
[indexer grpc] Create env var to toggle logging (#11309)
Browse files Browse the repository at this point in the history
* Add grpc logging env override

* tweak

---------

Co-authored-by: bowenyang007 <[email protected]>
  • Loading branch information
rtso and bowenyang007 authored Dec 13, 2023
1 parent 42fb458 commit ce30868
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
4 changes: 4 additions & 0 deletions config/src/config/config_optimizer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use super::IndexerGrpcConfig;
use crate::{
config::{
node_config_loader::NodeType, utils::get_config_name, AdminServiceConfig, Error,
Expand Down Expand Up @@ -100,6 +101,9 @@ impl ConfigOptimizer for NodeConfig {
if IndexerConfig::optimize(node_config, local_config_yaml, node_type, chain_id)? {
optimizers_with_modifications.push(IndexerConfig::get_optimizer_name());
}
if IndexerGrpcConfig::optimize(node_config, local_config_yaml, node_type, chain_id)? {
optimizers_with_modifications.push(IndexerGrpcConfig::get_optimizer_name());
}
if AdminServiceConfig::optimize(node_config, local_config_yaml, node_type, chain_id)? {
optimizers_with_modifications.push(AdminServiceConfig::get_optimizer_name());
}
Expand Down
78 changes: 75 additions & 3 deletions config/src/config/indexer_grpc_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
// SPDX-License-Identifier: Apache-2.0

use crate::config::{
config_sanitizer::ConfigSanitizer, node_config_loader::NodeType, Error, NodeConfig,
config_optimizer::ConfigOptimizer, config_sanitizer::ConfigSanitizer,
node_config_loader::NodeType, Error, NodeConfig,
};
use aptos_types::chain_id::ChainId;
use serde::{Deserialize, Serialize};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use serde_yaml::Value;
use std::{
fmt::{Debug, Formatter},
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
};

// Useful indexer defaults
const DEFAULT_PROCESSOR_TASK_COUNT: u16 = 20;
const DEFAULT_PROCESSOR_BATCH_SIZE: u16 = 1000;
const DEFAULT_OUTPUT_BATCH_SIZE: u16 = 100;
pub const DEFAULT_GRPC_STREAM_PORT: u16 = 50051;

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[derive(Clone, Deserialize, PartialEq, Eq, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct IndexerGrpcConfig {
pub enabled: bool,
Expand All @@ -40,6 +45,23 @@ pub struct IndexerGrpcConfig {
pub enable_verbose_logging: bool,
}

impl Debug for IndexerGrpcConfig {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IndexerGrpcConfig")
.field("enabled", &self.enabled)
.field(
"use_data_service_interface",
&self.use_data_service_interface,
)
.field("address", &self.address)
.field("processor_task_count", &self.processor_task_count)
.field("processor_batch_size", &self.processor_batch_size)
.field("output_batch_size", &self.output_batch_size)
.field("enable_verbose_logging", &self.enable_verbose_logging)
.finish()
}
}

// Reminder, #[serde(default)] on IndexerGrpcConfig means that the default values for
// fields will come from this Default impl, unless the field has a specific
// #[serde(default)] on it (which none of the above do).
Expand Down Expand Up @@ -82,6 +104,36 @@ impl ConfigSanitizer for IndexerGrpcConfig {
}
}

impl ConfigOptimizer for IndexerGrpcConfig {
fn optimize(
node_config: &mut NodeConfig,
_local_config_yaml: &Value,
_node_type: NodeType,
_chain_id: Option<ChainId>,
) -> Result<bool, Error> {
let indexer_config = &mut node_config.indexer_grpc;
// If the indexer is not enabled, there's nothing to do
if !indexer_config.enabled {
return Ok(false);
}

// TODO: we really shouldn't be overriding the configs if they are
// specified in the local node config file. This optimizer should
// migrate to the pattern used by other optimizers, but for now, we'll
// just keep the legacy behaviour to avoid breaking anything.

// Override with environment variables if they are set
indexer_config.enable_verbose_logging = env_var_or_default(
"INDEXER_GRPC_ENABLE_VERBOSE_LOGGING",
Some(indexer_config.enable_verbose_logging),
None,
)
.unwrap_or(false);

Ok(true)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -120,3 +172,23 @@ mod tests {
.unwrap();
}
}

/// Returns the value of the environment variable `env_var`
/// if it is set, otherwise returns `default`.
fn env_var_or_default<T: std::str::FromStr>(
env_var: &'static str,
default: Option<T>,
expected_message: Option<String>,
) -> Option<T> {
let partial = std::env::var(env_var).ok().map(|s| s.parse().ok());
match default {
None => partial.unwrap_or_else(|| {
panic!(
"{}",
expected_message
.unwrap_or_else(|| { format!("Expected env var {} to be set", env_var) })
)
}),
Some(default_value) => partial.unwrap_or(Some(default_value)),
}
}

0 comments on commit ce30868

Please sign in to comment.