Explorar el Código

HTTP alerting

Brendan Abolivier hace 7 años
padre
commit
cc9862f3d5
Firmado por: Brendan Abolivier <contact@brendanabolivier.com> ID clave GPG: 8EF1500759F70623
Se han modificado 1 ficheros con 35 adiciones y 1 borrados
  1. 35
    1
      src/metrics-alerting/alert/http.go

+ 35
- 1
src/metrics-alerting/alert/http.go Ver fichero

@@ -1,9 +1,43 @@
1 1
 package alert
2 2
 
3 3
 import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"net/http"
7
+
4 8
 	"metrics-alerting/config"
5 9
 )
6 10
 
11
+type alertBody struct {
12
+	Key   string `json:"scriptKey"`
13
+	Value string `json:"value"`
14
+}
15
+
7 16
 func alertHttp(script config.Script, result interface{}) error {
8
-	return nil
17
+	alert := alertBody{
18
+		Key:   script.Key,
19
+		Value: result,
20
+	}
21
+
22
+	body, err := json.Marshal(alert)
23
+	if err != nil {
24
+		return err
25
+	}
26
+
27
+	req, err := http.NewRequest("POST", script.Target, bytes.NewBuffer(body))
28
+	if err != nil {
29
+		return err
30
+	}
31
+	req.Header.Set("Content-Type", "application/json")
32
+
33
+	client := http.Client{}
34
+	resp, err := client.Do(req)
35
+
36
+	if resp.StatusCode != http.StatusOK {
37
+		return fmt.Errorf(
38
+			"target %s returned non-200 status code %d", script.Target, resp.StatusCode
39
+		)
40
+	}
41
+
42
+	return err
9 43
 }