-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathreqwest_checker.rs
More file actions
1621 lines (1427 loc) · 54.7 KB
/
reqwest_checker.rs
File metadata and controls
1621 lines (1427 loc) · 54.7 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::ip_filter::is_external_ip;
use super::{make_trace_header, make_trace_id, Checker};
use crate::assertions::compiled::extract_failure_data;
use crate::assertions::{self, Assertion};
use crate::check_executor::ScheduledCheck;
use crate::types::result::{to_request_info_list, Check, RequestDurations, Timing};
use crate::types::{
check_config::CheckConfig,
result::{CheckResult, CheckStatus, CheckStatusReason, CheckStatusReasonType, RequestInfo},
};
use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine as _};
use chrono::{DateTime, TimeDelta, Utc};
use hyper::stats::RequestId;
use openssl::error::ErrorStack;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Client, ClientBuilder, Response, Url};
use sentry::protocol::SpanId;
use std::error::Error;
use std::net::IpAddr;
use std::time::Duration;
use texting_robots::Robot;
use tokio::time::{timeout, Instant};
use uuid::Uuid;
const UPTIME_USER_AGENT: &str =
"SentryUptimeBot/1.0 (+http://docs.sentry.io/product/alerts/uptime-monitoring/)";
const ROBOTS_TIMEOUT: Duration = Duration::from_secs(5);
const MAX_ROBOTS_BYTES: usize = 10_000;
const MAX_BODY_BYTES: usize = 10_000;
/// Responsible for making HTTP requests to check if a domain is up.
#[derive(Debug)]
pub struct ReqwestChecker {
client: Client,
assert_cache: assertions::cache::Cache,
disable_assertions: bool,
response_capture_enabled: bool,
assertion_complexity: u32,
max_assertion_ops: u32,
}
struct Options {
/// When set to true (the default) resolution to internal network addresses will be restricted.
/// This should primarily be disabled for tests.
validate_url: bool,
/// When set to true sets the pool_max_idle_per_host to 0. Effectively removing connection
/// pooling and forcing a new connection for each new request. This may help reduce connection
/// errors due to connections being held open too long.
disable_connection_reuse: bool,
pool_idle_timeout: Duration,
dns_nameservers: Option<Vec<IpAddr>>,
/// Specifies the network interface to bind the client to.
interface: Option<String>,
/// Disable runtime assertion evaluation
disable_assertions: bool,
/// Enable response capture feature. When enabled and the check fails,
/// response body and headers will be captured and included in the result.
response_capture_enabled: bool,
assertion_complexity: u32,
max_assertion_ops: u32,
}
impl Default for Options {
fn default() -> Self {
Self {
validate_url: true,
disable_connection_reuse: false,
pool_idle_timeout: Duration::from_secs(90),
dns_nameservers: None,
interface: None,
disable_assertions: false,
response_capture_enabled: false,
assertion_complexity: 100,
max_assertion_ops: 16,
}
}
}
/// Fetches the response from a URL.
async fn do_request(
client: &Client,
check_config: &CheckConfig,
sentry_trace: &str,
) -> Result<(Response, RequestId), reqwest::Error> {
let timeout = check_config
.timeout
.to_std()
.expect("Timeout duration should be representable as a duration");
let url = check_config.url.as_str();
let headers: HeaderMap = check_config
.request_headers
.clone()
.into_iter()
.filter_map(|(key, value)| {
// Try to convert key and value to HeaderName and HeaderValue
let header_name = HeaderName::try_from(key).ok()?;
let header_value = HeaderValue::from_str(&value).ok()?;
Some((header_name, header_value))
})
.collect();
let req = client
.request(check_config.request_method.into(), url)
.timeout(timeout)
.headers(headers)
.header("sentry-trace", sentry_trace.to_owned())
.body(check_config.request_body.to_owned())
.build()?;
let req_id = req.req_id().clone();
let resp = client.execute(req).await?;
Ok((resp, req_id))
}
/// Check if the request error is a DNS error.
fn dns_error(err: &reqwest::Error) -> Option<String> {
let mut inner = &err as &dyn Error;
while let Some(source) = inner.source() {
inner = source;
if let Some(inner_err) = source.downcast_ref::<hickory_resolver::error::ResolveError>() {
return Some(format!("{inner_err}"));
}
}
None
}
fn tls_error(err: &reqwest::Error) -> Option<String> {
let mut inner = &err as &dyn Error;
while let Some(source) = inner.source() {
if let Some(e) = source.downcast_ref::<ErrorStack>() {
return Some(
e.errors()
.iter()
.map(|e| e.reason().unwrap_or("unknown error"))
.collect::<Vec<_>>()
.join(", "),
);
}
inner = source;
}
None
}
fn connection_error(err: &reqwest::Error) -> Option<String> {
let mut inner = &err as &dyn Error;
while let Some(source) = inner.source() {
if let Some(io_err) = source.downcast_ref::<std::io::Error>() {
// TODO: should we return the OS code error as well?
if io_err.kind() == std::io::ErrorKind::ConnectionRefused {
return Some("Connection refused".to_string());
} else if io_err.kind() == std::io::ErrorKind::ConnectionReset {
return Some("Connection reset".to_string());
}
}
inner = source;
}
None
}
fn hyper_error(err: &reqwest::Error) -> Option<(CheckStatusReasonType, String)> {
let mut inner = &err as &dyn Error;
while let Some(source) = inner.source() {
if let Some(hyper_error) = source.downcast_ref::<hyper::Error>() {
if hyper_error.is_incomplete_message() {
return Some((
CheckStatusReasonType::ConnectionError,
hyper_error.to_string(),
));
}
return Some((CheckStatusReasonType::Failure, hyper_error.to_string()));
}
inner = source;
}
None
}
fn hyper_util_error(err: &reqwest::Error) -> Option<(CheckStatusReasonType, String)> {
let mut inner = &err as &dyn Error;
while let Some(source) = inner.source() {
if let Some(hyper_util_error) = source.downcast_ref::<hyper_util::client::legacy::Error>() {
if hyper_util_error.is_connect() {
return Some((
CheckStatusReasonType::ConnectionError,
hyper_util_error.to_string(),
));
}
return Some((CheckStatusReasonType::Failure, hyper_util_error.to_string()));
}
inner = source;
}
None
}
impl ReqwestChecker {
fn new_internal(options: Options, assert_cache: assertions::cache::Cache) -> Self {
let mut default_headers = HeaderMap::new();
default_headers.insert(
"User-Agent",
UPTIME_USER_AGENT
.to_string()
.parse()
.expect("Valid by construction"),
);
let mut builder = ClientBuilder::new()
.hickory_dns(true)
.default_headers(default_headers)
.pool_idle_timeout(options.pool_idle_timeout);
builder = builder.tls_info(true);
if options.validate_url {
builder = builder.ip_filter(is_external_ip);
}
if let Some(dns_nameservers) = options.dns_nameservers {
builder = builder.dns_nameservers(dns_nameservers)
}
if options.disable_connection_reuse {
builder = builder.pool_max_idle_per_host(0);
}
#[cfg(not(target_os = "linux"))]
if options.interface.is_some() {
tracing::info!("HTTP Client interface can only be configured for the linux platform");
}
#[cfg(target_os = "linux")]
if let Some(nic) = options.interface {
builder = builder.interface(&nic);
}
let client = builder.build().expect("builder should be buildable");
Self {
client,
assert_cache,
disable_assertions: options.disable_assertions,
response_capture_enabled: options.response_capture_enabled,
assertion_complexity: options.assertion_complexity,
max_assertion_ops: options.max_assertion_ops,
}
}
#[allow(clippy::too_many_arguments)]
pub fn new(
validate_url: bool,
disable_connection_reuse: bool,
pool_idle_timeout: Duration,
dns_nameservers: Option<Vec<IpAddr>>,
interface: Option<String>,
assert_cache: assertions::cache::Cache,
disable_assertions: bool,
response_capture_enabled: bool,
assertion_complexity: u32,
max_assertion_ops: u32,
) -> Self {
Self::new_internal(
Options {
validate_url,
disable_connection_reuse,
pool_idle_timeout,
dns_nameservers,
interface,
disable_assertions,
response_capture_enabled,
assertion_complexity,
max_assertion_ops,
},
assert_cache,
)
}
}
async fn read_body_bounded(
time_allotment: Duration,
max_bytes: usize,
start_time: Instant,
res: &mut Response,
) -> Vec<u8> {
let mut all_bytes = vec![];
loop {
let maybe_timeout = timeout(
time_allotment.saturating_sub(start_time.elapsed()),
res.chunk(),
)
.await;
let Ok(maybe_conn_err) = maybe_timeout else {
tracing::info!("waited too long for body");
break all_bytes;
};
let Ok(maybe_chunk) = maybe_conn_err else {
tracing::info!("connection error during body");
break all_bytes;
};
let Some(chunk) = maybe_chunk else {
break all_bytes;
};
all_bytes.extend_from_slice(&chunk);
if all_bytes.len() > max_bytes {
tracing::info!("aborting huge body");
break all_bytes;
}
}
}
#[allow(clippy::too_many_arguments)]
fn to_check_result(
assert_cache: &assertions::cache::Cache,
response: Result<(Response, RequestId), reqwest::Error>,
check: &ScheduledCheck,
body_bytes: &[u8],
disable_assertions: bool,
assertion_complexity: u32,
max_assertion_ops: u32,
region: &'static str,
) -> Check {
match response {
Ok((r, _)) => {
if !disable_assertions {
if let Some(assertion) = &check.get_config().assertion {
run_assertion(
assert_cache,
body_bytes,
&check.get_config().subscription_id,
&r,
assertion,
assertion_complexity,
max_assertion_ops,
region,
)
} else {
Check::success()
}
} else {
Check::success()
}
}
Err(e) => Check::other_failure(e.into()),
}
}
#[allow(clippy::too_many_arguments)]
fn run_assertion(
assert_cache: &assertions::cache::Cache,
body_bytes: &[u8],
subscription_id: &Uuid,
r: &Response,
assertion: &assertions::Assertion,
assertion_complexity: u32,
max_assertion_ops: u32,
region: &'static str,
) -> Check {
let comp_assert = assert_cache.get_or_compile(assertion, max_assertion_ops, region);
let assertion = match comp_assert {
Err(err) => {
tracing::warn!(
"a bad assertion made it to compile from {} : {}",
subscription_id,
err.to_string(),
);
return Check::assert_compile_failure(&err);
}
Ok(assertion) => assertion,
};
let result = assertion.eval(
r.status().as_u16(),
r.headers(),
body_bytes,
assertion_complexity,
region,
);
result.into()
}
impl From<reqwest::Error> for CheckStatusReason {
fn from(e: reqwest::Error) -> Self {
if e.is_timeout() {
CheckStatusReason {
status_type: CheckStatusReasonType::Timeout,
description: "Request timed out".to_string(),
details: None,
}
} else if e.is_redirect() {
CheckStatusReason {
status_type: CheckStatusReasonType::RedirectError,
description: "Too many redirects".to_string(),
details: None,
}
} else if let Some(message) = dns_error(&e) {
CheckStatusReason {
status_type: CheckStatusReasonType::DnsError,
description: message,
details: None,
}
} else if let Some(message) = tls_error(&e) {
CheckStatusReason {
status_type: CheckStatusReasonType::TlsError,
description: message,
details: None,
}
} else if let Some(message) = connection_error(&e) {
CheckStatusReason {
status_type: CheckStatusReasonType::ConnectionError,
description: message,
details: None,
}
} else if let Some((status_type, message)) = hyper_error(&e) {
CheckStatusReason {
status_type,
description: message,
details: None,
}
} else if let Some((status_type, message)) = hyper_util_error(&e) {
CheckStatusReason {
status_type,
description: message,
details: None,
}
} else {
// if any error falls through we should log it,
// none should fall through.
let error_msg = e.without_url();
tracing::info!("check_url.error: {:?}", error_msg);
CheckStatusReason {
status_type: CheckStatusReasonType::Failure,
description: format!("{error_msg:?}"),
details: None,
}
}
}
}
fn to_errored_request_infos(
actual_check_time: &DateTime<Utc>,
start: Instant,
err: &reqwest::Error,
check: &ScheduledCheck,
) -> Vec<RequestInfo> {
// This is a best-effort at getting timings for the individual bits of a connection-oriented error.
// Surfacing the timings for each part of DNS/TCP connect/TLS negotiation _in the event of a
// connection error_ will require some effort, so for now, just bill the full time to the part that
// we failed on, leaving the others at zero.
let request_duration =
TimeDelta::from_std(start.elapsed()).expect("duration shouldn't be large");
let zero_timing = Timing {
start_us: actual_check_time.timestamp_micros() as u128,
duration_us: 0,
};
let full_duration = Timing {
start_us: actual_check_time.timestamp_micros() as u128,
duration_us: request_duration.num_microseconds().unwrap() as u64,
};
let mut dns_timing = zero_timing;
let mut connection_timing = zero_timing;
let mut tls_timing = zero_timing;
let mut send_request_timing = zero_timing;
if dns_error(err).is_some() {
dns_timing = full_duration
} else if connection_error(err).is_some() {
connection_timing = full_duration
} else if tls_error(err).is_some() {
tls_timing = full_duration
} else {
tracing::info!("unknown reqwest error during check: {}", err.to_string());
send_request_timing = full_duration
};
let http_status_code = err.status().map(|s| s.as_u16());
vec![RequestInfo {
http_status_code,
request_type: check.get_config().request_method,
request_body_size_bytes: check.get_config().request_body.len() as u32,
url: check.get_config().url.clone(),
response_body_size_bytes: 0,
request_duration_us: request_duration.num_microseconds().unwrap() as u64,
durations: RequestDurations {
dns_lookup: dns_timing,
tcp_connection: connection_timing,
tls_handshake: tls_timing,
time_to_first_byte: zero_timing,
send_request: send_request_timing,
receive_response: zero_timing,
},
certificate_info: None,
response_body: None,
response_headers: None,
}]
}
impl Checker for ReqwestChecker {
/// Makes a request to a url to determine whether it is up.
/// Up is defined as responding within a specific timeframe, along with passing
/// an optional user-defined assert that is specified by the user which can use
/// the result json, status code, and response header values.
#[tracing::instrument]
async fn check_url(&self, check: &ScheduledCheck, region: &'static str) -> CheckResult {
let scheduled_check_time = check.get_tick().time();
let actual_check_time = Utc::now();
let span_id = SpanId::default();
let trace_id = make_trace_id(check.get_config(), check.get_tick(), check.get_retry());
let trace_header = make_trace_header(check.get_config(), &trace_id, span_id);
let start = Instant::now();
let mut response = do_request(&self.client, check.get_config(), &trace_header).await;
let force_capture = check.should_force_capture();
// Determine if we should capture response data on failure
let should_capture = force_capture
|| (self.response_capture_enabled && check.get_config().capture_response_on_failure);
// Read body bytes if we have an assertion OR if we should capture on failure.
let needs_body_for_assertion = if let Some(assertion) = &check.get_config().assertion {
assertion.requires_body()
} else {
false
};
let body_bytes = if (needs_body_for_assertion || should_capture) && response.is_ok() {
let Ok((resp, _req_id)) = &mut response else {
unreachable!("enclosing if-statement means this cannot happen");
};
read_body_bounded(
Duration::from_millis(check.get_config().timeout.num_milliseconds() as u64),
MAX_BODY_BYTES,
start,
resp,
)
.await
} else {
vec![]
};
let captured_headers: Option<Vec<(String, String)>> = if should_capture {
response.as_ref().ok().map(|(resp, _)| {
resp.headers()
.iter()
.map(|(name, value)| {
(
name.to_string(),
value.to_str().unwrap_or("<non-utf8>").to_string(),
)
})
.collect()
})
} else {
None
};
// TODO: this is how to extract the leaf cert from the request we run.
// let cert = response
// .as_ref()
// .map(|resp| resp.extensions().get::<reqwest::tls::TlsInfo>());
// if let Ok(Some(cert)) = cert {
// eprintln!(
// "got a cert: {}",
// cert.peer_certificate().map_or(12345, |bytes| bytes.len())
// );
// }
let rinfos = match &response {
Ok((_resp, req_id)) => {
let stats = hyper::stats::consume_request_stats(req_id.clone());
to_request_info_list(&stats, check.get_config().request_method)
}
Err(err) => to_errored_request_infos(&actual_check_time, start, err, check),
};
let check_result = to_check_result(
&self.assert_cache,
response,
check,
&body_bytes,
self.disable_assertions,
self.assertion_complexity,
self.max_assertion_ops,
region,
);
// Our total duration includes the additional processing time, including running the assert.
let duration = TimeDelta::from_std(start.elapsed()).expect("duration shouldn't be large");
let mut rinfos = rinfos;
// Add captured response data if this is a failure
if force_capture || (should_capture && check_result.result == CheckStatus::Failure) {
if let Some(last_req) = rinfos.last_mut() {
// Base64 encode the body and truncate if needed
if !body_bytes.is_empty() {
last_req.response_body = Some(BASE64_STANDARD.encode(&body_bytes));
}
last_req.response_headers = captured_headers;
}
}
let final_req = rinfos.last().unwrap().clone();
let assertion_failure_data = if let Some(path) = check_result.assert_path {
Assertion {
root: extract_failure_data(
&path,
&check
.get_config()
.assertion
.as_ref()
.expect("cannot have assertion failure data with an assertion")
.root,
),
}
.into()
} else {
None
};
CheckResult {
guid: trace_id,
subscription_id: check.get_config().subscription_id,
status: check_result.result,
status_reason: check_result.reason,
trace_id,
span_id,
scheduled_check_time,
scheduled_check_time_us: scheduled_check_time,
actual_check_time,
actual_check_time_us: actual_check_time,
duration: Some(duration),
duration_us: Some(duration),
request_info: Some(final_req),
region,
request_info_list: rinfos,
assertion_failure_data,
}
}
async fn check_robots(
&self,
check: &ScheduledCheck,
region: &'static str,
) -> Option<CheckResult> {
let Ok(url) = check.get_config().url.parse::<Url>() else {
return None;
};
let mut robots_url = url.clone();
robots_url.set_path("robots.txt");
let robots_txt = {
// Request a robots.txt, bounding both the get call as well as the body-stream to a 10 second
// window.
let start_time = Instant::now();
let time_allotment = ROBOTS_TIMEOUT;
let res = self
.client
.get(robots_url)
.timeout(time_allotment)
.send()
.await;
let Ok(mut res) = res else {
tracing::debug!("could not retrieve robots.txt");
return None;
};
read_body_bounded(time_allotment, MAX_ROBOTS_BYTES, start_time, &mut res).await
};
let Ok(r) = Robot::new("SentryUptimeBot", &robots_txt) else {
tracing::info!("Could not create Robot");
return None;
};
if r.allowed(url.as_str()) {
return None;
}
let scheduled_check_time = check.get_tick().time();
let actual_check_time = Utc::now();
let span_id = SpanId::default();
let trace_id = make_trace_id(check.get_config(), check.get_tick(), check.get_retry());
Some(CheckResult {
guid: trace_id,
subscription_id: check.get_config().subscription_id,
status: CheckStatus::DisallowedByRobots,
status_reason: None,
trace_id,
span_id,
scheduled_check_time,
scheduled_check_time_us: scheduled_check_time,
actual_check_time,
actual_check_time_us: actual_check_time,
duration: None,
duration_us: None,
request_info: None,
region,
request_info_list: Vec::new(),
assertion_failure_data: None,
})
}
}
#[cfg(test)]
mod tests {
use crate::assertions::{self, Assertion};
use crate::check_executor::ScheduledCheck;
use crate::checker::Checker;
use crate::config_store::Tick;
use crate::types::check_config::CheckConfig;
use crate::types::result::{CheckStatus, CheckStatusReasonType};
use crate::types::shared::RequestMethod;
use std::net::IpAddr;
use super::{make_trace_header, Options, ReqwestChecker, BASE64_STANDARD, UPTIME_USER_AGENT};
use base64::Engine;
use chrono::{TimeDelta, Utc};
use httpmock::prelude::*;
use httpmock::Method;
use sentry::protocol::SpanId;
use uuid::Uuid;
#[cfg(target_os = "linux")]
use {
rcgen::{Certificate, CertificateParams},
rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer},
rustls::ServerConfig,
std::sync::Arc,
tokio::io::AsyncWriteExt,
tokio::net::TcpListener,
tokio_rustls::TlsAcceptor,
};
fn make_tick() -> Tick {
Tick::from_time(Utc::now() - TimeDelta::seconds(60))
}
#[tokio::test]
async fn test_default_get() {
let server = MockServer::start();
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
..Default::default()
},
assertions::cache::Cache::new(),
);
let get_mock = server.mock(|when, then| {
when.method(Method::GET)
.path("/no-head")
.header_exists("sentry-trace")
.header("User-Agent", UPTIME_USER_AGENT.to_string());
then.status(200);
});
let config = CheckConfig {
url: server.url("/no-head").to_string(),
..Default::default()
};
let tick = make_tick();
let check = ScheduledCheck::new_for_test(tick, config);
let result = checker.check_url(&check, "us-west").await;
assert_eq!(result.status, CheckStatus::Success);
assert_eq!(
result.request_info.as_ref().map(|i| i.request_type),
Some(RequestMethod::Get)
);
get_mock.assert();
}
#[tokio::test]
async fn test_configured_post() {
let server = MockServer::start();
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
..Default::default()
},
assertions::cache::Cache::new(),
);
let get_mock = server.mock(|when, then| {
when.method(Method::POST)
.path("/no-head")
.header_exists("sentry-trace")
.body("{\"key\":\"value\"}")
.header("User-Agent", UPTIME_USER_AGENT.to_string())
.header("Authorization", "Bearer my-token".to_string())
.header("X-My-Custom-Header", "value".to_string());
then.status(200);
});
let config = CheckConfig {
url: server.url("/no-head").to_string(),
request_method: RequestMethod::Post,
request_headers: vec![
("Authorization".to_string(), "Bearer my-token".to_string()),
("X-My-Custom-Header".to_string(), "value".to_string()),
],
request_body: "{\"key\":\"value\"}".to_string(),
..Default::default()
};
let tick = make_tick();
let check = ScheduledCheck::new_for_test(tick, config);
let result = checker.check_url(&check, "us-west").await;
assert_eq!(result.status, CheckStatus::Success);
assert_eq!(
result.request_info.as_ref().map(|i| i.request_type),
Some(RequestMethod::Post)
);
get_mock.assert();
}
#[tokio::test]
async fn test_simple_timeout() {
static TIMEOUT: i64 = 200;
let server = MockServer::start();
let timeout = TimeDelta::milliseconds(TIMEOUT);
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
..Default::default()
},
assertions::cache::Cache::new(),
);
let timeout_mock = server.mock(|when, then| {
when.method(Method::GET)
.path("/timeout")
.header_exists("sentry-trace");
then.delay((timeout + TimeDelta::milliseconds(200)).to_std().unwrap())
.status(200);
});
let config = CheckConfig {
url: server.url("/timeout").to_string(),
timeout,
..Default::default()
};
let tick = make_tick();
let check = ScheduledCheck::new_for_test(tick, config);
let result = checker.check_url(&check, "us-west").await;
assert_eq!(result.status, CheckStatus::Failure);
assert!(result.duration.is_some_and(|d| d > timeout));
assert_eq!(result.request_info.and_then(|i| i.http_status_code), None);
assert_eq!(
result.status_reason.as_ref().map(|r| r.description.clone()),
Some("Request timed out".to_string())
);
assert_eq!(
result.status_reason.map(|r| r.status_type),
Some(CheckStatusReasonType::Timeout)
);
timeout_mock.assert();
}
#[tokio::test]
async fn test_simple_400_no_assertion() {
let server = MockServer::start();
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
..Default::default()
},
assertions::cache::Cache::new(),
);
let head_mock = server.mock(|when, then| {
when.method(Method::GET)
.path("/get")
.header_exists("sentry-trace");
then.status(400);
});
let config = CheckConfig {
url: server.url("/get").to_string(),
..Default::default()
};
let tick = make_tick();
let check = ScheduledCheck::new_for_test(tick, config);
let result = checker.check_url(&check, "us-west").await;
// Without an assertion, a non-2xx response is still considered a success.
// Callers are expected to configure a status code assertion to validate the response.
assert_eq!(result.status, CheckStatus::Success);
assert_eq!(
result.request_info.and_then(|i| i.http_status_code),
Some(400)
);
head_mock.assert();
}
#[tokio::test]
async fn test_response_capture_on_failure() {
let server = MockServer::start();
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
response_capture_enabled: true,
..Default::default()
},
assertions::cache::Cache::new(),
);
let mock = server.mock(|when, then| {
when.method(Method::GET)
.path("/error")
.header_exists("sentry-trace");
then.status(500)
.header("X-Error-Code", "ERR123")
.header("Content-Type", "application/json")
.body(r#"{"error": "something went wrong"}"#);
});
let config = CheckConfig {
url: server.url("/error").to_string(),
assertion: crate::assertions::Assertion {
root: crate::assertions::Op::StatusCodeCheck {
value: 500,
operator: crate::assertions::Comparison::NotEqual,
},
}
.into(),
..Default::default()
};
let tick = make_tick();
let check = ScheduledCheck::new_for_test(tick, config);
let result = checker.check_url(&check, "us-west").await;
assert_eq!(result.status, CheckStatus::Failure);
let request_info = result.request_info.unwrap();
assert_eq!(request_info.http_status_code, Some(500));
// Verify response body is captured and base64 encoded
let body = request_info.response_body.unwrap();
let decoded = BASE64_STANDARD.decode(&body).unwrap();
assert_eq!(
String::from_utf8(decoded).unwrap(),
r#"{"error": "something went wrong"}"#
);
// Verify response headers are captured
let headers = request_info.response_headers.unwrap();
assert!(headers
.iter()
.any(|(k, v)| k == "x-error-code" && v == "ERR123"));
assert!(headers
.iter()
.any(|(k, v)| k == "content-type" && v == "application/json"));
mock.assert();
}
#[tokio::test]
async fn test_response_capture_disabled() {
let server = MockServer::start();
let checker = ReqwestChecker::new_internal(
Options {
validate_url: false,
disable_connection_reuse: true,
response_capture_enabled: false, // disabled
..Default::default()
},
assertions::cache::Cache::new(),
);
let mock = server.mock(|when, then| {