Skip to content

Commit

Permalink
fix(api): Clear last check on trigger update
Browse files Browse the repository at this point in the history
In last check some targets have a records in MetricsToTargetRelation
dictionary. This records did not cleaned during trigger update that led
checker to use metric names from this dictionary to populate the set of
metrics to check.

Closes #593
  • Loading branch information
litleleprikon committed Nov 25, 2020
1 parent a55b5cb commit f7a544c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions api/controller/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func saveTrigger(dataBase moira.Database, trigger *moira.Trigger, triggerID stri
lastCheck.RemoveMetricState(metric)
}
}
lastCheck.RemoveMetricsToTargetRelation()
} else {
triggerState := moira.StateNODATA
if trigger.TTLState != nil {
Expand Down
10 changes: 7 additions & 3 deletions api/controller/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ func TestSaveTrigger(t *testing.T) {
"super.metric1": {},
"super.metric2": {},
},
MetricsToTargetRelation: map[string]string{
"t2": "super.metric3",
},
}
emptyLastCheck := moira.CheckData{
Metrics: make(map[string]moira.MetricState),
Metrics: make(map[string]moira.MetricState),
MetricsToTargetRelation: map[string]string{},
}

Convey("No timeSeries", t, func() {
Expand All @@ -80,13 +84,13 @@ func TestSaveTrigger(t *testing.T) {
So(resp, ShouldResemble, &dto.SaveTriggerResponse{ID: triggerID, Message: "trigger updated"})
})
Convey("Has last check", func() {
actualLastCheck := lastCheck
actualLastCheck := emptyLastCheck
dataBase.EXPECT().AcquireTriggerCheckLock(triggerID, 10)
dataBase.EXPECT().DeleteTriggerCheckLock(triggerID)
dataBase.EXPECT().GetTriggerLastCheck(triggerID).Return(actualLastCheck, nil)
dataBase.EXPECT().SetTriggerLastCheck(triggerID, &actualLastCheck, trigger.IsRemote).Return(nil)
dataBase.EXPECT().SaveTrigger(triggerID, &trigger).Return(nil)
resp, err := saveTrigger(dataBase, &trigger, triggerID, make(map[string]bool))
resp, err := saveTrigger(dataBase, &trigger, triggerID, map[string]bool{"super.metric1": true, "super.metric2": true})
So(err, ShouldBeNil)
So(resp, ShouldResemble, &dto.SaveTriggerResponse{ID: triggerID, Message: "trigger updated"})
So(actualLastCheck, ShouldResemble, emptyLastCheck)
Expand Down
5 changes: 5 additions & 0 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ func (checkData CheckData) RemoveMetricState(metricName string) {
delete(checkData.Metrics, metricName)
}

// RemoveMetricsToTargetRelation is a function that sets an empty map to MetricsToTargetRelation.
func (checkData *CheckData) RemoveMetricsToTargetRelation() {
checkData.MetricsToTargetRelation = make(map[string]string)
}

// MetricState represents metric state data for given timestamp
type MetricState struct {
EventTimestamp int64 `json:"event_timestamp"`
Expand Down

0 comments on commit f7a544c

Please sign in to comment.