Skip to content

Commit

Permalink
feat(fmt_style): address review
Browse files Browse the repository at this point in the history
Signed-off-by: Abhilash Shetty <abhilash.shetty@datacore.com>
  • Loading branch information
abhilashshetty04 committed Feb 23, 2024
1 parent 1bbe1de commit d4ead8d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 73 deletions.
6 changes: 3 additions & 3 deletions control-plane/agents/src/bin/core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub(crate) struct CliArgs {
pub(crate) etcd_page_limit: u32,

/// Formatting style to be used while logging.
#[clap(global = true, default_value = FmtStyle::Pretty.as_ref(), short, long)]
#[clap(default_value = FmtStyle::Pretty.as_ref(), short, long)]
fmt_style: FmtStyle,

/// Use ANSI colors for the logs.
Expand Down Expand Up @@ -164,10 +164,10 @@ async fn main() -> anyhow::Result<()> {
let cli_args = CliArgs::args();
utils::print_package_info!();
println!("Using options: {cli_args:?}");
let fmt_style = cli_args.fmt_style.clone();
// let fmt_style = cli_args.fmt_style.clone();
utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style)
.with_style(cli_args.fmt_style)
.with_colors(cli_args.ansi_colors)
.init(
"agent-core",
Expand Down
5 changes: 2 additions & 3 deletions control-plane/agents/src/bin/ha/cluster/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct Cli {
events_url: Option<url::Url>,

/// Formatting style to be used while logging.
#[clap(global = true, default_value = FmtStyle::Pretty.as_ref(), short, long)]
#[clap(default_value = FmtStyle::Pretty.as_ref(), short, long)]
fmt_style: FmtStyle,

/// If set, configures the output to be in ansi color format.
Expand All @@ -77,10 +77,9 @@ pub(crate) fn core_grpc<'a>() -> &'a CoreClient {
}

fn initialize_tracing(args: &Cli) {
let fmt_style = &args.fmt_style;
utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style.clone())
.with_style(args.fmt_style)
.with_colors(args.ansi_colors)
.init(
"agent-ha-cluster",
Expand Down
6 changes: 2 additions & 4 deletions control-plane/agents/src/bin/ha/node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct Cli {
events_url: Option<url::Url>,

/// Formatting style to be used while logging.
#[clap(global = true, default_value = FmtStyle::Pretty.as_ref(), short, long)]
#[clap(default_value = FmtStyle::Pretty.as_ref(), short, long)]
fmt_style: FmtStyle,

/// Enable ansi colors for logs.
Expand Down Expand Up @@ -116,11 +116,9 @@ async fn main() -> anyhow::Result<()> {

utils::print_package_info!();

let fmt_style = cli_args.fmt_style.clone();

utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style)
.with_style(cli_args.fmt_style)
.with_colors(cli_args.ansi_colors)
.init(
"agent-ha-node",
Expand Down
2 changes: 1 addition & 1 deletion control-plane/csi-driver/src/bin/controller/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async fn main() -> anyhow::Result<()> {
let ansi_colors = args.get_flag("ansi-colors");
utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style.clone())
.with_style(*fmt_style)
.with_colors(ansi_colors)
.init(
"csi-controller",
Expand Down
2 changes: 1 addition & 1 deletion control-plane/csi-driver/src/bin/node/main_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub(super) async fn main() -> anyhow::Result<()> {
let colors = matches.get_flag("ansi-colors");
utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style.clone())
.with_style(*fmt_style)
.with_colors(colors)
.init("csi-node", tags, None, None);

Expand Down
6 changes: 2 additions & 4 deletions control-plane/rest/service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) struct CliArgs {
max_workers: usize,

/// Formatting style to be used while logging.
#[clap(global = true, default_value = FmtStyle::Pretty.as_ref(), short, long)]
#[clap(default_value = FmtStyle::Pretty.as_ref(), short, long)]
fmt_style: FmtStyle,

/// Use ANSI colors for logs.
Expand Down Expand Up @@ -214,11 +214,9 @@ async fn main() -> anyhow::Result<()> {
let cli_args = CliArgs::args();
println!("Using options: {:?}", &cli_args);

let fmt_style = cli_args.fmt_style.clone();

utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style)
.with_style(cli_args.fmt_style)
.with_colors(cli_args.ansi_colors)
.init(
"rest-server",
Expand Down
2 changes: 1 addition & 1 deletion k8s/operators/src/pool/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async fn main() -> anyhow::Result<()> {
let ansi_colors = matches.get_flag("ansi-colors");
utils::tracing_telemetry::TracingTelemetry::builder()
.with_writer(FmtLayer::Stdout)
.with_style(fmt_style.clone())
.with_style(*fmt_style)
.with_colors(ansi_colors)
.init(
"agent-ha-node",
Expand Down
2 changes: 1 addition & 1 deletion utils/dependencies
102 changes: 47 additions & 55 deletions utils/utils-lib/src/tracing_telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use opentelemetry::{global, trace, Context, KeyValue};

use event_publisher::event_handler::EventHandle;
use opentelemetry::sdk::{propagation::TraceContextPropagator, trace::Tracer, Resource};
use tracing::{error, Level};
use tracing::Level;
use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt, Layer, Registry};

/// Parse KeyValues from structopt's cmdline arguments
Expand Down Expand Up @@ -51,12 +51,12 @@ pub enum FmtLayer {
}

/// Tracing telemetry style.
#[derive(Debug, Clone, strum_macros::EnumString, strum_macros::AsRefStr)]
#[derive(Debug, Clone, Copy, strum_macros::EnumString, strum_macros::AsRefStr)]
#[strum(serialize_all = "lowercase")]
pub enum FmtStyle {
/// Compact style.
Compact,
/// Pretty Style
/// Pretty Style.
Pretty,
/// JSON Style.
Json,
Expand Down Expand Up @@ -105,49 +105,41 @@ impl TracingTelemetry {
let stderr = tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.with_ansi(self.colors);
let init_jaeger: bool;
let tracer: Option<Tracer> = match jaeger {
Some(val) => {
init_jaeger = true;
let tracing_tags =
tracing_tags
.into_iter()
.fold(Vec::<KeyValue>::new(), |mut acc, kv| {
if !acc.iter().any(|acc| acc.key == kv.key) {
acc.push(kv);
}
acc
});
set_jaeger_env();
global::set_text_map_propagator(TraceContextPropagator::new());
let pipeline = opentelemetry_jaeger::new_agent_pipeline()
.with_endpoint(val)
.with_service_name("service_name")
.with_trace_config(
opentelemetry::sdk::trace::Config::default()
.with_resource(Resource::new(tracing_tags)),
);
match pipeline.install_batch(opentelemetry::runtime::TokioCurrentThread) {
Ok(tracer) => Some(tracer),
Err(err) => {
panic!("Failed to install OpenTelemetry pipeline: {}", err);
}
let tracer: Option<Tracer> = if let Some(val) = jaeger {
let tracing_tags =
tracing_tags
.into_iter()
.fold(Vec::<KeyValue>::new(), |mut acc, kv| {
if !acc.iter().any(|acc| acc.key == kv.key) {
acc.push(kv);
}
acc
});
set_jaeger_env();
global::set_text_map_propagator(TraceContextPropagator::new());
let pipeline = opentelemetry_jaeger::new_agent_pipeline()
.with_endpoint(val)
.with_service_name("service_name")
.with_trace_config(
opentelemetry::sdk::trace::Config::default()
.with_resource(Resource::new(tracing_tags)),
);
match pipeline.install_batch(opentelemetry::runtime::TokioCurrentThread) {
Ok(tracer) => Some(tracer),
Err(err) => {
panic!("Failed to install OpenTelemetry pipeline: {}", err);
}
}
None => {
init_jaeger = false;
error!("Jaeger endpoint not configured");
None
}
} else {
None
};

// Get the optional eventing layer.
let events_layer = match events_url {
Some(url) => {
let target = filter::Targets::new().with_target(EVENT_BUS, Level::INFO);
Some(EventHandle::init(url.to_string(), service_name).with_filter(target))
}
None => None,
let events_layer = if let Some(url) = events_url {
let target = filter::Targets::new().with_target(EVENT_BUS, Level::INFO);
Some(EventHandle::init(url.to_string(), service_name).with_filter(target))
} else {
None
};

let subscriber = Registry::default()
Expand All @@ -156,57 +148,57 @@ impl TracingTelemetry {

match (self.writer, self.style) {
(FmtLayer::Stderr, FmtStyle::Compact) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stderr.compact()).with(telemetry).init();
} else {
subscriber.with(stderr.compact()).init();
}
}
(FmtLayer::Stdout, FmtStyle::Compact) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stdout.compact()).with(telemetry).init();
} else {
subscriber.with(stdout.compact()).init();
}
}
(FmtLayer::Stderr, FmtStyle::Pretty) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stderr.pretty()).with(telemetry).init();
} else {
subscriber.with(stderr.pretty()).init();
}
}
(FmtLayer::Stdout, FmtStyle::Pretty) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stdout.pretty()).with(telemetry).init();
} else {
subscriber.with(stdout.pretty()).init();
}
}
(FmtLayer::Stdout, FmtStyle::Json) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stdout.json()).with(telemetry).init();
} else {
subscriber.with(stdout.json()).init();
}
}
(FmtLayer::Stderr, FmtStyle::Json) => {
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(stderr.json()).with(telemetry).init();
} else {
subscriber.with(stderr.json()).init();
}
}
(FmtLayer::None, _) => {
let subscriber = Registry::default().with(tracing_filter::rust_log_filter());
if init_jaeger {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer.unwrap());
if let Some(tracer) = tracer {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
subscriber.with(telemetry).init();
} else {
subscriber.init()
Expand Down

0 comments on commit d4ead8d

Please sign in to comment.