Skip to content

Commit bdfeaf1

Browse files
szechyjsDav1dde
andauthored
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 <[email protected]>
1 parent 5daa98d commit bdfeaf1

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add configuration option to limit the amount of concurrent http connections. ([#4453](https://github.com/getsentry/relay/pull/4453))
1010
- Add flags context to event schema. ([#4458](https://github.com/getsentry/relay/pull/4458))
1111
- Add support for view hierarchy attachment scrubbing. ([#4452](https://github.com/getsentry/relay/pull/4452))
12+
- Allow configuration of Relay's log format via an environment variable. ([#4484](https://github.com/getsentry/relay/pull/4484))
1213

1314
**Bug Fixes**:
1415

relay-config/src/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ pub struct OverridableConfig {
226226
pub instance: Option<String>,
227227
/// The log level of this relay.
228228
pub log_level: Option<String>,
229+
/// The log format of this relay.
230+
pub log_format: Option<String>,
229231
/// The upstream relay or sentry instance.
230232
pub upstream: Option<String>,
231233
/// Alternate upstream provided through a Sentry DSN. Key and project will be ignored.
@@ -1641,6 +1643,10 @@ impl Config {
16411643
self.values.logging.level = log_level.parse()?;
16421644
}
16431645

1646+
if let Some(log_format) = overrides.log_format {
1647+
self.values.logging.format = log_format.parse()?;
1648+
}
1649+
16441650
if let Some(upstream) = overrides.upstream {
16451651
relay.upstream = upstream
16461652
.parse::<UpstreamDescriptor>()

relay-log/src/setup.rs

+33
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,39 @@ pub enum LogFormat {
5050
Json,
5151
}
5252

53+
/// The logging format parse error.
54+
#[derive(Clone, Debug)]
55+
pub struct FormatParseError(String);
56+
57+
impl Display for FormatParseError {
58+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59+
write!(
60+
f,
61+
r#"error parsing "{}" as format: expected one of "auto", "pretty", "simplified", "json""#,
62+
self.0
63+
)
64+
}
65+
}
66+
67+
impl FromStr for LogFormat {
68+
type Err = FormatParseError;
69+
70+
fn from_str(s: &str) -> Result<Self, Self::Err> {
71+
let result = match s {
72+
"" => LogFormat::Auto,
73+
s if s.eq_ignore_ascii_case("auto") => LogFormat::Auto,
74+
s if s.eq_ignore_ascii_case("pretty") => LogFormat::Pretty,
75+
s if s.eq_ignore_ascii_case("simplified") => LogFormat::Simplified,
76+
s if s.eq_ignore_ascii_case("json") => LogFormat::Json,
77+
s => return Err(FormatParseError(s.into())),
78+
};
79+
80+
Ok(result)
81+
}
82+
}
83+
84+
impl std::error::Error for FormatParseError {}
85+
5386
/// The logging level parse error.
5487
#[derive(Clone, Debug)]
5588
pub struct LevelParseError(String);

relay/src/cli.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub fn extract_config_args(matches: &ArgMatches) -> OverridableConfig {
8181
OverridableConfig {
8282
mode: matches.get_one("mode").cloned(),
8383
log_level: matches.get_one("log_level").cloned(),
84+
log_format: matches.get_one("log_format").cloned(),
8485
upstream: matches.get_one("upstream").cloned(),
8586
upstream_dsn: matches.get_one("upstream_dsn").cloned(),
8687
host: matches.get_one("host").cloned(),
@@ -103,6 +104,7 @@ pub fn extract_config_env_vars() -> OverridableConfig {
103104
OverridableConfig {
104105
mode: env::var("RELAY_MODE").ok(),
105106
log_level: env::var("RELAY_LOG_LEVEL").ok(),
107+
log_format: env::var("RELAY_LOG_FORMAT").ok(),
106108
upstream: env::var("RELAY_UPSTREAM_URL").ok(),
107109
upstream_dsn: env::var("RELAY_UPSTREAM_DSN").ok(),
108110
host: env::var("RELAY_HOST").ok(),

relay/src/cliapp.rs

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub fn make_app() -> Command {
4343
.help("The relay log level")
4444
.value_parser(["info", "warn", "error", "debug", "trace"]),
4545
)
46+
.arg(
47+
Arg::new("log_format")
48+
.long("log-format")
49+
.help("The relay log format")
50+
.value_parser(["auto", "pretty", "simplified", "json"]),
51+
)
4652
.arg(
4753
Arg::new("secret_key")
4854
.long("secret-key")

0 commit comments

Comments
 (0)