Skip to content

Commit

Permalink
feat(config): Allow setting log format from envar (#4484)
Browse files Browse the repository at this point in the history
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 <david.herberth@sentry.io>
  • Loading branch information
szechyjs and Dav1dde authored Feb 4, 2025
1 parent 5daa98d commit bdfeaf1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down
6 changes: 6 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ pub struct OverridableConfig {
pub instance: Option<String>,
/// The log level of this relay.
pub log_level: Option<String>,
/// The log format of this relay.
pub log_format: Option<String>,
/// The upstream relay or sentry instance.
pub upstream: Option<String>,
/// Alternate upstream provided through a Sentry DSN. Key and project will be ignored.
Expand Down Expand Up @@ -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::<UpstreamDescriptor>()
Expand Down
33 changes: 33 additions & 0 deletions relay-log/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
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);
Expand Down
2 changes: 2 additions & 0 deletions relay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
6 changes: 6 additions & 0 deletions relay/src/cliapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit bdfeaf1

Please sign in to comment.