-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbackground_send_metrics.rs
226 lines (216 loc) · 9.79 KB
/
background_send_metrics.rs
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
use std::cmp::max;
use std::sync::Arc;
use chrono::Duration;
use dashmap::DashMap;
use lazy_static::lazy_static;
use prometheus::{IntGauge, IntGaugeVec, Opts, register_int_gauge, register_int_gauge_vec};
use reqwest::StatusCode;
use tracing::{error, info, trace, warn};
use crate::types::TokenRefresh;
use crate::{
error::EdgeError,
metrics::client_metrics::{MetricsCache, size_of_batch},
};
use super::refresher::feature_refresher::FeatureRefresher;
lazy_static! {
pub static ref METRICS_UPSTREAM_HTTP_ERRORS: IntGaugeVec = register_int_gauge_vec!(
Opts::new(
"metrics_upstream_http_errors",
"Failing requests against upstream metrics endpoint"
),
&["status_code"]
)
.unwrap();
pub static ref METRICS_UNEXPECTED_ERRORS: IntGauge =
register_int_gauge!(Opts::new("metrics_send_error", "Failures to send metrics")).unwrap();
pub static ref METRICS_UPSTREAM_OUTDATED: IntGaugeVec = register_int_gauge_vec!(
Opts::new(
"metrics_upstream_outdated",
"Number of times we have tried to send metrics to an outdated endpoint"
),
&["environment"]
)
.unwrap();
pub static ref METRICS_UPSTREAM_CLIENT_BULK: IntGaugeVec = register_int_gauge_vec!(
Opts::new(
"metrics_upstream_client_bulk",
"Number of times we have tried to send metrics to the client bulk endpoint"
),
&["environment"]
)
.unwrap();
pub static ref METRICS_INTERVAL_BETWEEN_SEND: IntGauge = register_int_gauge!(Opts::new(
"metrics_interval_between_send",
"Interval between sending metrics"
))
.unwrap();
}
fn decide_where_to_post(
environment: &String,
known_tokens: Arc<DashMap<String, TokenRefresh>>,
) -> (bool, String) {
if let Some(token_refresh) = known_tokens
.iter()
.find(|t| t.token.environment == Some(environment.to_string()))
{
METRICS_UPSTREAM_CLIENT_BULK
.with_label_values(&[environment])
.inc();
(true, token_refresh.token.token.clone())
} else {
(false, "".into())
}
}
pub async fn send_metrics_one_shot(
metrics_cache: Arc<MetricsCache>,
feature_refresher: Arc<FeatureRefresher>,
) {
let envs = metrics_cache.get_metrics_by_environment();
for (env, batch) in envs.iter() {
let (use_new_endpoint, token) =
decide_where_to_post(env, feature_refresher.tokens_to_refresh.clone());
let batches = metrics_cache.get_appropriately_sized_env_batches(batch);
trace!("Posting {} batches for {env}", batches.len());
for batch in batches {
if !batch.applications.is_empty() || !batch.metrics.is_empty() {
let result = if use_new_endpoint {
feature_refresher
.unleash_client
.send_bulk_metrics_to_client_endpoint(batch.clone(), &token)
.await
} else {
feature_refresher
.unleash_client
.send_batch_metrics(batch.clone(), None)
.await
};
if let Err(edge_error) = result {
warn!("Shut down metrics flush failed with {edge_error:?}")
}
}
}
}
}
pub async fn send_metrics_task(
metrics_cache: Arc<MetricsCache>,
feature_refresher: Arc<FeatureRefresher>,
send_interval: i64,
) {
let mut failures = 0;
let mut interval = Duration::seconds(send_interval);
loop {
trace!("Looping metrics");
let envs = metrics_cache.get_metrics_by_environment();
for (env, batch) in envs.iter() {
let (use_new_endpoint, token) =
decide_where_to_post(env, feature_refresher.tokens_to_refresh.clone());
let batches = metrics_cache.get_appropriately_sized_env_batches(batch);
trace!("Posting {} batches for {env}", batches.len());
for batch in batches {
if !batch.applications.is_empty() || !batch.metrics.is_empty() {
let result = if use_new_endpoint {
feature_refresher
.unleash_client
.send_bulk_metrics_to_client_endpoint(batch.clone(), &token)
.await
} else {
feature_refresher
.unleash_client
.send_batch_metrics(batch.clone(), Some(interval.num_milliseconds()))
.await
};
if let Err(edge_error) = result {
match edge_error {
EdgeError::EdgeMetricsRequestError(status_code, message) => {
METRICS_UPSTREAM_HTTP_ERRORS
.with_label_values(&[status_code.as_str()])
.inc();
match status_code {
StatusCode::PAYLOAD_TOO_LARGE => error!(
"Metrics were too large. They were {}",
size_of_batch(&batch)
),
StatusCode::BAD_REQUEST => {
error!(
"Unleash said [{message:?}]. Dropping this metric bucket to avoid consuming too much memory"
);
}
StatusCode::NOT_FOUND => {
failures = 10;
interval = new_interval(send_interval, failures);
error!(
"Upstream said we are trying to post to an endpoint that doesn't exist. backing off to {} seconds",
interval.num_seconds()
);
}
StatusCode::FORBIDDEN | StatusCode::UNAUTHORIZED => {
failures = 10;
interval = new_interval(send_interval, failures);
error!(
"Upstream said we were not allowed to post metrics, backing off to {} seconds",
interval.num_seconds()
);
}
StatusCode::TOO_MANY_REQUESTS => {
failures = max(10, failures + 1);
interval = new_interval(send_interval, failures);
info!(
"Upstream said it was too busy, backing off to {} seconds",
interval.num_seconds()
);
metrics_cache.reinsert_batch(batch);
}
StatusCode::INTERNAL_SERVER_ERROR
| StatusCode::BAD_GATEWAY
| StatusCode::SERVICE_UNAVAILABLE
| StatusCode::GATEWAY_TIMEOUT => {
failures = max(10, failures + 1);
interval = new_interval(send_interval, failures);
info!(
"Upstream said it is struggling. It returned Http status {}. Backing off to {} seconds",
status_code,
interval.num_seconds()
);
metrics_cache.reinsert_batch(batch);
}
_ => {
warn!(
"Failed to send metrics. Status code was {status_code}. Will reinsert metrics for next attempt"
);
metrics_cache.reinsert_batch(batch);
}
}
}
_ => {
warn!("Failed to send metrics: {edge_error:?}");
METRICS_UNEXPECTED_ERRORS.inc();
}
}
} else {
failures = max(0, failures - 1);
interval = new_interval(send_interval, failures);
}
}
}
}
trace!(
"Done posting traces. Sleeping for {} seconds and then going again",
interval.num_seconds()
);
METRICS_INTERVAL_BETWEEN_SEND.set(interval.num_seconds());
tokio::time::sleep(std::time::Duration::from_secs(interval.num_seconds() as u64)).await;
}
}
fn new_interval(send_interval: i64, failures: i64) -> Duration {
let added_interval_from_failure = send_interval * failures;
Duration::seconds(send_interval + added_interval_from_failure)
}
#[cfg(test)]
mod tests {
use crate::http::background_send_metrics::new_interval;
#[tokio::test]
pub async fn new_interval_does_not_overflow() {
let metrics = new_interval(300, 10);
assert!(metrics.num_seconds() < 3305);
}
}