feat(logging): Emit a setup error when a restricted keys are used for logging default keys (#5185)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Abhishek Kanojia
2024-07-14 19:28:21 +05:30
committed by GitHub
parent 21499947ad
commit ff96a62b95
14 changed files with 151 additions and 176 deletions

View File

@ -8,6 +8,7 @@ use std::{
io::Write,
};
use config::ConfigError;
use once_cell::sync::Lazy;
use serde::ser::{SerializeMap, Serializer};
use serde_json::{ser::Formatter, Value};
@ -155,7 +156,11 @@ where
/// let formatting_layer = router_env::FormattingLayer::new("my_service", std::io::stdout, serde_json::ser::CompactFormatter);
/// ```
///
pub fn new(service: &str, dst_writer: W, formatter: F) -> Self {
pub fn new(
service: &str,
dst_writer: W,
formatter: F,
) -> error_stack::Result<Self, ConfigError> {
Self::new_with_implicit_entries(service, dst_writer, HashMap::new(), formatter)
}
@ -163,9 +168,9 @@ where
pub fn new_with_implicit_entries(
service: &str,
dst_writer: W,
mut default_fields: HashMap<String, Value>,
default_fields: HashMap<String, Value>,
formatter: F,
) -> Self {
) -> error_stack::Result<Self, ConfigError> {
let pid = std::process::id();
let hostname = gethostname::gethostname().to_string_lossy().into_owned();
let service = service.to_string();
@ -174,20 +179,16 @@ where
#[cfg(feature = "vergen")]
let build = crate::build!().to_string();
let env = crate::env::which().to_string();
default_fields.retain(|key, value| {
if !IMPLICIT_KEYS.contains(key.as_str()) {
true
} else {
tracing::warn!(
?key,
?value,
"Attempting to log a reserved entry. It won't be added to the logs"
);
false
for key in default_fields.keys() {
if IMPLICIT_KEYS.contains(key.as_str()) {
return Err(ConfigError::Message(format!(
"A reserved key `{key}` was included in `default_fields` in the log formatting layer"
))
.into());
}
});
}
Self {
Ok(Self {
dst_writer,
pid,
hostname,
@ -199,7 +200,7 @@ where
build,
default_fields,
formatter,
}
})
}
/// Serialize common for both span and event entries.

View File

@ -2,6 +2,7 @@
use std::time::Duration;
use ::config::ConfigError;
use opentelemetry::{
global, runtime,
sdk::{
@ -36,7 +37,7 @@ pub fn setup(
config: &config::Log,
service_name: &str,
crates_to_filter: impl AsRef<[&'static str]>,
) -> TelemetryGuard {
) -> error_stack::Result<TelemetryGuard, ConfigError> {
let mut guards = Vec::new();
// Setup OpenTelemetry traces and metrics
@ -69,11 +70,9 @@ pub fn setup(
&crates_to_filter,
);
println!("Using file logging filter: {file_filter}");
Some(
FormattingLayer::new(service_name, file_writer, CompactFormatter)
.with_filter(file_filter),
)
let layer = FormattingLayer::new(service_name, file_writer, CompactFormatter)?
.with_filter(file_filter);
Some(layer)
} else {
None
};
@ -107,17 +106,21 @@ pub fn setup(
}
config::LogFormat::Json => {
error_stack::Report::set_color_mode(error_stack::fmt::ColorMode::None);
let logging_layer =
FormattingLayer::new(service_name, console_writer, CompactFormatter)
.with_filter(console_filter);
subscriber.with(logging_layer).init();
subscriber
.with(
FormattingLayer::new(service_name, console_writer, CompactFormatter)?
.with_filter(console_filter),
)
.init();
}
config::LogFormat::PrettyJson => {
error_stack::Report::set_color_mode(error_stack::fmt::ColorMode::None);
let logging_layer =
FormattingLayer::new(service_name, console_writer, PrettyFormatter::new())
.with_filter(console_filter);
subscriber.with(logging_layer).init();
subscriber
.with(
FormattingLayer::new(service_name, console_writer, PrettyFormatter::new())?
.with_filter(console_filter),
)
.init();
}
}
} else {
@ -126,10 +129,10 @@ pub fn setup(
// Returning the TelemetryGuard for logs to be printed and metrics to be collected until it is
// dropped
TelemetryGuard {
Ok(TelemetryGuard {
_log_guards: guards,
_metrics_controller,
}
})
}
fn get_opentelemetry_exporter(config: &config::LogTelemetry) -> TonicExporterBuilder {