-
Notifications
You must be signed in to change notification settings - Fork 208
/
operation.go
158 lines (133 loc) · 4.44 KB
/
operation.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
package operation
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/hashicorp/go-multierror"
)
type MetricOperation struct {
Name string `json:"name"`
// Deprecated: use Value + Action="add" instead. Add only works for parsing from file
Add *float64 `json:"add,omitempty"` // shortcut for action=add value=num
// Deprecated: use Value + Action="set" instead. Set only works for parsing from file
Set *float64 `json:"set,omitempty"` // shortcut for action=set value=num
Value *float64 `json:"value,omitempty"`
Buckets []float64 `json:"buckets,omitempty"`
Labels map[string]string `json:"labels"`
Group string `json:"group,omitempty"`
Action string `json:"action,omitempty"`
}
func (m MetricOperation) String() string {
parts := make([]string, 0)
if m.Group != "" {
parts = append(parts, "group="+m.Group)
}
if m.Name != "" {
parts = append(parts, "name="+m.Name)
}
if m.Action != "" {
parts = append(parts, "action="+m.Action)
}
if m.Value != nil {
parts = append(parts, fmt.Sprintf("value=%f", *m.Value))
}
if m.Set != nil {
parts = append(parts, fmt.Sprintf("set=%f", *m.Set))
}
if m.Add != nil {
parts = append(parts, fmt.Sprintf("add=%f", *m.Add))
}
if m.Buckets != nil {
parts = append(parts, fmt.Sprintf("buckets=%+v", m.Buckets))
}
if m.Labels != nil {
parts = append(parts, fmt.Sprintf("labels=%+v", m.Labels))
}
return "[" + strings.Join(parts, ", ") + "]"
}
func MetricOperationsFromReader(r io.Reader) ([]MetricOperation, error) {
operations := make([]MetricOperation, 0)
dec := json.NewDecoder(r)
for {
var metricOperation MetricOperation
if err := dec.Decode(&metricOperation); err == io.EOF {
break
} else if err != nil {
return nil, err
}
// shortcut transforms
if metricOperation.Set != nil && metricOperation.Add == nil {
metricOperation.Action = "set"
metricOperation.Value = metricOperation.Set
}
if metricOperation.Add != nil && metricOperation.Set == nil {
metricOperation.Action = "add"
metricOperation.Value = metricOperation.Add
}
operations = append(operations, metricOperation)
}
return operations, nil
}
func MetricOperationsFromBytes(data []byte) ([]MetricOperation, error) {
return MetricOperationsFromReader(bytes.NewReader(data))
}
func MetricOperationsFromFile(filePath string) ([]MetricOperation, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("cannot read %s: %s", filePath, err)
}
if len(data) == 0 {
return nil, nil
}
return MetricOperationsFromBytes(data)
}
func ValidateOperations(ops []MetricOperation) error {
var opsErrs *multierror.Error
for _, op := range ops {
err := ValidateMetricOperation(op)
if err != nil {
opsErrs = multierror.Append(opsErrs, err)
}
}
return opsErrs.ErrorOrNil()
}
func ValidateMetricOperation(op MetricOperation) error {
var opErrs *multierror.Error
if op.Action == "" {
opErrs = multierror.Append(opErrs, fmt.Errorf("one of: 'action', 'set' or 'add' is required: %s", op))
}
if op.Group == "" {
if op.Action != "set" && op.Action != "add" && op.Action != "observe" {
opErrs = multierror.Append(opErrs, fmt.Errorf("unsupported action '%s': %s", op.Action, op))
}
} else {
if op.Action != "expire" && op.Action != "set" && op.Action != "add" {
opErrs = multierror.Append(opErrs, fmt.Errorf("unsupported action '%s': %s", op.Action, op))
}
}
if op.Name == "" && op.Group == "" {
opErrs = multierror.Append(opErrs, fmt.Errorf("'name' is required: %s", op))
}
if op.Name == "" && op.Group != "" && op.Action != "expire" {
opErrs = multierror.Append(opErrs, fmt.Errorf("'name' is required when action is not 'expire': %s", op))
}
if op.Action == "set" && op.Value == nil {
opErrs = multierror.Append(opErrs, fmt.Errorf("'value' is required for action 'set': %s", op))
}
if op.Action == "add" && op.Value == nil {
opErrs = multierror.Append(opErrs, fmt.Errorf("'value' is required for action 'add': %s", op))
}
if op.Action == "observe" && op.Value == nil {
opErrs = multierror.Append(opErrs, fmt.Errorf("'value' is required for action 'observe': %s", op))
}
if op.Action == "observe" && op.Buckets == nil {
opErrs = multierror.Append(opErrs, fmt.Errorf("'buckets' is required for action 'observe': %s", op))
}
if op.Set != nil && op.Add != nil {
opErrs = multierror.Append(opErrs, fmt.Errorf("'set' and 'add' are mutual exclusive: %s", op))
}
return opErrs.ErrorOrNil()
}