@@ -352,18 +352,20 @@ fn run_worker(
352352 // Check if we need to flush based on elapsed time
353353 let time_based_flush = state. last_flush . elapsed ( ) >= flush_duration;
354354
355- let ( should_flush, should_exit) = match receiver. recv_timeout ( flush_duration) {
355+ let mut should_flush = false ;
356+ let mut should_exit = false ;
357+ match receiver. recv_timeout ( flush_duration) {
356358 Ok ( Event :: Stop ) => {
357359 info ! ( "Stopping SQLiteExporter worker, flushing & exiting" ) ;
358- ( true , true )
360+ should_flush = true ;
361+ should_exit = true ;
359362 }
360363 Ok ( Event :: SetHousekeeping {
361364 retention_period,
362365 housekeeping_period,
363366 record_limit,
364367 } ) => {
365368 state. set_housekeeping ( retention_period, housekeeping_period, record_limit) ;
366- ( false , false )
367369 }
368370 Ok ( Event :: DescribeKey ( _key_type, key, unit, desc) ) => {
369371 info ! ( "Describing key {:?}" , key) ;
@@ -375,11 +377,9 @@ fn run_worker(
375377 ) {
376378 error ! ( "Failed to create key entry: {:?}" , e) ;
377379 }
378- ( false , false )
379380 }
380381 Ok ( Event :: RegisterKey ( _key_type, _key, _handle) ) => {
381382 // we currently don't do anything with register...
382- ( false , false )
383383 }
384384 Ok ( Event :: IncrementCounter ( timestamp, key, value) ) => {
385385 let key_name = key. name ( ) ;
@@ -391,16 +391,15 @@ fn run_worker(
391391 if let Err ( e) = state. queue_metric ( timestamp, key_name, value as _ ) {
392392 error ! ( "Error queueing metric: {:?}" , e) ;
393393 }
394-
395- ( state. should_flush ( ) , false )
394+ should_flush = state. should_flush ( ) ;
396395 }
397396 Ok ( Event :: AbsoluteCounter ( timestamp, key, value) ) => {
398397 let key_name = key. name ( ) ;
399398 state. counters . insert ( key. clone ( ) , value) ;
400399 if let Err ( e) = state. queue_metric ( timestamp, key_name, value as _ ) {
401400 error ! ( "Error queueing metric: {:?}" , e) ;
402401 }
403- ( state. should_flush ( ) , false )
402+ should_flush = state. should_flush ( ) ;
404403 }
405404 Ok ( Event :: UpdateGauge ( timestamp, key, value) ) => {
406405 let key_name = key. name ( ) ;
@@ -422,15 +421,14 @@ fn run_worker(
422421 if let Err ( e) = state. queue_metric ( timestamp, key_name, value) {
423422 error ! ( "Error queueing metric: {:?}" , e) ;
424423 }
425- ( state. should_flush ( ) , false )
424+ should_flush = state. should_flush ( ) ;
426425 }
427426 Ok ( Event :: UpdateHistogram ( timestamp, key, value) ) => {
428427 let key_name = key. name ( ) ;
429428 if let Err ( e) = state. queue_metric ( timestamp, key_name, value) {
430429 error ! ( "Error queueing metric: {:?}" , e) ;
431430 }
432-
433- ( state. should_flush ( ) , false )
431+ should_flush = state. should_flush ( ) ;
434432 }
435433 Ok ( Event :: RequestSummaryFromSignpost {
436434 signpost_key,
@@ -468,16 +466,16 @@ fn run_worker(
468466 }
469467 }
470468 }
471- ( false , false )
472469 }
473470 Err ( RecvTimeoutError :: Timeout ) => {
474- ( true , false )
471+ should_flush = true ;
475472 }
476473 Err ( RecvTimeoutError :: Disconnected ) => {
477474 warn ! ( "SQLiteExporter channel disconnected, exiting worker" ) ;
478- ( true , true )
475+ should_flush = true ;
476+ should_exit = true ;
479477 }
480- } ;
478+ }
481479
482480 // Flush if time-based flush is triggered OR if event-based flush is triggered
483481 if time_based_flush || should_flush {
@@ -587,11 +585,11 @@ impl SqliteExporter {
587585 let excess = records - record_limit + ( record_limit / 4 ) ; // delete excess + 25% of limit
588586 trace ! (
589587 "Exceeded limit! {} > {}, deleting {} oldest" ,
590- records,
591- record_limit,
592- excess
588+ records, record_limit, excess
589+ ) ;
590+ let query = format ! (
591+ "DELETE FROM metrics WHERE id IN (SELECT id FROM metrics ORDER BY timestamp ASC LIMIT {excess});"
593592 ) ;
594- let query = format ! ( "DELETE FROM metrics WHERE id IN (SELECT id FROM metrics ORDER BY timestamp ASC LIMIT {excess});" ) ;
595593 if let Err ( e) = sql_query ( query) . execute ( db) {
596594 error ! ( "Failed to delete excessive records: {:?}" , e) ;
597595 }
0 commit comments