Skip to content

Commit

Permalink
Merge pull request #664 from mackerelio/query-monitor
Browse files Browse the repository at this point in the history
Support query monitorings
  • Loading branch information
rmatsuoka authored Nov 26, 2024
2 parents d45e294 + ec3a8da commit dab5bc0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
14 changes: 14 additions & 0 deletions monitors/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func decodeMonitor(mes json.RawMessage) (mackerel.Monitor, error) {
m = &mackerel.MonitorExpression{}
case "anomalyDetection":
m = &mackerel.MonitorAnomalyDetection{}
case "query":
m = &mackerel.MonitorQuery{}
default:
return nil, fmt.Errorf("unknown type: %q", typeData.Type)
}
if err := json.Unmarshal(mes, m); err != nil {
return nil, err
Expand Down Expand Up @@ -309,6 +313,16 @@ func validateRules(monitors []mackerel.Monitor, label string) (bool, error) {
return false, err
}
case *mackerel.MonitorConnectivity:
case *mackerel.MonitorQuery:
if m.Name == "" {
return false, fmt.Errorf("Query Monitoring should have 'name'")
}
if m.Query == "" {
return false, fmt.Errorf("Query Monitoring '%s' should have 'query'", m.Name)
}
if m.Operator == "" {
return false, fmt.Errorf("Query monitoring '%s' should have 'operator'", m.Name)
}
default:
return false, fmt.Errorf("Unknown type is found: %s", m.MonitorType())
}
Expand Down
30 changes: 24 additions & 6 deletions monitors/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,50 @@ func TestIsSameMonitor(t *testing.T) {
}

func TestValidateRoles(t *testing.T) {
{
t.Run("connectivitiy", func(t *testing.T) {
a := &mackerel.MonitorConnectivity{ID: "12345", Name: "foo", Type: "connectivity"}

ret, err := validateRules([](mackerel.Monitor){a}, "test monitor")
if ret != true {
t.Errorf("should validate the rule: %s", err.Error())
}
}
})

{
t.Run("valid anomalyDetection", func(t *testing.T) {
a := &mackerel.MonitorAnomalyDetection{ID: "12345", Name: "anomaly", Type: "anomalyDetection", WarningSensitivity: "sensitive", Scopes: []string{"MyService: MyRole"}}

ret, err := validateRules([](mackerel.Monitor){a}, "anomaly detection monitor")
if ret != true {
t.Errorf("should validate the rule: %s", err.Error())
}
}
})

{
t.Run("invalid anomalyDetection", func(t *testing.T) {
a := &mackerel.MonitorAnomalyDetection{ID: "12345", Name: "anomaly", Type: "anomalyDetection", WarningSensitivity: "sensitive"}

ret, err := validateRules([](mackerel.Monitor){a}, "anomaly detection monitor")
if ret == true || err == nil {
t.Error("should invalidate the rule")
}
}
})

t.Run("valid query monitoring rule", func(t *testing.T) {
a := &mackerel.MonitorQuery{Name: "name", Type: "query", Query: "http.monitor.count", Operator: "<"}

ret, err := validateRules([](mackerel.Monitor){a}, "query monitor")
if !ret {
t.Errorf("should validate the rule: %v", err)
}
})

t.Run("invalid query monitoring rule", func(t *testing.T) {
a := &mackerel.MonitorQuery{Name: "name", Type: "query", Operator: "<"}

ret, err := validateRules([](mackerel.Monitor){a}, "query monitor")
if ret == true || err == nil {
t.Error("should invalidate the rule")
}
})
}

func pfloat64(x float64) *float64 {
Expand Down

0 comments on commit dab5bc0

Please sign in to comment.