/
validate.go
207 lines (190 loc) · 5.69 KB
/
validate.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"errors"
"fmt"
"regexp"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.uber.org/multierr"
)
func (md *metadata) Validate() error {
var errs error
if err := md.validateType(); err != nil {
errs = multierr.Append(errs, err)
}
if err := md.validateStatus(); err != nil {
errs = multierr.Append(errs, err)
}
if err := md.validateResourceAttributes(); err != nil {
errs = multierr.Append(errs, err)
}
if err := md.validateMetrics(); err != nil {
errs = multierr.Append(errs, err)
}
return errs
}
// typeRegexp is used to validate the type of a component.
// A type must start with an ASCII alphabetic character and
// can only contain ASCII alphanumeric characters and '_'.
// We allow '/' for subcomponents.
// This must be kept in sync with the regex in component/config.go.
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`)
func (md *metadata) validateType() error {
if md.Type == "" {
return errors.New("missing type")
}
if md.Parent != "" {
// subcomponents are allowed to have a '/' in their type.
return nil
}
if !typeRegexp.MatchString(md.Type) {
return fmt.Errorf("invalid character(s) in type %q", md.Type)
}
return nil
}
func (md *metadata) validateStatus() error {
if md.Parent != "" && md.Status == nil {
// status is not required for subcomponents.
return nil
}
var errs error
if md.Status == nil {
return errors.New("missing status")
}
if err := md.Status.validateClass(); err != nil {
errs = multierr.Append(errs, err)
}
if md.Parent == "" {
if err := md.Status.validateStability(); err != nil {
errs = multierr.Append(errs, err)
}
}
return errs
}
func (s *Status) validateClass() error {
if s.Class == "" {
return errors.New("missing class")
}
if s.Class != "receiver" && s.Class != "processor" && s.Class != "exporter" && s.Class != "connector" && s.Class != "extension" && s.Class != "cmd" && s.Class != "pkg" {
return fmt.Errorf("invalid class: %v", s.Class)
}
return nil
}
func (s *Status) validateStability() error {
var errs error
if len(s.Stability) == 0 {
return errors.New("missing stability")
}
for stability, component := range s.Stability {
if len(component) == 0 {
errs = multierr.Append(errs, fmt.Errorf("missing component for stability: %v", stability))
}
for _, c := range component {
if c != "metrics" &&
c != "traces" &&
c != "logs" &&
c != "traces_to_traces" &&
c != "traces_to_metrics" &&
c != "traces_to_logs" &&
c != "metrics_to_traces" &&
c != "metrics_to_metrics" &&
c != "metrics_to_logs" &&
c != "logs_to_traces" &&
c != "logs_to_metrics" &&
c != "logs_to_logs" &&
c != "extension" {
errs = multierr.Append(errs, fmt.Errorf("invalid component: %v", c))
}
}
}
return errs
}
func (md *metadata) validateResourceAttributes() error {
var errs error
for name, attr := range md.ResourceAttributes {
if attr.Description == "" {
errs = multierr.Append(errs, fmt.Errorf("empty description for resource attribute: %v", name))
}
empty := ValueType{ValueType: pcommon.ValueTypeEmpty}
if attr.Type == empty {
errs = multierr.Append(errs, fmt.Errorf("empty type for resource attribute: %v", name))
}
}
return errs
}
func (md *metadata) validateMetrics() error {
var errs error
usedAttrs := map[attributeName]bool{}
for mn, m := range md.Metrics {
if m.Sum == nil && m.Gauge == nil {
errs = multierr.Append(errs, fmt.Errorf("metric %v doesn't have a metric type key, "+
"one of the following has to be specified: sum, gauge", mn))
continue
}
if m.Sum != nil && m.Gauge != nil {
errs = multierr.Append(errs, fmt.Errorf("metric %v has more than one metric type keys, "+
"only one of the following has to be specified: sum, gauge", mn))
continue
}
if err := m.validate(); err != nil {
errs = multierr.Append(errs, fmt.Errorf(`metric "%v": %w`, mn, err))
continue
}
unknownAttrs := make([]attributeName, 0, len(m.Attributes))
for _, attr := range m.Attributes {
if _, ok := md.Attributes[attr]; ok {
usedAttrs[attr] = true
} else {
unknownAttrs = append(unknownAttrs, attr)
}
}
if len(unknownAttrs) > 0 {
errs = multierr.Append(errs, fmt.Errorf(`metric "%v" refers to undefined attributes: %v`, mn, unknownAttrs))
}
}
errs = multierr.Append(errs, md.validateAttributes(usedAttrs))
return errs
}
func (m *metric) validate() error {
var errs error
if m.Description == "" {
errs = multierr.Append(errs, errors.New(`missing metric description`))
}
if m.Unit == nil {
errs = multierr.Append(errs, errors.New(`missing metric unit`))
}
if m.Sum != nil {
errs = multierr.Append(errs, m.Sum.Validate())
}
if m.Gauge != nil {
errs = multierr.Append(errs, m.Gauge.Validate())
}
return errs
}
func (mit MetricInputType) Validate() error {
if mit.InputType != "" && mit.InputType != "string" {
return fmt.Errorf("invalid `input_type` value \"%v\", must be \"\" or \"string\"", mit.InputType)
}
return nil
}
func (md *metadata) validateAttributes(usedAttrs map[attributeName]bool) error {
var errs error
unusedAttrs := make([]attributeName, 0, len(md.Attributes))
for attrName, attr := range md.Attributes {
if attr.Description == "" {
errs = multierr.Append(errs, fmt.Errorf(`missing attribute description for: %v`, attrName))
}
empty := ValueType{ValueType: pcommon.ValueTypeEmpty}
if attr.Type == empty {
errs = multierr.Append(errs, fmt.Errorf("empty type for attribute: %v", attrName))
}
if !usedAttrs[attrName] {
unusedAttrs = append(unusedAttrs, attrName)
}
}
if len(unusedAttrs) > 0 {
errs = multierr.Append(errs, fmt.Errorf("unused attributes: %v", unusedAttrs))
}
return errs
}