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