-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.rs
More file actions
544 lines (476 loc) · 19.8 KB
/
config.rs
File metadata and controls
544 lines (476 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use figment::{
providers::{Env, Format, Serialized, Yaml},
Figment,
};
use std::net::IpAddr;
use std::{borrow::Cow, collections::BTreeMap, net::SocketAddr};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::formats::CommaSeparator;
use serde_with::serde_as;
use std::str::FromStr;
use crate::{app::cli, logging};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfigProviderMode {
Redis,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProducerMode {
Kafka,
Vector,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckerMode {
Reqwest,
// XXX(epurkhiser): In the future there may be other implementations of the HttpChecker
// interface.
}
#[serde_as]
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct MetricsConfig {
/// The statsd address to report metrics to.
pub statsd_addr: SocketAddr,
/// Default tags to apply to all metrics.
pub default_tags: BTreeMap<String, String>,
/// Tag name to report the hostname to for each metric. Defaults to not sending such a tag.
pub hostname_tag: Option<String>,
}
#[serde_as]
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct KafkaConfig {
/// Kafka security protocol to use. The value must be one of "plaintext, "ssl", "sasl_plaintext", "sasl_ssl".
/// If not specified, defaults to "plaintext".
pub kafka_security_protocol: Option<String>,
/// TLS CA certificate location for Kafka.
pub kafka_ssl_ca_location: Option<String>,
/// TLS certificate location for Kafka.
pub kafka_ssl_cert_location: Option<String>,
/// TLS private key location for Kafka.
pub kafka_ssl_key_location: Option<String>,
/// SASL mechanism to use for Kafka. The value must be one of "PLAIN", "SCRAM-SHA-256", "SCRAM-SHA-512".
pub kafka_sasl_mechanism: Option<String>,
/// SASL username for Kafka.
pub kafka_sasl_username: Option<String>,
/// SASL password for Kafka.
pub kafka_sasl_password: Option<String>,
}
#[serde_as]
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Config {
/// The sentry DSN to use for error reporting.
pub sentry_dsn: Option<String>,
/// The environment to report to sentry errors to.
pub sentry_env: Option<Cow<'static, str>>,
/// The log level to filter logging to.
pub log_level: logging::Level,
/// The log format to output.
pub log_format: logging::LogFormat,
/// The number of HTTP checks that will be executed at once.
pub checker_concurrency: usize,
/// Whether the HTTP checks are multiplexed on more than one thread.
pub checker_parallel: bool,
/// The network interface to bind the uptime checker HTTP client to if set.
pub interface: Option<String>,
/// Metric configurations
#[serde(flatten)]
pub metrics: MetricsConfig,
/// The kafka cluster to report results to. Expected to be a string of comma separated
/// addresses.
///
/// ```txt
/// 10.0.0.1:5000,10.0.0.2:6000
/// ```
#[serde_as(as = "serde_with::StringWithSeparator::<CommaSeparator, String>")]
pub results_kafka_cluster: Vec<String>,
/// The topic to produce uptime checks into.
pub results_kafka_topic: String,
/// Kafka extended configuration
#[serde(flatten)]
pub kafka_config: KafkaConfig,
/// Which config provider to use to load configs into memory
pub config_provider_mode: ConfigProviderMode,
/// Which checker implementation to use to run the HTTP checks.
pub checker_mode: CheckerMode,
/// which producer to use to send results
pub producer_mode: ProducerMode,
/// How frequently to poll redis for config updates when using the redis config provider
pub config_provider_redis_update_ms: u64,
/// How many config partitions do we want to keep in redis? We shouldn't change this once
/// assigned unless we plan a migration process.
pub config_provider_redis_total_partitions: u16,
/// The batch size to use for vector producer
pub vector_batch_size: usize,
/// The vector endpoint to send results to
pub vector_endpoint: String,
/// Whether we're using redis in standalone or cluster mode
pub redis_enable_cluster: bool,
/// The general purpose redis node to use with this service
pub redis_host: String,
/// The region that this checker is running in
#[serde(default, deserialize_with = "deserialize_region")]
pub region: &'static str,
/// Allow uptime checks against internal IP addresses
pub allow_internal_ips: bool,
/// Whether to disable connection re-use in the http checker
pub disable_connection_reuse: bool,
/// Whether to record metrics for check executor tasks. May be used to diagnose scheduling
/// problems with the http check executors.
pub record_task_metrics: bool,
/// Sets the maximum time in seconds to keep idle sockets alive in the http checker.
pub pool_idle_timeout_secs: u64,
/// The unique index of this checker out of the total nuimber of checkers. Should be
/// zero-indexed.
pub checker_number: u16,
/// Total number of uptime checkers running
pub total_checkers: u16,
/// The number of times to retry failed checks before reporting them as failed
pub failure_retries: u16,
/// How many threads we should create per cpu core
pub thread_cpu_scale_factor: usize,
/// DNS name servers to use when making checks in the http checker
#[serde(default, deserialize_with = "deserialize_nameservers")]
pub http_checker_dns_nameservers: Option<Vec<IpAddr>>,
/// Redis connection/response timeouts, in milliseconds.
pub redis_timeouts_ms: u64,
/// Whether to collect connection-level metrics (only available on Isahc)
pub enable_metrics: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
sentry_dsn: None,
sentry_env: None,
checker_concurrency: 200,
checker_parallel: false,
log_level: logging::Level::Warn,
log_format: logging::LogFormat::Auto,
interface: None,
metrics: MetricsConfig {
statsd_addr: "127.0.0.1:8126".parse().expect("Parsable by construction"),
default_tags: BTreeMap::new(),
hostname_tag: None,
},
results_kafka_cluster: vec!["127.0.0.1:9092".to_owned()],
results_kafka_topic: "uptime-results".to_owned(),
kafka_config: KafkaConfig {
kafka_security_protocol: None,
kafka_ssl_ca_location: None,
kafka_ssl_cert_location: None,
kafka_ssl_key_location: None,
kafka_sasl_mechanism: None,
kafka_sasl_username: None,
kafka_sasl_password: None,
},
config_provider_mode: ConfigProviderMode::Redis,
checker_mode: CheckerMode::Reqwest,
vector_batch_size: 10,
vector_endpoint: "http://localhost:8020".to_owned(),
producer_mode: ProducerMode::Kafka,
config_provider_redis_update_ms: 1000,
config_provider_redis_total_partitions: 128,
redis_enable_cluster: false,
redis_host: "redis://127.0.0.1:6379".to_owned(),
region: "default",
allow_internal_ips: false,
disable_connection_reuse: true,
pool_idle_timeout_secs: 90,
record_task_metrics: false,
checker_number: 0,
total_checkers: 1,
failure_retries: 0,
http_checker_dns_nameservers: None,
thread_cpu_scale_factor: 1,
redis_timeouts_ms: 30_000,
enable_metrics: false,
}
}
}
impl Config {
/// Load configuration from an optional configuration file and environment
pub fn extract(app: &cli::CliApp) -> anyhow::Result<Config> {
let mut builder = Figment::from(Serialized::defaults(Config::default()));
if let Some(path) = &app.config {
builder = builder.merge(Yaml::file(path));
};
// Override with env variables if provided
builder = builder.merge(Env::prefixed("UPTIME_CHECKER_"));
// Override some values from the CliApp
if let Some(log_level) = app.log_level {
builder = builder.merge(Serialized::default("log_level", log_level))
}
if let Some(log_format) = app.log_format {
builder = builder.merge(Serialized::default("log_format", log_format))
}
let config: Config = builder.extract()?;
Ok(config)
}
}
fn deserialize_region<'de, D>(deserializer: D) -> Result<&'static str, D::Error>
where
D: Deserializer<'de>,
{
let s: String = String::deserialize(deserializer)?;
// We're going to deliberately leak the region string here--at the cost of some unfreeable
// memory, we eliminate a major source of clones, especially in the inner-loops of the
// schedueler and executor.
Ok(s.leak())
}
fn deserialize_nameservers<'de, D>(deserializer: D) -> Result<Option<Vec<IpAddr>>, D::Error>
where
D: Deserializer<'de>,
{
let s: Option<String> = Option::deserialize(deserializer)?;
Ok(match s {
None => None,
Some(s) if s.is_empty() => None,
Some(s) => Some(
s.split(',')
.map(|s| IpAddr::from_str(s.trim()))
.collect::<Result<Vec<_>, _>>()
.map_err(serde::de::Error::custom)?,
),
})
}
#[cfg(test)]
mod tests {
use figment::Jail;
use similar_asserts::assert_eq;
use std::net::IpAddr;
use std::{borrow::Cow, collections::BTreeMap, path::PathBuf};
use crate::{app::cli, logging};
use super::{
CheckerMode, Config, ConfigProviderMode, KafkaConfig, MetricsConfig, ProducerMode,
};
fn test_with_config<F>(yaml: &str, env_vars: &[(&str, &str)], test_fn: F)
where
F: FnOnce(Config),
{
Jail::expect_with(|jail| {
jail.create_file("config.yaml", yaml)?;
for (key, value) in env_vars {
jail.set_env(key, value);
}
let app = cli::CliApp {
config: Some(PathBuf::from("config.yaml")),
log_level: None,
log_format: None,
command: cli::Commands::Run,
};
let config = Config::extract(&app).unwrap();
test_fn(config);
Ok(())
})
}
#[test]
fn test_simple() {
test_with_config(
r#"
sentry_dsn: my_dsn
sentry_env: my_env
checker_concurrency: 100
results_kafka_cluster: '10.0.0.1,10.0.0.2:9000'
configs_kafka_cluster: '10.0.0.1,10.0.0.2:9000'
statsd_addr: 10.0.0.1:8126
default_tags: {"someTag": "value"}
hostname_tag: pod_name
redis_host: redis://127.0.0.1:6379
"#,
&[],
|config| {
assert_eq!(
config,
Config {
sentry_dsn: Some("my_dsn".to_owned()),
sentry_env: Some(Cow::from("my_env")),
checker_concurrency: 100,
checker_parallel: false,
log_level: logging::Level::Warn,
log_format: logging::LogFormat::Auto,
interface: None,
metrics: MetricsConfig {
statsd_addr: "10.0.0.1:8126".parse().unwrap(),
default_tags: BTreeMap::from([(
"someTag".to_owned(),
"value".to_owned()
),]),
hostname_tag: Some("pod_name".to_owned()),
},
results_kafka_cluster: vec![
"10.0.0.1".to_owned(),
"10.0.0.2:9000".to_owned()
],
results_kafka_topic: "uptime-results".to_owned(),
kafka_config: KafkaConfig {
kafka_security_protocol: None,
kafka_ssl_ca_location: None,
kafka_ssl_cert_location: None,
kafka_ssl_key_location: None,
kafka_sasl_mechanism: None,
kafka_sasl_username: None,
kafka_sasl_password: None,
},
config_provider_mode: ConfigProviderMode::Redis,
checker_mode: CheckerMode::Reqwest,
config_provider_redis_update_ms: 1000,
config_provider_redis_total_partitions: 128,
redis_enable_cluster: false,
redis_host: "redis://127.0.0.1:6379".to_owned(),
region: "default",
allow_internal_ips: false,
disable_connection_reuse: true,
record_task_metrics: false,
pool_idle_timeout_secs: 90,
checker_number: 0,
total_checkers: 1,
producer_mode: ProducerMode::Kafka,
vector_batch_size: 10,
vector_endpoint: "http://localhost:8020".to_owned(),
failure_retries: 0,
http_checker_dns_nameservers: None,
thread_cpu_scale_factor: 1,
redis_timeouts_ms: 30_000,
enable_metrics: false,
}
);
},
)
}
#[test]
fn test_overrides() {
test_with_config(
r#"
sentry_dsn: my_dsn
sentry_env: my_env
log_format: json
"#,
&[
("UPTIME_CHECKER_SENTRY_ENV", "my_env_override"),
(
"UPTIME_CHECKER_RESULTS_KAFKA_CLUSTER",
"10.0.0.1,10.0.0.2:7000",
),
(
"UPTIME_CHECKER_CONFIGS_KAFKA_CLUSTER",
"10.0.0.1,10.0.0.2:7000",
),
("UPTIME_CHECKER_KAFKA_SECURITY_PROTOCOL", "plaintext"),
("UPTIME_CHECKER_KAFKA_SSL_CA_LOCATION", "/path/to/ca.crt"),
(
"UPTIME_CHECKER_KAFKA_SSL_CERT_LOCATION",
"/path/to/cert.crt",
),
("UPTIME_CHECKER_KAFKA_SSL_KEY_LOCATION", "/path/to/key.key"),
("UPTIME_CHECKER_KAFKA_SASL_MECHANISM", "scram-sha-256"),
("UPTIME_CHECKER_KAFKA_SASL_USERNAME", "my_user"),
("UPTIME_CHECKER_KAFKA_SASL_PASSWORD", "my_password"),
("UPTIME_CHECKER_CONFIG_PROVIDER_MODE", "redis"),
("UPTIME_CHECKER_CONFIG_PROVIDER_REDIS_UPDATE_MS", "2000"),
(
"UPTIME_CHECKER_CONFIG_PROVIDER_REDIS_TOTAL_PARTITIONS",
"32",
),
("UPTIME_CHECKER_STATSD_ADDR", "10.0.0.1:1234"),
("UPTIME_CHECKER_REDIS_ENABLE_CLUSTER", "true"),
("UPTIME_CHECKER_REDIS_HOST", "10.0.0.3:6379"),
("UPTIME_CHECKER_REGION", "us-west"),
("UPTIME_CHECKER_ALLOW_INTERNAL_IPS", "true"),
("UPTIME_CHECKER_DISABLE_CONNECTION_REUSE", "false"),
("UPTIME_CHECKER_POOL_IDLE_TIMEOUT_SECS", "600"),
("UPTIME_CHECKER_CHECKER_NUMBER", "2"),
("UPTIME_CHECKER_TOTAL_CHECKERS", "5"),
("UPTIME_CHECKER_FAILURE_RETRIES", "2"),
("UPTIME_CHECKER_THREAD_CPU_SCALE_FACTOR", "3"),
(
"UPTIME_CHECKER_HTTP_CHECKER_DNS_NAMESERVERS",
"8.8.8.8,8.8.4.4",
),
("UPTIME_CHECKER_INTERFACE", "eth0"),
],
|config| {
assert_eq!(
config,
Config {
sentry_dsn: Some("my_dsn".to_owned()),
sentry_env: Some(Cow::from("my_env_override")),
checker_concurrency: 200,
checker_parallel: false,
log_level: logging::Level::Warn,
log_format: logging::LogFormat::Json,
interface: Some("eth0".to_owned()),
metrics: MetricsConfig {
statsd_addr: "10.0.0.1:1234".parse().unwrap(),
default_tags: BTreeMap::new(),
hostname_tag: None,
},
results_kafka_cluster: vec![
"10.0.0.1".to_owned(),
"10.0.0.2:7000".to_owned()
],
results_kafka_topic: "uptime-results".to_owned(),
kafka_config: KafkaConfig {
kafka_security_protocol: Some("plaintext".to_owned()),
kafka_ssl_ca_location: Some("/path/to/ca.crt".to_owned()),
kafka_ssl_cert_location: Some("/path/to/cert.crt".to_owned()),
kafka_ssl_key_location: Some("/path/to/key.key".to_owned()),
kafka_sasl_mechanism: Some("scram-sha-256".to_owned()),
kafka_sasl_username: Some("my_user".to_owned()),
kafka_sasl_password: Some("my_password".to_owned()),
},
config_provider_mode: ConfigProviderMode::Redis,
checker_mode: CheckerMode::Reqwest,
config_provider_redis_update_ms: 2000,
config_provider_redis_total_partitions: 32,
redis_enable_cluster: true,
redis_host: "10.0.0.3:6379".to_owned(),
region: "us-west",
allow_internal_ips: true,
disable_connection_reuse: false,
record_task_metrics: false,
pool_idle_timeout_secs: 600,
checker_number: 2,
total_checkers: 5,
producer_mode: ProducerMode::Kafka,
vector_batch_size: 10,
vector_endpoint: "http://localhost:8020".to_owned(),
failure_retries: 2,
http_checker_dns_nameservers: Some(vec![
IpAddr::from([8, 8, 8, 8]),
IpAddr::from([8, 8, 4, 4])
]),
thread_cpu_scale_factor: 3,
redis_timeouts_ms: 30_000,
enable_metrics: false,
}
);
},
)
}
#[test]
fn test_config_default_checker_ordinal() {
test_with_config(
r#"
sentry_dsn: my_dsn
sentry_env: my_env
"#,
&[],
|config| {
assert_eq!(config.checker_number, 0);
},
)
}
#[test]
fn test_config_empty_http_checker_dns_nameservers() {
test_with_config(
r#"
sentry_dsn: my_dsn
sentry_env: my_env
"#,
&[("UPTIME_CHECKER_HTTP_CHECKER_DNS_NAMESERVERS", "")],
|config| {
assert_eq!(config.http_checker_dns_nameservers, None);
},
)
}
}