Przeglądaj źródła

fix double push of metrics by properly handling tickers (#2374)

mmetc 1 rok temu
rodzic
commit
395cace69f

+ 7 - 4
pkg/apiserver/apic_metrics.go

@@ -81,7 +81,7 @@ func (a *apic) SendMetrics(stop chan (bool)) {
 	const checkInt = 20 * time.Second
 	const checkInt = 20 * time.Second
 
 
 	// intervals must always be > 0
 	// intervals must always be > 0
-	metInts := []time.Duration{1, a.metricsIntervalFirst, a.metricsInterval}
+	metInts := []time.Duration{1*time.Millisecond, a.metricsIntervalFirst, a.metricsInterval}
 
 
 	log.Infof("Start send metrics to CrowdSec Central API (interval: %s once, then %s)",
 	log.Infof("Start send metrics to CrowdSec Central API (interval: %s once, then %s)",
 		metInts[1].Round(time.Second), metInts[2])
 		metInts[1].Round(time.Second), metInts[2])
@@ -94,8 +94,6 @@ func (a *apic) SendMetrics(stop chan (bool)) {
 		return metInts[count]
 		return metInts[count]
 	}
 	}
 
 
-	// store the list of machine IDs to compare
-	// with the next list
 	machineIDs := []string{}
 	machineIDs := []string{}
 
 
 	reloadMachineIDs := func() {
 	reloadMachineIDs := func() {
@@ -107,6 +105,10 @@ func (a *apic) SendMetrics(stop chan (bool)) {
 		machineIDs = ids
 		machineIDs = ids
 	}
 	}
 
 
+	// store the list of machine IDs to compare
+	// with the next list
+	reloadMachineIDs()
+
 	checkTicker := time.NewTicker(checkInt)
 	checkTicker := time.NewTicker(checkInt)
 	metTicker := time.NewTicker(nextMetInt())
 	metTicker := time.NewTicker(nextMetInt())
 
 
@@ -121,9 +123,10 @@ func (a *apic) SendMetrics(stop chan (bool)) {
 			reloadMachineIDs()
 			reloadMachineIDs()
 			if !slices.Equal(oldIDs, machineIDs) {
 			if !slices.Equal(oldIDs, machineIDs) {
 				log.Infof("capi metrics: machines changed, immediate send")
 				log.Infof("capi metrics: machines changed, immediate send")
-				metTicker.Reset(1)
+				metTicker.Reset(1*time.Millisecond)
 			}
 			}
 		case <-metTicker.C:
 		case <-metTicker.C:
+			metTicker.Stop()
 			metrics, err := a.GetMetrics()
 			metrics, err := a.GetMetrics()
 			if err != nil {
 			if err != nil {
 				log.Errorf("unable to get metrics (%s), will retry", err)
 				log.Errorf("unable to get metrics (%s), will retry", err)

+ 4 - 4
pkg/apiserver/apic_metrics_test.go

@@ -26,15 +26,15 @@ func TestAPICSendMetrics(t *testing.T) {
 	}{
 	}{
 		{
 		{
 			name:            "basic",
 			name:            "basic",
-			duration:        time.Millisecond * 30,
-			metricsInterval: time.Millisecond * 5,
+			duration:        time.Millisecond * 60,
+			metricsInterval: time.Millisecond * 10,
 			expectedCalls:   5,
 			expectedCalls:   5,
 			setUp:           func(api *apic) {},
 			setUp:           func(api *apic) {},
 		},
 		},
 		{
 		{
 			name:            "with some metrics",
 			name:            "with some metrics",
-			duration:        time.Millisecond * 30,
-			metricsInterval: time.Millisecond * 5,
+			duration:        time.Millisecond * 60,
+			metricsInterval: time.Millisecond * 10,
 			expectedCalls:   5,
 			expectedCalls:   5,
 			setUp: func(api *apic) {
 			setUp: func(api *apic) {
 				api.dbClient.Ent.Machine.Delete().ExecX(context.Background())
 				api.dbClient.Ent.Machine.Delete().ExecX(context.Background())