Skip to content

Commit

Permalink
Merge pull request #154 from mashiike/feature/get-query-monitor
Browse files Browse the repository at this point in the history
Add new monitor for Labeled Metric
  • Loading branch information
lufia authored Apr 4, 2024
2 parents 3d928b0 + 7a882e1 commit a304dae
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
42 changes: 41 additions & 1 deletion monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ import (
],
"maxCheckAttempts": 3,
"warningSensitivity": "insensitive"
},
{
"id": "57We5nNtpZA",
"type": "query",
"isMute": false,
"name": "LabeldMetric - custom.access_counter",
"query": "custom.access_counter",
"operator": ">",
"warning": 30.0,
"critical": 300.0,
"legend":""
}
]
}
*/

// Monitor represents interface to which each monitor type must confirm to.
type Monitor interface {
MonitorType() string
Expand All @@ -106,6 +116,7 @@ const (
monitorTypeExternalHTTP = "external"
monitorTypeExpression = "expression"
monitorTypeAnomalyDetection = "anomalyDetection"
monitorTypeQuery = "query"
)

// Ensure each monitor type conforms to the Monitor interface.
Expand All @@ -116,6 +127,7 @@ var (
_ Monitor = (*MonitorExternalHTTP)(nil)
_ Monitor = (*MonitorExpression)(nil)
_ Monitor = (*MonitorAnomalyDetection)(nil)
_ Monitor = (*MonitorQuery)(nil)
)

// Ensure only monitor types defined in this package can be assigned to the
Expand All @@ -126,6 +138,7 @@ func (m *MonitorServiceMetric) isMonitor() {}
func (m *MonitorExternalHTTP) isMonitor() {}
func (m *MonitorExpression) isMonitor() {}
func (m *MonitorAnomalyDetection) isMonitor() {}
func (m *MonitorQuery) isMonitor() {}

// MonitorConnectivity represents connectivity monitor.
type MonitorConnectivity struct {
Expand Down Expand Up @@ -302,6 +315,31 @@ func (m *MonitorAnomalyDetection) MonitorName() string { return m.Name }
// MonitorID returns monitor id.
func (m *MonitorAnomalyDetection) MonitorID() string { return m.ID }

// MonitorQuery represents query monitor.
type MonitorQuery struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Memo string `json:"memo,omitempty"`
Type string `json:"type,omitempty"`
IsMute bool `json:"isMute,omitempty"`
NotificationInterval uint64 `json:"notificationInterval,omitempty"`

Query string `json:"query,omitempty"`
Operator string `json:"operator,omitempty"`
Warning *float64 `json:"warning"`
Critical *float64 `json:"critical"`
Legend string `json:"legend,omitempty"`
}

// MonitorType returns monitor type.
func (m *MonitorQuery) MonitorType() string { return monitorTypeQuery }

// MonitorName returns monitor name.
func (m *MonitorQuery) MonitorName() string { return m.Name }

// MonitorID returns monitor id.
func (m *MonitorQuery) MonitorID() string { return m.ID }

// FindMonitors finds monitors.
func (c *Client) FindMonitors() ([]Monitor, error) {
data, err := requestGet[struct {
Expand Down Expand Up @@ -399,6 +437,8 @@ func decodeMonitor(mes json.RawMessage) (Monitor, error) {
m = &MonitorExpression{}
case monitorTypeAnomalyDetection:
m = &MonitorAnomalyDetection{}
case monitorTypeQuery:
m = &MonitorQuery{}
default:
return nil, &unknownMonitorTypeError{Type: typeData.Type}
}
Expand Down
24 changes: 24 additions & 0 deletions monitors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,30 @@ var testCases = []struct {
"notificationInterval": 60
}`,
},
{
"query monitor",
&MonitorQuery{
ID: "2cSZzK3XfmI",
Name: "query monitor",
Type: "query",
IsMute: false,
NotificationInterval: 60,
Query: "custom.counter",
Operator: ">",
Warning: pfloat64(10.0),
Critical: nil,
},
`{
"id" : "2cSZzK3XfmI",
"type": "query",
"name": "query monitor",
"query": "custom.counter",
"operator": ">",
"warning": 10.0,
"critical": null,
"notificationInterval": 60
}`,
},
}

func TestDecodeEncodeMonitor(t *testing.T) {
Expand Down

0 comments on commit a304dae

Please sign in to comment.