From dc5240413dd41247e8b29d0c36f90becf788a64f Mon Sep 17 00:00:00 2001 From: RajeshR Date: Fri, 13 Oct 2023 22:26:54 +0530 Subject: [PATCH 01/11] Add alerting --- src/plugin.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugin.json b/src/plugin.json index a28df9c..c5beaf3 100644 --- a/src/plugin.json +++ b/src/plugin.json @@ -6,6 +6,7 @@ "metrics": true, "backend": true, "executable": "gpx_mongodb-community", + "alerting": true, "info": { "description": "Community-supported MongoDB Datasource Plugin", "author": { From ce7f2f85ceb86fcfcdbb535316201aa375005d9e Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 06:27:51 +0000 Subject: [PATCH 02/11] add integration tests for alerting --- e2e/e2e_test.go | 44 +++++++++++++++++++ .../queries/weather/alerts/create-folder.json | 1 + .../weather/alerts/create-rule-group.json | 1 + 3 files changed, 46 insertions(+) create mode 100644 integration-test/queries/weather/alerts/create-folder.json create mode 100644 integration-test/queries/weather/alerts/create-rule-group.json diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 01f40af..81271dc 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -106,6 +106,50 @@ var _ = Describe("The plugin", func() { Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) } + + createFolder := "weather/alerts/create-folder" + createRuleGroup := "weather/alerts/create-rule-group" + It(fmt.Sprintf("should execute the %s query", createFolder), func() { + f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) + Expect(err).ToNot(HaveOccurred()) + + req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/folders", f) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + req.Header.Set("content-type", "application/json") + resp, err := client.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) + It(fmt.Sprintf("should execute the %s query", createRuleGroup), func() { + f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) + Expect(err).ToNot(HaveOccurred()) + + req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alert_folder?subtype=cortex", f) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + req.Header.Set("content-type", "application/json") + resp, err := client.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) + It(fmt.Sprintf("should execute the alerts evaluation query", createRuleGroup), func() { + req, err := http.NewRequest(http.MethodGet, "http://grafana.grafana-mongodb-it.cluster/api/prometheus/grafana/api/v1/rules", nil) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + resp, err := client.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) } }) diff --git a/integration-test/queries/weather/alerts/create-folder.json b/integration-test/queries/weather/alerts/create-folder.json new file mode 100644 index 0000000..ee69d00 --- /dev/null +++ b/integration-test/queries/weather/alerts/create-folder.json @@ -0,0 +1 @@ +{"title":"alerts","uid":"c0a02352-8f64-4b5e-a40c-3d19780d45a4"} \ No newline at end of file diff --git a/integration-test/queries/weather/alerts/create-rule-group.json b/integration-test/queries/weather/alerts/create-rule-group.json new file mode 100644 index 0000000..54df8af --- /dev/null +++ b/integration-test/queries/weather/alerts/create-rule-group.json @@ -0,0 +1 @@ +{"name":"a","interval":"5m","rules":[{"grafana_alert":{"title":"sensoralert","condition":"C","no_data_state":"NoData","exec_err_state":"Error","data":[{"refId":"A","datasourceUid":"P1CC9A79BDAF09793","queryType":"Timeseries","relativeTimeRange":{"from":600,"to":0},"model":{"refId":"A","database":"test","collection":"tweets","queryType":"Timeseries","timestampField":"timestamp","timestampFormat":"","labelFields":["sensorID"],"legendFormat":"","valueFields":["temperature"],"valueFieldTypes":["int32"],"aggregation":"[{\"$project\":{\"timestamp\":1,\"sensorID\":\"$metadata.sensorId\",\"temperature\":\"$temp\"}}]","autoTimeBound":false,"autoTimeSort":false,"schemaInference":false,"schemaInferenceDepth":20}},{"refId":"B","datasourceUid":"__expr__","queryType":"","model":{"refId":"B","type":"reduce","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[],"type":"gt"},"operator":{"type":"and"},"query":{"params":["B"]},"reducer":{"params":[],"type":"last"}}],"reducer":"last","expression":"A"},"relativeTimeRange":{"from":600,"to":0}},{"refId":"C","datasourceUid":"__expr__","queryType":"","model":{"refId":"C","type":"threshold","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[0],"type":"gt"},"operator":{"type":"and"},"query":{"params":["C"]},"reducer":{"params":[],"type":"last"}}],"expression":"B"},"relativeTimeRange":{"from":600,"to":0}}],"is_paused":false},"for":"5m","annotations":{},"labels":{}}]} \ No newline at end of file From 9e94fa8a6be6ebd803f01761410239b07e7a3277 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 06:34:30 +0000 Subject: [PATCH 03/11] import os --- e2e/e2e_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 81271dc..18f23d2 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "os" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/dom" From 6e04f307f8abe71412d55d40520f36c28d04a50d Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 06:35:04 +0000 Subject: [PATCH 04/11] import filepath --- e2e/e2e_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 18f23d2..f635404 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "os" + "path/filepath" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/dom" From 8d3506bf49a1585950d23f096a0aade7be342a90 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 06:39:55 +0000 Subject: [PATCH 05/11] update to clusterHTTPClient --- e2e/e2e_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index f635404..c8b08b7 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -120,7 +120,7 @@ var _ = Describe("The plugin", func() { req.SetBasicAuth("admin", "adminPassword") req.Header.Set("accept", "application/json, text/plain, */*") req.Header.Set("content-type", "application/json") - resp, err := client.Do(req) + resp, err := clusterHTTPClient.Do(req) Expect(err).ToNot(HaveOccurred()) _, err = io.Copy(GinkgoWriter, resp.Body) Expect(err).ToNot(HaveOccurred()) @@ -135,7 +135,7 @@ var _ = Describe("The plugin", func() { req.SetBasicAuth("admin", "adminPassword") req.Header.Set("accept", "application/json, text/plain, */*") req.Header.Set("content-type", "application/json") - resp, err := client.Do(req) + resp, err := clusterHTTPClient.Do(req) Expect(err).ToNot(HaveOccurred()) _, err = io.Copy(GinkgoWriter, resp.Body) Expect(err).ToNot(HaveOccurred()) @@ -146,7 +146,7 @@ var _ = Describe("The plugin", func() { Expect(err).ToNot(HaveOccurred()) req.SetBasicAuth("admin", "adminPassword") req.Header.Set("accept", "application/json, text/plain, */*") - resp, err := client.Do(req) + resp, err := clusterHTTPClient.Do(req) Expect(err).ToNot(HaveOccurred()) _, err = io.Copy(GinkgoWriter, resp.Body) Expect(err).ToNot(HaveOccurred()) From b69cffd2b799546f5a3e7799526aea3a0c8c74e1 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 06:49:37 +0000 Subject: [PATCH 06/11] fix print bug --- e2e/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index c8b08b7..71fa057 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -141,7 +141,7 @@ var _ = Describe("The plugin", func() { Expect(err).ToNot(HaveOccurred()) Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) - It(fmt.Sprintf("should execute the alerts evaluation query", createRuleGroup), func() { + It(fmt.Sprintf("should execute the alerts evaluation query"), func() { req, err := http.NewRequest(http.MethodGet, "http://grafana.grafana-mongodb-it.cluster/api/prometheus/grafana/api/v1/rules", nil) Expect(err).ToNot(HaveOccurred()) req.SetBasicAuth("admin", "adminPassword") From e5fd7391a2df74b94f2e854a9816b3f480c7aac0 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 08:23:01 +0000 Subject: [PATCH 07/11] update tests --- e2e/e2e_test.go | 88 ++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 71fa057..95a916a 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -108,52 +108,52 @@ var _ = Describe("The plugin", func() { Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) } - - createFolder := "weather/alerts/create-folder" - createRuleGroup := "weather/alerts/create-rule-group" - It(fmt.Sprintf("should execute the %s query", createFolder), func() { - f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) - Expect(err).ToNot(HaveOccurred()) - - req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/folders", f) - Expect(err).ToNot(HaveOccurred()) - req.SetBasicAuth("admin", "adminPassword") - req.Header.Set("accept", "application/json, text/plain, */*") - req.Header.Set("content-type", "application/json") - resp, err := clusterHTTPClient.Do(req) - Expect(err).ToNot(HaveOccurred()) - _, err = io.Copy(GinkgoWriter, resp.Body) - Expect(err).ToNot(HaveOccurred()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - }) - It(fmt.Sprintf("should execute the %s query", createRuleGroup), func() { - f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) - Expect(err).ToNot(HaveOccurred()) - - req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alert_folder?subtype=cortex", f) - Expect(err).ToNot(HaveOccurred()) - req.SetBasicAuth("admin", "adminPassword") - req.Header.Set("accept", "application/json, text/plain, */*") - req.Header.Set("content-type", "application/json") - resp, err := clusterHTTPClient.Do(req) - Expect(err).ToNot(HaveOccurred()) - _, err = io.Copy(GinkgoWriter, resp.Body) - Expect(err).ToNot(HaveOccurred()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - }) - It(fmt.Sprintf("should execute the alerts evaluation query"), func() { - req, err := http.NewRequest(http.MethodGet, "http://grafana.grafana-mongodb-it.cluster/api/prometheus/grafana/api/v1/rules", nil) - Expect(err).ToNot(HaveOccurred()) - req.SetBasicAuth("admin", "adminPassword") - req.Header.Set("accept", "application/json, text/plain, */*") - resp, err := clusterHTTPClient.Do(req) - Expect(err).ToNot(HaveOccurred()) - _, err = io.Copy(GinkgoWriter, resp.Body) - Expect(err).ToNot(HaveOccurred()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) - }) } + createFolder := "weather/alerts/create-folder" + createRuleGroup := "weather/alerts/create-rule-group" + It(fmt.Sprintf("should execute the %s query", createFolder), func() { + f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) + Expect(err).ToNot(HaveOccurred()) + + req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/folders", f) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + req.Header.Set("content-type", "application/json") + resp, err := clusterHTTPClient.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) + It(fmt.Sprintf("should execute the %s query", createRuleGroup), func() { + f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) + Expect(err).ToNot(HaveOccurred()) + + req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alert_folder?subtype=cortex", f) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + req.Header.Set("content-type", "application/json") + resp, err := clusterHTTPClient.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) + It(fmt.Sprintf("should execute the alerts evaluation query"), func() { + req, err := http.NewRequest(http.MethodGet, "http://grafana.grafana-mongodb-it.cluster/api/prometheus/grafana/api/v1/rules", nil) + Expect(err).ToNot(HaveOccurred()) + req.SetBasicAuth("admin", "adminPassword") + req.Header.Set("accept", "application/json, text/plain, */*") + resp, err := clusterHTTPClient.Do(req) + Expect(err).ToNot(HaveOccurred()) + _, err = io.Copy(GinkgoWriter, resp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + }) + }) func nodes(sel interface{}) []*cdp.Node { From c39841e332a1c36bc046c71e4686e78381e5b9c2 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 08:41:08 +0000 Subject: [PATCH 08/11] change folder name --- e2e/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 95a916a..91aab44 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -131,7 +131,7 @@ var _ = Describe("The plugin", func() { f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) Expect(err).ToNot(HaveOccurred()) - req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alert_folder?subtype=cortex", f) + req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alerts?subtype=cortex", f) Expect(err).ToNot(HaveOccurred()) req.SetBasicAuth("admin", "adminPassword") req.Header.Set("accept", "application/json, text/plain, */*") From 00ce9b32a684e568375ba472b995db3d93c0d13f Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 09:16:17 +0000 Subject: [PATCH 09/11] change name --- integration-test/queries/weather/alerts/create-rule-group.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-test/queries/weather/alerts/create-rule-group.json b/integration-test/queries/weather/alerts/create-rule-group.json index 54df8af..a80f7b2 100644 --- a/integration-test/queries/weather/alerts/create-rule-group.json +++ b/integration-test/queries/weather/alerts/create-rule-group.json @@ -1 +1 @@ -{"name":"a","interval":"5m","rules":[{"grafana_alert":{"title":"sensoralert","condition":"C","no_data_state":"NoData","exec_err_state":"Error","data":[{"refId":"A","datasourceUid":"P1CC9A79BDAF09793","queryType":"Timeseries","relativeTimeRange":{"from":600,"to":0},"model":{"refId":"A","database":"test","collection":"tweets","queryType":"Timeseries","timestampField":"timestamp","timestampFormat":"","labelFields":["sensorID"],"legendFormat":"","valueFields":["temperature"],"valueFieldTypes":["int32"],"aggregation":"[{\"$project\":{\"timestamp\":1,\"sensorID\":\"$metadata.sensorId\",\"temperature\":\"$temp\"}}]","autoTimeBound":false,"autoTimeSort":false,"schemaInference":false,"schemaInferenceDepth":20}},{"refId":"B","datasourceUid":"__expr__","queryType":"","model":{"refId":"B","type":"reduce","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[],"type":"gt"},"operator":{"type":"and"},"query":{"params":["B"]},"reducer":{"params":[],"type":"last"}}],"reducer":"last","expression":"A"},"relativeTimeRange":{"from":600,"to":0}},{"refId":"C","datasourceUid":"__expr__","queryType":"","model":{"refId":"C","type":"threshold","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[0],"type":"gt"},"operator":{"type":"and"},"query":{"params":["C"]},"reducer":{"params":[],"type":"last"}}],"expression":"B"},"relativeTimeRange":{"from":600,"to":0}}],"is_paused":false},"for":"5m","annotations":{},"labels":{}}]} \ No newline at end of file +{"name":"alert_rule","interval":"5m","rules":[{"grafana_alert":{"title":"sensoralert","condition":"C","no_data_state":"NoData","exec_err_state":"Error","data":[{"refId":"A","datasourceUid":"P1CC9A79BDAF09793","queryType":"Timeseries","relativeTimeRange":{"from":600,"to":0},"model":{"refId":"A","database":"test","collection":"tweets","queryType":"Timeseries","timestampField":"timestamp","timestampFormat":"","labelFields":["sensorID"],"legendFormat":"","valueFields":["temperature"],"valueFieldTypes":["int32"],"aggregation":"[{\"$project\":{\"timestamp\":1,\"sensorID\":\"$metadata.sensorId\",\"temperature\":\"$temp\"}}]","autoTimeBound":false,"autoTimeSort":false,"schemaInference":false,"schemaInferenceDepth":20}},{"refId":"B","datasourceUid":"__expr__","queryType":"","model":{"refId":"B","type":"reduce","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[],"type":"gt"},"operator":{"type":"and"},"query":{"params":["B"]},"reducer":{"params":[],"type":"last"}}],"reducer":"last","expression":"A"},"relativeTimeRange":{"from":600,"to":0}},{"refId":"C","datasourceUid":"__expr__","queryType":"","model":{"refId":"C","type":"threshold","datasource":{"uid":"__expr__","type":"__expr__"},"conditions":[{"type":"query","evaluator":{"params":[0],"type":"gt"},"operator":{"type":"and"},"query":{"params":["C"]},"reducer":{"params":[],"type":"last"}}],"expression":"B"},"relativeTimeRange":{"from":600,"to":0}}],"is_paused":false},"for":"5m","annotations":{},"labels":{}}]} \ No newline at end of file From c8c047e0d01b9f35462dcc901abe3c189f1b4235 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 09:31:47 +0000 Subject: [PATCH 10/11] change json --- e2e/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 91aab44..6fce975 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -128,7 +128,7 @@ var _ = Describe("The plugin", func() { Expect(resp.StatusCode).To(Equal(http.StatusOK)) }) It(fmt.Sprintf("should execute the %s query", createRuleGroup), func() { - f, err := os.Open(filepath.Join("../integration-test/queries", createFolder+".json")) + f, err := os.Open(filepath.Join("../integration-test/queries", createRuleGroup+".json")) Expect(err).ToNot(HaveOccurred()) req, err := http.NewRequest(http.MethodPost, "http://grafana.grafana-mongodb-it.cluster/api/ruler/grafana/api/v1/rules/alerts?subtype=cortex", f) From da4aaa75a1c397b79bcc50bfac8acfd9e4144469 Mon Sep 17 00:00:00 2001 From: RajeshR Date: Thu, 26 Oct 2023 13:18:46 +0000 Subject: [PATCH 11/11] change ok to accepted --- e2e/e2e_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 6fce975..67cdeaa 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -140,7 +140,7 @@ var _ = Describe("The plugin", func() { Expect(err).ToNot(HaveOccurred()) _, err = io.Copy(GinkgoWriter, resp.Body) Expect(err).ToNot(HaveOccurred()) - Expect(resp.StatusCode).To(Equal(http.StatusOK)) + Expect(resp.StatusCode).To(Equal(http.StatusAccepted)) }) It(fmt.Sprintf("should execute the alerts evaluation query"), func() { req, err := http.NewRequest(http.MethodGet, "http://grafana.grafana-mongodb-it.cluster/api/prometheus/grafana/api/v1/rules", nil)