Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alerting to the plugin #23

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
46 changes: 46 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"

"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
Expand Down Expand Up @@ -108,6 +110,50 @@ var _ = Describe("The plugin", func() {
}
}

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", 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)
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.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)
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 {
Expand Down
1 change: 1 addition & 0 deletions integration-test/queries/weather/alerts/create-folder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"alerts","uid":"c0a02352-8f64-4b5e-a40c-3d19780d45a4"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"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":{}}]}
1 change: 1 addition & 0 deletions src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"metrics": true,
"backend": true,
"executable": "gpx_mongodb-community",
"alerting": true,
"info": {
"description": "Community-supported MongoDB Datasource Plugin",
"author": {
Expand Down
Loading