@@ -16,6 +16,8 @@ use serde_json::json;
16
16
use slog:: * ;
17
17
use slog_async;
18
18
19
+ use crate :: util:: futures:: retry;
20
+
19
21
/// General configuration parameters for Elasticsearch logging.
20
22
#[ derive( Clone , Debug ) ]
21
23
pub struct ElasticLoggingConfig {
@@ -140,6 +142,8 @@ pub struct ElasticDrainConfig {
140
142
pub custom_id_value : String ,
141
143
/// The batching interval.
142
144
pub flush_interval : Duration ,
145
+ /// Maximum retries in case of error.
146
+ pub max_retries : usize ,
143
147
}
144
148
145
149
/// An slog `Drain` for logging to Elasticsearch.
@@ -187,6 +191,7 @@ impl ElasticDrain {
187
191
let logs = self . logs . clone ( ) ;
188
192
let config = self . config . clone ( ) ;
189
193
let mut interval = tokio:: time:: interval ( self . config . flush_interval ) ;
194
+ let max_retries = self . config . max_retries ;
190
195
191
196
crate :: task_spawn:: spawn ( async move {
192
197
loop {
@@ -261,7 +266,6 @@ impl ElasticDrain {
261
266
262
267
// Send batch of logs to Elasticsearch
263
268
let client = Client :: new ( ) ;
264
- let logger_for_err = flush_logger. clone ( ) ;
265
269
266
270
let header = match config. general . username {
267
271
Some ( username) => client
@@ -272,19 +276,24 @@ impl ElasticDrain {
272
276
. post ( batch_url)
273
277
. header ( CONTENT_TYPE , "application/json" ) ,
274
278
} ;
275
- header
276
- . body ( batch_body)
277
- . send ( )
278
- . and_then ( |response| async { response. error_for_status ( ) } )
279
- . map_ok ( |_| ( ) )
280
- . unwrap_or_else ( move |e| {
279
+
280
+ retry ( "send logs to elasticsearch" , & flush_logger)
281
+ . limit ( max_retries)
282
+ . timeout_secs ( 30 )
283
+ . run ( move || {
284
+ header
285
+ . try_clone ( )
286
+ . unwrap ( ) // Unwrap: Request body not yet set
287
+ . body ( batch_body. clone ( ) )
288
+ . send ( )
289
+ . and_then ( |response| async { response. error_for_status ( ) } )
290
+ . map_ok ( |_| ( ) )
291
+ } )
292
+ . await
293
+ . unwrap_or_else ( |e| {
281
294
// Log if there was a problem sending the logs
282
- error ! (
283
- logger_for_err,
284
- "Failed to send logs to Elasticsearch: {}" , e
285
- ) ;
295
+ error ! ( flush_logger, "Failed to send logs to Elasticsearch: {}" , e) ;
286
296
} )
287
- . await ;
288
297
}
289
298
} ) ;
290
299
}
0 commit comments