Skip to content

Commit

Permalink
feat: support redirecting the logs to the terminal
Browse files Browse the repository at this point in the history
Signed-off-by: GFX9 <[email protected]>
  • Loading branch information
GFX9 authored and Phoenix500526 committed Apr 25, 2024
1 parent 6f7308a commit be2da2f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
2 changes: 1 addition & 1 deletion crates/xline/src/utils/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub struct ServerArgs {
#[clap(long, value_parser = parse_metrics_push_protocol, default_value_t = default_metrics_push_protocol())]
metrics_push_protocol: MetricsPushProtocol,
/// Log file path
#[clap(long, default_value = "/var/log/xline")]
#[clap(long, default_value = "/std")]
log_file: PathBuf,
/// Log rotate strategy, eg: never, hourly, daily
#[clap(long, value_parser = parse_rotation, default_value_t = default_rotation())]
Expand Down
60 changes: 40 additions & 20 deletions crates/xline/src/utils/trace.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::Result;
use anyhow::{Ok, Result};
use opentelemetry_contrib::trace::exporter::jaeger_json::JaegerJsonExporter;
use opentelemetry_sdk::runtime::Tokio;
use tracing::warn;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{fmt::format, layer::SubscriberExt, util::SubscriberInitExt, Layer};
use utils::config::{file_appender, LogConfig, TraceConfig};
use utils::config::{default_rotation, file_appender, LogConfig, TraceConfig};

/// init tracing subscriber
/// # Errors
Expand All @@ -14,19 +15,6 @@ pub fn init_subscriber(
log_config: &LogConfig,
trace_config: &TraceConfig,
) -> Result<Option<WorkerGuard>> {
let mut guard = None;
let log_file_layer = log_config.path().as_ref().map(|log_path| {
let file_appender = file_appender(*log_config.rotation(), log_path, name);
// `WorkerGuard` should be assigned in the `main` function or whatever the entrypoint of the program is.
let (non_blocking, guard_inner) = tracing_appender::non_blocking(file_appender);
guard = Some(guard_inner);
tracing_subscriber::fmt::layer()
.event_format(format().compact())
.with_writer(non_blocking)
.with_ansi(false)
.with_filter(*log_config.level())
});

let jaeger_level = *trace_config.jaeger_level();
let jaeger_online_layer = trace_config
.jaeger_online()
Expand Down Expand Up @@ -59,11 +47,43 @@ pub fn init_subscriber(
let jaeger_fmt_layer = tracing_subscriber::fmt::layer()
.with_filter(tracing_subscriber::EnvFilter::from_default_env());

tracing_subscriber::registry()
.with(log_file_layer)
let prev_layers = tracing_subscriber::registry()
.with(jaeger_fmt_layer)
.with(jaeger_online_layer)
.with(jaeger_offline_layer)
.try_init()?;
Ok(guard)
.with(jaeger_offline_layer);

match *log_config.path() {
Some(ref file_path) => {
if file_path.to_string_lossy() == "/std" {
if *log_config.rotation() != default_rotation() {
warn!(
"The log is output to the terminal, so the rotation parameter is ignored."
);
}
let stdout_layer = tracing_subscriber::fmt::layer()
.event_format(format().compact())
.with_writer(std::io::stderr)
.with_ansi(false)
.with_filter(*log_config.level());

prev_layers.with(stdout_layer).try_init()?;
return Ok(None);
}
let mut guard = None;
let log_file_layer = log_config.path().as_ref().map(|log_path| {
let file_appender = file_appender(*log_config.rotation(), log_path, name);
// `WorkerGuard` should be assigned in the `main` function or whatever the entrypoint of the program is.
let (non_blocking, guard_inner) = tracing_appender::non_blocking(file_appender);
guard = Some(guard_inner);
tracing_subscriber::fmt::layer()
.event_format(format().compact())
.with_writer(non_blocking)
.with_ansi(false)
.with_filter(*log_config.level())
});
prev_layers.with(log_file_layer).try_init()?;
Ok(guard)
}
None => unreachable!(),
}
}
60 changes: 33 additions & 27 deletions scripts/quick_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ stop_all() {
# args:
# $1: index of the node
run_xline() {
log::info xline${1} starting
image="ghcr.io/xline-kv/xline:latest"

cmd="/usr/local/bin/xline \
--name node${1} \
--members ${MEMBERS} \
Expand All @@ -47,8 +50,35 @@ run_xline() {
cmd="${cmd} --is-leader"
fi

docker exec -e RUST_LOG=debug -d node${1} ${cmd}
log::info "command is: docker exec -e RUST_LOG=debug -d node${1} ${cmd}"
mount_point="-v ${DIR}:/mnt"
if [ -n "$LOG_PATH" ]; then
mkdir -p ${LOG_PATH}/node${i}
mount_point="${mount_point} -v ${LOG_PATH}/node${i}:/var/log/xline"
fi

docker run -d -it --rm --name=node${1} -d --net=xline_net \
--ip=${SERVERS[$1]} --cap-add=NET_ADMIN --cpu-shares=1024 \
-m=512M -v ${DIR}:/mnt ${image} \
${cmd}

log::info "run_xline node${1}: docker run -d -it --rm --name=node${1} -d --net=xline_net \
--ip=${SERVERS[$1]} --cap-add=NET_ADMIN --cpu-shares=1024 \
-m=512M ${mount_point} ${image} \
${cmd}"
log::info xline${1} started
}

# run etcdctl
# args:
# $1: size of cluster
run_etcdctl() {
log::info etcdctl starting

docker run -d -it --rm --name=client \
--net=xline_net --ip=${SERVERS[0]} --cap-add=NET_ADMIN \
--cpu-shares=1024 -m=512M -v ${DIR}:/mnt ghcr.io/xline-kv/etcdctl:v3.5.9 bash &
wait
log::info etcdctl started
}

# run cluster of xline/etcd in container
Expand All @@ -61,30 +91,6 @@ run_cluster() {
log::info cluster started
}

# run container of xline/etcd use specified image
# args:
# $1: size of cluster
run_container() {
log::info container starting
size=${1}
image="ghcr.io/xline-kv/xline:latest"
for ((i = 1; i <= ${size}; i++)); do
mount_point="-v ${DIR}:/mnt"
if [ -n "$LOG_PATH" ]; then
mkdir -p ${LOG_PATH}/node${i}
mount_point="${mount_point} -v ${LOG_PATH}/node${i}:/var/log/xline"
fi
docker run -d -it --rm --name=node${i} --net=xline_net \
--ip=${SERVERS[$i]} --cap-add=NET_ADMIN --cpu-shares=1024 \
-m=512M ${mount_point} ${image} bash &
done
docker run -d -it --rm --name=client \
--net=xline_net --ip=${SERVERS[0]} --cap-add=NET_ADMIN \
--cpu-shares=1024 -m=512M -v ${DIR}:/mnt ghcr.io/xline-kv/etcdctl:v3.5.9 bash &
wait
log::info container started
}

# run prometheus
run_prometheus() {
docker run -d -it --rm --name=prometheus --net=xline_net -p 9090:9090 \
Expand All @@ -96,8 +102,8 @@ if [ -z "$1" ]; then
stop_all
docker network create --subnet=172.20.0.0/24 xline_net >/dev/null 2>&1
log::warn "A Docker network named 'xline_net' is created for communication among various xline nodes. You can use the command 'docker network rm xline_net' to remove it after use."
run_container 3
run_cluster
run_etcdctl
run_prometheus "172.20.0.6"
echo "Prometheus starts on http://172.20.0.6:9090/graph and http://127.0.0.1:9090/graph"
exit 0
Expand Down

0 comments on commit be2da2f

Please sign in to comment.