Skip to content

Commit ceeb462

Browse files
authored
[processor/attributes] validate metrics configuration parameters before processing (#37435)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Add validation for metrics-only configuration parameters <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #36077 --------- Signed-off-by: odubajDT <[email protected]>
1 parent 9579cef commit ceeb462

File tree

9 files changed

+413
-15
lines changed

9 files changed

+413
-15
lines changed

Diff for: .chloggen/fix-metric-validation.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: attributesprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Validate metrics configuration parameters before processing"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36077]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

Diff for: internal/filter/filterconfig/config.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ type MatchProperties struct {
126126
}
127127

128128
var (
129-
ErrMissingRequiredField = errors.New(`at least one of "attributes", "libraries", or "resources" field must be specified`)
130-
ErrInvalidLogField = errors.New("services, span_names, and span_kinds are not valid for log records")
131-
ErrMissingRequiredLogField = errors.New(`at least one of "attributes", "libraries", "span_kinds", "resources", "log_bodies", "log_severity_texts" or "log_severity_number" field must be specified`)
129+
ErrMissingRequiredSpanField = errors.New(`at least one of "attributes", "libraries", or "resources" field must be specified`)
130+
ErrInvalidLogField = errors.New("services, span_names, span_kinds and metric_names are not valid for log records")
131+
ErrMissingRequiredLogField = errors.New(`at least one of "attributes", "libraries", "span_kinds", "resources", "log_bodies", "log_severity_texts" or "log_severity_number" field must be specified`)
132+
ErrMissingRequiredMetricField = errors.New(`at least one of "metric_names" or "resources" field must be specified`)
133+
ErrInvalidMetricField = errors.New(`"span_names", "span_kinds", "log_bodies", "log_severity_texts", "log_severity_number", "services", "attributes" and "libraries" are not valid for metrics`)
132134

133135
spanKinds = map[string]bool{
134136
traceutil.SpanKindStr(ptrace.SpanKindInternal): true,
@@ -153,9 +155,13 @@ func (mp *MatchProperties) ValidateForSpans() error {
153155
return errors.New("log_severity_number should not be specified for trace spans")
154156
}
155157

158+
if len(mp.MetricNames) > 0 {
159+
return errors.New("metric_names should not be specified for trace spans")
160+
}
161+
156162
if len(mp.Services) == 0 && len(mp.SpanNames) == 0 && len(mp.Attributes) == 0 &&
157163
len(mp.Libraries) == 0 && len(mp.Resources) == 0 && len(mp.SpanKinds) == 0 {
158-
return ErrMissingRequiredField
164+
return ErrMissingRequiredSpanField
159165
}
160166

161167
if len(mp.SpanKinds) > 0 && mp.MatchType == "strict" {
@@ -176,7 +182,7 @@ func (mp *MatchProperties) ValidateForSpans() error {
176182

177183
// ValidateForLogs validates properties for logs.
178184
func (mp *MatchProperties) ValidateForLogs() error {
179-
if len(mp.SpanNames) > 0 || len(mp.Services) > 0 || len(mp.SpanKinds) > 0 {
185+
if len(mp.SpanNames) > 0 || len(mp.Services) > 0 || len(mp.SpanKinds) > 0 || len(mp.MetricNames) > 0 {
180186
return ErrInvalidLogField
181187
}
182188

@@ -190,6 +196,26 @@ func (mp *MatchProperties) ValidateForLogs() error {
190196
return nil
191197
}
192198

199+
// ValidateForMetrics validates properties for metrics.
200+
func (mp *MatchProperties) ValidateForMetrics() error {
201+
if len(mp.LogBodies) > 0 ||
202+
len(mp.LogSeverityTexts) > 0 ||
203+
len(mp.SpanNames) > 0 ||
204+
len(mp.Services) > 0 ||
205+
len(mp.SpanKinds) > 0 ||
206+
len(mp.Attributes) > 0 ||
207+
len(mp.Libraries) > 0 ||
208+
mp.LogSeverityNumber != nil {
209+
return ErrInvalidMetricField
210+
}
211+
212+
if len(mp.MetricNames) == 0 && len(mp.Resources) == 0 {
213+
return ErrMissingRequiredMetricField
214+
}
215+
216+
return nil
217+
}
218+
193219
// Attribute specifies the attribute key and optional value to match against.
194220
type Attribute struct {
195221
// Key specifies the attribute key.
@@ -261,15 +287,19 @@ type MetricMatchProperties struct {
261287
ResourceAttributes []Attribute `mapstructure:"resource_attributes"`
262288
}
263289

264-
func CreateMetricMatchPropertiesFromDefault(properties *MatchProperties) *MetricMatchProperties {
290+
func CreateMetricMatchPropertiesFromDefault(properties *MatchProperties) (*MetricMatchProperties, error) {
265291
if properties == nil {
266-
return nil
292+
return nil, nil
293+
}
294+
295+
if err := properties.ValidateForMetrics(); err != nil {
296+
return nil, err
267297
}
268298

269299
return &MetricMatchProperties{
270300
MatchType: MetricMatchType(properties.Config.MatchType),
271301
RegexpConfig: properties.Config.RegexpConfig,
272302
MetricNames: properties.MetricNames,
273303
ResourceAttributes: properties.Resources,
274-
}
304+
}, nil
275305
}

0 commit comments

Comments
 (0)