From 92036d81ee61dd1556a3218243833dc7557a6942 Mon Sep 17 00:00:00 2001 From: mashiike Date: Tue, 2 Apr 2024 11:25:24 +0900 Subject: [PATCH 1/2] add new monitor for Labeled Metric --- monitors.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- monitors_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/monitors.go b/monitors.go index c24d71e..adfa59e 100644 --- a/monitors.go +++ b/monitors.go @@ -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 @@ -106,6 +116,7 @@ const ( monitorTypeExternalHTTP = "external" monitorTypeExpression = "expression" monitorTypeAnomalyDetection = "anomalyDetection" + monitorTypeQuery = "query" ) // Ensure each monitor type conforms to the Monitor interface. @@ -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 @@ -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 { @@ -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 { @@ -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} } diff --git a/monitors_test.go b/monitors_test.go index 51a10dd..cd87d0c 100644 --- a/monitors_test.go +++ b/monitors_test.go @@ -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) { From 7a882e1a51884e19da7f13bdf5d7cca3e5f09935 Mon Sep 17 00:00:00 2001 From: mashiike Date: Tue, 2 Apr 2024 11:35:34 +0900 Subject: [PATCH 2/2] style change: no tab --- monitors.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monitors.go b/monitors.go index adfa59e..79586d1 100644 --- a/monitors.go +++ b/monitors.go @@ -93,10 +93,10 @@ import ( "name": "LabeldMetric - custom.access_counter", "query": "custom.access_counter", "operator": ">", - "warning": 30.0, - "critical": 300.0, - "legend":"" - } + "warning": 30.0, + "critical": 300.0, + "legend":"" + } ] } */