-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathvalidation_config.go
More file actions
192 lines (158 loc) · 6.28 KB
/
Copy pathvalidation_config.go
File metadata and controls
192 lines (158 loc) · 6.28 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package models // import "github.com/aws/amazon-cloudwatch-agent-test/validator/models"
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/google/uuid"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
)
var supportedReceivers = []string{"logs", "statsd", "collectd", "system", "emf", "xray", "app_signals", "prometheus", "traces"}
var retryCount = 0
type ValidateConfig interface {
GetPluginsConfig() []string
GetValidateType() string
GetTestCase() string
GetDataType() string
GetNumberMonitoredLogs() int
GetDataRate() int
GetCloudWatchAgentConfigPath() string
GetScrapeInterval() int
GetAgentCollectionPeriod() time.Duration
GetMetricNamespace() string
GetMetricValidation() []MetricValidation
GetLogValidation() []LogValidation
GetCommitInformation() (string, int64)
GetUniqueID() string
GetOSFamily() string
}
type validatorConfig struct {
Receivers []string `yaml:"receivers"` // Receivers that agent needs to tests
TestCase string `yaml:"test_case"` // Test case name
// Validate type for the test https://github.com/aws/amazon-cloudwatch-agent-test/blob/39a9e16c70f07a17c43c0630647158cd496bd168/validator/validators/validator.go#L15-L24
ValidateType string `yaml:"validate_type"`
DataType string `yaml:"data_type"` // Only supports metrics/logs/traces
NumberMonitoredLogs int `yaml:"number_monitored_logs"` // Number of logs to be monitored
ValuesPerMinute string `yaml:"values_per_minute"` // Number of metrics to be sent or number of log lines to write
ScrapeInterval string `yaml:"scrape_interval"` // Prometheus Scraping interval
AgentCollectionPeriod int `yaml:"agent_collection_period"` // Number of seconds the agent should run and collect the metrics
OSFamily string `yaml:"os_family"` // OS Family for the validator test
ConfigPath string `yaml:"cloudwatch_agent_config"`
MetricNamespace string `yaml:"metric_namespace"`
MetricValidation []MetricValidation `yaml:"metric_validation"`
LogValidation []LogValidation `yaml:"log_validation"`
CommitHash string `yaml:"commit_hash"`
CommitDate string `yaml:"commit_date"`
retryCount int
}
type MetricValidation struct {
MetricName string `yaml:"metric_name"`
MetricDimension []MetricDimension `yaml:"metric_dimension"`
MetricValue float64 `yaml:"metric_value"`
MetricSampleCount int `yaml:"metric_sample_count"`
}
type LogValidation struct {
LogValue string `yaml:"log_value"`
LogLines int `yaml:"log_lines"`
LogChannel string `yaml:"log_channel"`
LogStream string `yaml:"log_stream"`
LogLevel string `yaml:"log_level"`
LogEventID string `yaml:"log_event_id"`
LogSource string `yaml:"log_source"`
}
type MetricDimension struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
var _ ValidateConfig = (*validatorConfig)(nil)
func NewValidateConfig(configPath string) (*validatorConfig, error) {
configPathBytes, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("%v with file %s", err, configPath)
}
vConfig := validatorConfig{}
err = yaml.Unmarshal(configPathBytes, &vConfig)
if err != nil {
return nil, err
}
log.Printf("Parameters validation for %v", vConfig)
if err := ValidateValidatorConfig(vConfig); err != nil {
return nil, err
}
return &vConfig, nil
}
func ValidateValidatorConfig(vConfig validatorConfig) error {
for _, receiver := range vConfig.Receivers {
if !slices.Contains(supportedReceivers, receiver) {
return fmt.Errorf("only support %v, the validator does not support %s", supportedReceivers, receiver)
}
}
return nil
}
// GetTestCase return the test case name
func (v *validatorConfig) GetTestCase() string {
return v.TestCase
}
// GetTestCase return the validation type (e.g stress https://github.com/aws/amazon-cloudwatch-agent-test/pull/109/files#diff-36fa5ec31f40a4d9a878623ba1993272853ab2125e64152317da2a66cc7365d6R17-R18)
func (v *validatorConfig) GetValidateType() string {
return v.ValidateType
}
// GetPluginsConfig returns the agent plugin being used or need to validate (e.g statsd, collectd, cpu)
func (v *validatorConfig) GetPluginsConfig() []string {
return v.Receivers
}
// GetPluginsConfig returns the type needs to validate or send. Only supports metrics, traces, logs
func (v *validatorConfig) GetDataType() string {
return v.DataType
}
// GetDataRate returns number of metrics to be sent or number of log lines to write
func (v *validatorConfig) GetDataRate() int {
if dataRate, err := strconv.Atoi(v.ValuesPerMinute); err == nil {
return dataRate
}
return 0
}
func (v *validatorConfig) GetScrapeInterval() int {
if scrapeInterval, err := strconv.Atoi(v.ScrapeInterval); err == nil {
return scrapeInterval
}
return 0
}
// GetNumberMonitoredLogs returns number of log to be monitored by cloudwatchagent so the validator configuration will setup the agent config dynamically
func (v *validatorConfig) GetNumberMonitoredLogs() int {
return v.NumberMonitoredLogs
}
// GetNumberMonitoredLogs returns the cloudwatch agent path configuration
func (v *validatorConfig) GetCloudWatchAgentConfigPath() string {
return v.ConfigPath
}
// GetNumberMonitoredLogs returns the number of seconds the agent should run and collect the metrics
func (v *validatorConfig) GetAgentCollectionPeriod() time.Duration {
return time.Duration(v.AgentCollectionPeriod) * time.Second
}
// GetNumberMonitoredLogs returns the namespace that metrics need to be validated
func (v *validatorConfig) GetMetricNamespace() string {
return v.MetricNamespace
}
// GetMetricValidation returns the metrics need for validation
func (v *validatorConfig) GetMetricValidation() []MetricValidation {
return v.MetricValidation
}
// GetLogValidation returns the logs need for validation
func (v *validatorConfig) GetLogValidation() []LogValidation {
return v.LogValidation
}
func (v *validatorConfig) GetCommitInformation() (string, int64) {
commitDate, _ := strconv.ParseInt(v.CommitDate, 10, 64)
return v.CommitHash, commitDate
}
func (v *validatorConfig) GetUniqueID() string {
return uuid.NewString()
}
func (v *validatorConfig) GetOSFamily() string {
return v.OSFamily
}