Skip to content

Commit

Permalink
feat(monitors): added missing monitor fields to client lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Rambatino committed Feb 19, 2025
1 parent eb0e191 commit 82472e3
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 12 deletions.
21 changes: 19 additions & 2 deletions axiom/monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ type MonitorType uint8

// All available [MonitorTypes]s.
const (
MonitorTypeThreshold MonitorType = iota // Threshold
MonitorTypeMatchEvent // MatchEvent
MonitorTypeThreshold MonitorType = iota // Threshold
MonitorTypeMatchEvent // MatchEvent
MonitorTypeAnonalyDetection // AnomalyDetection
)

func typeFromString(s string) (c MonitorType) {
switch s {
case MonitorTypeMatchEvent.String():
return MonitorTypeMatchEvent
case MonitorTypeAnonalyDetection.String():
return MonitorTypeAnonalyDetection
default:
return MonitorTypeThreshold
}
Expand Down Expand Up @@ -145,6 +148,20 @@ type Monitor struct {
Disabled bool `json:"disabled"`
// DisabledUntil is the time that the monitor will be disabled until.
DisabledUntil time.Time `json:"disabledUntil"`
// SecondDelay is the delay in seconds after the end time that the monitor runs.
SecondDelay int64 `json:"secondDelay,omitempty"`
// NotifyEveryRun indicates whether to notify on every trigger.
NotifyEveryRun bool `json:"notifyEveryRun,omitempty"`
// SkipResolved indicates whether to skip resolved alerts.
SkipResolved bool `json:"skipResolved,omitempty"`
// Tolerance is the acceptable tolerance for an anomaly detection monitor.
Tolerance float64 `json:"tolerance,omitempty"`
// TriggerFromNRuns indicates the number of runs to consider when determining whether the current run should notify.
TriggerFromNRuns int64 `json:"triggerFromNRuns,omitempty"`
// TriggerAfterNPositiveResults indicates the number of positive results required to trigger a notification (can be from a larger set of TriggerFromNRuns)
TriggerAfterNPositiveResults int64 `json:"triggerAfterNPositiveResults,omitempty"`
// CompareDays indicates the number of days to compare for anomaly detection only.
CompareDays int64 `json:"compareDays,omitempty"`
}

// MarshalJSON implements [json.Marshaler]. It is in place to marshal the
Expand Down
21 changes: 13 additions & 8 deletions axiom/monitors_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ func (s *MonitorsTestSuite) SetupTest() {
var err error
s.monitor, err = s.client.Monitors.Create(s.ctx, axiom.MonitorCreateRequest{
Monitor: axiom.Monitor{
AlertOnNoData: false,
APLQuery: fmt.Sprintf("['%s'] | summarize count() by bin_auto(_time)", s.datasetID),
Description: "A test monitor",
Interval: time.Minute,
Name: "Test Monitor",
Operator: axiom.BelowOrEqual,
Range: time.Minute * 5,
Threshold: 1,
AlertOnNoData: false,
APLQuery: fmt.Sprintf("['%s'] | summarize count() by bin_auto(_time)", s.datasetID),
Description: "A test monitor",
Interval: time.Minute,
Name: "Test Monitor",
Operator: axiom.BelowOrEqual,
Range: time.Minute * 5,
Threshold: 1,
SecondDelay: 10,
NotifyEveryRun: true,
SkipResolved: false,
TriggerFromNRuns: 3,
TriggerAfterNPositiveResults: 2,
},
})
s.Require().NoError(err)
Expand Down
5 changes: 3 additions & 2 deletions axiom/monitors_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

182 changes: 182 additions & 0 deletions axiom/monitors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,185 @@ func TestMonitorsService_Delete(t *testing.T) {
err := client.Monitors.Delete(context.Background(), "testID")
require.NoError(t, err)
}

func TestMonitorsService_AdvancedUsage(t *testing.T) {
exp := &Monitor{
AlertOnNoData: false,
APLQuery: "['dataset'] | summarize count() by bin_auto(_time)",
Description: "test",
DisabledUntil: time.Time{},
ID: "newTestID",
Interval: 0,
Name: "newTest",
NotifierIDs: nil,
Operator: Above,
Range: time.Minute,
Threshold: 1,
SecondDelay: 10,
NotifyEveryRun: true,
SkipResolved: false,
TriggerFromNRuns: 5,
TriggerAfterNPositiveResults: 3,
Type: MonitorTypeThreshold,
}
hf := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, mediaTypeJSON, r.Header.Get("Content-Type"))

w.Header().Set("Content-Type", mediaTypeJSON)
_, err := fmt.Fprint(w, `{
"alertOnNoData": false,
"aplQuery": "['dataset'] | summarize count() by bin_auto(_time)",
"description": "test",
"id": "newTestID",
"IntervalMinutes": 0,
"name": "newTest",
"NotifierIDs": null,
"operator": "Above",
"RangeMinutes": 1,
"threshold": 1,
"secondDelay": 10,
"notifyEveryRun": true,
"skipResolved": false,
"triggerFromNRuns": 5,
"triggerAfterNPositiveResults": 3
}`)
assert.NoError(t, err)
}
client := setup(t, "POST /v2/monitors", hf)

res, err := client.Monitors.Create(context.Background(), MonitorCreateRequest{Monitor{
AlertOnNoData: false,
APLQuery: "['dataset'] | summarize count() by bin_auto(_time)",
Description: "test",
DisabledUntil: time.Time{},
ID: "newTestID",
Interval: 0,
Name: "newTest",
NotifierIDs: nil,
Operator: Above,
Range: time.Minute,
Threshold: 1,
SecondDelay: 10,
NotifyEveryRun: true,
SkipResolved: false,
TriggerFromNRuns: 5,
TriggerAfterNPositiveResults: 3,
}})
require.NoError(t, err)

assert.Equal(t, exp, res)
}

