1use std::collections::{BTreeMap, HashMap};
2use std::error::Error;
3use std::io::Write;
4use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
5use std::num::NonZeroU8;
6use std::path::{Path, PathBuf};
7use std::str::FromStr;
8use std::time::Duration;
9use std::{env, fmt, fs, io};
10
11use anyhow::Context;
12use relay_auth::{PublicKey, RelayId, SecretKey, generate_key_pair, generate_relay_id};
13use relay_common::Dsn;
14use relay_kafka::{
15 ConfigError as KafkaConfigError, KafkaConfigParam, KafkaTopic, KafkaTopicConfig,
16 TopicAssignments,
17};
18use relay_metrics::MetricNamespace;
19use serde::de::{DeserializeOwned, Unexpected, Visitor};
20use serde::{Deserialize, Deserializer, Serialize, Serializer};
21use uuid::Uuid;
22
23use crate::aggregator::{AggregatorServiceConfig, ScopedAggregatorConfig};
24use crate::byte_size::ByteSize;
25use crate::upstream::UpstreamDescriptor;
26use crate::{RedisConfig, RedisConfigs, RedisConfigsRef, build_redis_configs};
27
28const DEFAULT_NETWORK_OUTAGE_GRACE_PERIOD: u64 = 10;
29
30static CONFIG_YAML_HEADER: &str = r###"# Please see the relevant documentation.
31# Performance tuning: https://docs.sentry.io/product/relay/operating-guidelines/
32# All config options: https://docs.sentry.io/product/relay/options/
33"###;
34
35#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
37#[non_exhaustive]
38pub enum ConfigErrorKind {
39 CouldNotOpenFile,
41 CouldNotWriteFile,
43 BadYaml,
45 BadJson,
47 InvalidValue,
49 ProcessingNotAvailable,
52}
53
54impl fmt::Display for ConfigErrorKind {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 match self {
57 Self::CouldNotOpenFile => write!(f, "could not open config file"),
58 Self::CouldNotWriteFile => write!(f, "could not write config file"),
59 Self::BadYaml => write!(f, "could not parse yaml config file"),
60 Self::BadJson => write!(f, "could not parse json config file"),
61 Self::InvalidValue => write!(f, "invalid config value"),
62 Self::ProcessingNotAvailable => write!(
63 f,
64 "was not compiled with processing, cannot enable processing"
65 ),
66 }
67 }
68}
69
70#[derive(Debug)]
72enum ConfigErrorSource {
73 None,
75 File(PathBuf),
77 FieldOverride(String),
79}
80
81impl Default for ConfigErrorSource {
82 fn default() -> Self {
83 Self::None
84 }
85}
86
87impl fmt::Display for ConfigErrorSource {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 match self {
90 ConfigErrorSource::None => Ok(()),
91 ConfigErrorSource::File(file_name) => {
92 write!(f, " (file {})", file_name.display())
93 }
94 ConfigErrorSource::FieldOverride(name) => write!(f, " (field {name})"),
95 }
96 }
97}
98
99#[derive(Debug)]
101pub struct ConfigError {
102 source: ConfigErrorSource,
103 kind: ConfigErrorKind,
104}
105
106impl ConfigError {
107 #[inline]
108 fn new(kind: ConfigErrorKind) -> Self {
109 Self {
110 source: ConfigErrorSource::None,
111 kind,
112 }
113 }
114
115 #[inline]
116 fn field(field: &'static str) -> Self {
117 Self {
118 source: ConfigErrorSource::FieldOverride(field.to_owned()),
119 kind: ConfigErrorKind::InvalidValue,
120 }
121 }
122
123 #[inline]
124 fn file(kind: ConfigErrorKind, p: impl AsRef<Path>) -> Self {
125 Self {
126 source: ConfigErrorSource::File(p.as_ref().to_path_buf()),
127 kind,
128 }
129 }
130
131 pub fn kind(&self) -> ConfigErrorKind {
133 self.kind
134 }
135}
136
137impl fmt::Display for ConfigError {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 write!(f, "{}{}", self.kind(), self.source)
140 }
141}
142
143impl Error for ConfigError {}
144
145enum ConfigFormat {
146 Yaml,
147 Json,
148}
149
150impl ConfigFormat {
151 pub fn extension(&self) -> &'static str {
152 match self {
153 ConfigFormat::Yaml => "yml",
154 ConfigFormat::Json => "json",
155 }
156 }
157}
158
159trait ConfigObject: DeserializeOwned + Serialize {
160 fn format() -> ConfigFormat;
162
163 fn name() -> &'static str;
165
166 fn path(base: &Path) -> PathBuf {
168 base.join(format!("{}.{}", Self::name(), Self::format().extension()))
169 }
170
171 fn load(base: &Path) -> anyhow::Result<Self> {
173 let path = Self::path(base);
174
175 let f = fs::File::open(&path)
176 .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotOpenFile, &path))?;
177 let f = io::BufReader::new(f);
178
179 let mut source = serde_vars::EnvSource::default();
180 match Self::format() {
181 ConfigFormat::Yaml => {
182 serde_vars::deserialize(serde_yaml::Deserializer::from_reader(f), &mut source)
183 .with_context(|| ConfigError::file(ConfigErrorKind::BadYaml, &path))
184 }
185 ConfigFormat::Json => {
186 serde_vars::deserialize(&mut serde_json::Deserializer::from_reader(f), &mut source)
187 .with_context(|| ConfigError::file(ConfigErrorKind::BadJson, &path))
188 }
189 }
190 }
191
192 fn save(&self, base: &Path) -> anyhow::Result<()> {
194 let path = Self::path(base);
195 let mut options = fs::OpenOptions::new();
196 options.write(true).truncate(true).create(true);
197
198 #[cfg(unix)]
200 {
201 use std::os::unix::fs::OpenOptionsExt;
202 options.mode(0o600);
203 }
204
205 let mut f = options
206 .open(&path)
207 .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?;
208
209 match Self::format() {
210 ConfigFormat::Yaml => {
211 f.write_all(CONFIG_YAML_HEADER.as_bytes())?;
212 serde_yaml::to_writer(&mut f, self)
213 .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?
214 }
215 ConfigFormat::Json => serde_json::to_writer_pretty(&mut f, self)
216 .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path))?,
217 }
218
219 f.write_all(b"\n").ok();
220
221 Ok(())
222 }
223}
224
225#[derive(Debug, Default)]
228pub struct OverridableConfig {
229 pub mode: Option<String>,
231 pub instance: Option<String>,
233 pub log_level: Option<String>,
235 pub log_format: Option<String>,
237 pub upstream: Option<String>,
239 pub upstream_dsn: Option<String>,
241 pub host: Option<String>,
243 pub port: Option<String>,
245 pub processing: Option<String>,
247 pub kafka_url: Option<String>,
249 pub redis_url: Option<String>,
251 pub id: Option<String>,
253 pub secret_key: Option<String>,
255 pub public_key: Option<String>,
257 pub outcome_source: Option<String>,
259 pub shutdown_timeout: Option<String>,
261 pub server_name: Option<String>,
263}
264
265#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
267pub struct Credentials {
268 pub secret_key: SecretKey,
270 pub public_key: PublicKey,
272 pub id: RelayId,
274}
275
276impl Credentials {
277 pub fn generate() -> Self {
279 relay_log::info!("generating new relay credentials");
280 let (sk, pk) = generate_key_pair();
281 Self {
282 secret_key: sk,
283 public_key: pk,
284 id: generate_relay_id(),
285 }
286 }
287
288 pub fn to_json_string(&self) -> anyhow::Result<String> {
290 serde_json::to_string(self)
291 .with_context(|| ConfigError::new(ConfigErrorKind::CouldNotWriteFile))
292 }
293}
294
295impl ConfigObject for Credentials {
296 fn format() -> ConfigFormat {
297 ConfigFormat::Json
298 }
299 fn name() -> &'static str {
300 "credentials"
301 }
302}
303
304#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
306#[serde(rename_all = "camelCase")]
307pub struct RelayInfo {
308 pub public_key: PublicKey,
310
311 #[serde(default)]
313 pub internal: bool,
314}
315
316impl RelayInfo {
317 pub fn new(public_key: PublicKey) -> Self {
319 Self {
320 public_key,
321 internal: false,
322 }
323 }
324}
325
326#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
328#[serde(rename_all = "camelCase")]
329pub enum RelayMode {
330 Proxy,
336
337 Managed,
343}
344
345impl<'de> Deserialize<'de> for RelayMode {
346 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
347 where
348 D: Deserializer<'de>,
349 {
350 let s = String::deserialize(deserializer)?;
351 match s.as_str() {
352 "proxy" => Ok(RelayMode::Proxy),
353 "managed" => Ok(RelayMode::Managed),
354 "static" => Err(serde::de::Error::custom(
355 "Relay mode 'static' has been removed. Please use 'managed' or 'proxy' instead.",
356 )),
357 other => Err(serde::de::Error::unknown_variant(
358 other,
359 &["proxy", "managed"],
360 )),
361 }
362 }
363}
364
365impl fmt::Display for RelayMode {
366 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
367 match self {
368 RelayMode::Proxy => write!(f, "proxy"),
369 RelayMode::Managed => write!(f, "managed"),
370 }
371 }
372}
373
374#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
376#[serde(rename_all = "camelCase")]
377pub enum RelayInstance {
378 Default,
380
381 Canary,
383}
384
385impl RelayInstance {
386 pub fn is_canary(&self) -> bool {
388 matches!(self, RelayInstance::Canary)
389 }
390}
391
392impl fmt::Display for RelayInstance {
393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394 match self {
395 RelayInstance::Default => write!(f, "default"),
396 RelayInstance::Canary => write!(f, "canary"),
397 }
398 }
399}
400
401impl FromStr for RelayInstance {
402 type Err = fmt::Error;
403
404 fn from_str(s: &str) -> Result<Self, Self::Err> {
405 match s {
406 "canary" => Ok(RelayInstance::Canary),
407 _ => Ok(RelayInstance::Default),
408 }
409 }
410}
411
412#[derive(Clone, Copy, Debug, Eq, PartialEq)]
414pub struct ParseRelayModeError;
415
416impl fmt::Display for ParseRelayModeError {
417 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
418 write!(f, "Relay mode must be one of: managed or proxy")
419 }
420}
421
422impl Error for ParseRelayModeError {}
423
424impl FromStr for RelayMode {
425 type Err = ParseRelayModeError;
426
427 fn from_str(s: &str) -> Result<Self, Self::Err> {
428 match s {
429 "proxy" => Ok(RelayMode::Proxy),
430 "managed" => Ok(RelayMode::Managed),
431 _ => Err(ParseRelayModeError),
432 }
433 }
434}
435
436fn is_default<T: Default + PartialEq>(t: &T) -> bool {
438 *t == T::default()
439}
440
441fn is_docker() -> bool {
443 if fs::metadata("/.dockerenv").is_ok() {
444 return true;
445 }
446
447 fs::read_to_string("/proc/self/cgroup").is_ok_and(|s| s.contains("/docker"))
448}
449
450fn default_host() -> IpAddr {
452 if is_docker() {
453 "0.0.0.0".parse().unwrap()
455 } else {
456 "127.0.0.1".parse().unwrap()
457 }
458}
459
460#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
464#[serde(rename_all = "lowercase")]
465pub enum ReadinessCondition {
466 Authenticated,
475 Always,
477}
478
479impl Default for ReadinessCondition {
480 fn default() -> Self {
481 Self::Authenticated
482 }
483}
484
485#[derive(Serialize, Deserialize, Debug)]
487#[serde(default)]
488pub struct Relay {
489 pub mode: RelayMode,
491 pub instance: RelayInstance,
493 pub upstream: UpstreamDescriptor<'static>,
495 pub host: IpAddr,
497 pub port: u16,
499 #[serde(skip_serializing)]
501 pub tls_port: Option<u16>,
502 #[serde(skip_serializing)]
504 pub tls_identity_path: Option<PathBuf>,
505 #[serde(skip_serializing)]
507 pub tls_identity_password: Option<String>,
508 #[serde(skip_serializing_if = "is_default")]
513 pub override_project_ids: bool,
514}
515
516impl Default for Relay {
517 fn default() -> Self {
518 Relay {
519 mode: RelayMode::Managed,
520 instance: RelayInstance::Default,
521 upstream: "https://sentry.io/".parse().unwrap(),
522 host: default_host(),
523 port: 3000,
524 tls_port: None,
525 tls_identity_path: None,
526 tls_identity_password: None,
527 override_project_ids: false,
528 }
529 }
530}
531
532#[derive(Serialize, Deserialize, Debug)]
534#[serde(default)]
535pub struct Metrics {
536 pub statsd: Option<String>,
540 pub prefix: String,
544 pub default_tags: BTreeMap<String, String>,
546 pub hostname_tag: Option<String>,
548 pub sample_rate: f32,
553 pub periodic_secs: u64,
558 pub aggregate: bool,
562 pub allow_high_cardinality_tags: bool,
570}
571
572impl Default for Metrics {
573 fn default() -> Self {
574 Metrics {
575 statsd: None,
576 prefix: "sentry.relay".into(),
577 default_tags: BTreeMap::new(),
578 hostname_tag: None,
579 sample_rate: 1.0,
580 periodic_secs: 5,
581 aggregate: true,
582 allow_high_cardinality_tags: false,
583 }
584 }
585}
586
587#[derive(Serialize, Deserialize, Debug)]
589#[serde(default)]
590pub struct Limits {
591 pub max_concurrent_requests: usize,
594 pub max_concurrent_queries: usize,
599 pub max_event_size: ByteSize,
601 pub max_attachment_size: ByteSize,
603 pub max_attachments_size: ByteSize,
605 pub max_client_reports_size: ByteSize,
607 pub max_check_in_size: ByteSize,
609 pub max_envelope_size: ByteSize,
611 pub max_session_count: usize,
613 pub max_span_count: usize,
615 pub max_log_count: usize,
617 pub max_api_payload_size: ByteSize,
619 pub max_api_file_upload_size: ByteSize,
621 pub max_api_chunk_upload_size: ByteSize,
623 pub max_profile_size: ByteSize,
625 pub max_log_size: ByteSize,
627 pub max_span_size: ByteSize,
629 pub max_container_size: ByteSize,
631 pub max_statsd_size: ByteSize,
633 pub max_metric_buckets_size: ByteSize,
635 pub max_replay_compressed_size: ByteSize,
637 #[serde(alias = "max_replay_size")]
639 max_replay_uncompressed_size: ByteSize,
640 pub max_replay_message_size: ByteSize,
642 pub max_thread_count: usize,
647 pub max_pool_concurrency: usize,
654 pub query_timeout: u64,
657 pub shutdown_timeout: u64,
660 pub keepalive_timeout: u64,
664 pub idle_timeout: Option<u64>,
671 pub max_connections: Option<usize>,
677 pub tcp_listen_backlog: u32,
685}
686
687impl Default for Limits {
688 fn default() -> Self {
689 Limits {
690 max_concurrent_requests: 100,
691 max_concurrent_queries: 5,
692 max_event_size: ByteSize::mebibytes(1),
693 max_attachment_size: ByteSize::mebibytes(100),
694 max_attachments_size: ByteSize::mebibytes(100),
695 max_client_reports_size: ByteSize::kibibytes(4),
696 max_check_in_size: ByteSize::kibibytes(100),
697 max_envelope_size: ByteSize::mebibytes(100),
698 max_session_count: 100,
699 max_span_count: 1000,
700 max_log_count: 1000,
701 max_api_payload_size: ByteSize::mebibytes(20),
702 max_api_file_upload_size: ByteSize::mebibytes(40),
703 max_api_chunk_upload_size: ByteSize::mebibytes(100),
704 max_profile_size: ByteSize::mebibytes(50),
705 max_log_size: ByteSize::mebibytes(1),
706 max_span_size: ByteSize::mebibytes(1),
707 max_container_size: ByteSize::mebibytes(3),
708 max_statsd_size: ByteSize::mebibytes(1),
709 max_metric_buckets_size: ByteSize::mebibytes(1),
710 max_replay_compressed_size: ByteSize::mebibytes(10),
711 max_replay_uncompressed_size: ByteSize::mebibytes(100),
712 max_replay_message_size: ByteSize::mebibytes(15),
713 max_thread_count: num_cpus::get(),
714 max_pool_concurrency: 1,
715 query_timeout: 30,
716 shutdown_timeout: 10,
717 keepalive_timeout: 5,
718 idle_timeout: None,
719 max_connections: None,
720 tcp_listen_backlog: 1024,
721 }
722 }
723}
724
725#[derive(Debug, Default, Deserialize, Serialize)]
727#[serde(default)]
728pub struct Routing {
729 pub accept_unknown_items: Option<bool>,
739}
740
741#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
743#[serde(rename_all = "lowercase")]
744pub enum HttpEncoding {
745 #[default]
750 Identity,
751 Deflate,
757 Gzip,
764 Br,
766 Zstd,
768}
769
770impl HttpEncoding {
771 pub fn parse(str: &str) -> Self {
773 let str = str.trim();
774 if str.eq_ignore_ascii_case("zstd") {
775 Self::Zstd
776 } else if str.eq_ignore_ascii_case("br") {
777 Self::Br
778 } else if str.eq_ignore_ascii_case("gzip") || str.eq_ignore_ascii_case("x-gzip") {
779 Self::Gzip
780 } else if str.eq_ignore_ascii_case("deflate") {
781 Self::Deflate
782 } else {
783 Self::Identity
784 }
785 }
786
787 pub fn name(&self) -> Option<&'static str> {
791 match self {
792 Self::Identity => None,
793 Self::Deflate => Some("deflate"),
794 Self::Gzip => Some("gzip"),
795 Self::Br => Some("br"),
796 Self::Zstd => Some("zstd"),
797 }
798 }
799}
800
801#[derive(Serialize, Deserialize, Debug)]
803#[serde(default)]
804pub struct Http {
805 pub timeout: u32,
811 pub connection_timeout: u32,
816 pub max_retry_interval: u32,
818 pub host_header: Option<String>,
820 pub auth_interval: Option<u64>,
828 pub outage_grace_period: u64,
834 pub retry_delay: u64,
838 pub project_failure_interval: u64,
843 pub encoding: HttpEncoding,
859 pub global_metrics: bool,
866}
867
868impl Default for Http {
869 fn default() -> Self {
870 Http {
871 timeout: 5,
872 connection_timeout: 3,
873 max_retry_interval: 60, host_header: None,
875 auth_interval: Some(600), outage_grace_period: DEFAULT_NETWORK_OUTAGE_GRACE_PERIOD,
877 retry_delay: default_retry_delay(),
878 project_failure_interval: default_project_failure_interval(),
879 encoding: HttpEncoding::Zstd,
880 global_metrics: false,
881 }
882 }
883}
884
885fn default_retry_delay() -> u64 {
887 1
888}
889
890fn default_project_failure_interval() -> u64 {
892 90
893}
894
895fn spool_envelopes_max_disk_size() -> ByteSize {
897 ByteSize::mebibytes(500)
898}
899
900fn spool_envelopes_batch_size_bytes() -> ByteSize {
902 ByteSize::kibibytes(10)
903}
904
905fn spool_envelopes_max_envelope_delay_secs() -> u64 {
906 24 * 60 * 60
907}
908
909fn spool_disk_usage_refresh_frequency_ms() -> u64 {
911 100
912}
913
914fn spool_max_backpressure_envelopes() -> usize {
916 500
917}
918
919fn spool_max_backpressure_memory_percent() -> f32 {
921 0.9
922}
923
924fn spool_envelopes_partitions() -> NonZeroU8 {
926 NonZeroU8::new(1).unwrap()
927}
928
929#[derive(Debug, Serialize, Deserialize)]
931pub struct EnvelopeSpool {
932 pub path: Option<PathBuf>,
938 #[serde(default = "spool_envelopes_max_disk_size")]
944 pub max_disk_size: ByteSize,
945 #[serde(default = "spool_envelopes_batch_size_bytes")]
952 pub batch_size_bytes: ByteSize,
953 #[serde(default = "spool_envelopes_max_envelope_delay_secs")]
960 pub max_envelope_delay_secs: u64,
961 #[serde(default = "spool_disk_usage_refresh_frequency_ms")]
966 pub disk_usage_refresh_frequency_ms: u64,
967 #[serde(default = "spool_max_backpressure_envelopes")]
971 pub max_backpressure_envelopes: usize,
972 #[serde(default = "spool_max_backpressure_memory_percent")]
1002 pub max_backpressure_memory_percent: f32,
1003 #[serde(default = "spool_envelopes_partitions")]
1010 pub partitions: NonZeroU8,
1011}
1012
1013impl Default for EnvelopeSpool {
1014 fn default() -> Self {
1015 Self {
1016 path: None,
1017 max_disk_size: spool_envelopes_max_disk_size(),
1018 batch_size_bytes: spool_envelopes_batch_size_bytes(),
1019 max_envelope_delay_secs: spool_envelopes_max_envelope_delay_secs(),
1020 disk_usage_refresh_frequency_ms: spool_disk_usage_refresh_frequency_ms(),
1021 max_backpressure_envelopes: spool_max_backpressure_envelopes(),
1022 max_backpressure_memory_percent: spool_max_backpressure_memory_percent(),
1023 partitions: spool_envelopes_partitions(),
1024 }
1025 }
1026}
1027
1028#[derive(Debug, Serialize, Deserialize, Default)]
1030pub struct Spool {
1031 #[serde(default)]
1033 pub envelopes: EnvelopeSpool,
1034}
1035
1036#[derive(Serialize, Deserialize, Debug)]
1038#[serde(default)]
1039pub struct Cache {
1040 pub project_request_full_config: bool,
1042 pub project_expiry: u32,
1044 pub project_grace_period: u32,
1049 pub project_refresh_interval: Option<u32>,
1055 pub relay_expiry: u32,
1057 #[serde(alias = "event_expiry")]
1063 envelope_expiry: u32,
1064 #[serde(alias = "event_buffer_size")]
1066 envelope_buffer_size: u32,
1067 pub miss_expiry: u32,
1069 pub batch_interval: u32,
1071 pub downstream_relays_batch_interval: u32,
1073 pub batch_size: usize,
1077 pub file_interval: u32,
1079 pub global_config_fetch_interval: u32,
1081}
1082
1083impl Default for Cache {
1084 fn default() -> Self {
1085 Cache {
1086 project_request_full_config: false,
1087 project_expiry: 300, project_grace_period: 120, project_refresh_interval: None,
1090 relay_expiry: 3600, envelope_expiry: 600, envelope_buffer_size: 1000,
1093 miss_expiry: 60, batch_interval: 100, downstream_relays_batch_interval: 100, batch_size: 500,
1097 file_interval: 10, global_config_fetch_interval: 10, }
1100 }
1101}
1102
1103fn default_max_secs_in_future() -> u32 {
1104 60 }
1106
1107fn default_max_session_secs_in_past() -> u32 {
1108 5 * 24 * 3600 }
1110
1111fn default_chunk_size() -> ByteSize {
1112 ByteSize::mebibytes(1)
1113}
1114
1115fn default_projectconfig_cache_prefix() -> String {
1116 "relayconfig".to_owned()
1117}
1118
1119#[allow(clippy::unnecessary_wraps)]
1120fn default_max_rate_limit() -> Option<u32> {
1121 Some(300) }
1123
1124#[derive(Serialize, Deserialize, Debug)]
1126pub struct Processing {
1127 pub enabled: bool,
1129 #[serde(default)]
1131 pub geoip_path: Option<PathBuf>,
1132 #[serde(default = "default_max_secs_in_future")]
1134 pub max_secs_in_future: u32,
1135 #[serde(default = "default_max_session_secs_in_past")]
1137 pub max_session_secs_in_past: u32,
1138 pub kafka_config: Vec<KafkaConfigParam>,
1140 #[serde(default)]
1142 pub span_producers: SpanProducers,
1143 #[serde(default)]
1163 pub secondary_kafka_configs: BTreeMap<String, Vec<KafkaConfigParam>>,
1164 #[serde(default)]
1166 pub topics: TopicAssignments,
1167 #[serde(default)]
1169 pub kafka_validate_topics: bool,
1170 #[serde(default)]
1172 pub redis: Option<RedisConfigs>,
1173 #[serde(default = "default_chunk_size")]
1175 pub attachment_chunk_size: ByteSize,
1176 #[serde(default = "default_projectconfig_cache_prefix")]
1178 pub projectconfig_cache_prefix: String,
1179 #[serde(default = "default_max_rate_limit")]
1181 pub max_rate_limit: Option<u32>,
1182}
1183
1184impl Default for Processing {
1185 fn default() -> Self {
1187 Self {
1188 enabled: false,
1189 geoip_path: None,
1190 max_secs_in_future: default_max_secs_in_future(),
1191 max_session_secs_in_past: default_max_session_secs_in_past(),
1192 kafka_config: Vec::new(),
1193 secondary_kafka_configs: BTreeMap::new(),
1194 topics: TopicAssignments::default(),
1195 kafka_validate_topics: false,
1196 redis: None,
1197 attachment_chunk_size: default_chunk_size(),
1198 projectconfig_cache_prefix: default_projectconfig_cache_prefix(),
1199 max_rate_limit: default_max_rate_limit(),
1200 span_producers: Default::default(),
1201 }
1202 }
1203}
1204
1205#[derive(Debug, Serialize, Deserialize)]
1207#[serde(default)]
1208pub struct SpanProducers {
1209 pub produce_json_sample_rate: Option<f32>,
1213 pub produce_json_orgs: Vec<u64>,
1217 pub produce_json: bool,
1219 pub produce_protobuf: bool,
1221}
1222
1223impl Default for SpanProducers {
1224 fn default() -> Self {
1225 Self {
1226 produce_json_sample_rate: None,
1227 produce_json_orgs: vec![],
1228 produce_json: false,
1229 produce_protobuf: true,
1230 }
1231 }
1232}
1233
1234#[derive(Debug, Default, Serialize, Deserialize)]
1236#[serde(default)]
1237pub struct Normalization {
1238 #[serde(default)]
1240 pub level: NormalizationLevel,
1241}
1242
1243#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
1245#[serde(rename_all = "lowercase")]
1246pub enum NormalizationLevel {
1247 #[default]
1251 Default,
1252 Full,
1257}
1258
1259#[derive(Serialize, Deserialize, Debug)]
1261#[serde(default)]
1262pub struct OutcomeAggregatorConfig {
1263 pub bucket_interval: u64,
1265 pub flush_interval: u64,
1267}
1268
1269impl Default for OutcomeAggregatorConfig {
1270 fn default() -> Self {
1271 Self {
1272 bucket_interval: 60,
1273 flush_interval: 120,
1274 }
1275 }
1276}
1277
1278#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1281
1282pub enum EmitOutcomes {
1283 None,
1285 AsClientReports,
1287 AsOutcomes,
1289}
1290
1291impl EmitOutcomes {
1292 pub fn any(&self) -> bool {
1294 !matches!(self, EmitOutcomes::None)
1295 }
1296}
1297
1298impl Serialize for EmitOutcomes {
1299 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1300 where
1301 S: Serializer,
1302 {
1303 match self {
1305 Self::None => serializer.serialize_bool(false),
1306 Self::AsClientReports => serializer.serialize_str("as_client_reports"),
1307 Self::AsOutcomes => serializer.serialize_bool(true),
1308 }
1309 }
1310}
1311
1312struct EmitOutcomesVisitor;
1313
1314impl Visitor<'_> for EmitOutcomesVisitor {
1315 type Value = EmitOutcomes;
1316
1317 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1318 formatter.write_str("true, false, or 'as_client_reports'")
1319 }
1320
1321 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
1322 where
1323 E: serde::de::Error,
1324 {
1325 Ok(if v {
1326 EmitOutcomes::AsOutcomes
1327 } else {
1328 EmitOutcomes::None
1329 })
1330 }
1331
1332 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
1333 where
1334 E: serde::de::Error,
1335 {
1336 if v == "as_client_reports" {
1337 Ok(EmitOutcomes::AsClientReports)
1338 } else {
1339 Err(E::invalid_value(Unexpected::Str(v), &"as_client_reports"))
1340 }
1341 }
1342}
1343
1344impl<'de> Deserialize<'de> for EmitOutcomes {
1345 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1346 where
1347 D: Deserializer<'de>,
1348 {
1349 deserializer.deserialize_any(EmitOutcomesVisitor)
1350 }
1351}
1352
1353#[derive(Serialize, Deserialize, Debug)]
1355#[serde(default)]
1356pub struct Outcomes {
1357 pub emit_outcomes: EmitOutcomes,
1361 pub emit_client_outcomes: bool,
1363 pub batch_size: usize,
1366 pub batch_interval: u64,
1369 pub source: Option<String>,
1372 pub aggregator: OutcomeAggregatorConfig,
1374}
1375
1376impl Default for Outcomes {
1377 fn default() -> Self {
1378 Outcomes {
1379 emit_outcomes: EmitOutcomes::AsClientReports,
1380 emit_client_outcomes: true,
1381 batch_size: 1000,
1382 batch_interval: 500,
1383 source: None,
1384 aggregator: OutcomeAggregatorConfig::default(),
1385 }
1386 }
1387}
1388
1389#[derive(Serialize, Deserialize, Debug, Default)]
1391pub struct MinimalConfig {
1392 pub relay: Relay,
1394}
1395
1396impl MinimalConfig {
1397 pub fn save_in_folder<P: AsRef<Path>>(&self, p: P) -> anyhow::Result<()> {
1399 let path = p.as_ref();
1400 if fs::metadata(path).is_err() {
1401 fs::create_dir_all(path)
1402 .with_context(|| ConfigError::file(ConfigErrorKind::CouldNotOpenFile, path))?;
1403 }
1404 self.save(path)
1405 }
1406}
1407
1408impl ConfigObject for MinimalConfig {
1409 fn format() -> ConfigFormat {
1410 ConfigFormat::Yaml
1411 }
1412
1413 fn name() -> &'static str {
1414 "config"
1415 }
1416}
1417
1418mod config_relay_info {
1420 use serde::ser::SerializeMap;
1421
1422 use super::*;
1423
1424 #[derive(Debug, Serialize, Deserialize, Clone)]
1426 struct RelayInfoConfig {
1427 public_key: PublicKey,
1428 #[serde(default)]
1429 internal: bool,
1430 }
1431
1432 impl From<RelayInfoConfig> for RelayInfo {
1433 fn from(v: RelayInfoConfig) -> Self {
1434 RelayInfo {
1435 public_key: v.public_key,
1436 internal: v.internal,
1437 }
1438 }
1439 }
1440
1441 impl From<RelayInfo> for RelayInfoConfig {
1442 fn from(v: RelayInfo) -> Self {
1443 RelayInfoConfig {
1444 public_key: v.public_key,
1445 internal: v.internal,
1446 }
1447 }
1448 }
1449
1450 pub(super) fn deserialize<'de, D>(des: D) -> Result<HashMap<RelayId, RelayInfo>, D::Error>
1451 where
1452 D: Deserializer<'de>,
1453 {
1454 let map = HashMap::<RelayId, RelayInfoConfig>::deserialize(des)?;
1455 Ok(map.into_iter().map(|(k, v)| (k, v.into())).collect())
1456 }
1457
1458 pub(super) fn serialize<S>(elm: &HashMap<RelayId, RelayInfo>, ser: S) -> Result<S::Ok, S::Error>
1459 where
1460 S: Serializer,
1461 {
1462 let mut map = ser.serialize_map(Some(elm.len()))?;
1463
1464 for (k, v) in elm {
1465 map.serialize_entry(k, &RelayInfoConfig::from(v.clone()))?;
1466 }
1467
1468 map.end()
1469 }
1470}
1471
1472#[derive(Serialize, Deserialize, Debug, Default)]
1474pub struct AuthConfig {
1475 #[serde(default, skip_serializing_if = "is_default")]
1477 pub ready: ReadinessCondition,
1478
1479 #[serde(default, with = "config_relay_info")]
1481 pub static_relays: HashMap<RelayId, RelayInfo>,
1482
1483 #[serde(default = "default_max_age")]
1487 pub signature_max_age: u64,
1488}
1489
1490fn default_max_age() -> u64 {
1491 300
1492}
1493
1494#[derive(Serialize, Deserialize, Debug, Default)]
1496pub struct GeoIpConfig {
1497 pub path: Option<PathBuf>,
1499}
1500
1501#[derive(Serialize, Deserialize, Debug)]
1503#[serde(default)]
1504pub struct CardinalityLimiter {
1505 pub cache_vacuum_interval: u64,
1511}
1512
1513impl Default for CardinalityLimiter {
1514 fn default() -> Self {
1515 Self {
1516 cache_vacuum_interval: 180,
1517 }
1518 }
1519}
1520
1521#[derive(Serialize, Deserialize, Debug)]
1526#[serde(default)]
1527pub struct Health {
1528 pub refresh_interval_ms: u64,
1535 pub max_memory_bytes: Option<ByteSize>,
1540 pub max_memory_percent: f32,
1544 pub probe_timeout_ms: u64,
1551 pub memory_stat_refresh_frequency_ms: u64,
1557}
1558
1559impl Default for Health {
1560 fn default() -> Self {
1561 Self {
1562 refresh_interval_ms: 3000,
1563 max_memory_bytes: None,
1564 max_memory_percent: 0.95,
1565 probe_timeout_ms: 900,
1566 memory_stat_refresh_frequency_ms: 100,
1567 }
1568 }
1569}
1570
1571#[derive(Serialize, Deserialize, Debug)]
1573#[serde(default)]
1574pub struct Cogs {
1575 pub max_queue_size: u64,
1581 pub relay_resource_id: String,
1587}
1588
1589impl Default for Cogs {
1590 fn default() -> Self {
1591 Self {
1592 max_queue_size: 10_000,
1593 relay_resource_id: "relay_service".to_owned(),
1594 }
1595 }
1596}
1597
1598#[derive(Serialize, Deserialize, Debug, Default)]
1599struct ConfigValues {
1600 #[serde(default)]
1601 relay: Relay,
1602 #[serde(default)]
1603 http: Http,
1604 #[serde(default)]
1605 cache: Cache,
1606 #[serde(default)]
1607 spool: Spool,
1608 #[serde(default)]
1609 limits: Limits,
1610 #[serde(default)]
1611 logging: relay_log::LogConfig,
1612 #[serde(default)]
1613 routing: Routing,
1614 #[serde(default)]
1615 metrics: Metrics,
1616 #[serde(default)]
1617 sentry: relay_log::SentryConfig,
1618 #[serde(default)]
1619 processing: Processing,
1620 #[serde(default)]
1621 outcomes: Outcomes,
1622 #[serde(default)]
1623 aggregator: AggregatorServiceConfig,
1624 #[serde(default)]
1625 secondary_aggregators: Vec<ScopedAggregatorConfig>,
1626 #[serde(default)]
1627 auth: AuthConfig,
1628 #[serde(default)]
1629 geoip: GeoIpConfig,
1630 #[serde(default)]
1631 normalization: Normalization,
1632 #[serde(default)]
1633 cardinality_limiter: CardinalityLimiter,
1634 #[serde(default)]
1635 health: Health,
1636 #[serde(default)]
1637 cogs: Cogs,
1638}
1639
1640impl ConfigObject for ConfigValues {
1641 fn format() -> ConfigFormat {
1642 ConfigFormat::Yaml
1643 }
1644
1645 fn name() -> &'static str {
1646 "config"
1647 }
1648}
1649
1650pub struct Config {
1652 values: ConfigValues,
1653 credentials: Option<Credentials>,
1654 path: PathBuf,
1655}
1656
1657impl fmt::Debug for Config {
1658 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1659 f.debug_struct("Config")
1660 .field("path", &self.path)
1661 .field("values", &self.values)
1662 .finish()
1663 }
1664}
1665
1666impl Config {
1667 pub fn from_path<P: AsRef<Path>>(path: P) -> anyhow::Result<Config> {
1669 let path = env::current_dir()
1670 .map(|x| x.join(path.as_ref()))
1671 .unwrap_or_else(|_| path.as_ref().to_path_buf());
1672
1673 let config = Config {
1674 values: ConfigValues::load(&path)?,
1675 credentials: if Credentials::path(&path).exists() {
1676 Some(Credentials::load(&path)?)
1677 } else {
1678 None
1679 },
1680 path: path.clone(),
1681 };
1682
1683 if cfg!(not(feature = "processing")) && config.processing_enabled() {
1684 return Err(ConfigError::file(ConfigErrorKind::ProcessingNotAvailable, &path).into());
1685 }
1686
1687 Ok(config)
1688 }
1689
1690 pub fn from_json_value(value: serde_json::Value) -> anyhow::Result<Config> {
1694 Ok(Config {
1695 values: serde_json::from_value(value)
1696 .with_context(|| ConfigError::new(ConfigErrorKind::BadJson))?,
1697 credentials: None,
1698 path: PathBuf::new(),
1699 })
1700 }
1701
1702 pub fn apply_override(
1705 &mut self,
1706 mut overrides: OverridableConfig,
1707 ) -> anyhow::Result<&mut Self> {
1708 let relay = &mut self.values.relay;
1709
1710 if let Some(mode) = overrides.mode {
1711 relay.mode = mode
1712 .parse::<RelayMode>()
1713 .with_context(|| ConfigError::field("mode"))?;
1714 }
1715
1716 if let Some(deployment) = overrides.instance {
1717 relay.instance = deployment
1718 .parse::<RelayInstance>()
1719 .with_context(|| ConfigError::field("deployment"))?;
1720 }
1721
1722 if let Some(log_level) = overrides.log_level {
1723 self.values.logging.level = log_level.parse()?;
1724 }
1725
1726 if let Some(log_format) = overrides.log_format {
1727 self.values.logging.format = log_format.parse()?;
1728 }
1729
1730 if let Some(upstream) = overrides.upstream {
1731 relay.upstream = upstream
1732 .parse::<UpstreamDescriptor>()
1733 .with_context(|| ConfigError::field("upstream"))?;
1734 } else if let Some(upstream_dsn) = overrides.upstream_dsn {
1735 relay.upstream = upstream_dsn
1736 .parse::<Dsn>()
1737 .map(|dsn| UpstreamDescriptor::from_dsn(&dsn).into_owned())
1738 .with_context(|| ConfigError::field("upstream_dsn"))?;
1739 }
1740
1741 if let Some(host) = overrides.host {
1742 relay.host = host
1743 .parse::<IpAddr>()
1744 .with_context(|| ConfigError::field("host"))?;
1745 }
1746
1747 if let Some(port) = overrides.port {
1748 relay.port = port
1749 .as_str()
1750 .parse()
1751 .with_context(|| ConfigError::field("port"))?;
1752 }
1753
1754 let processing = &mut self.values.processing;
1755 if let Some(enabled) = overrides.processing {
1756 match enabled.to_lowercase().as_str() {
1757 "true" | "1" => processing.enabled = true,
1758 "false" | "0" | "" => processing.enabled = false,
1759 _ => return Err(ConfigError::field("processing").into()),
1760 }
1761 }
1762
1763 if let Some(redis) = overrides.redis_url {
1764 processing.redis = Some(RedisConfigs::Unified(RedisConfig::single(redis)))
1765 }
1766
1767 if let Some(kafka_url) = overrides.kafka_url {
1768 let existing = processing
1769 .kafka_config
1770 .iter_mut()
1771 .find(|e| e.name == "bootstrap.servers");
1772
1773 if let Some(config_param) = existing {
1774 config_param.value = kafka_url;
1775 } else {
1776 processing.kafka_config.push(KafkaConfigParam {
1777 name: "bootstrap.servers".to_owned(),
1778 value: kafka_url,
1779 })
1780 }
1781 }
1782 let id = if let Some(id) = overrides.id {
1784 let id = Uuid::parse_str(&id).with_context(|| ConfigError::field("id"))?;
1785 Some(id)
1786 } else {
1787 None
1788 };
1789 let public_key = if let Some(public_key) = overrides.public_key {
1790 let public_key = public_key
1791 .parse::<PublicKey>()
1792 .with_context(|| ConfigError::field("public_key"))?;
1793 Some(public_key)
1794 } else {
1795 None
1796 };
1797
1798 let secret_key = if let Some(secret_key) = overrides.secret_key {
1799 let secret_key = secret_key
1800 .parse::<SecretKey>()
1801 .with_context(|| ConfigError::field("secret_key"))?;
1802 Some(secret_key)
1803 } else {
1804 None
1805 };
1806 let outcomes = &mut self.values.outcomes;
1807 if overrides.outcome_source.is_some() {
1808 outcomes.source = overrides.outcome_source.take();
1809 }
1810
1811 if let Some(credentials) = &mut self.credentials {
1812 if let Some(id) = id {
1814 credentials.id = id;
1815 }
1816 if let Some(public_key) = public_key {
1817 credentials.public_key = public_key;
1818 }
1819 if let Some(secret_key) = secret_key {
1820 credentials.secret_key = secret_key
1821 }
1822 } else {
1823 match (id, public_key, secret_key) {
1825 (Some(id), Some(public_key), Some(secret_key)) => {
1826 self.credentials = Some(Credentials {
1827 secret_key,
1828 public_key,
1829 id,
1830 })
1831 }
1832 (None, None, None) => {
1833 }
1836 _ => {
1837 return Err(ConfigError::field("incomplete credentials").into());
1838 }
1839 }
1840 }
1841
1842 let limits = &mut self.values.limits;
1843 if let Some(shutdown_timeout) = overrides.shutdown_timeout
1844 && let Ok(shutdown_timeout) = shutdown_timeout.parse::<u64>()
1845 {
1846 limits.shutdown_timeout = shutdown_timeout;
1847 }
1848
1849 if let Some(server_name) = overrides.server_name {
1850 self.values.sentry.server_name = Some(server_name.into());
1851 }
1852
1853 Ok(self)
1854 }
1855
1856 pub fn config_exists<P: AsRef<Path>>(path: P) -> bool {
1858 fs::metadata(ConfigValues::path(path.as_ref())).is_ok()
1859 }
1860
1861 pub fn path(&self) -> &Path {
1863 &self.path
1864 }
1865
1866 pub fn to_yaml_string(&self) -> anyhow::Result<String> {
1868 serde_yaml::to_string(&self.values)
1869 .with_context(|| ConfigError::new(ConfigErrorKind::CouldNotWriteFile))
1870 }
1871
1872 pub fn regenerate_credentials(&mut self, save: bool) -> anyhow::Result<()> {
1876 let creds = Credentials::generate();
1877 if save {
1878 creds.save(&self.path)?;
1879 }
1880 self.credentials = Some(creds);
1881 Ok(())
1882 }
1883
1884 pub fn credentials(&self) -> Option<&Credentials> {
1886 self.credentials.as_ref()
1887 }
1888
1889 pub fn replace_credentials(
1893 &mut self,
1894 credentials: Option<Credentials>,
1895 ) -> anyhow::Result<bool> {
1896 if self.credentials == credentials {
1897 return Ok(false);
1898 }
1899
1900 match credentials {
1901 Some(ref creds) => {
1902 creds.save(&self.path)?;
1903 }
1904 None => {
1905 let path = Credentials::path(&self.path);
1906 if fs::metadata(&path).is_ok() {
1907 fs::remove_file(&path).with_context(|| {
1908 ConfigError::file(ConfigErrorKind::CouldNotWriteFile, &path)
1909 })?;
1910 }
1911 }
1912 }
1913
1914 self.credentials = credentials;
1915 Ok(true)
1916 }
1917
1918 pub fn has_credentials(&self) -> bool {
1920 self.credentials.is_some()
1921 }
1922
1923 pub fn secret_key(&self) -> Option<&SecretKey> {
1925 self.credentials.as_ref().map(|x| &x.secret_key)
1926 }
1927
1928 pub fn public_key(&self) -> Option<&PublicKey> {
1930 self.credentials.as_ref().map(|x| &x.public_key)
1931 }
1932
1933 pub fn relay_id(&self) -> Option<&RelayId> {
1935 self.credentials.as_ref().map(|x| &x.id)
1936 }
1937
1938 pub fn relay_mode(&self) -> RelayMode {
1940 self.values.relay.mode
1941 }
1942
1943 pub fn relay_instance(&self) -> RelayInstance {
1945 self.values.relay.instance
1946 }
1947
1948 pub fn upstream_descriptor(&self) -> &UpstreamDescriptor<'_> {
1950 &self.values.relay.upstream
1951 }
1952
1953 pub fn http_host_header(&self) -> Option<&str> {
1955 self.values.http.host_header.as_deref()
1956 }
1957
1958 pub fn listen_addr(&self) -> SocketAddr {
1960 (self.values.relay.host, self.values.relay.port).into()
1961 }
1962
1963 pub fn tls_listen_addr(&self) -> Option<SocketAddr> {
1965 if self.values.relay.tls_identity_path.is_some() {
1966 let port = self.values.relay.tls_port.unwrap_or(3443);
1967 Some((self.values.relay.host, port).into())
1968 } else {
1969 None
1970 }
1971 }
1972
1973 pub fn tls_identity_path(&self) -> Option<&Path> {
1975 self.values.relay.tls_identity_path.as_deref()
1976 }
1977
1978 pub fn tls_identity_password(&self) -> Option<&str> {
1980 self.values.relay.tls_identity_password.as_deref()
1981 }
1982
1983 pub fn override_project_ids(&self) -> bool {
1987 self.values.relay.override_project_ids
1988 }
1989
1990 pub fn requires_auth(&self) -> bool {
1994 match self.values.auth.ready {
1995 ReadinessCondition::Authenticated => self.relay_mode() == RelayMode::Managed,
1996 ReadinessCondition::Always => false,
1997 }
1998 }
1999
2000 pub fn http_auth_interval(&self) -> Option<Duration> {
2004 if self.processing_enabled() {
2005 return None;
2006 }
2007
2008 match self.values.http.auth_interval {
2009 None | Some(0) => None,
2010 Some(secs) => Some(Duration::from_secs(secs)),
2011 }
2012 }
2013
2014 pub fn http_outage_grace_period(&self) -> Duration {
2017 Duration::from_secs(self.values.http.outage_grace_period)
2018 }
2019
2020 pub fn http_retry_delay(&self) -> Duration {
2025 Duration::from_secs(self.values.http.retry_delay)
2026 }
2027
2028 pub fn http_project_failure_interval(&self) -> Duration {
2030 Duration::from_secs(self.values.http.project_failure_interval)
2031 }
2032
2033 pub fn http_encoding(&self) -> HttpEncoding {
2035 self.values.http.encoding
2036 }
2037
2038 pub fn http_global_metrics(&self) -> bool {
2040 self.values.http.global_metrics
2041 }
2042
2043 pub fn emit_outcomes(&self) -> EmitOutcomes {
2048 if self.processing_enabled() {
2049 return EmitOutcomes::AsOutcomes;
2050 }
2051 self.values.outcomes.emit_outcomes
2052 }
2053
2054 pub fn emit_client_outcomes(&self) -> bool {
2064 self.values.outcomes.emit_client_outcomes
2065 }
2066
2067 pub fn outcome_batch_size(&self) -> usize {
2069 self.values.outcomes.batch_size
2070 }
2071
2072 pub fn outcome_batch_interval(&self) -> Duration {
2074 Duration::from_millis(self.values.outcomes.batch_interval)
2075 }
2076
2077 pub fn outcome_source(&self) -> Option<&str> {
2079 self.values.outcomes.source.as_deref()
2080 }
2081
2082 pub fn outcome_aggregator(&self) -> &OutcomeAggregatorConfig {
2084 &self.values.outcomes.aggregator
2085 }
2086
2087 pub fn logging(&self) -> &relay_log::LogConfig {
2089 &self.values.logging
2090 }
2091
2092 pub fn sentry(&self) -> &relay_log::SentryConfig {
2094 &self.values.sentry
2095 }
2096
2097 pub fn statsd_addrs(&self) -> anyhow::Result<Vec<SocketAddr>> {
2101 if let Some(ref addr) = self.values.metrics.statsd {
2102 let addrs = addr
2103 .as_str()
2104 .to_socket_addrs()
2105 .with_context(|| ConfigError::file(ConfigErrorKind::InvalidValue, &self.path))?
2106 .collect();
2107 Ok(addrs)
2108 } else {
2109 Ok(vec![])
2110 }
2111 }
2112
2113 pub fn metrics_prefix(&self) -> &str {
2115 &self.values.metrics.prefix
2116 }
2117
2118 pub fn metrics_default_tags(&self) -> &BTreeMap<String, String> {
2120 &self.values.metrics.default_tags
2121 }
2122
2123 pub fn metrics_hostname_tag(&self) -> Option<&str> {
2125 self.values.metrics.hostname_tag.as_deref()
2126 }
2127
2128 pub fn metrics_sample_rate(&self) -> f32 {
2130 self.values.metrics.sample_rate
2131 }
2132
2133 pub fn metrics_aggregate(&self) -> bool {
2135 self.values.metrics.aggregate
2136 }
2137
2138 pub fn metrics_allow_high_cardinality_tags(&self) -> bool {
2140 self.values.metrics.allow_high_cardinality_tags
2141 }
2142
2143 pub fn metrics_periodic_interval(&self) -> Option<Duration> {
2147 match self.values.metrics.periodic_secs {
2148 0 => None,
2149 secs => Some(Duration::from_secs(secs)),
2150 }
2151 }
2152
2153 pub fn http_timeout(&self) -> Duration {
2155 Duration::from_secs(self.values.http.timeout.into())
2156 }
2157
2158 pub fn http_connection_timeout(&self) -> Duration {
2160 Duration::from_secs(self.values.http.connection_timeout.into())
2161 }
2162
2163 pub fn http_max_retry_interval(&self) -> Duration {
2165 Duration::from_secs(self.values.http.max_retry_interval.into())
2166 }
2167
2168 pub fn project_cache_expiry(&self) -> Duration {
2170 Duration::from_secs(self.values.cache.project_expiry.into())
2171 }
2172
2173 pub fn request_full_project_config(&self) -> bool {
2175 self.values.cache.project_request_full_config
2176 }
2177
2178 pub fn relay_cache_expiry(&self) -> Duration {
2180 Duration::from_secs(self.values.cache.relay_expiry.into())
2181 }
2182
2183 pub fn envelope_buffer_size(&self) -> usize {
2185 self.values
2186 .cache
2187 .envelope_buffer_size
2188 .try_into()
2189 .unwrap_or(usize::MAX)
2190 }
2191
2192 pub fn cache_miss_expiry(&self) -> Duration {
2194 Duration::from_secs(self.values.cache.miss_expiry.into())
2195 }
2196
2197 pub fn project_grace_period(&self) -> Duration {
2199 Duration::from_secs(self.values.cache.project_grace_period.into())
2200 }
2201
2202 pub fn project_refresh_interval(&self) -> Option<Duration> {
2206 self.values
2207 .cache
2208 .project_refresh_interval
2209 .map(Into::into)
2210 .map(Duration::from_secs)
2211 }
2212
2213 pub fn query_batch_interval(&self) -> Duration {
2216 Duration::from_millis(self.values.cache.batch_interval.into())
2217 }
2218
2219 pub fn downstream_relays_batch_interval(&self) -> Duration {
2221 Duration::from_millis(self.values.cache.downstream_relays_batch_interval.into())
2222 }
2223
2224 pub fn local_cache_interval(&self) -> Duration {
2226 Duration::from_secs(self.values.cache.file_interval.into())
2227 }
2228
2229 pub fn global_config_fetch_interval(&self) -> Duration {
2232 Duration::from_secs(self.values.cache.global_config_fetch_interval.into())
2233 }
2234
2235 pub fn spool_envelopes_path(&self, partition_id: u8) -> Option<PathBuf> {
2240 let mut path = self
2241 .values
2242 .spool
2243 .envelopes
2244 .path
2245 .as_ref()
2246 .map(|path| path.to_owned())?;
2247
2248 if partition_id == 0 {
2249 return Some(path);
2250 }
2251
2252 let file_name = path.file_name().and_then(|f| f.to_str())?;
2253 let new_file_name = format!("{file_name}.{partition_id}");
2254 path.set_file_name(new_file_name);
2255
2256 Some(path)
2257 }
2258
2259 pub fn spool_envelopes_max_disk_size(&self) -> usize {
2261 self.values.spool.envelopes.max_disk_size.as_bytes()
2262 }
2263
2264 pub fn spool_envelopes_batch_size_bytes(&self) -> usize {
2267 self.values.spool.envelopes.batch_size_bytes.as_bytes()
2268 }
2269
2270 pub fn spool_envelopes_max_age(&self) -> Duration {
2272 Duration::from_secs(self.values.spool.envelopes.max_envelope_delay_secs)
2273 }
2274
2275 pub fn spool_disk_usage_refresh_frequency_ms(&self) -> Duration {
2277 Duration::from_millis(self.values.spool.envelopes.disk_usage_refresh_frequency_ms)
2278 }
2279
2280 pub fn spool_max_backpressure_envelopes(&self) -> usize {
2282 self.values.spool.envelopes.max_backpressure_envelopes
2283 }
2284
2285 pub fn spool_max_backpressure_memory_percent(&self) -> f32 {
2287 self.values.spool.envelopes.max_backpressure_memory_percent
2288 }
2289
2290 pub fn spool_partitions(&self) -> NonZeroU8 {
2292 self.values.spool.envelopes.partitions
2293 }
2294
2295 pub fn max_event_size(&self) -> usize {
2297 self.values.limits.max_event_size.as_bytes()
2298 }
2299
2300 pub fn max_attachment_size(&self) -> usize {
2302 self.values.limits.max_attachment_size.as_bytes()
2303 }
2304
2305 pub fn max_attachments_size(&self) -> usize {
2308 self.values.limits.max_attachments_size.as_bytes()
2309 }
2310
2311 pub fn max_client_reports_size(&self) -> usize {
2313 self.values.limits.max_client_reports_size.as_bytes()
2314 }
2315
2316 pub fn max_check_in_size(&self) -> usize {
2318 self.values.limits.max_check_in_size.as_bytes()
2319 }
2320
2321 pub fn max_log_size(&self) -> usize {
2323 self.values.limits.max_log_size.as_bytes()
2324 }
2325
2326 pub fn max_span_size(&self) -> usize {
2328 self.values.limits.max_span_size.as_bytes()
2329 }
2330
2331 pub fn max_container_size(&self) -> usize {
2333 self.values.limits.max_container_size.as_bytes()
2334 }
2335
2336 pub fn max_envelope_size(&self) -> usize {
2340 self.values.limits.max_envelope_size.as_bytes()
2341 }
2342
2343 pub fn max_session_count(&self) -> usize {
2345 self.values.limits.max_session_count
2346 }
2347
2348 pub fn max_span_count(&self) -> usize {
2350 self.values.limits.max_span_count
2351 }
2352
2353 pub fn max_log_count(&self) -> usize {
2355 self.values.limits.max_log_count
2356 }
2357
2358 pub fn max_statsd_size(&self) -> usize {
2360 self.values.limits.max_statsd_size.as_bytes()
2361 }
2362
2363 pub fn max_metric_buckets_size(&self) -> usize {
2365 self.values.limits.max_metric_buckets_size.as_bytes()
2366 }
2367
2368 pub fn max_api_payload_size(&self) -> usize {
2370 self.values.limits.max_api_payload_size.as_bytes()
2371 }
2372
2373 pub fn max_api_file_upload_size(&self) -> usize {
2375 self.values.limits.max_api_file_upload_size.as_bytes()
2376 }
2377
2378 pub fn max_api_chunk_upload_size(&self) -> usize {
2380 self.values.limits.max_api_chunk_upload_size.as_bytes()
2381 }
2382
2383 pub fn max_profile_size(&self) -> usize {
2385 self.values.limits.max_profile_size.as_bytes()
2386 }
2387
2388 pub fn max_replay_compressed_size(&self) -> usize {
2390 self.values.limits.max_replay_compressed_size.as_bytes()
2391 }
2392
2393 pub fn max_replay_uncompressed_size(&self) -> usize {
2395 self.values.limits.max_replay_uncompressed_size.as_bytes()
2396 }
2397
2398 pub fn max_replay_message_size(&self) -> usize {
2404 self.values.limits.max_replay_message_size.as_bytes()
2405 }
2406
2407 pub fn max_concurrent_requests(&self) -> usize {
2409 self.values.limits.max_concurrent_requests
2410 }
2411
2412 pub fn max_concurrent_queries(&self) -> usize {
2414 self.values.limits.max_concurrent_queries
2415 }
2416
2417 pub fn query_timeout(&self) -> Duration {
2419 Duration::from_secs(self.values.limits.query_timeout)
2420 }
2421
2422 pub fn shutdown_timeout(&self) -> Duration {
2425 Duration::from_secs(self.values.limits.shutdown_timeout)
2426 }
2427
2428 pub fn keepalive_timeout(&self) -> Duration {
2432 Duration::from_secs(self.values.limits.keepalive_timeout)
2433 }
2434
2435 pub fn idle_timeout(&self) -> Option<Duration> {
2437 self.values.limits.idle_timeout.map(Duration::from_secs)
2438 }
2439
2440 pub fn max_connections(&self) -> Option<usize> {
2442 self.values.limits.max_connections
2443 }
2444
2445 pub fn tcp_listen_backlog(&self) -> u32 {
2447 self.values.limits.tcp_listen_backlog
2448 }
2449
2450 pub fn cpu_concurrency(&self) -> usize {
2452 self.values.limits.max_thread_count
2453 }
2454
2455 pub fn pool_concurrency(&self) -> usize {
2457 self.values.limits.max_pool_concurrency
2458 }
2459
2460 pub fn query_batch_size(&self) -> usize {
2462 self.values.cache.batch_size
2463 }
2464
2465 pub fn project_configs_path(&self) -> PathBuf {
2467 self.path.join("projects")
2468 }
2469
2470 pub fn processing_enabled(&self) -> bool {
2472 self.values.processing.enabled
2473 }
2474
2475 pub fn normalization_level(&self) -> NormalizationLevel {
2477 self.values.normalization.level
2478 }
2479
2480 pub fn geoip_path(&self) -> Option<&Path> {
2482 self.values
2483 .geoip
2484 .path
2485 .as_deref()
2486 .or(self.values.processing.geoip_path.as_deref())
2487 }
2488
2489 pub fn max_secs_in_future(&self) -> i64 {
2493 self.values.processing.max_secs_in_future.into()
2494 }
2495
2496 pub fn max_session_secs_in_past(&self) -> i64 {
2498 self.values.processing.max_session_secs_in_past.into()
2499 }
2500
2501 pub fn kafka_configs(
2503 &self,
2504 topic: KafkaTopic,
2505 ) -> Result<KafkaTopicConfig<'_>, KafkaConfigError> {
2506 self.values.processing.topics.get(topic).kafka_configs(
2507 &self.values.processing.kafka_config,
2508 &self.values.processing.secondary_kafka_configs,
2509 )
2510 }
2511
2512 pub fn kafka_validate_topics(&self) -> bool {
2514 self.values.processing.kafka_validate_topics
2515 }
2516
2517 pub fn unused_topic_assignments(&self) -> &relay_kafka::Unused {
2519 &self.values.processing.topics.unused
2520 }
2521
2522 pub fn redis(&self) -> Option<RedisConfigsRef<'_>> {
2525 let redis_configs = self.values.processing.redis.as_ref()?;
2526
2527 Some(build_redis_configs(
2528 redis_configs,
2529 self.cpu_concurrency() as u32,
2530 ))
2531 }
2532
2533 pub fn attachment_chunk_size(&self) -> usize {
2535 self.values.processing.attachment_chunk_size.as_bytes()
2536 }
2537
2538 pub fn metrics_max_batch_size_bytes(&self) -> usize {
2540 self.values.aggregator.max_flush_bytes
2541 }
2542
2543 pub fn projectconfig_cache_prefix(&self) -> &str {
2546 &self.values.processing.projectconfig_cache_prefix
2547 }
2548
2549 pub fn max_rate_limit(&self) -> Option<u64> {
2551 self.values.processing.max_rate_limit.map(u32::into)
2552 }
2553
2554 pub fn cardinality_limiter_cache_vacuum_interval(&self) -> Duration {
2558 Duration::from_secs(self.values.cardinality_limiter.cache_vacuum_interval)
2559 }
2560
2561 pub fn health_refresh_interval(&self) -> Duration {
2563 Duration::from_millis(self.values.health.refresh_interval_ms)
2564 }
2565
2566 pub fn health_max_memory_watermark_bytes(&self) -> u64 {
2568 self.values
2569 .health
2570 .max_memory_bytes
2571 .as_ref()
2572 .map_or(u64::MAX, |b| b.as_bytes() as u64)
2573 }
2574
2575 pub fn health_max_memory_watermark_percent(&self) -> f32 {
2577 self.values.health.max_memory_percent
2578 }
2579
2580 pub fn health_probe_timeout(&self) -> Duration {
2582 Duration::from_millis(self.values.health.probe_timeout_ms)
2583 }
2584
2585 pub fn memory_stat_refresh_frequency_ms(&self) -> u64 {
2587 self.values.health.memory_stat_refresh_frequency_ms
2588 }
2589
2590 pub fn cogs_max_queue_size(&self) -> u64 {
2592 self.values.cogs.max_queue_size
2593 }
2594
2595 pub fn cogs_relay_resource_id(&self) -> &str {
2597 &self.values.cogs.relay_resource_id
2598 }
2599
2600 pub fn default_aggregator_config(&self) -> &AggregatorServiceConfig {
2602 &self.values.aggregator
2603 }
2604
2605 pub fn secondary_aggregator_configs(&self) -> &Vec<ScopedAggregatorConfig> {
2607 &self.values.secondary_aggregators
2608 }
2609
2610 pub fn aggregator_config_for(&self, namespace: MetricNamespace) -> &AggregatorServiceConfig {
2612 for entry in &self.values.secondary_aggregators {
2613 if entry.condition.matches(Some(namespace)) {
2614 return &entry.config;
2615 }
2616 }
2617 &self.values.aggregator
2618 }
2619
2620 pub fn static_relays(&self) -> &HashMap<RelayId, RelayInfo> {
2622 &self.values.auth.static_relays
2623 }
2624
2625 pub fn signature_max_age(&self) -> Duration {
2627 Duration::from_secs(self.values.auth.signature_max_age)
2628 }
2629
2630 pub fn accept_unknown_items(&self) -> bool {
2632 let forward = self.values.routing.accept_unknown_items;
2633 forward.unwrap_or_else(|| !self.processing_enabled())
2634 }
2635
2636 pub fn span_producers(&self) -> &SpanProducers {
2638 &self.values.processing.span_producers
2639 }
2640}
2641
2642impl Default for Config {
2643 fn default() -> Self {
2644 Self {
2645 values: ConfigValues::default(),
2646 credentials: None,
2647 path: PathBuf::new(),
2648 }
2649 }
2650}
2651
2652#[cfg(test)]
2653mod tests {
2654
2655 use super::*;
2656
2657 #[test]
2659 fn test_event_buffer_size() {
2660 let yaml = r###"
2661cache:
2662 event_buffer_size: 1000000
2663 event_expiry: 1800
2664"###;
2665
2666 let values: ConfigValues = serde_yaml::from_str(yaml).unwrap();
2667 assert_eq!(values.cache.envelope_buffer_size, 1_000_000);
2668 assert_eq!(values.cache.envelope_expiry, 1800);
2669 }
2670
2671 #[test]
2672 fn test_emit_outcomes() {
2673 for (serialized, deserialized) in &[
2674 ("true", EmitOutcomes::AsOutcomes),
2675 ("false", EmitOutcomes::None),
2676 ("\"as_client_reports\"", EmitOutcomes::AsClientReports),
2677 ] {
2678 let value: EmitOutcomes = serde_json::from_str(serialized).unwrap();
2679 assert_eq!(value, *deserialized);
2680 assert_eq!(serde_json::to_string(&value).unwrap(), *serialized);
2681 }
2682 }
2683
2684 #[test]
2685 fn test_emit_outcomes_invalid() {
2686 assert!(serde_json::from_str::<EmitOutcomes>("asdf").is_err());
2687 }
2688}