-
Notifications
You must be signed in to change notification settings - Fork 187
/
commonconfigutil.go
executable file
·201 lines (178 loc) · 7.27 KB
/
commonconfigutil.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package util
import (
"fmt"
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/agent"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/util"
)
const (
Measurement_Key = "measurement"
Collect_Interval_Key = "metrics_collection_interval"
Collect_Interval_Mapped_Key = "interval"
Aggregation_Interval_Key = "metrics_aggregation_interval"
Append_Dimensions_Key = "append_dimensions"
Append_Dimensions_Mapped_Key = "tags"
Windows_Object_Name_Key = "ObjectName"
Windows_Measurement_Key = "Measurement"
Windows_WarnOnMissing_Key = "WarnOnMissing"
Windows_Disable_Replacer_Key = "DisableReplacer"
)
// ProcessLinuxCommonConfig is used by both Linux and Darwin.
func ProcessLinuxCommonConfig(input interface{}, pluginName string, path string, result map[string]interface{}) bool {
isHighRsolution := IsHighResolution(agent.Global_Config.Interval)
inputMap := input.(map[string]interface{})
// Generate whitelisted metric list, process only if Measurement_Key exist
if translator.IsValid(inputMap, Measurement_Key, path) {
// NOTE: the logic here is a bit tricky, even windows uses linux config for metric like procstat.
os := config.OS_TYPE_LINUX
if translator.GetTargetPlatform() == config.OS_TYPE_DARWIN {
os = config.OS_TYPE_DARWIN
}
returnKey, returnVal := ApplyMeasurementRule(inputMap[Measurement_Key], pluginName, os, path)
if returnKey != "" {
result[returnKey] = returnVal
} else {
// No valid metric get generated, stop processing
return false
}
} else {
return false
}
// Set input plugin specific interval
isHighRsolution = setTimeInterval(inputMap, result, isHighRsolution, pluginName)
//Set append_dimensions as tags
if val, ok := inputMap[Append_Dimensions_Key]; ok {
result[Append_Dimensions_Mapped_Key] = val
util.Cleanup(val)
}
// Add HighResolution tags
if isHighRsolution {
if result[Append_Dimensions_Mapped_Key] != nil {
util.AddHighResolutionTag(result[Append_Dimensions_Mapped_Key])
} else {
result[Append_Dimensions_Mapped_Key] = map[string]interface{}{util.High_Resolution_Tag_Key: "true"}
}
}
return true
}
// Windows common config returnVal would be three parts:
// 1. interval: Collect_Interval_Mapped_Key
// 2. tags: Append_Dimensions_Mapped_Key
// 3. object config
func ProcessWindowsCommonConfig(input interface{}, pluginName string, path string) (returnVal map[string]interface{}) {
inputMap := input.(map[string]interface{})
objectConfig := map[string]interface{}{}
isHighRsolution := IsHighResolution(agent.Global_Config.Interval)
returnVal = map[string]interface{}{}
returnVal[Windows_Disable_Replacer_Key] = true
// 1. Set input plugin specific interval
isHighRsolution = setTimeInterval(inputMap, returnVal, isHighRsolution, pluginName)
// 2. Set append_dimensions as tags
if val, ok := inputMap[Append_Dimensions_Key]; ok {
returnVal[Append_Dimensions_Mapped_Key] = val
}
// 3. object config
// Generate whitelisted metric list, process only if Measurement_Key exist
if translator.IsValid(inputMap, Measurement_Key, path) {
returnKey, returnVal := ApplyMeasurementRule(inputMap[Measurement_Key], pluginName, config.OS_TYPE_WINDOWS, path)
if returnKey != "" && len(returnVal) > 0 {
objectConfig[returnKey] = returnVal
}
}
// Add common field ObkectName
objectConfig[Windows_Object_Name_Key] = pluginName
// Output the message about the missing perf counter metrics
objectConfig[Windows_WarnOnMissing_Key] = true
//Measurement behaves like prefix in cloudwatch
objectConfig[Windows_Measurement_Key] = pluginName
//instances field is required in windows perf counter config
if InstanceDisabled(pluginName) {
objectConfig[Mapped_Instance_Key_Windows] = []string{Disabled_Instance_Val_Windows}
} else if val, ok := inputMap[Resource_Key]; ok {
if ContainAsterisk(input, Resource_Key) {
objectConfig[Mapped_Instance_Key_Windows] = []string{Asterisk_Key}
} else {
objectConfig[Mapped_Instance_Key_Windows] = val
}
} else {
// if no instance field specified, we assume the Objects have no instances to collect, use "------" to comply with win_perf_counters.go
objectConfig[Mapped_Instance_Key_Windows] = []string{Disabled_Instance_Val_Windows}
}
// Add HighResolution tags
if isHighRsolution {
if returnVal[Append_Dimensions_Mapped_Key] != nil {
util.AddHighResolutionTag(returnVal[Append_Dimensions_Mapped_Key])
} else {
returnVal[Append_Dimensions_Mapped_Key] = map[string]interface{}{util.High_Resolution_Tag_Key: "true"}
}
}
returnVal["object"] = []interface{}{objectConfig}
return
}
func setTimeInterval(inputMap map[string]interface{}, returnVal map[string]interface{}, isHighRsolution bool, pluginName string) bool {
if val, ok := inputMap[Collect_Interval_Key]; ok {
if floatVal, ok := val.(float64); ok {
val = fmt.Sprintf("%ds", int(floatVal))
returnVal[Collect_Interval_Mapped_Key] = val
//Check if this metric is high resolution
isHighRsolution = IsHighResolution(val.(string))
} else {
translator.AddErrorMessages(
fmt.Sprintf("metrics plugin %s", pluginName),
fmt.Sprintf("metrics_collection_interval value (%v) in json is not valid for time interval.", val))
}
}
return isHighRsolution
}
func ProcessMetricsCollectionInterval(input interface{}, defaultValue, pluginName string) (returnKey string, returnVal interface{}) {
if inputMap, ok := input.(map[string]interface{}); ok {
if val, ok := inputMap[Collect_Interval_Key]; ok {
if floatVal, ok := val.(float64); ok {
val = fmt.Sprintf("%ds", int(floatVal))
return Collect_Interval_Mapped_Key, val
} else {
translator.AddErrorMessages(
fmt.Sprintf("metrics plugin %s", pluginName),
fmt.Sprintf("metrics_collection_interval value (%v) in json is not valid for time interval.", val))
}
}
if defaultValue != "" {
return Collect_Interval_Mapped_Key, defaultValue
}
}
return
}
func ProcessMetricsAggregationInterval(input interface{}, defaultValue, pluginName string) (returnKey string, returnVal interface{}) {
if inputMap, ok := input.(map[string]interface{}); ok {
if val, ok := inputMap[Aggregation_Interval_Key]; ok {
if floatVal, ok := val.(float64); ok {
val = fmt.Sprintf("%ds", int(floatVal))
if valStr, ok := val.(string); ok && valStr == "0s" {
// customer specifically disabled the metrics aggregation interval by putting "0"
return Append_Dimensions_Mapped_Key, map[string]interface{}{util.High_Resolution_Tag_Key: "true"}
}
return Append_Dimensions_Mapped_Key, map[string]interface{}{util.Aggregation_Interval_Tag_Key: val}
} else {
translator.AddErrorMessages(
fmt.Sprintf("metrics plugin %s", pluginName),
fmt.Sprintf("metrics_aggregation_interval value (%v) in json is not valid for time interval.", val))
}
}
if defaultValue != "" {
return Append_Dimensions_Mapped_Key, map[string]interface{}{util.Aggregation_Interval_Tag_Key: defaultValue}
}
}
return
}
//check if desiredVal exist in inputs list
func ListContains(inputs []string, desiredVal string) bool {
for _, val := range inputs {
if val == desiredVal {
return true
}
}
return false
}