Skip to content

Commit 08edb4a

Browse files
committed
Implemented the alerts logs API
1 parent 011e342 commit 08edb4a

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

alerts.go

+37
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ type UpdateAlertResponse struct {
5959
Memo string `json:"memo,omitempty"`
6060
}
6161

62+
// AlertLog is the log of alert
63+
type AlertLog struct {
64+
ID string `json:"id"`
65+
CreatedAt int64 `json:"createdAt"`
66+
Status string `json:"status"`
67+
Trigger string `json:"trigger"`
68+
MonitorID *string `json:"monitorId"`
69+
TargetValue *float64 `json:"targetValue"`
70+
StatusDetail *struct {
71+
Type string `json:"type"`
72+
Detail struct {
73+
Message string `json:"message"`
74+
Memo string `json:"memo"`
75+
} `json:"detail"`
76+
} `json:"statusDetail,omitempty"`
77+
}
78+
79+
// AlertLogsResp is for FindAlertLogs and FindAlertLogsByNextID
80+
type AlertLogsResp struct {
81+
AlertLogs []*AlertLog `json:"logs"`
82+
NextID string `json:"nextId,omitempty"`
83+
}
84+
6285
func (c *Client) findAlertsWithParams(params url.Values) (*AlertsResp, error) {
6386
return requestGetWithParams[AlertsResp](c, "/api/v0/alerts", params)
6487
}
@@ -107,3 +130,17 @@ func (c *Client) UpdateAlert(alertID string, param UpdateAlertParam) (*UpdateAle
107130
path := fmt.Sprintf("/api/v0/alerts/%s", alertID)
108131
return requestPut[UpdateAlertResponse](c, path, param)
109132
}
133+
134+
// FindAlertLogs gets alert logs.
135+
func (c *Client) FindAlertLogs(alertId string) (*AlertLogsResp, error) {
136+
path := fmt.Sprintf("/api/v0/alerts/%s/logs", alertId)
137+
return requestGet[AlertLogsResp](c, path)
138+
}
139+
140+
// FindAlertLogsByNextID finds alert logs by next id.
141+
func (c *Client) FindAlertLogsByNextID(alertId, nextId string) (*AlertLogsResp, error) {
142+
params := url.Values{}
143+
params.Set("nextId", nextId)
144+
path := fmt.Sprintf("/api/v0/alerts/%s/logs", alertId)
145+
return requestGetWithParams[AlertLogsResp](c, path, params)
146+
}

alerts_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,104 @@ func TestGetAlert(t *testing.T) {
381381
t.Errorf("Wrong data for alert: %v", alert)
382382
}
383383
}
384+
385+
func TestFindAlertLogs(t *testing.T) {
386+
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
387+
url := fmt.Sprintf("/api/v0/alerts/%s/logs", "2wpLU5fBXbG")
388+
if req.URL.Path != url {
389+
t.Error("request URL should be /api/v0/alerts/<ID>/logs but: ", req.URL.Path)
390+
}
391+
392+
if req.Method != "GET" {
393+
t.Error("request method should be GET but: ", req.Method)
394+
}
395+
396+
respJSON, _ := json.Marshal(map[string]interface{}{
397+
"logs": []map[string]interface{}{
398+
{
399+
"id": "5m7fewuu5tS",
400+
"createdAt": 1735290407,
401+
"status": "WARNING",
402+
"trigger": "monitoring",
403+
"monitorId": "5m72DB7s7sU",
404+
"targetValue": (*float64)(nil),
405+
"statusDetail": map[string]interface{}{
406+
"type": "check",
407+
"detail": map[string]interface{}{
408+
"message": "Uptime WARNING: 0 day(s) 0 hour(s) 6 minute(s) (398 second(s))",
409+
"memo": "",
410+
},
411+
},
412+
}, {
413+
"id": "5m7fewuu5tS",
414+
"createdAt": 1735290407,
415+
"status": "WARNING",
416+
"trigger": "monitoring",
417+
"monitorId": "5m72DB7s7sU",
418+
"targetValue": (*float64)(nil),
419+
"statusDetail": nil,
420+
},
421+
},
422+
"nextId": "2fsf8jRxFG1",
423+
})
424+
425+
res.Header()["Content-Type"] = []string{"application/json"}
426+
fmt.Fprint(res, string(respJSON))
427+
}))
428+
defer ts.Close()
429+
430+
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
431+
logs, err := client.FindAlertLogs("2wpLU5fBXbG")
432+
if err != nil {
433+
t.Error("err should be nil but: ", err)
434+
}
435+
436+
if len(logs.AlertLogs) != 2 {
437+
t.Error("logs should have 1 elements but: ", len(logs.AlertLogs))
438+
}
439+
440+
if logs.NextID != "2fsf8jRxFG1" {
441+
t.Error("request sends json including nextId but: ", logs.NextID)
442+
}
443+
444+
if logs.AlertLogs[0].ID != "5m7fewuu5tS" {
445+
t.Error("alert id should be \"5m7fewuu5tS\" but: ", logs.AlertLogs[0].ID)
446+
}
447+
448+
if logs.AlertLogs[0].CreatedAt != 1735290407 {
449+
t.Error("createdAt should be 1735290407 but: ", logs.AlertLogs[0].CreatedAt)
450+
}
451+
452+
if logs.AlertLogs[0].Trigger != "monitoring" {
453+
t.Error("trigger should be \"monitoring\" but: ", logs.AlertLogs[0].Trigger)
454+
}
455+
456+
if *logs.AlertLogs[0].MonitorID != "5m72DB7s7sU" {
457+
t.Error("monitorId should be \"5m72DB7s7sU\" but: ", *logs.AlertLogs[0].MonitorID)
458+
}
459+
460+
if logs.AlertLogs[0].TargetValue != nil {
461+
t.Error("targetValue should be nil but: ", logs.AlertLogs[0].TargetValue)
462+
}
463+
464+
if logs.AlertLogs[0].Status != "WARNING" {
465+
t.Error("alert status should be \"WARNING\" but: ", logs.AlertLogs[0].Status)
466+
}
467+
468+
if logs.AlertLogs[0].StatusDetail.Type != "check" {
469+
t.Error("statusDetail type should be \"check\" but: ", logs.AlertLogs[0].StatusDetail.Type)
470+
}
471+
472+
if logs.AlertLogs[0].StatusDetail.Detail.Message != "Uptime WARNING: 0 day(s) 0 hour(s) 6 minute(s) (398 second(s))" {
473+
t.Error("statusDetail message should be \"Uptime WARNING: 0 day(s) 0 hour(s) 6 minute(s) (398 second(s))\" but: ", logs.AlertLogs[0].StatusDetail.Detail.Message)
474+
}
475+
476+
if logs.AlertLogs[0].StatusDetail.Detail.Memo != "" {
477+
t.Error("statusDetail memo should be empty but: ", logs.AlertLogs[0].StatusDetail.Detail.Memo)
478+
}
479+
480+
if logs.AlertLogs[1].StatusDetail != nil {
481+
t.Error("statusDetail should be nil but: ", logs.AlertLogs[1].StatusDetail)
482+
}
483+
484+
}

0 commit comments

Comments
 (0)