-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug_action.go
192 lines (158 loc) · 4.84 KB
/
debug_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
package main
import (
//"bytes"
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)
// ***************************************************************************************
// ***************************************************************************************
// debug action
// ***************************************************************************************
// ***************************************************************************************
// ****************************
type DebugActionConfig struct {
MsgVal string `yaml:"msg"`
msg *Field
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for DebugActionConfig.
func (dc *DebugActionConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain DebugActionConfig
var err error
if err := unmarshal((*plain)(dc)); err != nil {
return err
}
// Check required fields
dc.msg, err = NewField(dc.MsgVal, nil)
if err != nil {
return fmt.Errorf("invalid template for debug message %q: %s", dc.MsgVal, err)
}
return checkOverflow(dc.XXX, "debug action")
}
// ****************************
type DebugAction struct {
Name *Field `yaml:"name,omitempty"`
With []any `yaml:"with,omitempty"`
When []*exporterTemplate `yaml:"when,omitempty"`
LoopVar string `yaml:"loop_var,omitempty"`
Vars map[string]any `yaml:"vars,omitempty"`
Until []*exporterTemplate `yaml:"until,omitempty"`
Debug *DebugActionConfig `yaml:"debug"`
vars [][]any
}
func (a *DebugAction) Type() int {
return debug_action
}
func (a *DebugAction) 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 *DebugAction) GetNameField() *Field {
return a.Name
}
func (a *DebugAction) SetNameField(name *Field) {
a.Name = name
}
func (a *DebugAction) GetWidh() []any {
return a.With
}
func (a *DebugAction) SetWidth(with []any) {
a.With = with
}
func (a *DebugAction) GetWhen() []*exporterTemplate {
return a.When
}
func (a *DebugAction) SetWhen(when []*exporterTemplate) {
a.When = when
}
func (a *DebugAction) GetLoopVar() string {
return a.LoopVar
}
func (a *DebugAction) SetLoopVar(loopvar string) {
a.LoopVar = loopvar
}
func (a *DebugAction) GetVars() [][]any {
return a.vars
}
func (a *DebugAction) SetVars(vars [][]any) {
a.vars = vars
}
func (a *DebugAction) GetUntil() []*exporterTemplate {
return a.Until
}
func (a *DebugAction) SetUntil(until []*exporterTemplate) {
a.Until = until
}
// func (a *DebugAction) GetBaseAction() *BaseAction {
// return nil
// }
func (a *DebugAction) 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 *DebugAction) PlayAction(script *YAMLScript, symtab map[string]any, logger log.Logger) error {
return PlayBaseAction(script, symtab, logger, a, a.CustomAction)
}
// only for MetricsAction
func (a *DebugAction) GetMetrics() []*GetMetricsRes {
return nil
}
// only for MetricAction
func (a *DebugAction) GetMetric() *MetricConfig {
return nil
}
func (a *DebugAction) SetMetricFamily(*MetricFamily) {
}
// only for PlayAction
func (a *DebugAction) SetPlayAction(scripts map[string]*YAMLScript) error {
return nil
}
// specific behavior for the DebugAction
func (a *DebugAction) CustomAction(script *YAMLScript, symtab map[string]any, logger log.Logger) error {
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", "[Type: DebugAction]")
str, err := a.Debug.msg.GetValueString(symtab, nil, false)
if err != nil {
str = a.Debug.MsgVal
level.Warn(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", fmt.Sprintf("invalid template for debug message '%s': %v", str, err))
}
level.Debug(logger).Log(
"collid", CollectorId(symtab, logger),
"script", ScriptName(symtab, logger),
"name", a.GetName(symtab, logger),
"msg", fmt.Sprintf(" message: %s", str))
return nil
}
func (a *DebugAction) AddCustomTemplate(customTemplate *exporterTemplate) error {
if err := AddCustomTemplate(a, customTemplate); err != nil {
return err
}
if a.Debug.msg != nil {
if err := a.Debug.msg.AddDefaultTemplate(customTemplate); err != nil {
return err
}
}
return nil
}
// ***************************************************************************************