-
Notifications
You must be signed in to change notification settings - Fork 3
/
histogram.go
176 lines (145 loc) · 4.5 KB
/
histogram.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"path"
"strconv"
"time"
"github.com/openhistogram/circonusllhist"
)
// HistogramValue values are individual data points of a histogram metric.
type HistogramValue struct {
Time time.Time
Period time.Duration
Data map[string]int64
}
// MarshalJSON encodes a HistogramValue value into a JSON format byte slice.
func (hv *HistogramValue) MarshalJSON() ([]byte, error) {
v := []interface{}{}
fv, err := strconv.ParseFloat(formatTimestamp(hv.Time), 64)
if err != nil {
return nil, fmt.Errorf("invalid histogram value time: " +
formatTimestamp(hv.Time))
}
v = append(v, fv)
v = append(v, hv.Period.Seconds())
v = append(v, hv.Data)
return json.Marshal(v)
}
// UnmarshalJSON decodes a JSON format byte slice into a HistogramValue value.
func (hv *HistogramValue) UnmarshalJSON(b []byte) error {
v := []interface{}{}
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
if len(v) != 3 {
return fmt.Errorf("histogram value should contain three entries: " +
string(b))
}
if fv, ok := v[0].(float64); ok {
tv, err := parseTimestamp(strconv.FormatFloat(fv, 'f', 3, 64))
if err != nil {
return err
}
hv.Time = tv
}
if fv, ok := v[1].(float64); ok {
hv.Period = time.Duration(fv) * time.Second
}
if m, ok := v[2].(map[string]interface{}); ok {
hv.Data = make(map[string]int64, len(m))
for k, iv := range m {
if fv, ok := iv.(float64); ok {
hv.Data[k] = int64(fv)
}
}
}
return nil
}
// Timestamp returns the HistogramValue time as a string in the IRONdb
// timestamp format.
func (hv *HistogramValue) Timestamp() string {
return formatTimestamp(hv.Time)
}
// ReadHistogramValues reads histogram data from a node.
func (sc *SnowthClient) ReadHistogramValues(
uuid, metric string, period time.Duration,
start, end time.Time, nodes ...*SnowthNode,
) ([]HistogramValue, error) {
return sc.ReadHistogramValuesContext(context.Background(), uuid,
metric, period, start, end, nodes...)
}
// ReadHistogramValuesContext is the context aware version of
// ReadHistogramValues.
func (sc *SnowthClient) ReadHistogramValuesContext(ctx context.Context,
uuid, metric string, period time.Duration,
start, end time.Time, nodes ...*SnowthNode,
) ([]HistogramValue, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(uuid, metric))
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
startTS := start.Unix() - start.Unix()%int64(period.Seconds())
endTS := end.Unix() - end.Unix()%int64(period.Seconds()) +
int64(period.Seconds())
r := []HistogramValue{}
body, _, err := sc.DoRequestContext(ctx, node, "GET",
path.Join("/histogram", strconv.FormatInt(startTS, 10),
strconv.FormatInt(endTS, 10),
strconv.FormatInt(int64(period.Seconds()), 10), uuid,
url.QueryEscape(metric)), nil, nil)
if err != nil {
return nil, err
}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}
// HistogramData values represent histogram data records in IRONdb.
type HistogramData struct {
AccountID int64 `json:"account_id"`
Metric string `json:"metric"`
ID string `json:"id"`
CheckName string `json:"check_name"`
Offset int64 `json:"offset"`
Period int64 `json:"period"`
Histogram *circonusllhist.Histogram `json:"histogram"`
}
// WriteHistogram sends a list of histogram data values to be written
// to an IRONdb node.
func (sc *SnowthClient) WriteHistogram(data []HistogramData,
nodes ...*SnowthNode,
) error {
return sc.WriteHistogramContext(context.Background(), data, nodes...)
}
// WriteHistogramContext is the context aware version of WriteHistogram.
func (sc *SnowthClient) WriteHistogramContext(ctx context.Context,
data []HistogramData, nodes ...*SnowthNode,
) error {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else if len(data) > 0 {
node = sc.GetActiveNode(sc.FindMetricNodeIDs(data[0].ID,
data[0].Metric))
}
if node == nil {
return fmt.Errorf("unable to get active node")
}
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(data); err != nil {
return fmt.Errorf("failed to encode HistogramData for write: %w", err)
}
_, _, err := sc.DoRequestContext(ctx, node, "POST", "/histogram/write", buf, nil)
return err
}