瀏覽代碼

Fix HTTP alerting

Brendan Abolivier 7 年之前
父節點
當前提交
b3c6b533bb
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 ID: 8EF1500759F70623
共有 1 個文件被更改,包括 12 次插入2 次删除
  1. 12
    2
      src/metrics-alerting/alert/http.go

+ 12
- 2
src/metrics-alerting/alert/http.go 查看文件

3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
 	"encoding/json"
5
 	"encoding/json"
6
+	"fmt"
6
 	"net/http"
7
 	"net/http"
8
+	"strconv"
7
 
9
 
8
 	"metrics-alerting/config"
10
 	"metrics-alerting/config"
9
 )
11
 )
14
 }
16
 }
15
 
17
 
16
 func alertHttp(script config.Script, result interface{}) error {
18
 func alertHttp(script config.Script, result interface{}) error {
19
+	var value string
20
+	switch script.Type {
21
+	case "number":
22
+		value = strconv.FormatFloat(result.(float64), 'e', -1, 64)
23
+	case "bool":
24
+		value = strconv.FormatBool(result.(bool))
25
+	}
26
+
17
 	alert := alertBody{
27
 	alert := alertBody{
18
 		Key:   script.Key,
28
 		Key:   script.Key,
19
-		Value: result,
29
+		Value: value,
20
 	}
30
 	}
21
 
31
 
22
 	body, err := json.Marshal(alert)
32
 	body, err := json.Marshal(alert)
35
 
45
 
36
 	if resp.StatusCode != http.StatusOK {
46
 	if resp.StatusCode != http.StatusOK {
37
 		return fmt.Errorf(
47
 		return fmt.Errorf(
38
-			"target %s returned non-200 status code %d", script.Target, resp.StatusCode
48
+			"target %s returned non-200 status code %d", script.Target, resp.StatusCode,
39
 		)
49
 		)
40
 	}
50
 	}
41
 
51