func TestMonitorsService_AnomalyDetection(t *testing.T) {
exp := &Monitor{
AlertOnNoData: false,
APLQuery: "['dataset'] | summarize count() by bin_auto(_time)",
Description: "test",
DisabledUntil: time.Time{},
ID: "newTestID",
Interval: 0,
Name: "newTest",
NotifierIDs: nil,
Operator: Above,
Range: time.Minute,
Threshold: 1,
SecondDelay: 10,
NotifyEveryRun: true,
SkipResolved: false,
Tolerance: 0.5,
TriggerFromNRuns: 5,
TriggerAfterNPositiveResults: 3,
CompareDays: 7,
Type: MonitorTypeAnonalyDetection,
}
hf := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, mediaTypeJSON, r.Header.Get("Content-Type"))

w.Header().Set("Content-Type", mediaTypeJSON)
_, err := fmt.Fprint(w, `{
"alertOnNoData": false,
"aplQuery": "['dataset'] | summarize count() by bin_auto(_time)",
"description": "test",
"id": "newTestID",
"IntervalMinutes": 0,
"name": "newTest",
"NotifierIDs": null,
"operator": "Above",
"RangeMinutes": 1,
"threshold": 1,
"secondDelay": 10,
"notifyEveryRun": true,
"skipResolved": false,
"tolerance": 0.5,
"triggerFromNRuns": 5,
"triggerAfterNPositiveResults": 3,
"compareDays": 7,
"type": "AnomalyDetection"
}`)
assert.NoError(t, err)
}
client := setup(t, "POST /v2/monitors", hf)

res, err := client.Monitors.Create(context.Background(), MonitorCreateRequest{Monitor{
AlertOnNoData: false,
APLQuery: "['dataset'] | summarize count() by bin_auto(_time)",
Description: "test",
DisabledUntil: time.Time{},
ID: "newTestID",
Interval: 0,
Name: "newTest",
NotifierIDs: nil,
Operator: Above,
Range: time.Minute,
Threshold: 1,
SecondDelay: 10,
NotifyEveryRun: true,
SkipResolved: false,
Tolerance: 0.5,
TriggerFromNRuns: 5,
TriggerAfterNPositiveResults: 3,
CompareDays: 7,
}})
require.NoError(t, err)

assert.Equal(t, exp, res)
}

func TestMonitorsService_MatchEvent(t *testing.T) {
exp := &Monitor{
APLQuery: "['dataset']",
Description: "test",
ID: "newTestID",
Name: "newTest",
Type: MonitorTypeMatchEvent,
}
hf := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, mediaTypeJSON, r.Header.Get("Content-Type"))

w.Header().Set("Content-Type", mediaTypeJSON)
_, err := fmt.Fprint(w, `{
"alertOnNoData": false,
"aplQuery": "['dataset']",
"description": "test",
"id": "newTestID",
"name": "newTest",
"type": "MatchEvent"
}`)
assert.NoError(t, err)
}
client := setup(t, "POST /v2/monitors", hf)

res, err := client.Monitors.Create(context.Background(), MonitorCreateRequest{Monitor{
AlertOnNoData: false,
APLQuery: "['dataset']",
Description: "test",
ID: "newTestID",
Name: "newTest",
}})
require.NoError(t, err)

assert.Equal(t, exp, res)
}

0 comments on commit 82472e3

Please sign in to comment.