relay_config/
config.rs

1use std::collections::{BTreeMap, HashMap};
2use std::error::Error;
3use std::io::Write;
4use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
5use std::num::NonZeroU8;
6use std::path::{Path, PathBuf};
7use std::str::FromStr;
8use std::time::Duration;
9use std::{env, fmt, fs, io};
10
11use anyhow::Context;
12use relay_auth::{PublicKey, RelayId, SecretKey, generate_key_pair, generate_relay_id};
13use relay_common::Dsn;
14use relay_kafka::{
15    ConfigError as KafkaConfigError, KafkaConfigParam, KafkaParams, KafkaTopic, TopicAssignment,
16    TopicAssignments,
17};
18use relay_metrics::MetricNamespace;
19use serde::de::{DeserializeOwned, Unexpected, Visitor};
20use serde::{Deserialize, Deserializer, Serialize, Serializer};
21use uuid::Uuid;
22
23use crate::aggregator::{AggregatorServiceConfig, ScopedAggregatorConfig};
24use crate::byte_size::ByteSize;
25use crate::upstream::UpstreamDescriptor;
26use crate::{RedisConfig, RedisConfigs, RedisConfigsRef, build_redis_configs};
27
28const DEFAULT_NETWORK_OUTAGE_GRACE_PERIOD: u64 = 10;
29
30static CONFIG_YAML_HEADER: &str = r###"# Please see the relevant documentation.
31# Performance tuning: https://docs.sentry.io/product/relay/operating-guidelines/
32# All config options: https://docs.sentry.io/product/relay/options/
33"###;
34
35/// Indicates config related errors.
36#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
37#[non_exhaustive]
38pub enum ConfigErrorKind {
39    /// Failed to open the file.
40    CouldNotOpenFile,
41    /// Failed to save a file.
42    CouldNotWriteFile,
43    /// Parsing YAML failed.
44    BadYaml,
45    /// Parsing JSON failed.
46    BadJson,
47    /// Invalid config value
48    InvalidValue,
49    /// The user attempted to run Relay with processing enabled, but uses a binary that was
50    /// compiled without the processing feature.
51    ProcessingNotAvailable,
52}
53
54impl fmt::Display for ConfigErrorKind {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::CouldNotOpenFile => write!(f, "could not open config file"),
58            Self::CouldNotWriteFile => write!(f, "could not write config file"),
59            Self::BadYaml => write!(f, "could not parse yaml config file"),
60            Self::BadJson => write!(f, "could not parse json config file"),
61            Self::InvalidValue => write!(f, "invalid config value"),
62            Self::ProcessingNotAvailable => write!(
63                f,
64                "was not compiled with processing, cannot enable processing"
65            ),
66        }
67    }
68}
69
70/// Defines the source of a config error
71#[derive(Debug)]
72enum ConfigErrorSource {
73    /// An error occurring independently.
74    None,
75    /// An error originating from a configuration file.
76    File(PathBuf),
77    /// An error originating in a field override (an env var, or a CLI parameter).
78    FieldOverride(String),
79}
80
81impl Default for ConfigErrorSource {
82    fn default() -> Self {
83        Self::None
84    }
85}
86
87impl fmt::Display for ConfigErrorSource {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            ConfigErrorSource::None => Ok(()),
91            ConfigErrorSource::File(file_name) => {
92                write!(f, " (file {})", file_name.display())
93            }
94            ConfigErrorSource::FieldOverride(name) => write!(f, " (field {name})"),
95        }
96    }
97}
98
99/// Indicates config related errors.
100#[derive(Debug)]
101pub struct ConfigError {
102    source: ConfigErrorSource,
103    kind: ConfigErrorKind,
104}
105
106impl ConfigError {
107    #[inline]
108    fn new(kind: ConfigErrorKind) -> Self {
109        Self {
110            source: ConfigErrorSource::None,
111            kind,
112        }
113    }
114
115    #[inline]
116    fn field(field: &'static str) -> Self {
117        Self {
118            source: ConfigErrorSource::FieldOverride(field.to_owned()),
119            kind: ConfigErrorKind::InvalidValue,
120        }
121    }
122
123    #[inline]
124    fn file(kind: ConfigErrorKind, p: impl AsRef<Path>) -> Self {
125        Self {
126            source: ConfigErrorSource::File(p.as_ref().to_path_buf()),
127            kind,
128        }
129    }
130
131    /// Returns the error kind of the error.
132    pub fn kind(&self) -> ConfigErrorKind {
133        self.kind
134    }
135}
136
137impl fmt::Display for ConfigError {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        write!(f, "{}{}", self.kind(), self.source)
140    }
141}
142
143impl Error for ConfigError {}
144
145enum ConfigFormat {
146    Yaml,
147    Json,
148}
149
150impl ConfigFormat {
151    pub fn extension(&self) -> &'static str {
152        match self {
153            ConfigFormat::Yaml => "yml",
154            ConfigFormat::Json => "json",
155        }
156    }
157}
158
159trait ConfigObject: DeserializeOwned + Serialize {
160    /// The format in which to serialize this configuration.
161    fn format() -> ConfigFormat;
162
163    /// The basename of the config file.
164    fn name() -> &'static str;
165
166    /// The full filename of the config file, including the file extension.
167    fn path(base: &Path) -> PathBuf {
168        base.join(format!("{}.{}", Self::name(), Self::format().extension()))
169    }
170
171    /// Loads the config file from a file within the given directory location.
172    fn load(base: &Path) -> anyhow::Result<Self> {
173        let path = Self::path(base);
174
175        let f = fs::File::open(&path)
176            .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotOpenFile, &path))?;
177        let f = io::BufReader::new(f);
178
179        let mut source = serde_vars::EnvSource::default();
180        match Self::format() {
181            ConfigFormat::Yaml => {
182                serde_vars::deserialize(serde_yaml::Deserializer::from_reader(f), &mut source)
183                    .with_context(|| ConfigError::file(ConfigErrorKind::BadYaml, &path))
184            }
185            ConfigFormat::Json => {
186                serde_vars::deserialize(&mut serde_json::Deserializer::from_reader(f), &mut source)
187                    .with_context(|| ConfigError::file(ConfigErrorKind::BadJson, &path))
188            }
189        }
190    }
191
192    /// Writes the configuration to a file within the given directory location.
193    fn save(&self, base: &Path) -> anyhow::Result<()> {
194        let path = Self::path(base);
195        let mut options = fs::OpenOptions::new();
196        options.write(true).truncate(true).create(true);
197
198        // Remove all non-user permissions for the newly created file
199        #[cfg(unix)]
200        {
201            use std::os::unix::fs::OpenOptionsExt;
202            options.mode(0o600);
203        }
204
205        let mut f = options
206            .open(&path)
207            .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?;
208
209        match Self::format() {
210            ConfigFormat::Yaml => {
211                f.write_all(CONFIG_YAML_HEADER.as_bytes())?;
212                serde_yaml::to_writer(&mut f, self)
213                    .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?
214            }
215            ConfigFormat::Json => serde_json::to_writer_pretty(&mut f, self)
216                .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?,
217        }
218
219        f.write_all(b"\n").ok();
220
221        Ok(())
222    }
223}
224
225/// Structure used to hold information about configuration overrides via
226/// CLI parameters or environment variables
227#[derive(Debug, Default)]
228pub struct OverridableConfig {
229    /// The operation mode of this relay.
230    pub mode: Option<String>,
231    /// The instance type of this relay.
232    pub instance: Option<String>,
233    /// The log level of this relay.
234    pub log_level: Option<String>,
235    /// The log format of this relay.
236    pub log_format: Option<String>,
237    /// The upstream relay or sentry instance.
238    pub upstream: Option<String>,
239    /// Alternate upstream provided through a Sentry DSN. Key and project will be ignored.
240    pub upstream_dsn: Option<String>,
241    /// The host the relay should bind to (network interface).
242    pub host: Option<String>,
243    /// The port to bind for the unencrypted relay HTTP server.
244    pub port: Option<String>,
245    /// "true" if processing is enabled "false" otherwise
246    pub processing: Option<String>,
247    /// the kafka bootstrap.servers configuration string
248    pub kafka_url: Option<String>,
249    /// the redis server url
250    pub redis_url: Option<String>,
251    /// The globally unique ID of the relay.
252    pub id: Option<String>,
253    /// The secret key of the relay
254    pub secret_key: Option<String>,
255    /// The public key of the relay
256    pub public_key: Option<String>,
257    /// Outcome source
258    pub outcome_source: Option<String>,
259    /// shutdown timeout
260    pub shutdown_timeout: Option<String>,
261    /// Server name reported in the Sentry SDK.
262    pub server_name: Option<String>,
263}
264
265/// The relay credentials
266#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
267pub struct Credentials {
268    /// The secret key of the relay
269    pub secret_key: SecretKey,
270    /// The public key of the relay
271    pub public_key: PublicKey,
272    /// The globally unique ID of the relay.
273    pub id: RelayId,
274}
275
276impl Credentials {
277    /// Generates new random credentials.
278    pub fn generate() -> Self {
279        relay_log::info!("generating new relay credentials");
280        let (sk, pk) = generate_key_pair();
281        Self {
282            secret_key: sk,
283            public_key: pk,
284            id: generate_relay_id(),
285        }
286    }
287
288    /// Serializes this configuration to JSON.
289    pub fn to_json_string(&self) -> anyhow::Result<String> {
290        serde_json::to_string(self)
291            .with_context(|| ConfigError::new(ConfigErrorKind::CouldNotWriteFile))
292    }
293}
294
295impl ConfigObject for Credentials {
296    fn format() -> ConfigFormat {
297        ConfigFormat::Json
298    }
299    fn name() -> &'static str {
300        "credentials"
301    }
302}
303
304/// Information on a downstream Relay.
305#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
306#[serde(rename_all = "camelCase")]
307pub struct RelayInfo {
308    /// The public key that this Relay uses to authenticate and sign requests.
309    pub public_key: PublicKey,
310
311    /// Marks an internal relay that has privileged access to more project configuration.
312    #[serde(default)]
313    pub internal: bool,
314}
315
316impl RelayInfo {
317    /// Creates a new RelayInfo
318    pub fn new(public_key: PublicKey) -> Self {
319        Self {
320            public_key,
321            internal: false,
322        }
323    }
324}
325
326/// The operation mode of a relay.
327#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
328#[serde(rename_all = "camelCase")]
329pub enum RelayMode {
330    /// This relay acts as a proxy for all requests and events.
331    ///
332    /// Events are normalized and rate limits from the upstream are enforced, but the relay will not
333    /// fetch project configurations from the upstream or perform PII stripping. All events are
334    /// accepted unless overridden on the file system.
335    Proxy,
336
337    /// This relay is configured statically in the file system.
338    ///
339    /// Events are only accepted for projects configured statically in the file system. All other
340    /// events are rejected. If configured, PII stripping is also performed on those events.
341    Static,
342
343    /// Project configurations are managed by the upstream.
344    ///
345    /// Project configurations are always fetched from the upstream, unless they are statically
346    /// overridden in the file system. This relay must be allowed in the upstream Sentry. This is
347    /// only possible, if the upstream is Sentry directly, or another managed Relay.
348    Managed,
349
350    /// Events are held in memory for inspection only.
351    ///
352    /// This mode is used for testing sentry SDKs.
353    Capture,
354}
355
356impl fmt::Display for RelayMode {
357    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
358        match self {
359            RelayMode::Proxy => write!(f, "proxy"),
360            RelayMode::Static => write!(f, "static"),
361            RelayMode::Managed => write!(f, "managed"),
362            RelayMode::Capture => write!(f, "capture"),
363        }
364    }
365}
366
367/// The instance type of Relay.
368#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
369#[serde(rename_all = "camelCase")]
370pub enum RelayInstance {
371    /// This Relay is run as a default instance.
372    Default,
373
374    /// This Relay is run as a canary instance where experiments can be run.
375    Canary,
376}
377
378impl RelayInstance {
379    /// Returns `true` if the [`RelayInstance`] is of type [`RelayInstance::Canary`].
380    pub fn is_canary(&self) -> bool {
381        matches!(self, RelayInstance::Canary)
382    }
383}
384
385impl fmt::Display for RelayInstance {
386    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
387        match self {
388            RelayInstance::Default => write!(f, "default"),
389            RelayInstance::Canary => write!(f, "canary"),
390        }
391    }
392}
393
394impl FromStr for RelayInstance {
395    type Err = fmt::Error;
396
397    fn from_str(s: &str) -> Result<Self, Self::Err> {
398        match s {
399            "canary" => Ok(RelayInstance::Canary),
400            _ => Ok(RelayInstance::Default),
401        }
402    }
403}
404
405/// Error returned when parsing an invalid [`RelayMode`].
406#[derive(Clone, Copy, Debug, Eq, PartialEq)]
407pub struct ParseRelayModeError;
408
409impl fmt::Display for ParseRelayModeError {
410    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
411        write!(
412            f,
413            "Relay mode must be one of: managed, static, proxy, capture"
414        )
415    }
416}
417
418impl Error for ParseRelayModeError {}
419
420impl FromStr for RelayMode {
421    type Err = ParseRelayModeError;
422
423    fn from_str(s: &str) -> Result<Self, Self::Err> {
424        match s {
425            "proxy" => Ok(RelayMode::Proxy),
426            "static" => Ok(RelayMode::Static),
427            "managed" => Ok(RelayMode::Managed),
428            "capture" => Ok(RelayMode::Capture),
429            _ => Err(ParseRelayModeError),
430        }
431    }
432}
433
434/// Returns `true` if this value is equal to `Default::default()`.
435fn is_default<T: Default + PartialEq>(t: &T) -> bool {
436    *t == T::default()
437}
438
439/// Checks if we are running in docker.
440fn is_docker() -> bool {
441    if fs::metadata("/.dockerenv").is_ok() {
442        return true;
443    }
444
445    fs::read_to_string("/proc/self/cgroup").is_ok_and(|s| s.contains("/docker"))
446}
447
448/// Default value for the "bind" configuration.
449fn default_host() -> IpAddr {
450    if is_docker() {
451        // Docker images rely on this service being exposed
452        "0.0.0.0".parse().unwrap()
453    } else {
454        "127.0.0.1".parse().unwrap()
455    }
456}
457
458/// Controls responses from the readiness health check endpoint based on authentication.
459///
460/// Independent of the the readiness condition, shutdown always switches Relay into unready state.
461#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
462#[serde(rename_all = "lowercase")]
463pub enum ReadinessCondition {
464    /// (default) Relay is ready when authenticated and connected to the upstream.
465    ///
466    /// Before authentication has succeeded and during network outages, Relay responds as not ready.
467    /// Relay reauthenticates based on the `http.auth_interval` parameter. During reauthentication,
468    /// Relay remains ready until authentication fails.
469    ///
470    /// Authentication is only required for Relays in managed mode. Other Relays will only check for
471    /// network outages.
472    Authenticated,
473    /// Relay reports readiness regardless of the authentication and networking state.
474    Always,
475}
476
477impl Default for ReadinessCondition {
478    fn default() -> Self {
479        Self::Authenticated
480    }
481}
482
483/// Relay specific configuration values.
484#[derive(Serialize, Deserialize, Debug)]
485#[serde(default)]
486pub struct Relay {
487    /// The operation mode of this relay.
488    pub mode: RelayMode,
489    /// The instance type of this relay.
490    pub instance: RelayInstance,
491    /// The upstream relay or sentry instance.
492    pub upstream: UpstreamDescriptor<'static>,
493    /// The host the relay should bind to (network interface).
494    pub host: IpAddr,
495    /// The port to bind for the unencrypted relay HTTP server.
496    pub port: u16,
497    /// Optional port to bind for the encrypted relay HTTPS server.
498    #[serde(skip_serializing)]
499    pub tls_port: Option<u16>,
500    /// The path to the identity (DER-encoded PKCS12) to use for TLS.
501    #[serde(skip_serializing)]
502    pub tls_identity_path: Option<PathBuf>,
503    /// Password for the PKCS12 archive.
504    #[serde(skip_serializing)]
505    pub tls_identity_password: Option<String>,
506    /// Always override project IDs from the URL and DSN with the identifier used at the upstream.
507    ///
508    /// Enable this setting for Relays used to redirect traffic to a migrated Sentry instance.
509    /// Validation of project identifiers can be safely skipped in these cases.
510    #[serde(skip_serializing_if = "is_default")]
511    pub override_project_ids: bool,
512}
513
514impl Default for Relay {
515    fn default() -> Self {
516        Relay {
517            mode: RelayMode::Managed,
518            instance: RelayInstance::Default,
519            upstream: "https://sentry.io/".parse().unwrap(),
520            host: default_host(),
521            port: 3000,
522            tls_port: None,
523            tls_identity_path: None,
524            tls_identity_password: None,
525            override_project_ids: false,
526        }
527    }
528}
529
530/// Control the metrics.
531#[derive(Serialize, Deserialize, Debug)]
532#[serde(default)]
533pub struct Metrics {
534    /// Hostname and port of the statsd server.
535    ///
536    /// Defaults to `None`.
537    pub statsd: Option<String>,
538    /// Common prefix that should be added to all metrics.
539    ///
540    /// Defaults to `"sentry.relay"`.
541    pub prefix: String,
542    /// Default tags to apply to all metrics.
543    pub default_tags: BTreeMap<String, String>,
544    /// Tag name to report the hostname to for each metric. Defaults to not sending such a tag.
545    pub hostname_tag: Option<String>,
546    /// Global sample rate for all emitted metrics between `0.0` and `1.0`.
547    ///
548    /// For example, a value of `0.3` means that only 30% of the emitted metrics will be sent.
549    /// Defaults to `1.0` (100%).
550    pub sample_rate: f32,
551    /// Interval for periodic metrics emitted from Relay.
552    ///
553    /// Setting it to `0` seconds disables the periodic metrics.
554    /// Defaults to 5 seconds.
555    pub periodic_secs: u64,
556    /// Whether local metric aggregation using statdsproxy should be enabled.
557    ///
558    /// Defaults to `true`.
559    pub aggregate: bool,
560}
561
562impl Default for Metrics {
563    fn default() -> Self {
564        Metrics {
565            statsd: None,
566            prefix: "sentry.relay".into(),
567            default_tags: BTreeMap::new(),
568            hostname_tag: None,
569            sample_rate: 1.0,
570            periodic_secs: 5,
571            aggregate: true,
572        }
573    }
574}
575
576/// Controls processing of Sentry metrics and metric metadata.
577#[derive(Serialize, Deserialize, Debug, Default)]
578#[serde(default)]
579pub struct SentryMetrics {
580    /// Whether metric stats are collected and emitted.
581    ///
582    /// Metric stats are always collected and emitted when processing
583    /// is enabled.
584    ///
585    /// This option is required for running multiple trusted Relays in a chain
586    /// and you want the metric stats to be collected and forwarded from
587    /// the first Relay in the chain.
588    ///
589    /// Defaults to `false`.
590    pub metric_stats_enabled: bool,
591}
592
593/// Controls various limits
594#[derive(Serialize, Deserialize, Debug)]
595#[serde(default)]
596pub struct Limits {
597    /// How many requests can be sent concurrently from Relay to the upstream before Relay starts
598    /// buffering.
599    pub max_concurrent_requests: usize,
600    /// How many queries can be sent concurrently from Relay to the upstream before Relay starts
601    /// buffering.
602    ///
603    /// The concurrency of queries is additionally constrained by `max_concurrent_requests`.
604    pub max_concurrent_queries: usize,
605    /// The maximum payload size for events.
606    pub max_event_size: ByteSize,
607    /// The maximum size for each attachment.
608    pub max_attachment_size: ByteSize,
609    /// The maximum combined size for all attachments in an envelope or request.
610    pub max_attachments_size: ByteSize,
611    /// The maximum combined size for all client reports in an envelope or request.
612    pub max_client_reports_size: ByteSize,
613    /// The maximum payload size for a monitor check-in.
614    pub max_check_in_size: ByteSize,
615    /// The maximum payload size for an entire envelopes. Individual limits still apply.
616    pub max_envelope_size: ByteSize,
617    /// The maximum number of session items per envelope.
618    pub max_session_count: usize,
619    /// The maximum payload size for general API requests.
620    pub max_api_payload_size: ByteSize,
621    /// The maximum payload size for file uploads and chunks.
622    pub max_api_file_upload_size: ByteSize,
623    /// The maximum payload size for chunks
624    pub max_api_chunk_upload_size: ByteSize,
625    /// The maximum payload size for a profile
626    pub max_profile_size: ByteSize,
627    /// The maximum payload size for a span.
628    pub max_log_size: ByteSize,
629    /// The maximum payload size for a span.
630    pub max_span_size: ByteSize,
631    /// The maximum payload size for a statsd metric.
632    pub max_statsd_size: ByteSize,
633    /// The maximum payload size for metric buckets.
634    pub max_metric_buckets_size: ByteSize,
635    /// The maximum payload size for a compressed replay.
636    pub max_replay_compressed_size: ByteSize,
637    /// The maximum payload size for an uncompressed replay.
638    #[serde(alias = "max_replay_size")]
639    max_replay_uncompressed_size: ByteSize,
640    /// The maximum size for a replay recording Kafka message.
641    pub max_replay_message_size: ByteSize,
642    /// The maximum number of threads to spawn for CPU and web work, each.
643    ///
644    /// The total number of threads spawned will roughly be `2 * max_thread_count`. Defaults to
645    /// the number of logical CPU cores on the host.
646    pub max_thread_count: usize,
647    /// Controls the maximum concurrency of each worker thread.
648    ///
649    /// Increasing the concurrency, can lead to a better utilization of worker threads by
650    /// increasing the amount of I/O done concurrently.
651    //
652    /// Currently has no effect on defaults to `1`.
653    pub max_pool_concurrency: usize,
654    /// The maximum number of seconds a query is allowed to take across retries. Individual requests
655    /// have lower timeouts. Defaults to 30 seconds.
656    pub query_timeout: u64,
657    /// The maximum number of seconds to wait for pending envelopes after receiving a shutdown
658    /// signal.
659    pub shutdown_timeout: u64,
660    /// Server keep-alive timeout in seconds.
661    ///
662    /// By default, keep-alive is set to 5 seconds.
663    pub keepalive_timeout: u64,
664    /// Server idle timeout in seconds.
665    ///
666    /// The idle timeout limits the amount of time a connection is kept open without activity.
667    /// Setting this too short may abort connections before Relay is able to send a response.
668    ///
669    /// By default there is no idle timeout.
670    pub idle_timeout: Option<u64>,
671    /// Sets the maximum number of concurrent connections.
672    ///
673    /// Upon reaching the limit, the server will stop accepting connections.
674    ///
675    /// By default there is no limit.
676    pub max_connections: Option<usize>,
677    /// The TCP listen backlog.
678    ///
679    /// Configures the TCP listen backlog for the listening socket of Relay.
680    /// See [`man listen(2)`](https://man7.org/linux/man-pages/man2/listen.2.html)
681    /// for a more detailed description of the listen backlog.
682    ///
683    /// Defaults to `1024`, a value [google has been using for a long time](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=19f92a030ca6d772ab44b22ee6a01378a8cb32d4).
684    pub tcp_listen_backlog: u32,
685}
686
687impl Default for Limits {
688    fn default() -> Self {
689        Limits {
690            max_concurrent_requests: 100,
691            max_concurrent_queries: 5,
692            max_event_size: ByteSize::mebibytes(1),
693            max_attachment_size: ByteSize::mebibytes(100),
694            max_attachments_size: ByteSize::mebibytes(100),
695            max_client_reports_size: ByteSize::kibibytes(4),
696            max_check_in_size: ByteSize::kibibytes(100),
697            max_envelope_size: ByteSize::mebibytes(100),
698            max_session_count: 100,
699            max_api_payload_size: ByteSize::mebibytes(20),
700            max_api_file_upload_size: ByteSize::mebibytes(40),
701            max_api_chunk_upload_size: ByteSize::mebibytes(100),
702            max_profile_size: ByteSize::mebibytes(50),
703            max_log_size: ByteSize::mebibytes(1),
704            max_span_size: ByteSize::mebibytes(1),
705            max_statsd_size: ByteSize::mebibytes(1),
706            max_metric_buckets_size: ByteSize::mebibytes(1),
707            max_replay_compressed_size: ByteSize::mebibytes(10),
708            max_replay_uncompressed_size: ByteSize::mebibytes(100),
709            max_replay_message_size: ByteSize::mebibytes(15),
710            max_thread_count: num_cpus::get(),
711            max_pool_concurrency: 1,
712            query_timeout: 30,
713            shutdown_timeout: 10,
714            keepalive_timeout: 5,
715            idle_timeout: None,
716            max_connections: None,
717            tcp_listen_backlog: 1024,
718        }
719    }
720}
721
722/// Controls traffic steering.
723#[derive(Debug, Default, Deserialize, Serialize)]
724#[serde(default)]
725pub struct Routing {
726    /// Accept and forward unknown Envelope items to the upstream.
727    ///
728    /// Forwarding unknown items should be enabled in most cases to allow proxying traffic for newer
729    /// SDK versions. The upstream in Sentry makes the final decision on which items are valid. If
730    /// this is disabled, just the unknown items are removed from Envelopes, and the rest is
731    /// processed as usual.
732    ///
733    /// Defaults to `true` for all Relay modes other than processing mode. In processing mode, this
734    /// is disabled by default since the item cannot be handled.
735    pub accept_unknown_items: Option<bool>,
736}
737
738/// Http content encoding for both incoming and outgoing web requests.
739#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
740#[serde(rename_all = "lowercase")]
741pub enum HttpEncoding {
742    /// Identity function without no compression.
743    ///
744    /// This is the default encoding and does not require the presence of the `content-encoding`
745    /// HTTP header.
746    #[default]
747    Identity,
748    /// Compression using a [zlib](https://en.wikipedia.org/wiki/Zlib) structure with
749    /// [deflate](https://en.wikipedia.org/wiki/DEFLATE) encoding.
750    ///
751    /// These structures are defined in [RFC 1950](https://datatracker.ietf.org/doc/html/rfc1950)
752    /// and [RFC 1951](https://datatracker.ietf.org/doc/html/rfc1951).
753    Deflate,
754    /// A format using the [Lempel-Ziv coding](https://en.wikipedia.org/wiki/LZ77_and_LZ78#LZ77)
755    /// (LZ77), with a 32-bit CRC.
756    ///
757    /// This is the original format of the UNIX gzip program. The HTTP/1.1 standard also recommends
758    /// that the servers supporting this content-encoding should recognize `x-gzip` as an alias, for
759    /// compatibility purposes.
760    Gzip,
761    /// A format using the [Brotli](https://en.wikipedia.org/wiki/Brotli) algorithm.
762    Br,
763    /// A format using the [Zstd](https://en.wikipedia.org/wiki/Zstd) compression algorithm.
764    Zstd,
765}
766
767impl HttpEncoding {
768    /// Parses a [`HttpEncoding`] from its `content-encoding` header value.
769    pub fn parse(str: &str) -> Self {
770        let str = str.trim();
771        if str.eq_ignore_ascii_case("zstd") {
772            Self::Zstd
773        } else if str.eq_ignore_ascii_case("br") {
774            Self::Br
775        } else if str.eq_ignore_ascii_case("gzip") || str.eq_ignore_ascii_case("x-gzip") {
776            Self::Gzip
777        } else if str.eq_ignore_ascii_case("deflate") {
778            Self::Deflate
779        } else {
780            Self::Identity
781        }
782    }
783
784    /// Returns the value for the `content-encoding` HTTP header.
785    ///
786    /// Returns `None` for [`Identity`](Self::Identity), and `Some` for other encodings.
787    pub fn name(&self) -> Option<&'static str> {
788        match self {
789            Self::Identity => None,
790            Self::Deflate => Some("deflate"),
791            Self::Gzip => Some("gzip"),
792            Self::Br => Some("br"),
793            Self::Zstd => Some("zstd"),
794        }
795    }
796}
797
798/// Controls authentication with upstream.
799#[derive(Serialize, Deserialize, Debug)]
800#[serde(default)]
801pub struct Http {
802    /// Timeout for upstream requests in seconds.
803    ///
804    /// This timeout covers the time from sending the request until receiving response headers.
805    /// Neither the connection process and handshakes, nor reading the response body is covered in
806    /// this timeout.
807    pub timeout: u32,
808    /// Timeout for establishing connections with the upstream in seconds.
809    ///
810    /// This includes SSL handshakes. Relay reuses connections when the upstream supports connection
811    /// keep-alive. Connections are retained for a maximum 75 seconds, or 15 seconds of inactivity.
812    pub connection_timeout: u32,
813    /// Maximum interval between failed request retries in seconds.
814    pub max_retry_interval: u32,
815    /// The custom HTTP Host header to send to the upstream.
816    pub host_header: Option<String>,
817    /// The interval in seconds at which Relay attempts to reauthenticate with the upstream server.
818    ///
819    /// Re-authentication happens even when Relay is idle. If authentication fails, Relay reverts
820    /// back into startup mode and tries to establish a connection. During this time, incoming
821    /// envelopes will be buffered.
822    ///
823    /// Defaults to `600` (10 minutes).
824    pub auth_interval: Option<u64>,
825    /// The maximum time of experiencing uninterrupted network failures until Relay considers that
826    /// it has encountered a network outage in seconds.
827    ///
828    /// During a network outage relay will try to reconnect and will buffer all upstream messages
829    /// until it manages to reconnect.
830    pub outage_grace_period: u64,
831    /// The time Relay waits before retrying an upstream request, in seconds.
832    ///
833    /// This time is only used before going into a network outage mode.
834    pub retry_delay: u64,
835    /// The interval in seconds for continued failed project fetches at which Relay will error.
836    ///
837    /// A successful fetch resets this interval. Relay does nothing during long
838    /// times without emitting requests.
839    pub project_failure_interval: u64,
840    /// Content encoding to apply to upstream store requests.
841    ///
842    /// By default, Relay applies `zstd` content encoding to compress upstream requests. Compression
843    /// can be disabled to reduce CPU consumption, but at the expense of increased network traffic.
844    ///
845    /// This setting applies to all store requests of SDK data, including events, transactions,
846    /// envelopes and sessions. At the moment, this does not apply to Relay's internal queries.
847    ///
848    /// Available options are:
849    ///
850    ///  - `identity`: Disables compression.
851    ///  - `deflate`: Compression using a zlib header with deflate encoding.
852    ///  - `gzip` (default): Compression using gzip.
853    ///  - `br`: Compression using the brotli algorithm.
854    ///  - `zstd`: Compression using the zstd algorithm.
855    pub encoding: HttpEncoding,
856    /// Submit metrics globally through a shared endpoint.
857    ///
858    /// As opposed to regular envelopes which are sent to an endpoint inferred from the project's
859    /// DSN, this submits metrics to the global endpoint with Relay authentication.
860    ///
861    /// This option does not have any effect on processing mode.
862    pub global_metrics: bool,
863}
864
865impl Default for Http {
866    fn default() -> Self {
867        Http {
868            timeout: 5,
869            connection_timeout: 3,
870            max_retry_interval: 60, // 1 minute
871            host_header: None,
872            auth_interval: Some(600), // 10 minutes
873            outage_grace_period: DEFAULT_NETWORK_OUTAGE_GRACE_PERIOD,
874            retry_delay: default_retry_delay(),
875            project_failure_interval: default_project_failure_interval(),
876            encoding: HttpEncoding::Zstd,
877            global_metrics: false,
878        }
879    }
880}
881
882/// Default for unavailable upstream retry period, 1s.
883fn default_retry_delay() -> u64 {
884    1
885}
886
887/// Default for project failure interval, 90s.
888fn default_project_failure_interval() -> u64 {
889    90
890}
891
892/// Default for max disk size, 500 MB.
893fn spool_envelopes_max_disk_size() -> ByteSize {
894    ByteSize::mebibytes(500)
895}
896
897/// Default number of encoded envelope bytes to cache before writing to disk.
898fn spool_envelopes_batch_size_bytes() -> ByteSize {
899    ByteSize::kibibytes(10)
900}
901
902fn spool_envelopes_max_envelope_delay_secs() -> u64 {
903    24 * 60 * 60
904}
905
906/// Default refresh frequency in ms for the disk usage monitoring.
907fn spool_disk_usage_refresh_frequency_ms() -> u64 {
908    100
909}
910
911/// Default bounded buffer size for handling backpressure.
912fn spool_max_backpressure_envelopes() -> usize {
913    500
914}
915
916/// Default max memory usage for unspooling.
917fn spool_max_backpressure_memory_percent() -> f32 {
918    0.9
919}
920
921/// Default number of partitions for the buffer.
922fn spool_envelopes_partitions() -> NonZeroU8 {
923    NonZeroU8::new(1).unwrap()
924}
925
926/// Persistent buffering configuration for incoming envelopes.
927#[derive(Debug, Serialize, Deserialize)]
928pub struct EnvelopeSpool {
929    /// The path of the SQLite database file(s) which persist the data.
930    ///
931    /// Based on the number of partitions, more database files will be created within the same path.
932    ///
933    /// If not set, the envelopes will be buffered in memory.
934    pub path: Option<PathBuf>,
935    /// The maximum size of the buffer to keep, in bytes.
936    ///
937    /// When the on-disk buffer reaches this size, new envelopes will be dropped.
938    ///
939    /// Defaults to 500MB.
940    #[serde(default = "spool_envelopes_max_disk_size")]
941    pub max_disk_size: ByteSize,
942    /// Size of the batch of compressed envelopes that are spooled to disk at once.
943    ///
944    /// Note that this is the size after which spooling will be triggered but it does not guarantee
945    /// that exactly this size will be spooled, it can be greater or equal.
946    ///
947    /// Defaults to 10 KiB.
948    #[serde(default = "spool_envelopes_batch_size_bytes")]
949    pub batch_size_bytes: ByteSize,
950    /// Maximum time between receiving the envelope and processing it.
951    ///
952    /// When envelopes spend too much time in the buffer (e.g. because their project cannot be loaded),
953    /// they are dropped.
954    ///
955    /// Defaults to 24h.
956    #[serde(default = "spool_envelopes_max_envelope_delay_secs")]
957    pub max_envelope_delay_secs: u64,
958    /// The refresh frequency in ms of how frequently disk usage is updated by querying SQLite
959    /// internal page stats.
960    ///
961    /// Defaults to 100ms.
962    #[serde(default = "spool_disk_usage_refresh_frequency_ms")]
963    pub disk_usage_refresh_frequency_ms: u64,
964    /// The amount of envelopes that the envelope buffer can push to its output queue.
965    ///
966    /// Defaults to 500.
967    #[serde(default = "spool_max_backpressure_envelopes")]
968    pub max_backpressure_envelopes: usize,
969    /// The relative memory usage above which the buffer service will stop dequeueing envelopes.
970    ///
971    /// Only applies when [`Self::path`] is set.
972    ///
973    /// This value should be lower than [`Health::max_memory_percent`] to prevent flip-flopping.
974    ///
975    /// Warning: This threshold can cause the buffer service to deadlock when the buffer consumes
976    /// excessive memory (as influenced by [`Self::batch_size_bytes`]).
977    ///
978    /// This scenario arises when the buffer stops spooling due to reaching the
979    /// [`Self::max_backpressure_memory_percent`] limit, but the batch threshold for spooling
980    /// ([`Self::batch_size_bytes`]) is never reached. As a result, no data is spooled, memory usage
981    /// continues to grow, and the system becomes deadlocked.
982    ///
983    /// ### Example
984    /// Suppose the system has 1GB of available memory and is configured to spool only after
985    /// accumulating 10GB worth of envelopes. If Relay consumes 900MB of memory, it will stop
986    /// unspooling due to reaching the [`Self::max_backpressure_memory_percent`] threshold.
987    ///
988    /// However, because the buffer hasn't accumulated the 10GB needed to trigger spooling,
989    /// no data will be offloaded. Memory usage keeps increasing until it hits the
990    /// [`Health::max_memory_percent`] threshold, e.g., at 950MB. At this point:
991    ///
992    /// - No more envelopes are accepted.
993    /// - The buffer remains stuck, as unspooling won’t resume until memory drops below 900MB which
994    ///   will not happen.
995    /// - A deadlock occurs, with the system unable to recover without manual intervention.
996    ///
997    /// Defaults to 90% (5% less than max memory).
998    #[serde(default = "spool_max_backpressure_memory_percent")]
999    pub max_backpressure_memory_percent: f32,
1000    /// Number of partitions of the buffer.
1001    ///
1002    /// A partition is a separate instance of the buffer which has its own isolated queue, stacks
1003    /// and other resources.
1004    ///
1005    /// Defaults to 1.
1006    #[serde(default = "spool_envelopes_partitions")]
1007    pub partitions: NonZeroU8,
1008}
1009
1010impl Default for EnvelopeSpool {
1011    fn default() -> Self {
1012        Self {
1013            path: None,
1014            max_disk_size: spool_envelopes_max_disk_size(),
1015            batch_size_bytes: spool_envelopes_batch_size_bytes(),
1016            max_envelope_delay_secs: spool_envelopes_max_envelope_delay_secs(),
1017            disk_usage_refresh_frequency_ms: spool_disk_usage_refresh_frequency_ms(),
1018            max_backpressure_envelopes: spool_max_backpressure_envelopes(),
1019            max_backpressure_memory_percent: spool_max_backpressure_memory_percent(),
1020            partitions: spool_envelopes_partitions(),
1021        }
1022    }
1023}
1024
1025/// Persistent buffering configuration.
1026#[derive(Debug, Serialize, Deserialize, Default)]
1027pub struct Spool {
1028    /// Configuration for envelope spooling.
1029    #[serde(default)]
1030    pub envelopes: EnvelopeSpool,
1031}
1032
1033/// Controls internal caching behavior.
1034#[derive(Serialize, Deserialize, Debug)]
1035#[serde(default)]
1036pub struct Cache {
1037    /// The full project state will be requested by this Relay if set to `true`.
1038    pub project_request_full_config: bool,
1039    /// The cache timeout for project configurations in seconds.
1040    pub project_expiry: u32,
1041    /// Continue using project state this many seconds after cache expiry while a new state is
1042    /// being fetched. This is added on top of `project_expiry`.
1043    ///
1044    /// Default is 2 minutes.
1045    pub project_grace_period: u32,
1046    /// Refresh a project after the specified seconds.
1047    ///
1048    /// The time must be between expiry time and the grace period.
1049    ///
1050    /// By default there are no refreshes enabled.
1051    pub project_refresh_interval: Option<u32>,
1052    /// The cache timeout for downstream relay info (public keys) in seconds.
1053    pub relay_expiry: u32,
1054    /// Unused cache timeout for envelopes.
1055    ///
1056    /// The envelope buffer is instead controlled by `envelope_buffer_size`, which controls the
1057    /// maximum number of envelopes in the buffer. A time based configuration may be re-introduced
1058    /// at a later point.
1059    #[serde(alias = "event_expiry")]
1060    envelope_expiry: u32,
1061    /// The maximum amount of envelopes to queue before dropping them.
1062    #[serde(alias = "event_buffer_size")]
1063    envelope_buffer_size: u32,
1064    /// The cache timeout for non-existing entries.
1065    pub miss_expiry: u32,
1066    /// The buffer timeout for batched project config queries before sending them upstream in ms.
1067    pub batch_interval: u32,
1068    /// The buffer timeout for batched queries of downstream relays in ms. Defaults to 100ms.
1069    pub downstream_relays_batch_interval: u32,
1070    /// The maximum number of project configs to fetch from Sentry at once. Defaults to 500.
1071    ///
1072    /// `cache.batch_interval` controls how quickly batches are sent, this controls the batch size.
1073    pub batch_size: usize,
1074    /// Interval for watching local cache override files in seconds.
1075    pub file_interval: u32,
1076    /// Interval for fetching new global configs from the upstream, in seconds.
1077    pub global_config_fetch_interval: u32,
1078}
1079
1080impl Default for Cache {
1081    fn default() -> Self {
1082        Cache {
1083            project_request_full_config: false,
1084            project_expiry: 300,       // 5 minutes
1085            project_grace_period: 120, // 2 minutes
1086            project_refresh_interval: None,
1087            relay_expiry: 3600,   // 1 hour
1088            envelope_expiry: 600, // 10 minutes
1089            envelope_buffer_size: 1000,
1090            miss_expiry: 60,                       // 1 minute
1091            batch_interval: 100,                   // 100ms
1092            downstream_relays_batch_interval: 100, // 100ms
1093            batch_size: 500,
1094            file_interval: 10,                // 10 seconds
1095            global_config_fetch_interval: 10, // 10 seconds
1096        }
1097    }
1098}
1099
1100fn default_max_secs_in_future() -> u32 {
1101    60 // 1 minute
1102}
1103
1104fn default_max_session_secs_in_past() -> u32 {
1105    5 * 24 * 3600 // 5 days
1106}
1107
1108fn default_chunk_size() -> ByteSize {
1109    ByteSize::mebibytes(1)
1110}
1111
1112fn default_projectconfig_cache_prefix() -> String {
1113    "relayconfig".to_owned()
1114}
1115
1116#[allow(clippy::unnecessary_wraps)]
1117fn default_max_rate_limit() -> Option<u32> {
1118    Some(300) // 5 minutes
1119}
1120
1121/// Controls Sentry-internal event processing.
1122#[derive(Serialize, Deserialize, Debug)]
1123pub struct Processing {
1124    /// True if the Relay should do processing. Defaults to `false`.
1125    pub enabled: bool,
1126    /// GeoIp DB file source.
1127    #[serde(default)]
1128    pub geoip_path: Option<PathBuf>,
1129    /// Maximum future timestamp of ingested events.
1130    #[serde(default = "default_max_secs_in_future")]
1131    pub max_secs_in_future: u32,
1132    /// Maximum age of ingested sessions. Older sessions will be dropped.
1133    #[serde(default = "default_max_session_secs_in_past")]
1134    pub max_session_secs_in_past: u32,
1135    /// Kafka producer configurations.
1136    pub kafka_config: Vec<KafkaConfigParam>,
1137    /// Additional kafka producer configurations.
1138    ///
1139    /// The `kafka_config` is the default producer configuration used for all topics. A secondary
1140    /// kafka config can be referenced in `topics:` like this:
1141    ///
1142    /// ```yaml
1143    /// secondary_kafka_configs:
1144    ///   mycustomcluster:
1145    ///     - name: 'bootstrap.servers'
1146    ///       value: 'sentry_kafka_metrics:9093'
1147    ///
1148    /// topics:
1149    ///   transactions: ingest-transactions
1150    ///   metrics:
1151    ///     name: ingest-metrics
1152    ///     config: mycustomcluster
1153    /// ```
1154    ///
1155    /// Then metrics will be produced to an entirely different Kafka cluster.
1156    #[serde(default)]
1157    pub secondary_kafka_configs: BTreeMap<String, Vec<KafkaConfigParam>>,
1158    /// Kafka topic names.
1159    #[serde(default)]
1160    pub topics: TopicAssignments,
1161    /// Whether to validate the supplied topics by calling Kafka's metadata endpoints.
1162    #[serde(default)]
1163    pub kafka_validate_topics: bool,
1164    /// Redis hosts to connect to for storing state for rate limits.
1165    #[serde(default)]
1166    pub redis: Option<RedisConfigs>,
1167    /// Maximum chunk size of attachments for Kafka.
1168    #[serde(default = "default_chunk_size")]
1169    pub attachment_chunk_size: ByteSize,
1170    /// Prefix to use when looking up project configs in Redis. Defaults to "relayconfig".
1171    #[serde(default = "default_projectconfig_cache_prefix")]
1172    pub projectconfig_cache_prefix: String,
1173    /// Maximum rate limit to report to clients.
1174    #[serde(default = "default_max_rate_limit")]
1175    pub max_rate_limit: Option<u32>,
1176}
1177
1178impl Default for Processing {
1179    /// Constructs a disabled processing configuration.
1180    fn default() -> Self {
1181        Self {
1182            enabled: false,
1183            geoip_path: None,
1184            max_secs_in_future: default_max_secs_in_future(),
1185            max_session_secs_in_past: default_max_session_secs_in_past(),
1186            kafka_config: Vec::new(),
1187            secondary_kafka_configs: BTreeMap::new(),
1188            topics: TopicAssignments::default(),
1189            kafka_validate_topics: false,
1190            redis: None,
1191            attachment_chunk_size: default_chunk_size(),
1192            projectconfig_cache_prefix: default_projectconfig_cache_prefix(),
1193            max_rate_limit: default_max_rate_limit(),
1194        }
1195    }
1196}
1197
1198/// Configuration for normalization in this Relay.
1199#[derive(Debug, Default, Serialize, Deserialize)]
1200#[serde(default)]
1201pub struct Normalization {
1202    /// Level of normalization for Relay to apply to incoming data.
1203    #[serde(default)]
1204    pub level: NormalizationLevel,
1205}
1206
1207/// Configuration for the level of normalization this Relay should do.
1208#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
1209#[serde(rename_all = "lowercase")]
1210pub enum NormalizationLevel {
1211    /// Runs normalization, excluding steps that break future compatibility.
1212    ///
1213    /// Processing Relays run [`NormalizationLevel::Full`] if this option is set.
1214    #[default]
1215    Default,
1216    /// Run full normalization.
1217    ///
1218    /// It includes steps that break future compatibility and should only run in
1219    /// the last layer of relays.
1220    Full,
1221}
1222
1223/// Configuration values for the outcome aggregator
1224#[derive(Serialize, Deserialize, Debug)]
1225#[serde(default)]
1226pub struct OutcomeAggregatorConfig {
1227    /// Defines the width of the buckets into which outcomes are aggregated, in seconds.
1228    pub bucket_interval: u64,
1229    /// Defines how often all buckets are flushed, in seconds.
1230    pub flush_interval: u64,
1231}
1232
1233impl Default for OutcomeAggregatorConfig {
1234    fn default() -> Self {
1235        Self {
1236            bucket_interval: 60,
1237            flush_interval: 120,
1238        }
1239    }
1240}
1241
1242/// Determines how to emit outcomes.
1243/// For compatibility reasons, this can either be true, false or AsClientReports
1244#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1245
1246pub enum EmitOutcomes {
1247    /// Do not emit any outcomes
1248    None,
1249    /// Emit outcomes as client reports
1250    AsClientReports,
1251    /// Emit outcomes as outcomes
1252    AsOutcomes,
1253}
1254
1255impl EmitOutcomes {
1256    /// Returns true of outcomes are emitted via http, kafka, or client reports.
1257    pub fn any(&self) -> bool {
1258        !matches!(self, EmitOutcomes::None)
1259    }
1260}
1261
1262impl Serialize for EmitOutcomes {
1263    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1264    where
1265        S: Serializer,
1266    {
1267        // For compatibility, serialize None and AsOutcomes as booleans.
1268        match self {
1269            Self::None => serializer.serialize_bool(false),
1270            Self::AsClientReports => serializer.serialize_str("as_client_reports"),
1271            Self::AsOutcomes => serializer.serialize_bool(true),
1272        }
1273    }
1274}
1275
1276struct EmitOutcomesVisitor;
1277
1278impl Visitor<'_> for EmitOutcomesVisitor {
1279    type Value = EmitOutcomes;
1280
1281    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1282        formatter.write_str("true, false, or 'as_client_reports'")
1283    }
1284
1285    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
1286    where
1287        E: serde::de::Error,
1288    {
1289        Ok(if v {
1290            EmitOutcomes::AsOutcomes
1291        } else {
1292            EmitOutcomes::None
1293        })
1294    }
1295
1296    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1297    where
1298        E: serde::de::Error,
1299    {
1300        if v == "as_client_reports" {
1301            Ok(EmitOutcomes::AsClientReports)
1302        } else {
1303            Err(E::invalid_value(Unexpected::Str(v), &"as_client_reports"))
1304        }
1305    }
1306}
1307
1308impl<'de> Deserialize<'de> for EmitOutcomes {
1309    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1310    where
1311        D: Deserializer<'de>,
1312    {
1313        deserializer.deserialize_any(EmitOutcomesVisitor)
1314    }
1315}
1316
1317/// Outcome generation specific configuration values.
1318#[derive(Serialize, Deserialize, Debug)]
1319#[serde(default)]
1320pub struct Outcomes {
1321    /// Controls whether outcomes will be emitted when processing is disabled.
1322    /// Processing relays always emit outcomes (for backwards compatibility).
1323    /// Can take the following values: false, "as_client_reports", true
1324    pub emit_outcomes: EmitOutcomes,
1325    /// Controls wheather client reported outcomes should be emitted.
1326    pub emit_client_outcomes: bool,
1327    /// The maximum number of outcomes that are batched before being sent
1328    /// via http to the upstream (only applies to non processing relays).
1329    pub batch_size: usize,
1330    /// The maximum time interval (in milliseconds) that an outcome may be batched
1331    /// via http to the upstream (only applies to non processing relays).
1332    pub batch_interval: u64,
1333    /// Defines the source string registered in the outcomes originating from
1334    /// this Relay (typically something like the region or the layer).
1335    pub source: Option<String>,
1336    /// Configures the outcome aggregator.
1337    pub aggregator: OutcomeAggregatorConfig,
1338}
1339
1340impl Default for Outcomes {
1341    fn default() -> Self {
1342        Outcomes {
1343            emit_outcomes: EmitOutcomes::AsClientReports,
1344            emit_client_outcomes: true,
1345            batch_size: 1000,
1346            batch_interval: 500,
1347            source: None,
1348            aggregator: OutcomeAggregatorConfig::default(),
1349        }
1350    }
1351}
1352
1353/// Minimal version of a config for dumping out.
1354#[derive(Serialize, Deserialize, Debug, Default)]
1355pub struct MinimalConfig {
1356    /// The relay part of the config.
1357    pub relay: Relay,
1358}
1359
1360impl MinimalConfig {
1361    /// Saves the config in the given config folder as config.yml
1362    pub fn save_in_folder<P: AsRef<Path>>(&self, p: P) -> anyhow::Result<()> {
1363        let path = p.as_ref();
1364        if fs::metadata(path).is_err() {
1365            fs::create_dir_all(path)
1366                .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotOpenFile, path))?;
1367        }
1368        self.save(path)
1369    }
1370}
1371
1372impl ConfigObject for MinimalConfig {
1373    fn format() -> ConfigFormat {
1374        ConfigFormat::Yaml
1375    }
1376
1377    fn name() -> &'static str {
1378        "config"
1379    }
1380}
1381
1382/// Alternative serialization of RelayInfo for config file using snake case.
1383mod config_relay_info {
1384    use serde::ser::SerializeMap;
1385
1386    use super::*;
1387
1388    // Uses snake_case as opposed to camelCase.
1389    #[derive(Debug, Serialize, Deserialize, Clone)]
1390    struct RelayInfoConfig {
1391        public_key: PublicKey,
1392        #[serde(default)]
1393        internal: bool,
1394    }
1395
1396    impl From<RelayInfoConfig> for RelayInfo {
1397        fn from(v: RelayInfoConfig) -> Self {
1398            RelayInfo {
1399                public_key: v.public_key,
1400                internal: v.internal,
1401            }
1402        }
1403    }
1404
1405    impl From<RelayInfo> for RelayInfoConfig {
1406        fn from(v: RelayInfo) -> Self {
1407            RelayInfoConfig {
1408                public_key: v.public_key,
1409                internal: v.internal,
1410            }
1411        }
1412    }
1413
1414    pub(super) fn deserialize<'de, D>(des: D) -> Result<HashMap<RelayId, RelayInfo>, D::Error>
1415    where
1416        D: Deserializer<'de>,
1417    {
1418        let map = HashMap::<RelayId, RelayInfoConfig>::deserialize(des)?;
1419        Ok(map.into_iter().map(|(k, v)| (k, v.into())).collect())
1420    }
1421
1422    pub(super) fn serialize<S>(elm: &HashMap<RelayId, RelayInfo>, ser: S) -> Result<S::Ok, S::Error>
1423    where
1424        S: Serializer,
1425    {
1426        let mut map = ser.serialize_map(Some(elm.len()))?;
1427
1428        for (k, v) in elm {
1429            map.serialize_entry(k, &RelayInfoConfig::from(v.clone()))?;
1430        }
1431
1432        map.end()
1433    }
1434}
1435
1436/// Authentication options.
1437#[derive(Serialize, Deserialize, Debug, Default)]
1438pub struct AuthConfig {
1439    /// Controls responses from the readiness health check endpoint based on authentication.
1440    #[serde(default, skip_serializing_if = "is_default")]
1441    pub ready: ReadinessCondition,
1442
1443    /// Statically authenticated downstream relays.
1444    #[serde(default, with = "config_relay_info")]
1445    pub static_relays: HashMap<RelayId, RelayInfo>,
1446}
1447
1448/// GeoIp database configuration options.
1449#[derive(Serialize, Deserialize, Debug, Default)]
1450pub struct GeoIpConfig {
1451    /// The path to GeoIP database.
1452    pub path: Option<PathBuf>,
1453}
1454
1455/// Cardinality Limiter configuration options.
1456#[derive(Serialize, Deserialize, Debug)]
1457#[serde(default)]
1458pub struct CardinalityLimiter {
1459    /// Cache vacuum interval in seconds for the in memory cache.
1460    ///
1461    /// The cache will scan for expired values based on this interval.
1462    ///
1463    /// Defaults to 180 seconds, 3 minutes.
1464    pub cache_vacuum_interval: u64,
1465}
1466
1467impl Default for CardinalityLimiter {
1468    fn default() -> Self {
1469        Self {
1470            cache_vacuum_interval: 180,
1471        }
1472    }
1473}
1474
1475/// Settings to control Relay's health checks.
1476///
1477/// After breaching one of the configured thresholds, Relay will
1478/// return an `unhealthy` status from its health endpoint.
1479#[derive(Serialize, Deserialize, Debug)]
1480#[serde(default)]
1481pub struct Health {
1482    /// Interval to refresh internal health checks.
1483    ///
1484    /// Shorter intervals will decrease the time it takes the health check endpoint to report
1485    /// issues, but can also increase sporadic unhealthy responses.
1486    ///
1487    /// Defaults to `3000`` (3 seconds).
1488    pub refresh_interval_ms: u64,
1489    /// Maximum memory watermark in bytes.
1490    ///
1491    /// By default, there is no absolute limit set and the watermark
1492    /// is only controlled by setting [`Self::max_memory_percent`].
1493    pub max_memory_bytes: Option<ByteSize>,
1494    /// Maximum memory watermark as a percentage of maximum system memory.
1495    ///
1496    /// Defaults to `0.95` (95%).
1497    pub max_memory_percent: f32,
1498    /// Health check probe timeout in milliseconds.
1499    ///
1500    /// Any probe exceeding the timeout will be considered failed.
1501    /// This limits the max execution time of Relay health checks.
1502    ///
1503    /// Defaults to 900 milliseconds.
1504    pub probe_timeout_ms: u64,
1505    /// The refresh frequency of memory stats which are used to poll memory
1506    /// usage of Relay.
1507    ///
1508    /// The implementation of memory stats guarantees that the refresh will happen at
1509    /// least every `x` ms since memory readings are lazy and are updated only if needed.
1510    pub memory_stat_refresh_frequency_ms: u64,
1511}
1512
1513impl Default for Health {
1514    fn default() -> Self {
1515        Self {
1516            refresh_interval_ms: 3000,
1517            max_memory_bytes: None,
1518            max_memory_percent: 0.95,
1519            probe_timeout_ms: 900,
1520            memory_stat_refresh_frequency_ms: 100,
1521        }
1522    }
1523}
1524
1525/// COGS configuration.
1526#[derive(Serialize, Deserialize, Debug)]
1527#[serde(default)]
1528pub struct Cogs {
1529    /// Maximium amount of COGS measurements allowed to backlog.
1530    ///
1531    /// Any additional COGS measurements recorded will be dropped.
1532    ///
1533    /// Defaults to `10_000`.
1534    pub max_queue_size: u64,
1535    /// Relay COGS resource id.
1536    ///
1537    /// All Relay related COGS measurements are emitted with this resource id.
1538    ///
1539    /// Defaults to `relay_service`.
1540    pub relay_resource_id: String,
1541}
1542
1543impl Default for Cogs {
1544    fn default() -> Self {
1545        Self {
1546            max_queue_size: 10_000,
1547            relay_resource_id: "relay_service".to_owned(),
1548        }
1549    }
1550}
1551
1552#[derive(Serialize, Deserialize, Debug, Default)]
1553struct ConfigValues {
1554    #[serde(default)]
1555    relay: Relay,
1556    #[serde(default)]
1557    http: Http,
1558    #[serde(default)]
1559    cache: Cache,
1560    #[serde(default)]
1561    spool: Spool,
1562    #[serde(default)]
1563    limits: Limits,
1564    #[serde(default)]
1565    logging: relay_log::LogConfig,
1566    #[serde(default)]
1567    routing: Routing,
1568    #[serde(default)]
1569    metrics: Metrics,
1570    #[serde(default)]
1571    sentry_metrics: SentryMetrics,
1572    #[serde(default)]
1573    sentry: relay_log::SentryConfig,
1574    #[serde(default)]
1575    processing: Processing,
1576    #[serde(default)]
1577    outcomes: Outcomes,
1578    #[serde(default)]
1579    aggregator: AggregatorServiceConfig,
1580    #[serde(default)]
1581    secondary_aggregators: Vec<ScopedAggregatorConfig>,
1582    #[serde(default)]
1583    auth: AuthConfig,
1584    #[serde(default)]
1585    geoip: GeoIpConfig,
1586    #[serde(default)]
1587    normalization: Normalization,
1588    #[serde(default)]
1589    cardinality_limiter: CardinalityLimiter,
1590    #[serde(default)]
1591    health: Health,
1592    #[serde(default)]
1593    cogs: Cogs,
1594}
1595
1596impl ConfigObject for ConfigValues {
1597    fn format() -> ConfigFormat {
1598        ConfigFormat::Yaml
1599    }
1600
1601    fn name() -> &'static str {
1602        "config"
1603    }
1604}
1605
1606/// Config struct.
1607pub struct Config {
1608    values: ConfigValues,
1609    credentials: Option<Credentials>,
1610    path: PathBuf,
1611}
1612
1613impl fmt::Debug for Config {
1614    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1615        f.debug_struct("Config")
1616            .field("path", &self.path)
1617            .field("values", &self.values)
1618            .finish()
1619    }
1620}
1621
1622impl Config {
1623    /// Loads a config from a given config folder.
1624    pub fn from_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Config> {
1625        let path = env::current_dir()
1626            .map(|x| x.join(path.as_ref()))
1627            .unwrap_or_else(|_| path.as_ref().to_path_buf());
1628
1629        let config = Config {
1630            values: ConfigValues::load(&path)?,
1631            credentials: if Credentials::path(&path).exists() {
1632                Some(Credentials::load(&path)?)
1633            } else {
1634                None
1635            },
1636            path: path.clone(),
1637        };
1638
1639        if cfg!(not(feature = "processing")) && config.processing_enabled() {
1640            return Err(ConfigError::file(ConfigErrorKind::ProcessingNotAvailable, &path).into());
1641        }
1642
1643        Ok(config)
1644    }
1645
1646    /// Creates a config from a JSON value.
1647    ///
1648    /// This is mostly useful for tests.
1649    pub fn from_json_value(value: serde_json::Value) -> anyhow::Result<Config> {
1650        Ok(Config {
1651            values: serde_json::from_value(value)
1652                .with_context(|| ConfigError::new(ConfigErrorKind::BadJson))?,
1653            credentials: None,
1654            path: PathBuf::new(),
1655        })
1656    }
1657
1658    /// Override configuration with values coming from other sources (e.g. env variables or
1659    /// command line parameters)
1660    pub fn apply_override(
1661        &mut self,
1662        mut overrides: OverridableConfig,
1663    ) -> anyhow::Result<&mut Self> {
1664        let relay = &mut self.values.relay;
1665
1666        if let Some(mode) = overrides.mode {
1667            relay.mode = mode
1668                .parse::<RelayMode>()
1669                .with_context(|| ConfigError::field("mode"))?;
1670        }
1671
1672        if let Some(deployment) = overrides.instance {
1673            relay.instance = deployment
1674                .parse::<RelayInstance>()
1675                .with_context(|| ConfigError::field("deployment"))?;
1676        }
1677
1678        if let Some(log_level) = overrides.log_level {
1679            self.values.logging.level = log_level.parse()?;
1680        }
1681
1682        if let Some(log_format) = overrides.log_format {
1683            self.values.logging.format = log_format.parse()?;
1684        }
1685
1686        if let Some(upstream) = overrides.upstream {
1687            relay.upstream = upstream
1688                .parse::<UpstreamDescriptor>()
1689                .with_context(|| ConfigError::field("upstream"))?;
1690        } else if let Some(upstream_dsn) = overrides.upstream_dsn {
1691            relay.upstream = upstream_dsn
1692                .parse::<Dsn>()
1693                .map(|dsn| UpstreamDescriptor::from_dsn(&dsn).into_owned())
1694                .with_context(|| ConfigError::field("upstream_dsn"))?;
1695        }
1696
1697        if let Some(host) = overrides.host {
1698            relay.host = host
1699                .parse::<IpAddr>()
1700                .with_context(|| ConfigError::field("host"))?;
1701        }
1702
1703        if let Some(port) = overrides.port {
1704            relay.port = port
1705                .as_str()
1706                .parse()
1707                .with_context(|| ConfigError::field("port"))?;
1708        }
1709
1710        let processing = &mut self.values.processing;
1711        if let Some(enabled) = overrides.processing {
1712            match enabled.to_lowercase().as_str() {
1713                "true" | "1" => processing.enabled = true,
1714                "false" | "0" | "" => processing.enabled = false,
1715                _ => return Err(ConfigError::field("processing").into()),
1716            }
1717        }
1718
1719        if let Some(redis) = overrides.redis_url {
1720            processing.redis = Some(RedisConfigs::Unified(RedisConfig::single(redis)))
1721        }
1722
1723        if let Some(kafka_url) = overrides.kafka_url {
1724            let existing = processing
1725                .kafka_config
1726                .iter_mut()
1727                .find(|e| e.name == "bootstrap.servers");
1728
1729            if let Some(config_param) = existing {
1730                config_param.value = kafka_url;
1731            } else {
1732                processing.kafka_config.push(KafkaConfigParam {
1733                    name: "bootstrap.servers".to_owned(),
1734                    value: kafka_url,
1735                })
1736            }
1737        }
1738        // credentials overrides
1739        let id = if let Some(id) = overrides.id {
1740            let id = Uuid::parse_str(&id).with_context(|| ConfigError::field("id"))?;
1741            Some(id)
1742        } else {
1743            None
1744        };
1745        let public_key = if let Some(public_key) = overrides.public_key {
1746            let public_key = public_key
1747                .parse::<PublicKey>()
1748                .with_context(|| ConfigError::field("public_key"))?;
1749            Some(public_key)
1750        } else {
1751            None
1752        };
1753
1754        let secret_key = if let Some(secret_key) = overrides.secret_key {
1755            let secret_key = secret_key
1756                .parse::<SecretKey>()
1757                .with_context(|| ConfigError::field("secret_key"))?;
1758            Some(secret_key)
1759        } else {
1760            None
1761        };
1762        let outcomes = &mut self.values.outcomes;
1763        if overrides.outcome_source.is_some() {
1764            outcomes.source = overrides.outcome_source.take();
1765        }
1766
1767        if let Some(credentials) = &mut self.credentials {
1768            //we have existing credentials we may override some entries
1769            if let Some(id) = id {
1770                credentials.id = id;
1771            }
1772            if let Some(public_key) = public_key {
1773                credentials.public_key = public_key;
1774            }
1775            if let Some(secret_key) = secret_key {
1776                credentials.secret_key = secret_key
1777            }
1778        } else {
1779            //no existing credentials we may only create the full credentials
1780            match (id, public_key, secret_key) {
1781                (Some(id), Some(public_key), Some(secret_key)) => {
1782                    self.credentials = Some(Credentials {
1783                        secret_key,
1784                        public_key,
1785                        id,
1786                    })
1787                }
1788                (None, None, None) => {
1789                    // nothing provided, we'll just leave the credentials None, maybe we
1790                    // don't need them in the current command or we'll override them later
1791                }
1792                _ => {
1793                    return Err(ConfigError::field("incomplete credentials").into());
1794                }
1795            }
1796        }
1797
1798        let limits = &mut self.values.limits;
1799        if let Some(shutdown_timeout) = overrides.shutdown_timeout {
1800            if let Ok(shutdown_timeout) = shutdown_timeout.parse::<u64>() {
1801                limits.shutdown_timeout = shutdown_timeout;
1802            }
1803        }
1804
1805        if let Some(server_name) = overrides.server_name {
1806            self.values.sentry.server_name = Some(server_name.into());
1807        }
1808
1809        Ok(self)
1810    }
1811
1812    /// Checks if the config is already initialized.
1813    pub fn config_exists<P: AsRef<Path>>(path: P) -> bool {
1814        fs::metadata(ConfigValues::path(path.as_ref())).is_ok()
1815    }
1816
1817    /// Returns the filename of the config file.
1818    pub fn path(&self) -> &Path {
1819        &self.path
1820    }
1821
1822    /// Dumps out a YAML string of the values.
1823    pub fn to_yaml_string(&self) -> anyhow::Result<String> {
1824        serde_yaml::to_string(&self.values)
1825            .with_context(|| ConfigError::new(ConfigErrorKind::CouldNotWriteFile))
1826    }
1827
1828    /// Regenerates the relay credentials.
1829    ///
1830    /// This also writes the credentials back to the file.
1831    pub fn regenerate_credentials(&mut self, save: bool) -> anyhow::Result<()> {
1832        let creds = Credentials::generate();
1833        if save {
1834            creds.save(&self.path)?;
1835        }
1836        self.credentials = Some(creds);
1837        Ok(())
1838    }
1839
1840    /// Return the current credentials
1841    pub fn credentials(&self) -> Option<&Credentials> {
1842        self.credentials.as_ref()
1843    }
1844
1845    /// Set new credentials.
1846    ///
1847    /// This also writes the credentials back to the file.
1848    pub fn replace_credentials(
1849        &mut self,
1850        credentials: Option<Credentials>,
1851    ) -> anyhow::Result<bool> {
1852        if self.credentials == credentials {
1853            return Ok(false);
1854        }
1855
1856        match credentials {
1857            Some(ref creds) => {
1858                creds.save(&self.path)?;
1859            }
1860            None => {
1861                let path = Credentials::path(&self.path);
1862                if fs::metadata(&path).is_ok() {
1863                    fs::remove_file(&path).with_context(|| {
1864                        ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path)
1865                    })?;
1866                }
1867            }
1868        }
1869
1870        self.credentials = credentials;
1871        Ok(true)
1872    }
1873
1874    /// Returns `true` if the config is ready to use.
1875    pub fn has_credentials(&self) -> bool {
1876        self.credentials.is_some()
1877    }
1878
1879    /// Returns the secret key if set.
1880    pub fn secret_key(&self) -> Option<&SecretKey> {
1881        self.credentials.as_ref().map(|x| &x.secret_key)
1882    }
1883
1884    /// Returns the public key if set.
1885    pub fn public_key(&self) -> Option<&PublicKey> {
1886        self.credentials.as_ref().map(|x| &x.public_key)
1887    }
1888
1889    /// Returns the relay ID.
1890    pub fn relay_id(&self) -> Option<&RelayId> {
1891        self.credentials.as_ref().map(|x| &x.id)
1892    }
1893
1894    /// Returns the relay mode.
1895    pub fn relay_mode(&self) -> RelayMode {
1896        self.values.relay.mode
1897    }
1898
1899    /// Returns the instance type of relay.
1900    pub fn relay_instance(&self) -> RelayInstance {
1901        self.values.relay.instance
1902    }
1903
1904    /// Returns the upstream target as descriptor.
1905    pub fn upstream_descriptor(&self) -> &UpstreamDescriptor<'_> {
1906        &self.values.relay.upstream
1907    }
1908
1909    /// Returns the custom HTTP "Host" header.
1910    pub fn http_host_header(&self) -> Option<&str> {
1911        self.values.http.host_header.as_deref()
1912    }
1913
1914    /// Returns the listen address.
1915    pub fn listen_addr(&self) -> SocketAddr {
1916        (self.values.relay.host, self.values.relay.port).into()
1917    }
1918
1919    /// Returns the TLS listen address.
1920    pub fn tls_listen_addr(&self) -> Option<SocketAddr> {
1921        if self.values.relay.tls_identity_path.is_some() {
1922            let port = self.values.relay.tls_port.unwrap_or(3443);
1923            Some((self.values.relay.host, port).into())
1924        } else {
1925            None
1926        }
1927    }
1928
1929    /// Returns the path to the identity bundle
1930    pub fn tls_identity_path(&self) -> Option<&Path> {
1931        self.values.relay.tls_identity_path.as_deref()
1932    }
1933
1934    /// Returns the password for the identity bundle
1935    pub fn tls_identity_password(&self) -> Option<&str> {
1936        self.values.relay.tls_identity_password.as_deref()
1937    }
1938
1939    /// Returns `true` when project IDs should be overriden rather than validated.
1940    ///
1941    /// Defaults to `false`, which requires project ID validation.
1942    pub fn override_project_ids(&self) -> bool {
1943        self.values.relay.override_project_ids
1944    }
1945
1946    /// Returns `true` if Relay requires authentication for readiness.
1947    ///
1948    /// See [`ReadinessCondition`] for more information.
1949    pub fn requires_auth(&self) -> bool {
1950        match self.values.auth.ready {
1951            ReadinessCondition::Authenticated => self.relay_mode() == RelayMode::Managed,
1952            ReadinessCondition::Always => false,
1953        }
1954    }
1955
1956    /// Returns the interval at which Realy should try to re-authenticate with the upstream.
1957    ///
1958    /// Always disabled in processing mode.
1959    pub fn http_auth_interval(&self) -> Option<Duration> {
1960        if self.processing_enabled() {
1961            return None;
1962        }
1963
1964        match self.values.http.auth_interval {
1965            None | Some(0) => None,
1966            Some(secs) => Some(Duration::from_secs(secs)),
1967        }
1968    }
1969
1970    /// The maximum time of experiencing uninterrupted network failures until Relay considers that
1971    /// it has encountered a network outage.
1972    pub fn http_outage_grace_period(&self) -> Duration {
1973        Duration::from_secs(self.values.http.outage_grace_period)
1974    }
1975
1976    /// Time Relay waits before retrying an upstream request.
1977    ///
1978    /// Before going into a network outage, Relay may fail to make upstream
1979    /// requests. This is the time Relay waits before retrying the same request.
1980    pub fn http_retry_delay(&self) -> Duration {
1981        Duration::from_secs(self.values.http.retry_delay)
1982    }
1983
1984    /// Time of continued project request failures before Relay emits an error.
1985    pub fn http_project_failure_interval(&self) -> Duration {
1986        Duration::from_secs(self.values.http.project_failure_interval)
1987    }
1988
1989    /// Content encoding of upstream requests.
1990    pub fn http_encoding(&self) -> HttpEncoding {
1991        self.values.http.encoding
1992    }
1993
1994    /// Returns whether metrics should be sent globally through a shared endpoint.
1995    pub fn http_global_metrics(&self) -> bool {
1996        self.values.http.global_metrics
1997    }
1998
1999    /// Returns whether this Relay should emit outcomes.
2000    ///
2001    /// This is `true` either if `outcomes.emit_outcomes` is explicitly enabled, or if this Relay is
2002    /// in processing mode.
2003    pub fn emit_outcomes(&self) -> EmitOutcomes {
2004        if self.processing_enabled() {
2005            return EmitOutcomes::AsOutcomes;
2006        }
2007        self.values.outcomes.emit_outcomes
2008    }
2009
2010    /// Returns whether this Relay should emit client outcomes
2011    ///
2012    /// Relays that do not emit client outcomes will forward client recieved outcomes
2013    /// directly to the next relay in the chain as client report envelope.  This is only done
2014    /// if this relay emits outcomes at all. A relay that will not emit outcomes
2015    /// will forward the envelope unchanged.
2016    ///
2017    /// This flag can be explicitly disabled on processing relays as well to prevent the
2018    /// emitting of client outcomes to the kafka topic.
2019    pub fn emit_client_outcomes(&self) -> bool {
2020        self.values.outcomes.emit_client_outcomes
2021    }
2022
2023    /// Returns the maximum number of outcomes that are batched before being sent
2024    pub fn outcome_batch_size(&self) -> usize {
2025        self.values.outcomes.batch_size
2026    }
2027
2028    /// Returns the maximum interval that an outcome may be batched
2029    pub fn outcome_batch_interval(&self) -> Duration {
2030        Duration::from_millis(self.values.outcomes.batch_interval)
2031    }
2032
2033    /// The originating source of the outcome
2034    pub fn outcome_source(&self) -> Option<&str> {
2035        self.values.outcomes.source.as_deref()
2036    }
2037
2038    /// Returns the width of the buckets into which outcomes are aggregated, in seconds.
2039    pub fn outcome_aggregator(&self) -> &OutcomeAggregatorConfig {
2040        &self.values.outcomes.aggregator
2041    }
2042
2043    /// Returns logging configuration.
2044    pub fn logging(&self) -> &relay_log::LogConfig {
2045        &self.values.logging
2046    }
2047
2048    /// Returns logging configuration.
2049    pub fn sentry(&self) -> &relay_log::SentryConfig {
2050        &self.values.sentry
2051    }
2052
2053    /// Returns the socket addresses for statsd.
2054    ///
2055    /// If stats is disabled an empty vector is returned.
2056    pub fn statsd_addrs(&self) -> anyhow::Result<Vec<SocketAddr>> {
2057        if let Some(ref addr) = self.values.metrics.statsd {
2058            let addrs = addr
2059                .as_str()
2060                .to_socket_addrs()
2061                .with_context(|| ConfigError::file(ConfigErrorKind::InvalidValue, &self.path))?
2062                .collect();
2063            Ok(addrs)
2064        } else {
2065            Ok(vec![])
2066        }
2067    }
2068
2069    /// Return the prefix for statsd metrics.
2070    pub fn metrics_prefix(&self) -> &str {
2071        &self.values.metrics.prefix
2072    }
2073
2074    /// Returns the default tags for statsd metrics.
2075    pub fn metrics_default_tags(&self) -> &BTreeMap<String, String> {
2076        &self.values.metrics.default_tags
2077    }
2078
2079    /// Returns the name of the hostname tag that should be attached to each outgoing metric.
2080    pub fn metrics_hostname_tag(&self) -> Option<&str> {
2081        self.values.metrics.hostname_tag.as_deref()
2082    }
2083
2084    /// Returns the global sample rate for all metrics.
2085    pub fn metrics_sample_rate(&self) -> f32 {
2086        self.values.metrics.sample_rate
2087    }
2088
2089    /// Returns whether local metric aggregation should be enabled.
2090    pub fn metrics_aggregate(&self) -> bool {
2091        self.values.metrics.aggregate
2092    }
2093
2094    /// Returns the interval for periodic metrics emitted from Relay.
2095    ///
2096    /// `None` if periodic metrics are disabled.
2097    pub fn metrics_periodic_interval(&self) -> Option<Duration> {
2098        match self.values.metrics.periodic_secs {
2099            0 => None,
2100            secs => Some(Duration::from_secs(secs)),
2101        }
2102    }
2103
2104    /// Returns the default timeout for all upstream HTTP requests.
2105    pub fn http_timeout(&self) -> Duration {
2106        Duration::from_secs(self.values.http.timeout.into())
2107    }
2108
2109    /// Returns the connection timeout for all upstream HTTP requests.
2110    pub fn http_connection_timeout(&self) -> Duration {
2111        Duration::from_secs(self.values.http.connection_timeout.into())
2112    }
2113
2114    /// Returns the failed upstream request retry interval.
2115    pub fn http_max_retry_interval(&self) -> Duration {
2116        Duration::from_secs(self.values.http.max_retry_interval.into())
2117    }
2118
2119    /// Returns the expiry timeout for cached projects.
2120    pub fn project_cache_expiry(&self) -> Duration {
2121        Duration::from_secs(self.values.cache.project_expiry.into())
2122    }
2123
2124    /// Returns `true` if the full project state should be requested from upstream.
2125    pub fn request_full_project_config(&self) -> bool {
2126        self.values.cache.project_request_full_config
2127    }
2128
2129    /// Returns the expiry timeout for cached relay infos (public keys).
2130    pub fn relay_cache_expiry(&self) -> Duration {
2131        Duration::from_secs(self.values.cache.relay_expiry.into())
2132    }
2133
2134    /// Returns the maximum number of buffered envelopes
2135    pub fn envelope_buffer_size(&self) -> usize {
2136        self.values
2137            .cache
2138            .envelope_buffer_size
2139            .try_into()
2140            .unwrap_or(usize::MAX)
2141    }
2142
2143    /// Returns the expiry timeout for cached misses before trying to refetch.
2144    pub fn cache_miss_expiry(&self) -> Duration {
2145        Duration::from_secs(self.values.cache.miss_expiry.into())
2146    }
2147
2148    /// Returns the grace period for project caches.
2149    pub fn project_grace_period(&self) -> Duration {
2150        Duration::from_secs(self.values.cache.project_grace_period.into())
2151    }
2152
2153    /// Returns the refresh interval for a project.
2154    ///
2155    /// Validates the refresh time to be between the grace period and expiry.
2156    pub fn project_refresh_interval(&self) -> Option<Duration> {
2157        self.values
2158            .cache
2159            .project_refresh_interval
2160            .map(Into::into)
2161            .map(Duration::from_secs)
2162    }
2163
2164    /// Returns the duration in which batchable project config queries are
2165    /// collected before sending them in a single request.
2166    pub fn query_batch_interval(&self) -> Duration {
2167        Duration::from_millis(self.values.cache.batch_interval.into())
2168    }
2169
2170    /// Returns the duration in which downstream relays are requested from upstream.
2171    pub fn downstream_relays_batch_interval(&self) -> Duration {
2172        Duration::from_millis(self.values.cache.downstream_relays_batch_interval.into())
2173    }
2174
2175    /// Returns the interval in seconds in which local project configurations should be reloaded.
2176    pub fn local_cache_interval(&self) -> Duration {
2177        Duration::from_secs(self.values.cache.file_interval.into())
2178    }
2179
2180    /// Returns the interval in seconds in which fresh global configs should be
2181    /// fetched from  upstream.
2182    pub fn global_config_fetch_interval(&self) -> Duration {
2183        Duration::from_secs(self.values.cache.global_config_fetch_interval.into())
2184    }
2185
2186    /// Returns the path of the buffer file if the `cache.persistent_envelope_buffer.path` is configured.
2187    ///
2188    /// In case a partition with id > 0 is supplied, the filename of the envelopes path will be
2189    /// suffixed with `.{partition_id}`.
2190    pub fn spool_envelopes_path(&self, partition_id: u8) -> Option<PathBuf> {
2191        let mut path = self
2192            .values
2193            .spool
2194            .envelopes
2195            .path
2196            .as_ref()
2197            .map(|path| path.to_owned())?;
2198
2199        if partition_id == 0 {
2200            return Some(path);
2201        }
2202
2203        let file_name = path.file_name().and_then(|f| f.to_str())?;
2204        let new_file_name = format!("{}.{}", file_name, partition_id);
2205        path.set_file_name(new_file_name);
2206
2207        Some(path)
2208    }
2209
2210    /// The maximum size of the buffer, in bytes.
2211    pub fn spool_envelopes_max_disk_size(&self) -> usize {
2212        self.values.spool.envelopes.max_disk_size.as_bytes()
2213    }
2214
2215    /// Number of encoded envelope bytes that need to be accumulated before
2216    /// flushing one batch to disk.
2217    pub fn spool_envelopes_batch_size_bytes(&self) -> usize {
2218        self.values.spool.envelopes.batch_size_bytes.as_bytes()
2219    }
2220
2221    /// Returns the time after which we drop envelopes as a [`Duration`] object.
2222    pub fn spool_envelopes_max_age(&self) -> Duration {
2223        Duration::from_secs(self.values.spool.envelopes.max_envelope_delay_secs)
2224    }
2225
2226    /// Returns the refresh frequency for disk usage monitoring as a [`Duration`] object.
2227    pub fn spool_disk_usage_refresh_frequency_ms(&self) -> Duration {
2228        Duration::from_millis(self.values.spool.envelopes.disk_usage_refresh_frequency_ms)
2229    }
2230
2231    /// Returns the maximum number of envelopes that can be put in the bounded buffer.
2232    pub fn spool_max_backpressure_envelopes(&self) -> usize {
2233        self.values.spool.envelopes.max_backpressure_envelopes
2234    }
2235
2236    /// Returns the relative memory usage up to which the disk buffer will unspool envelopes.
2237    pub fn spool_max_backpressure_memory_percent(&self) -> f32 {
2238        self.values.spool.envelopes.max_backpressure_memory_percent
2239    }
2240
2241    /// Returns the number of partitions for the buffer.
2242    pub fn spool_partitions(&self) -> NonZeroU8 {
2243        self.values.spool.envelopes.partitions
2244    }
2245
2246    /// Returns the maximum size of an event payload in bytes.
2247    pub fn max_event_size(&self) -> usize {
2248        self.values.limits.max_event_size.as_bytes()
2249    }
2250
2251    /// Returns the maximum size of each attachment.
2252    pub fn max_attachment_size(&self) -> usize {
2253        self.values.limits.max_attachment_size.as_bytes()
2254    }
2255
2256    /// Returns the maximum combined size of attachments or payloads containing attachments
2257    /// (minidump, unreal, standalone attachments) in bytes.
2258    pub fn max_attachments_size(&self) -> usize {
2259        self.values.limits.max_attachments_size.as_bytes()
2260    }
2261
2262    /// Returns the maximum combined size of client reports in bytes.
2263    pub fn max_client_reports_size(&self) -> usize {
2264        self.values.limits.max_client_reports_size.as_bytes()
2265    }
2266
2267    /// Returns the maximum payload size of a monitor check-in in bytes.
2268    pub fn max_check_in_size(&self) -> usize {
2269        self.values.limits.max_check_in_size.as_bytes()
2270    }
2271
2272    /// Returns the maximum payload size of a log in bytes.
2273    pub fn max_log_size(&self) -> usize {
2274        self.values.limits.max_log_size.as_bytes()
2275    }
2276
2277    /// Returns the maximum payload size of a span in bytes.
2278    pub fn max_span_size(&self) -> usize {
2279        self.values.limits.max_span_size.as_bytes()
2280    }
2281
2282    /// Returns the maximum size of an envelope payload in bytes.
2283    ///
2284    /// Individual item size limits still apply.
2285    pub fn max_envelope_size(&self) -> usize {
2286        self.values.limits.max_envelope_size.as_bytes()
2287    }
2288
2289    /// Returns the maximum number of sessions per envelope.
2290    pub fn max_session_count(&self) -> usize {
2291        self.values.limits.max_session_count
2292    }
2293
2294    /// Returns the maximum payload size of a statsd metric in bytes.
2295    pub fn max_statsd_size(&self) -> usize {
2296        self.values.limits.max_statsd_size.as_bytes()
2297    }
2298
2299    /// Returns the maximum payload size of metric buckets in bytes.
2300    pub fn max_metric_buckets_size(&self) -> usize {
2301        self.values.limits.max_metric_buckets_size.as_bytes()
2302    }
2303
2304    /// Whether metric stats are collected and emitted.
2305    ///
2306    /// Metric stats are always collected and emitted when processing
2307    /// is enabled.
2308    pub fn metric_stats_enabled(&self) -> bool {
2309        self.values.sentry_metrics.metric_stats_enabled || self.values.processing.enabled
2310    }
2311
2312    /// Returns the maximum payload size for general API requests.
2313    pub fn max_api_payload_size(&self) -> usize {
2314        self.values.limits.max_api_payload_size.as_bytes()
2315    }
2316
2317    /// Returns the maximum payload size for file uploads and chunks.
2318    pub fn max_api_file_upload_size(&self) -> usize {
2319        self.values.limits.max_api_file_upload_size.as_bytes()
2320    }
2321
2322    /// Returns the maximum payload size for chunks
2323    pub fn max_api_chunk_upload_size(&self) -> usize {
2324        self.values.limits.max_api_chunk_upload_size.as_bytes()
2325    }
2326
2327    /// Returns the maximum payload size for a profile
2328    pub fn max_profile_size(&self) -> usize {
2329        self.values.limits.max_profile_size.as_bytes()
2330    }
2331
2332    /// Returns the maximum payload size for a compressed replay.
2333    pub fn max_replay_compressed_size(&self) -> usize {
2334        self.values.limits.max_replay_compressed_size.as_bytes()
2335    }
2336
2337    /// Returns the maximum payload size for an uncompressed replay.
2338    pub fn max_replay_uncompressed_size(&self) -> usize {
2339        self.values.limits.max_replay_uncompressed_size.as_bytes()
2340    }
2341
2342    /// Returns the maximum message size for an uncompressed replay.
2343    ///
2344    /// This is greater than max_replay_compressed_size because
2345    /// it can include additional metadata about the replay in
2346    /// addition to the recording.
2347    pub fn max_replay_message_size(&self) -> usize {
2348        self.values.limits.max_replay_message_size.as_bytes()
2349    }
2350
2351    /// Returns the maximum number of active requests
2352    pub fn max_concurrent_requests(&self) -> usize {
2353        self.values.limits.max_concurrent_requests
2354    }
2355
2356    /// Returns the maximum number of active queries
2357    pub fn max_concurrent_queries(&self) -> usize {
2358        self.values.limits.max_concurrent_queries
2359    }
2360
2361    /// The maximum number of seconds a query is allowed to take across retries.
2362    pub fn query_timeout(&self) -> Duration {
2363        Duration::from_secs(self.values.limits.query_timeout)
2364    }
2365
2366    /// The maximum number of seconds to wait for pending envelopes after receiving a shutdown
2367    /// signal.
2368    pub fn shutdown_timeout(&self) -> Duration {
2369        Duration::from_secs(self.values.limits.shutdown_timeout)
2370    }
2371
2372    /// Returns the server keep-alive timeout in seconds.
2373    ///
2374    /// By default keep alive is set to a 5 seconds.
2375    pub fn keepalive_timeout(&self) -> Duration {
2376        Duration::from_secs(self.values.limits.keepalive_timeout)
2377    }
2378
2379    /// Returns the server idle timeout in seconds.
2380    pub fn idle_timeout(&self) -> Option<Duration> {
2381        self.values.limits.idle_timeout.map(Duration::from_secs)
2382    }
2383
2384    /// Returns the maximum connections.
2385    pub fn max_connections(&self) -> Option<usize> {
2386        self.values.limits.max_connections
2387    }
2388
2389    /// TCP listen backlog to configure on Relay's listening socket.
2390    pub fn tcp_listen_backlog(&self) -> u32 {
2391        self.values.limits.tcp_listen_backlog
2392    }
2393
2394    /// Returns the number of cores to use for thread pools.
2395    pub fn cpu_concurrency(&self) -> usize {
2396        self.values.limits.max_thread_count
2397    }
2398
2399    /// Returns the number of tasks that can run concurrently in the worker pool.
2400    pub fn pool_concurrency(&self) -> usize {
2401        self.values.limits.max_pool_concurrency
2402    }
2403
2404    /// Returns the maximum size of a project config query.
2405    pub fn query_batch_size(&self) -> usize {
2406        self.values.cache.batch_size
2407    }
2408
2409    /// Get filename for static project config.
2410    pub fn project_configs_path(&self) -> PathBuf {
2411        self.path.join("projects")
2412    }
2413
2414    /// True if the Relay should do processing.
2415    pub fn processing_enabled(&self) -> bool {
2416        self.values.processing.enabled
2417    }
2418
2419    /// Level of normalization for Relay to apply to incoming data.
2420    pub fn normalization_level(&self) -> NormalizationLevel {
2421        self.values.normalization.level
2422    }
2423
2424    /// The path to the GeoIp database required for event processing.
2425    pub fn geoip_path(&self) -> Option<&Path> {
2426        self.values
2427            .geoip
2428            .path
2429            .as_deref()
2430            .or(self.values.processing.geoip_path.as_deref())
2431    }
2432
2433    /// Maximum future timestamp of ingested data.
2434    ///
2435    /// Events past this timestamp will be adjusted to `now()`. Sessions will be dropped.
2436    pub fn max_secs_in_future(&self) -> i64 {
2437        self.values.processing.max_secs_in_future.into()
2438    }
2439
2440    /// Maximum age of ingested sessions. Older sessions will be dropped.
2441    pub fn max_session_secs_in_past(&self) -> i64 {
2442        self.values.processing.max_session_secs_in_past.into()
2443    }
2444
2445    /// Configuration name and list of Kafka configuration parameters for a given topic.
2446    pub fn kafka_config(&self, topic: KafkaTopic) -> Result<KafkaParams, KafkaConfigError> {
2447        self.values.processing.topics.get(topic).kafka_config(
2448            &self.values.processing.kafka_config,
2449            &self.values.processing.secondary_kafka_configs,
2450        )
2451    }
2452
2453    /// Whether to validate the topics against Kafka.
2454    pub fn kafka_validate_topics(&self) -> bool {
2455        self.values.processing.kafka_validate_topics
2456    }
2457
2458    /// All unused but configured topic assignments.
2459    pub fn unused_topic_assignments(&self) -> &BTreeMap<String, TopicAssignment> {
2460        &self.values.processing.topics.unused
2461    }
2462
2463    /// Redis servers to connect to for project configs, cardinality limits,
2464    /// rate limiting, and metrics metadata.
2465    pub fn redis(&self) -> Option<RedisConfigsRef> {
2466        let redis_configs = self.values.processing.redis.as_ref()?;
2467
2468        Some(build_redis_configs(
2469            redis_configs,
2470            self.cpu_concurrency() as u32,
2471        ))
2472    }
2473
2474    /// Chunk size of attachments in bytes.
2475    pub fn attachment_chunk_size(&self) -> usize {
2476        self.values.processing.attachment_chunk_size.as_bytes()
2477    }
2478
2479    /// Maximum metrics batch size in bytes.
2480    pub fn metrics_max_batch_size_bytes(&self) -> usize {
2481        self.values.aggregator.max_flush_bytes
2482    }
2483
2484    /// Default prefix to use when looking up project configs in Redis. This is only done when
2485    /// Relay is in processing mode.
2486    pub fn projectconfig_cache_prefix(&self) -> &str {
2487        &self.values.processing.projectconfig_cache_prefix
2488    }
2489
2490    /// Maximum rate limit to report to clients in seconds.
2491    pub fn max_rate_limit(&self) -> Option<u64> {
2492        self.values.processing.max_rate_limit.map(u32::into)
2493    }
2494
2495    /// Cache vacuum interval for the cardinality limiter in memory cache.
2496    ///
2497    /// The cache will scan for expired values based on this interval.
2498    pub fn cardinality_limiter_cache_vacuum_interval(&self) -> Duration {
2499        Duration::from_secs(self.values.cardinality_limiter.cache_vacuum_interval)
2500    }
2501
2502    /// Interval to refresh internal health checks.
2503    pub fn health_refresh_interval(&self) -> Duration {
2504        Duration::from_millis(self.values.health.refresh_interval_ms)
2505    }
2506
2507    /// Maximum memory watermark in bytes.
2508    pub fn health_max_memory_watermark_bytes(&self) -> u64 {
2509        self.values
2510            .health
2511            .max_memory_bytes
2512            .as_ref()
2513            .map_or(u64::MAX, |b| b.as_bytes() as u64)
2514    }
2515
2516    /// Maximum memory watermark as a percentage of maximum system memory.
2517    pub fn health_max_memory_watermark_percent(&self) -> f32 {
2518        self.values.health.max_memory_percent
2519    }
2520
2521    /// Health check probe timeout.
2522    pub fn health_probe_timeout(&self) -> Duration {
2523        Duration::from_millis(self.values.health.probe_timeout_ms)
2524    }
2525
2526    /// Refresh frequency for polling new memory stats.
2527    pub fn memory_stat_refresh_frequency_ms(&self) -> u64 {
2528        self.values.health.memory_stat_refresh_frequency_ms
2529    }
2530
2531    /// Maximum amount of COGS measurements buffered in memory.
2532    pub fn cogs_max_queue_size(&self) -> u64 {
2533        self.values.cogs.max_queue_size
2534    }
2535
2536    /// Resource ID to use for Relay COGS measurements.
2537    pub fn cogs_relay_resource_id(&self) -> &str {
2538        &self.values.cogs.relay_resource_id
2539    }
2540
2541    /// Returns configuration for the default metrics aggregator.
2542    pub fn default_aggregator_config(&self) -> &AggregatorServiceConfig {
2543        &self.values.aggregator
2544    }
2545
2546    /// Returns configuration for non-default metrics aggregator.
2547    pub fn secondary_aggregator_configs(&self) -> &Vec<ScopedAggregatorConfig> {
2548        &self.values.secondary_aggregators
2549    }
2550
2551    /// Returns aggregator config for a given metrics namespace.
2552    pub fn aggregator_config_for(&self, namespace: MetricNamespace) -> &AggregatorServiceConfig {
2553        for entry in &self.values.secondary_aggregators {
2554            if entry.condition.matches(Some(namespace)) {
2555                return &entry.config;
2556            }
2557        }
2558        &self.values.aggregator
2559    }
2560
2561    /// Return the statically configured Relays.
2562    pub fn static_relays(&self) -> &HashMap<RelayId, RelayInfo> {
2563        &self.values.auth.static_relays
2564    }
2565
2566    /// Returns `true` if unknown items should be accepted and forwarded.
2567    pub fn accept_unknown_items(&self) -> bool {
2568        let forward = self.values.routing.accept_unknown_items;
2569        forward.unwrap_or_else(|| !self.processing_enabled())
2570    }
2571}
2572
2573impl Default for Config {
2574    fn default() -> Self {
2575        Self {
2576            values: ConfigValues::default(),
2577            credentials: None,
2578            path: PathBuf::new(),
2579        }
2580    }
2581}
2582
2583#[cfg(test)]
2584mod tests {
2585
2586    use super::*;
2587
2588    /// Regression test for renaming the envelope buffer flags.
2589    #[test]
2590    fn test_event_buffer_size() {
2591        let yaml = r###"
2592cache:
2593    event_buffer_size: 1000000
2594    event_expiry: 1800
2595"###;
2596
2597        let values: ConfigValues = serde_yaml::from_str(yaml).unwrap();
2598        assert_eq!(values.cache.envelope_buffer_size, 1_000_000);
2599        assert_eq!(values.cache.envelope_expiry, 1800);
2600    }
2601
2602    #[test]
2603    fn test_emit_outcomes() {
2604        for (serialized, deserialized) in &[
2605            ("true", EmitOutcomes::AsOutcomes),
2606            ("false", EmitOutcomes::None),
2607            ("\"as_client_reports\"", EmitOutcomes::AsClientReports),
2608        ] {
2609            let value: EmitOutcomes = serde_json::from_str(serialized).unwrap();
2610            assert_eq!(value, *deserialized);
2611            assert_eq!(serde_json::to_string(&value).unwrap(), *serialized);
2612        }
2613    }
2614
2615    #[test]
2616    fn test_emit_outcomes_invalid() {
2617        assert!(serde_json::from_str::<EmitOutcomes>("asdf").is_err());
2618    }
2619}