From bdfeaf15461fc31c3a83dede4525062019ff6d46 Mon Sep 17 00:00:00 2001 From: Jared Szechy Date: Tue, 4 Feb 2025 01:54:58 -0500 Subject: [PATCH] feat(config): Allow setting log format from envar (#4484) There is a large discontinuity between config values that can be set in environment variables. When running in containers the prefered config approach is through environment variables and not config files. This PR enables setting of the logging format through an environment variable Co-authored-by: David Herberth --- CHANGELOG.md | 1 + relay-config/src/config.rs | 6 ++++++ relay-log/src/setup.rs | 33 +++++++++++++++++++++++++++++++++ relay/src/cli.rs | 2 ++ relay/src/cliapp.rs | 6 ++++++ 5 files changed, 48 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fceea4a075..0f8a4ff952 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Add configuration option to limit the amount of concurrent http connections. ([#4453](https://github.com/getsentry/relay/pull/4453)) - Add flags context to event schema. ([#4458](https://github.com/getsentry/relay/pull/4458)) - Add support for view hierarchy attachment scrubbing. ([#4452](https://github.com/getsentry/relay/pull/4452)) +- Allow configuration of Relay's log format via an environment variable. ([#4484](https://github.com/getsentry/relay/pull/4484)) **Bug Fixes**: diff --git a/relay-config/src/config.rs b/relay-config/src/config.rs index fa41ef8b1d..8561a297bf 100644 --- a/relay-config/src/config.rs +++ b/relay-config/src/config.rs @@ -226,6 +226,8 @@ pub struct OverridableConfig { pub instance: Option, /// The log level of this relay. pub log_level: Option, + /// The log format of this relay. + pub log_format: Option, /// The upstream relay or sentry instance. pub upstream: Option, /// Alternate upstream provided through a Sentry DSN. Key and project will be ignored. @@ -1641,6 +1643,10 @@ impl Config { self.values.logging.level = log_level.parse()?; } + if let Some(log_format) = overrides.log_format { + self.values.logging.format = log_format.parse()?; + } + if let Some(upstream) = overrides.upstream { relay.upstream = upstream .parse::() diff --git a/relay-log/src/setup.rs b/relay-log/src/setup.rs index ec971fc35e..19ad468000 100644 --- a/relay-log/src/setup.rs +++ b/relay-log/src/setup.rs @@ -50,6 +50,39 @@ pub enum LogFormat { Json, } +/// The logging format parse error. +#[derive(Clone, Debug)] +pub struct FormatParseError(String); + +impl Display for FormatParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + r#"error parsing "{}" as format: expected one of "auto", "pretty", "simplified", "json""#, + self.0 + ) + } +} + +impl FromStr for LogFormat { + type Err = FormatParseError; + + fn from_str(s: &str) -> Result { + let result = match s { + "" => LogFormat::Auto, + s if s.eq_ignore_ascii_case("auto") => LogFormat::Auto, + s if s.eq_ignore_ascii_case("pretty") => LogFormat::Pretty, + s if s.eq_ignore_ascii_case("simplified") => LogFormat::Simplified, + s if s.eq_ignore_ascii_case("json") => LogFormat::Json, + s => return Err(FormatParseError(s.into())), + }; + + Ok(result) + } +} + +impl std::error::Error for FormatParseError {} + /// The logging level parse error. #[derive(Clone, Debug)] pub struct LevelParseError(String); diff --git a/relay/src/cli.rs b/relay/src/cli.rs index 048807e6eb..a5c8fba536 100644 --- a/relay/src/cli.rs +++ b/relay/src/cli.rs @@ -81,6 +81,7 @@ pub fn extract_config_args(matches: &ArgMatches) -> OverridableConfig { OverridableConfig { mode: matches.get_one("mode").cloned(), log_level: matches.get_one("log_level").cloned(), + log_format: matches.get_one("log_format").cloned(), upstream: matches.get_one("upstream").cloned(), upstream_dsn: matches.get_one("upstream_dsn").cloned(), host: matches.get_one("host").cloned(), @@ -103,6 +104,7 @@ pub fn extract_config_env_vars() -> OverridableConfig { OverridableConfig { mode: env::var("RELAY_MODE").ok(), log_level: env::var("RELAY_LOG_LEVEL").ok(), + log_format: env::var("RELAY_LOG_FORMAT").ok(), upstream: env::var("RELAY_UPSTREAM_URL").ok(), upstream_dsn: env::var("RELAY_UPSTREAM_DSN").ok(), host: env::var("RELAY_HOST").ok(), diff --git a/relay/src/cliapp.rs b/relay/src/cliapp.rs index aac6fe8444..320bbab21d 100644 --- a/relay/src/cliapp.rs +++ b/relay/src/cliapp.rs @@ -43,6 +43,12 @@ pub fn make_app() -> Command { .help("The relay log level") .value_parser(["info", "warn", "error", "debug", "trace"]), ) + .arg( + Arg::new("log_format") + .long("log-format") + .help("The relay log format") + .value_parser(["auto", "pretty", "simplified", "json"]), + ) .arg( Arg::new("secret_key") .long("secret-key")