-
Notifications
You must be signed in to change notification settings - Fork 140
/
types.go
96 lines (82 loc) · 2.05 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package types
import (
"encoding/json"
"fmt"
"math"
"strconv"
)
// Value contains timestamp/value after parsing
type Value struct {
Timestamp float64
Value float64
}
// Result contains result as returned by Prometheus
type Result struct {
Metric map[string]string `json:"metric"`
Values []Value `json:"values"`
}
// Data - all useful data (except status and errors) that's returned by prometheus
type Data struct {
Result []Result `json:"result"`
ResultType string `json:"resultType"`
}
// HTTPResponse - full HTTP response from prometheus
type HTTPResponse struct {
Status string `json:"status"`
ErrorType string `json:"errorType"`
Error string `json:"error"`
Data Data `json:"data"`
}
func (p *Value) UnmarshalJSON(data []byte) error {
arr := make([]interface{}, 0)
err := json.Unmarshal(data, &arr)
if err != nil {
return err
}
if len(arr) != 2 {
return fmt.Errorf("length mismatch, got %v, expected 2", len(arr))
}
var ok bool
p.Timestamp, ok = arr[0].(float64)
if !ok {
return fmt.Errorf("type mismatch for element[0/1], expected 'float64', got '%T', str=%v", arr[0], string(data))
}
str, ok := arr[1].(string)
if !ok {
return fmt.Errorf("type mismatch for element[1/1], expected 'string', got '%T', str=%v", arr[1], string(data))
}
switch str {
case "NaN":
p.Value = math.NaN()
return nil
case "+Inf":
p.Value = math.Inf(1)
return nil
case "-Inf":
p.Value = math.Inf(-1)
return nil
default:
p.Value, err = strconv.ParseFloat(str, 64)
if err != nil {
return err
}
}
return nil
}
type PrometheusTagResponse struct {
Status string `json:"status"`
ErrorType string `json:"errorType"`
Error string `json:"error"`
Data []string `json:"data"`
}
type PrometheusFindResponse struct {
Status string `json:"status"`
ErrorType string `json:"errorType"`
Error string `json:"error"`
Data []map[string]string `json:"data"`
}
// Tag handles prometheus-specific tags
type Tag struct {
TagValue string
OP string
}