-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_stats_action.go
268 lines (233 loc) · 7.33 KB
/
set_stats_action.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
//"bytes"
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)
// ***************************************************************************************
// ***************************************************************************************
// set_fact
// ***************************************************************************************
// ***************************************************************************************
type SetStatsAction struct {
Name *Field `yaml:"name,omitempty"`
With []any `yaml:"with,omitempty"`
When []*exporterTemplate `yaml:"when,omitempty"`
LoopVar string `yaml:"loop_var,omitempty"`
Vars [][]any `yaml:"vars,omitempty"`
Until []*exporterTemplate `yaml:"until,omitempty"`
SetStats map[string]any `yaml:"set_stats"`
setStats [][]any
vars [][]any
}
func (a *SetStatsAction) Type() int {
return set_fact_action
}
func (a *SetStatsAction) GetName(symtab map[string]any, logger log.Logger) string {
str, err := a.Name.GetValueString(symtab, nil, false)
if err != nil {
level.Warn(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"msg", fmt.Sprintf("invalid action name: %v", err))
return ""
}
return str
}
func (a *SetStatsAction) GetNameField() *Field {
return a.Name
}
func (a *SetStatsAction) SetNameField(name *Field) {
a.Name = name
}
func (a *SetStatsAction) GetWidh() []any {
return a.With
}
func (a *SetStatsAction) SetWidth(with []any) {
a.With = with
}
func (a *SetStatsAction) GetWhen() []*exporterTemplate {
return a.When
}
func (a *SetStatsAction) SetWhen(when []*exporterTemplate) {
a.When = when
}
func (a *SetStatsAction) GetLoopVar() string {
return a.LoopVar
}
func (a *SetStatsAction) SetLoopVar(loopvar string) {
a.LoopVar = loopvar
}
func (a *SetStatsAction) GetVars() [][]any {
return a.vars
}
func (a *SetStatsAction) SetVars(vars [][]any) {
a.vars = vars
}
func (a *SetStatsAction) GetUntil() []*exporterTemplate {
return a.Until
}
func (a *SetStatsAction) SetUntil(until []*exporterTemplate) {
a.Until = until
}
func (a *SetStatsAction) setBasicElement(
nameField *Field,
vars [][]any,
with []any,
loopVar string,
when []*exporterTemplate,
until []*exporterTemplate) error {
return setBasicElement(a, nameField, vars, with, loopVar, when, until)
}
func (a *SetStatsAction) PlayAction(script *YAMLScript, symtab map[string]any, logger log.Logger) error {
return PlayBaseAction(script, symtab, logger, a, a.CustomAction)
}
// only for MetricsAction
func (a *SetStatsAction) GetMetrics() []*GetMetricsRes {
return nil
}
// only for MetricAction
func (a *SetStatsAction) GetMetric() *MetricConfig {
return nil
}
func (a *SetStatsAction) SetMetricFamily(*MetricFamily) {
}
// only for PlayAction
func (a *SetStatsAction) SetPlayAction(scripts map[string]*YAMLScript) error {
return nil
}
// specific behavior for the SetStatsAction
func (a *SetStatsAction) CustomAction(script *YAMLScript, symtab map[string]any, logger log.Logger) error {
var (
key_name string
err error
value_name any
)
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", "[Type: SetStatsAction]")
dst_symtab := make(map[string]any)
for _, pair := range a.setStats {
if pair == nil {
return fmt.Errorf("set_fact: invalid key value")
}
if key, ok := pair[0].(*Field); ok {
key_name, err = key.GetValueString(symtab, nil, false)
if err == nil {
if value_name, err = ValorizeValue(symtab, pair[1], logger, a.GetName(symtab, logger), 0); err != nil {
return err
}
if value_name == nil {
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", fmt.Sprintf(" %s is nil: not set into set_stats", key_name))
} else {
if value_name == "_" {
// null op key_name :
} else {
if key_name != "_" {
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", fmt.Sprintf(" add to symbols table: %s = '%v'", key_name, value_name))
if err := SetSymTab(dst_symtab, key_name, value_name); err != nil {
level.Warn(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", fmt.Sprintf("error setting map value for key '%s'", key_name), "errmsg", err)
continue
}
} else {
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", " result discard (key >'_')")
}
}
}
}
}
}
if len(dst_symtab) > 0 {
symtab["set_stats"] = dst_symtab
}
return nil
}
// func (a *SetStatsAction) TargetAction(src_symtab map[string]any, dst_symtab map[string]any, logger log.Logger) error {
// var (
// key_name string
// err error
// value_name any
// )
// level.Debug(logger).Log(
// "collid", CollectorId(src_symtab, logger),
// "script", ScriptName(src_symtab, logger),
// "msg", fmt.Sprintf("[Type: SetStatsAction] Name: %s", Name(a.Name, src_symtab, logger)))
// for _, pair := range a.setStats {
// if pair == nil {
// return fmt.Errorf("set_fact: invalid key value")
// }
// if key, ok := pair[0].(*Field); ok {
// key_name, err = key.GetValueString(src_symtab, nil, false)
// if err == nil {
// if value_name, err = ValorizeValue(src_symtab, pair[1], logger, 0); err != nil {
// return err
// }
// if value_name == nil {
// level.Debug(logger).Log(
// "collid", CollectorId(src_symtab, logger),
// "script", ScriptName(src_symtab, logger),
// "msg", fmt.Sprintf(" remove from symbols table: %s", key_name))
// delete(dst_symtab, key_name)
// } else {
// if key_name != "_" {
// level.Debug(logger).Log(
// "collid", CollectorId(src_symtab, logger),
// "script", ScriptName(src_symtab, logger),
// "msg", fmt.Sprintf(" add to symbols table: %s = '%v'", key_name, value_name))
// dst_symtab[key_name] = value_name
// } else {
// level.Debug(logger).Log(
// "collid", CollectorId(src_symtab, logger),
// "script", ScriptName(src_symtab, logger),
// "msg", " result discard (key >'_')")
// }
// }
// }
// }
// }
// return nil
// }
func (a *SetStatsAction) AddCustomTemplate(customTemplate *exporterTemplate) error {
if err := AddCustomTemplate(a, customTemplate); err != nil {
return err
}
for _, pair := range a.setStats {
// tmp_map := map[string]any{}
if pair == nil {
return fmt.Errorf("set_fact: invalid key value")
}
if key, ok := pair[0].(*Field); ok {
if key != nil {
if err := key.AddDefaultTemplate(customTemplate); err != nil {
return err
}
}
if pair[1] != nil {
if err := AddCustomTemplateElement(pair[1], customTemplate); err != nil {
return fmt.Errorf("error in set_stats value: %s", err)
}
}
}
}
return nil
}
// ***************************************************************************************