extractor_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package alerting
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/setting"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestAlertRuleExtraction(t *testing.T) {
  11. Convey("Parsing alert rules from dashboard json", t, func() {
  12. RegisterCondition("query", func(model *simplejson.Json, index int) (Condition, error) {
  13. return &FakeCondition{}, nil
  14. })
  15. setting.NewConfigContext(&setting.CommandLineArgs{
  16. HomePath: "../../../",
  17. })
  18. // mock data
  19. defaultDs := &m.DataSource{Id: 12, OrgId: 1, Name: "I am default", IsDefault: true}
  20. graphite2Ds := &m.DataSource{Id: 15, OrgId: 1, Name: "graphite2"}
  21. influxDBDs := &m.DataSource{Id: 16, OrgId: 1, Name: "InfluxDB"}
  22. bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
  23. query.Result = []*m.DataSource{defaultDs, graphite2Ds}
  24. return nil
  25. })
  26. bus.AddHandler("test", func(query *m.GetDataSourceByNameQuery) error {
  27. if query.Name == defaultDs.Name {
  28. query.Result = defaultDs
  29. }
  30. if query.Name == graphite2Ds.Name {
  31. query.Result = graphite2Ds
  32. }
  33. if query.Name == influxDBDs.Name {
  34. query.Result = influxDBDs
  35. }
  36. return nil
  37. })
  38. json := `
  39. {
  40. "id": 57,
  41. "title": "Graphite 4",
  42. "originalTitle": "Graphite 4",
  43. "tags": ["graphite"],
  44. "rows": [
  45. {
  46. "panels": [
  47. {
  48. "title": "Active desktop users",
  49. "editable": true,
  50. "type": "graph",
  51. "id": 3,
  52. "targets": [
  53. {
  54. "refId": "A",
  55. "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
  56. }
  57. ],
  58. "datasource": null,
  59. "alert": {
  60. "name": "name1",
  61. "message": "desc1",
  62. "handler": 1,
  63. "frequency": "60s",
  64. "conditions": [
  65. {
  66. "type": "query",
  67. "query": {"params": ["A", "5m", "now"]},
  68. "reducer": {"type": "avg", "params": []},
  69. "evaluator": {"type": ">", "params": [100]}
  70. }
  71. ]
  72. }
  73. },
  74. {
  75. "title": "Active mobile users",
  76. "id": 4,
  77. "targets": [
  78. {"refId": "A", "target": ""},
  79. {"refId": "B", "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
  80. ],
  81. "datasource": "graphite2",
  82. "alert": {
  83. "name": "name2",
  84. "message": "desc2",
  85. "handler": 0,
  86. "frequency": "60s",
  87. "severity": "warning",
  88. "conditions": [
  89. {
  90. "type": "query",
  91. "query": {"params": ["B", "5m", "now"]},
  92. "reducer": {"type": "avg", "params": []},
  93. "evaluator": {"type": ">", "params": [100]}
  94. }
  95. ]
  96. }
  97. }
  98. ]
  99. }
  100. ]
  101. }`
  102. Convey("Parsing and validating dashboard containing graphite alerts", func() {
  103. dashJson, err := simplejson.NewJson([]byte(json))
  104. So(err, ShouldBeNil)
  105. dash := m.NewDashboardFromJson(dashJson)
  106. extractor := NewDashAlertExtractor(dash, 1)
  107. alerts, err := extractor.GetAlerts()
  108. Convey("Get rules without error", func() {
  109. So(err, ShouldBeNil)
  110. })
  111. Convey("all properties have been set", func() {
  112. So(len(alerts), ShouldEqual, 2)
  113. for _, v := range alerts {
  114. So(v.DashboardId, ShouldEqual, 57)
  115. So(v.Name, ShouldNotBeEmpty)
  116. So(v.Message, ShouldNotBeEmpty)
  117. settings := simplejson.NewFromAny(v.Settings)
  118. So(settings.Get("interval").MustString(""), ShouldEqual, "")
  119. }
  120. Convey("should extract handler property", func() {
  121. So(alerts[0].Handler, ShouldEqual, 1)
  122. So(alerts[1].Handler, ShouldEqual, 0)
  123. })
  124. Convey("should extract frequency in seconds", func() {
  125. So(alerts[0].Frequency, ShouldEqual, 60)
  126. So(alerts[1].Frequency, ShouldEqual, 60)
  127. })
  128. Convey("should extract panel idc", func() {
  129. So(alerts[0].PanelId, ShouldEqual, 3)
  130. So(alerts[1].PanelId, ShouldEqual, 4)
  131. })
  132. Convey("should extract name and desc", func() {
  133. So(alerts[0].Name, ShouldEqual, "name1")
  134. So(alerts[0].Message, ShouldEqual, "desc1")
  135. So(alerts[1].Name, ShouldEqual, "name2")
  136. So(alerts[1].Message, ShouldEqual, "desc2")
  137. })
  138. Convey("should set datasourceId", func() {
  139. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  140. query := condition.Get("query")
  141. So(query.Get("datasourceId").MustInt64(), ShouldEqual, 12)
  142. })
  143. Convey("should copy query model to condition", func() {
  144. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  145. model := condition.Get("query").Get("model")
  146. So(model.Get("target").MustString(), ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)")
  147. })
  148. })
  149. })
  150. Convey("Parse and validate dashboard containing influxdb alert", func() {
  151. json2 := `{
  152. "id": 4,
  153. "title": "Influxdb",
  154. "tags": [
  155. "apa"
  156. ],
  157. "style": "dark",
  158. "timezone": "browser",
  159. "editable": true,
  160. "hideControls": false,
  161. "sharedCrosshair": false,
  162. "rows": [
  163. {
  164. "collapse": false,
  165. "editable": true,
  166. "height": "450px",
  167. "panels": [
  168. {
  169. "alert": {
  170. "conditions": [
  171. {
  172. "evaluator": {
  173. "params": [
  174. 10
  175. ],
  176. "type": "gt"
  177. },
  178. "query": {
  179. "params": [
  180. "B",
  181. "5m",
  182. "now"
  183. ]
  184. },
  185. "reducer": {
  186. "params": [],
  187. "type": "avg"
  188. },
  189. "type": "query"
  190. }
  191. ],
  192. "frequency": "3s",
  193. "handler": 1,
  194. "name": "Influxdb",
  195. "noDataState": "no_data",
  196. "notifications": [
  197. {
  198. "id": 6
  199. }
  200. ]
  201. },
  202. "alerting": {},
  203. "aliasColors": {
  204. "logins.count.count": "#890F02"
  205. },
  206. "bars": false,
  207. "datasource": "InfluxDB",
  208. "editable": true,
  209. "error": false,
  210. "fill": 1,
  211. "grid": {},
  212. "id": 1,
  213. "interval": ">10s",
  214. "isNew": true,
  215. "legend": {
  216. "avg": false,
  217. "current": false,
  218. "max": false,
  219. "min": false,
  220. "show": true,
  221. "total": false,
  222. "values": false
  223. },
  224. "lines": true,
  225. "linewidth": 2,
  226. "links": [],
  227. "nullPointMode": "connected",
  228. "percentage": false,
  229. "pointradius": 5,
  230. "points": false,
  231. "renderer": "flot",
  232. "seriesOverrides": [],
  233. "span": 10,
  234. "stack": false,
  235. "steppedLine": false,
  236. "targets": [
  237. {
  238. "dsType": "influxdb",
  239. "groupBy": [
  240. {
  241. "params": [
  242. "$interval"
  243. ],
  244. "type": "time"
  245. },
  246. {
  247. "params": [
  248. "datacenter"
  249. ],
  250. "type": "tag"
  251. },
  252. {
  253. "params": [
  254. "none"
  255. ],
  256. "type": "fill"
  257. }
  258. ],
  259. "hide": false,
  260. "measurement": "logins.count",
  261. "policy": "default",
  262. "query": "SELECT 8 * count(\"value\") FROM \"logins.count\" WHERE $timeFilter GROUP BY time($interval), \"datacenter\" fill(none)",
  263. "rawQuery": true,
  264. "refId": "B",
  265. "resultFormat": "time_series",
  266. "select": [
  267. [
  268. {
  269. "params": [
  270. "value"
  271. ],
  272. "type": "field"
  273. },
  274. {
  275. "params": [],
  276. "type": "count"
  277. }
  278. ]
  279. ],
  280. "tags": []
  281. },
  282. {
  283. "dsType": "influxdb",
  284. "groupBy": [
  285. {
  286. "params": [
  287. "$interval"
  288. ],
  289. "type": "time"
  290. },
  291. {
  292. "params": [
  293. "null"
  294. ],
  295. "type": "fill"
  296. }
  297. ],
  298. "hide": true,
  299. "measurement": "cpu",
  300. "policy": "default",
  301. "refId": "A",
  302. "resultFormat": "time_series",
  303. "select": [
  304. [
  305. {
  306. "params": [
  307. "value"
  308. ],
  309. "type": "field"
  310. },
  311. {
  312. "params": [],
  313. "type": "mean"
  314. }
  315. ],
  316. [
  317. {
  318. "params": [
  319. "value"
  320. ],
  321. "type": "field"
  322. },
  323. {
  324. "params": [],
  325. "type": "sum"
  326. }
  327. ]
  328. ],
  329. "tags": []
  330. }
  331. ],
  332. "thresholds": [
  333. {
  334. "colorMode": "critical",
  335. "fill": true,
  336. "line": true,
  337. "op": "gt",
  338. "value": 10
  339. }
  340. ],
  341. "timeFrom": null,
  342. "timeShift": null,
  343. "title": "Panel Title",
  344. "tooltip": {
  345. "msResolution": false,
  346. "ordering": "alphabetical",
  347. "shared": true,
  348. "sort": 0,
  349. "value_type": "cumulative"
  350. },
  351. "type": "graph",
  352. "xaxis": {
  353. "mode": "time",
  354. "name": null,
  355. "show": true,
  356. "values": []
  357. },
  358. "yaxes": [
  359. {
  360. "format": "short",
  361. "logBase": 1,
  362. "max": null,
  363. "min": null,
  364. "show": true
  365. },
  366. {
  367. "format": "short",
  368. "logBase": 1,
  369. "max": null,
  370. "min": null,
  371. "show": true
  372. }
  373. ]
  374. },
  375. {
  376. "editable": true,
  377. "error": false,
  378. "id": 2,
  379. "isNew": true,
  380. "limit": 10,
  381. "links": [],
  382. "show": "current",
  383. "span": 2,
  384. "stateFilter": [
  385. "alerting"
  386. ],
  387. "title": "Alert status",
  388. "type": "alertlist"
  389. }
  390. ],
  391. "title": "Row"
  392. }
  393. ],
  394. "time": {
  395. "from": "now-5m",
  396. "to": "now"
  397. },
  398. "timepicker": {
  399. "now": true,
  400. "refresh_intervals": [
  401. "5s",
  402. "10s",
  403. "30s",
  404. "1m",
  405. "5m",
  406. "15m",
  407. "30m",
  408. "1h",
  409. "2h",
  410. "1d"
  411. ],
  412. "time_options": [
  413. "5m",
  414. "15m",
  415. "1h",
  416. "6h",
  417. "12h",
  418. "24h",
  419. "2d",
  420. "7d",
  421. "30d"
  422. ]
  423. },
  424. "templating": {
  425. "list": []
  426. },
  427. "annotations": {
  428. "list": []
  429. },
  430. "schemaVersion": 13,
  431. "version": 120,
  432. "links": [],
  433. "gnetId": null
  434. }`
  435. dashJson, err := simplejson.NewJson([]byte(json2))
  436. So(err, ShouldBeNil)
  437. dash := m.NewDashboardFromJson(dashJson)
  438. extractor := NewDashAlertExtractor(dash, 1)
  439. alerts, err := extractor.GetAlerts()
  440. Convey("Get rules without error", func() {
  441. So(err, ShouldBeNil)
  442. })
  443. Convey("should be able to read interval", func() {
  444. So(len(alerts), ShouldEqual, 1)
  445. for _, alert := range alerts {
  446. So(alert.DashboardId, ShouldEqual, 4)
  447. conditions := alert.Settings.Get("conditions").MustArray()
  448. cond := simplejson.NewFromAny(conditions[0])
  449. So(cond.Get("query").Get("model").Get("interval").MustString(), ShouldEqual, ">10s")
  450. }
  451. })
  452. })
  453. })
  454. }