Skip to content

Commit

Permalink
feat(config): Add relay configuration for detecting canary (#3938)
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo authored Aug 27, 2024
1 parent dc843b0 commit 7da488e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Keep frames from both ends of the stacktrace when trimming frames. ([#3905](https://github.com/getsentry/relay/pull/3905))

**Features**:

- Add configuration option to specify the instance type of Relay. ([#3938](https://github.com/getsentry/relay/pull/3938))

## 24.8.0

**Bug Fixes**:
Expand Down
54 changes: 54 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ trait ConfigObject: DeserializeOwned + Serialize {
pub struct OverridableConfig {
/// The operation mode of this relay.
pub mode: Option<String>,
/// The instance type of this relay.
pub instance: Option<String>,
/// The log level of this relay.
pub log_level: Option<String>,
/// The upstream relay or sentry instance.
Expand Down Expand Up @@ -352,6 +354,44 @@ impl fmt::Display for RelayMode {
}
}

/// The instance type of Relay.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum RelayInstance {
/// This Relay is run as a default instance.
Default,

/// This Relay is run as a canary instance where experiments can be run.
Canary,
}

impl RelayInstance {
/// Returns `true` if the [`RelayInstance`] is of type [`RelayInstance::Canary`].
pub fn is_canary(&self) -> bool {
matches!(self, RelayInstance::Canary)
}
}

impl fmt::Display for RelayInstance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RelayInstance::Default => write!(f, "default"),
RelayInstance::Canary => write!(f, "canary"),
}
}
}

impl FromStr for RelayInstance {
type Err = fmt::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"canary" => Ok(RelayInstance::Canary),
_ => Ok(RelayInstance::Default),
}
}
}

/// Error returned when parsing an invalid [`RelayMode`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParseRelayModeError;
Expand Down Expand Up @@ -436,6 +476,8 @@ impl Default for ReadinessCondition {
pub struct Relay {
/// The operation mode of this relay.
pub mode: RelayMode,
/// The instance type of this relay.
pub instance: RelayInstance,
/// The upstream relay or sentry instance.
pub upstream: UpstreamDescriptor<'static>,
/// The host the relay should bind to (network interface).
Expand Down Expand Up @@ -463,6 +505,7 @@ impl Default for Relay {
fn default() -> Self {
Relay {
mode: RelayMode::Managed,
instance: RelayInstance::Default,
upstream: "https://sentry.io/".parse().unwrap(),
host: default_host(),
port: 3000,
Expand Down Expand Up @@ -1607,6 +1650,12 @@ impl Config {
.with_context(|| ConfigError::field("mode"))?;
}

if let Some(deployment) = overrides.instance {
relay.instance = deployment
.parse::<RelayInstance>()
.with_context(|| ConfigError::field("deployment"))?;
}

if let Some(log_level) = overrides.log_level {
self.values.logging.level = log_level.parse()?;
}
Expand Down Expand Up @@ -1820,6 +1869,11 @@ impl Config {
self.values.relay.mode
}

/// Returns the instance type of relay.
pub fn relay_instance(&self) -> RelayInstance {
self.values.relay.instance
}

/// Returns the upstream target as descriptor.
pub fn upstream_descriptor(&self) -> &UpstreamDescriptor<'_> {
&self.values.relay.upstream
Expand Down
2 changes: 2 additions & 0 deletions relay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub fn extract_config_args(matches: &ArgMatches) -> OverridableConfig {
secret_key: matches.get_one("secret_key").cloned(),
outcome_source: matches.get_one("source_id").cloned(),
shutdown_timeout: matches.get_one("shutdown_timeout").cloned(),
instance: matches.get_one("instance").cloned(),
}
}

Expand All @@ -117,6 +118,7 @@ pub fn extract_config_env_vars() -> OverridableConfig {
secret_key: env::var("RELAY_SECRET_KEY").ok(),
outcome_source: None, //already extracted in params
shutdown_timeout: env::var("SHUTDOWN_TIMEOUT").ok(),
instance: env::var("RELAY_INSTANCE").ok(),
}
}

Expand Down
5 changes: 5 additions & 0 deletions relay/src/cliapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ pub fn make_app() -> Command {
"Maximum number of seconds to wait for pending envelopes on shutdown.",
),
)
.arg(
Arg::new("instance")
.long("instance")
.help("The instance type of this Relay.")
)
)
.subcommand(
Command::new("credentials")
Expand Down

0 comments on commit 7da488e

Please sign in to comment.