Skip to main content

libdd_trace_utils/send_data/
send_data_result.rs

1// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4use 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    /// Keeps track of the last request result.
12    pub last_result: anyhow::Result<http_common::HttpResponse>,
13    /// Count metric for 'trace_api.requests'.
14    pub requests_count: u64,
15    /// Count metric for 'trace_api.responses'. Each key maps a different HTTP status code.
16    pub responses_count_per_code: HashMap<u16, u64>,
17    /// Count metric for 'trace_api.errors' (type: timeout).
18    pub errors_timeout: u64,
19    /// Count metric for 'trace_api.errors' (type: network).
20    pub errors_network: u64,
21    /// Count metric for 'trace_api.errors' (type: status_code).
22    pub errors_status_code: u64,
23    /// Count metric for 'trace_api.bytes'
24    pub bytes_sent: u64,
25    /// Count metric for 'trace_chunks_sent'
26    pub chunks_sent: u64,
27    /// Count metric for 'trace_chunks_dropped'
28    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    ///
49    /// Updates [`SendDataResult`] internal information with the request's result information.
50    ///
51    /// # Arguments
52    ///
53    /// * `res` -  [`SendWithRetryResult`].
54    /// * `bytes_sent` -  Number of bytes in the payload sent.
55    /// * `chunks` -  Number of chunks sent or dropped in the request.
56    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    ///
99    /// Sets `SendDataResult` last result information.
100    /// expected result.
101    ///
102    /// # Arguments
103    ///
104    /// * `err` - Error to be set.
105    pub(crate) fn error(mut self, err: anyhow::Error) -> SendDataResult {
106        self.last_result = Err(err);
107        self
108    }
109}