libdd_trace_utils/send_data/
send_data_result.rs1use crate::send_with_retry::{SendWithRetryError, SendWithRetryResult};
5use anyhow::anyhow;
6use libdd_common::http_common;
7use std::collections::HashMap;
8
9#[derive(Debug)]
10pub struct SendDataResult {
11 pub last_result: anyhow::Result<http_common::HttpResponse>,
13 pub requests_count: u64,
15 pub responses_count_per_code: HashMap<u16, u64>,
17 pub errors_timeout: u64,
19 pub errors_network: u64,
21 pub errors_status_code: u64,
23 pub bytes_sent: u64,
25 pub chunks_sent: u64,
27 pub chunks_dropped: u64,
29}
30
31impl Default for SendDataResult {
32 fn default() -> Self {
33 SendDataResult {
34 last_result: Err(anyhow!("No requests sent")),
35 requests_count: 0,
36 responses_count_per_code: Default::default(),
37 errors_timeout: 0,
38 errors_network: 0,
39 errors_status_code: 0,
40 bytes_sent: 0,
41 chunks_sent: 0,
42 chunks_dropped: 0,
43 }
44 }
45}
46
47impl SendDataResult {
48 pub(crate) fn update(&mut self, res: SendWithRetryResult, bytes_sent: u64, chunks: u64) {
57 match res {
58 Ok((response, attempts)) => {
59 *self
60 .responses_count_per_code
61 .entry(response.status().as_u16())
62 .or_default() += 1;
63 self.bytes_sent += bytes_sent;
64 self.chunks_sent += chunks;
65 self.last_result = Ok(response);
66 self.requests_count += u64::from(attempts);
67 }
68 Err(err) => match err {
69 SendWithRetryError::Http(response, attempts) => {
70 let status_code = response.status().as_u16();
71 self.errors_status_code += 1;
72 *self
73 .responses_count_per_code
74 .entry(status_code)
75 .or_default() += 1;
76 self.chunks_dropped += chunks;
77 self.requests_count += u64::from(attempts);
78 self.last_result = Ok(response);
79 }
80 SendWithRetryError::Timeout(attempts) => {
81 self.errors_timeout += 1;
82 self.chunks_dropped += chunks;
83 self.requests_count += u64::from(attempts);
84 }
85 SendWithRetryError::Network(_, attempts) => {
86 self.errors_network += 1;
87 self.chunks_dropped += chunks;
88 self.requests_count += u64::from(attempts);
89 }
90 SendWithRetryError::Build(attempts) => {
91 self.chunks_dropped += chunks;
92 self.requests_count += u64::from(attempts);
93 }
94 },
95 }
96 }
97
98 pub(crate) fn error(mut self, err: anyhow::Error) -> SendDataResult {
106 self.last_result = Err(err);
107 self
108 }
109}