-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
testing.go
161 lines (133 loc) · 4.44 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package definitions
import (
"encoding/json"
"fmt"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/prometheus/promql"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
// swagger:route Post /api/v1/rule/test/grafana testing RouteTestRuleGrafanaConfig
//
// Test a rule against Grafana ruler
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 200: TestRuleResponse
// swagger:route Post /api/v1/rule/test/{Recipient} testing RouteTestRuleConfig
//
// Test a rule against external data source ruler
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 200: TestRuleResponse
// swagger:route Post /api/v1/eval testing RouteEvalQueries
//
// Test rule
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Responses:
// 200: EvalQueriesResponse
// swagger:parameters RouteTestReceiverConfig
type TestReceiverRequest struct {
// in:body
Body ExtendedReceiver
}
// swagger:parameters RouteTestRuleConfig RouteTestRuleGrafanaConfig
type TestRuleRequest struct {
// in:body
Body TestRulePayload
}
// swagger:model
type TestRulePayload struct {
// Example: (node_filesystem_avail_bytes{fstype!="",job="integrations/node_exporter"} node_filesystem_size_bytes{fstype!="",job="integrations/node_exporter"} * 100 < 5 and node_filesystem_readonly{fstype!="",job="integrations/node_exporter"} == 0)
Expr string `json:"expr,omitempty"`
// GrafanaManagedCondition for grafana alerts
GrafanaManagedCondition *models.EvalAlertConditionCommand `json:"grafana_condition,omitempty"`
}
// swagger:parameters RouteEvalQueries
type EvalQueriesRequest struct {
// in:body
Body EvalQueriesPayload
}
// swagger:model
type EvalQueriesPayload struct {
Data []models.AlertQuery `json:"data"`
Now time.Time `json:"now"`
}
func (p *TestRulePayload) UnmarshalJSON(b []byte) error {
type plain TestRulePayload
if err := json.Unmarshal(b, (*plain)(p)); err != nil {
return err
}
return p.validate()
}
func (p *TestRulePayload) validate() error {
if p.Expr != "" && p.GrafanaManagedCondition != nil {
return fmt.Errorf("cannot mix Grafana & Prometheus style expressions")
}
if p.Expr == "" && p.GrafanaManagedCondition == nil {
return fmt.Errorf("missing either Grafana or Prometheus style expressions")
}
return nil
}
func (p *TestRulePayload) Type() (backend Backend) {
if p.Expr != "" {
return LoTexRulerBackend
}
if p.GrafanaManagedCondition != nil {
return GrafanaBackend
}
return
}
// swagger:model
type TestRuleResponse struct {
Alerts promql.Vector `json:"alerts"`
GrafanaAlertInstances AlertInstancesResponse `json:"grafana_alert_instances"`
}
// swagger:model
type EvalQueriesResponse = backend.QueryDataResponse
// swagger:model
type AlertInstancesResponse struct {
// Instances is an array of arrow encoded dataframes
// each frame has a single row, and a column for each instance (alert identified by unique labels) with a boolean value (firing/not firing)
Instances [][]byte `json:"instances"`
}
// swagger:model
type ExtendedReceiver struct {
EmailConfigs config.EmailConfig `yaml:"email_configs,omitempty" json:"email_configs,omitempty"`
PagerdutyConfigs config.PagerdutyConfig `yaml:"pagerduty_configs,omitempty" json:"pagerduty_configs,omitempty"`
SlackConfigs config.SlackConfig `yaml:"slack_configs,omitempty" json:"slack_configs,omitempty"`
WebhookConfigs config.WebhookConfig `yaml:"webhook_configs,omitempty" json:"webhook_configs,omitempty"`
OpsGenieConfigs config.OpsGenieConfig `yaml:"opsgenie_configs,omitempty" json:"opsgenie_configs,omitempty"`
WechatConfigs config.WechatConfig `yaml:"wechat_configs,omitempty" json:"wechat_configs,omitempty"`
PushoverConfigs config.PushoverConfig `yaml:"pushover_configs,omitempty" json:"pushover_configs,omitempty"`
VictorOpsConfigs config.VictorOpsConfig `yaml:"victorops_configs,omitempty" json:"victorops_configs,omitempty"`
GrafanaReceiver PostableGrafanaReceiver `yaml:"grafana_managed_receiver,omitempty" json:"grafana_managed_receiver,omitempty"`
}
// swagger:model
type Success ResponseDetails
// swagger:model
type SmtpNotEnabled ResponseDetails
// swagger:model
type Failure ResponseDetails
// swagger:model
type ResponseDetails struct {
Msg string `json:"msg"`
}