This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
176 lines (165 loc) · 4.39 KB
/
main.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 main
import (
"strings"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-go/types"
)
// Config represents the mutator plugin config.
type Config struct {
sensu.PluginConfig
EntityName bool
EntityNamespace bool
EntityLabels bool
CheckName bool
CheckLabels bool
EventLabels bool
Filter string
Tags string
Required string
}
var (
config = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-metric-tag-mutator",
Short: "Enrich Sensu Metrics with Event, Entity, and Check labels as metric tags.",
Keyspace: "sensu.io/plugins/sensu-metric-tag-mutator/config",
},
}
options = []*sensu.PluginConfigOption{
&sensu.PluginConfigOption{
Path: "entity",
Env: "",
Argument: "entity",
Shorthand: "e",
Default: false,
Usage: "Add an \"entity\" tag containing the entity name to every metric.",
Value: &config.EntityName,
},
&sensu.PluginConfigOption{
Path: "namespace",
Env: "",
Argument: "namespace",
Shorthand: "n",
Default: false,
Usage: "Add a \"namespace\" tag containing the entity namespace to every metric.",
Value: &config.EntityNamespace,
},
&sensu.PluginConfigOption{
Path: "entity-labels",
Env: "",
Argument: "entity-labels",
Shorthand: "",
Default: false,
Usage: "Add a tag for every entity label to every metric.",
Value: &config.EntityLabels,
},
&sensu.PluginConfigOption{
Path: "check",
Env: "",
Argument: "check",
Shorthand: "c",
Default: false,
Usage: "Add a \"check\" tag containing the check name to every metric.",
Value: &config.CheckName,
},
&sensu.PluginConfigOption{
Path: "check-labels",
Env: "",
Argument: "check-labels",
Shorthand: "",
Default: false,
Usage: "Add a tag for every check label to every metric.",
Value: &config.CheckLabels,
},
&sensu.PluginConfigOption{
Path: "event-labels",
Env: "",
Argument: "event-labels",
Shorthand: "",
Default: false,
Usage: "Add a tag for every event label to every metric.",
Value: &config.EventLabels,
},
&sensu.PluginConfigOption{
Path: "require",
Env: "",
Argument: "require",
Shorthand: "r",
Default: "",
Usage: "Comma separated list of tags to include on all metrics, even if no matching labels are found",
Value: &config.Required,
},
&sensu.PluginConfigOption{
Path: "filter",
Env: "",
Argument: "filter",
Shorthand: "f",
Default: "sensu.io/managed_by",
Usage: "Comma separated list of tags to exclude.",
Value: &config.Filter,
},
}
)
func main() {
mutator := sensu.NewGoMutator(&config.PluginConfig, options, checkArgs, executeMutator)
mutator.Execute()
}
func indexOf(items []string, item string) int {
for k, v := range items {
if v == item {
return k
}
}
return -1
}
func checkArgs(_ *types.Event) error {
// if len(mutatorConfig.Example) == 0 {
// return fmt.Errorf("--example or MUTATOR_EXAMPLE environment variable is required")
// }
return nil
}
func addTag(point *types.MetricPoint, key string, value string) {
if len(config.Filter) == 0 || indexOf(strings.Split(config.Filter, ","), key) < 0 {
point.Tags = append(
point.Tags,
&types.MetricTag{
Name: key,
Value: value,
},
)
}
}
func extractTags(event *types.Event) {
if event.Metrics != nil {
for _, point := range event.Metrics.Points {
if config.EntityNamespace {
addTag(point, "namespace", event.Namespace)
}
if config.EntityName && event.Entity != nil {
addTag(point, "entity", event.Entity.Name)
}
if config.CheckName && event.Check != nil {
addTag(point, "check", event.Check.Name)
}
if config.EventLabels && event.Labels != nil {
for name, value := range event.Labels {
addTag(point, name, value)
}
}
if config.EntityLabels && event.Entity != nil && event.Entity.Labels != nil {
for name, value := range event.Entity.Labels {
addTag(point, name, value)
}
}
if config.CheckLabels && event.Check != nil && event.Check.Labels != nil {
for name, value := range event.Check.Labels {
addTag(point, name, value)
}
}
}
}
}
func executeMutator(event *types.Event) (*types.Event, error) {
extractTags(event)
return event, nil
}