/
metric.go
51 lines (48 loc) · 1.58 KB
/
metric.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
package reply
import (
"fmt"
"strconv"
"strings"
"github.com/gomodule/redigo/redis"
"github.com/moira-alert/moira"
)
// MetricValues converts redis DB reply struct "RetentionTimestamp Value" "Timestamp" to moira.MetricValue object
func MetricValues(values interface{}) ([]*moira.MetricValue, error) {
resultByMetricArr, err := redis.Values(values, nil)
if err != nil {
if err == redis.ErrNil {
return make([]*moira.MetricValue, 0), nil
}
return nil, fmt.Errorf("failed to read metricValues: %s", err.Error())
}
metricsValues := make([]*moira.MetricValue, 0, len(resultByMetricArr)/2)
for i := 0; i < len(resultByMetricArr); i += 2 {
val, err := redis.String(resultByMetricArr[i], nil)
if err != nil {
return nil, err
}
valuesArr := strings.Split(val, " ")
if len(valuesArr) != 2 {
return nil, fmt.Errorf("value format is not valid: %s", val)
}
timestamp, err := strconv.ParseInt(valuesArr[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("metric timestamp format is not valid: %s", err.Error())
}
value, err := strconv.ParseFloat(valuesArr[1], 64)
if err != nil {
return nil, fmt.Errorf("metric value format is not valid: %s", err.Error())
}
retentionTimestamp, err := redis.Int64(resultByMetricArr[i+1], nil)
if err != nil {
return nil, fmt.Errorf("retention timestamp format is not valid: %s", err.Error())
}
metricValue := moira.MetricValue{
RetentionTimestamp: retentionTimestamp,
Timestamp: timestamp,
Value: value,
}
metricsValues = append(metricsValues, &metricValue)
}
return metricsValues, nil
}