Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support query monitorings #664

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading