Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mdatagen] mdatagen support validate metrics in in telemetry section #10942

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/fix-metadatagen-validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: mdatagen `validateMetrics()` support validate metrics in `telemetry.metric`

# One or more tracking issues or pull requests related to the change
issues: [10925]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
17 changes: 15 additions & 2 deletions cmd/mdatagen/testdata/unused_attribute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ status:
stable: [metrics]

attributes:
used_attr:
used_attr_in_metrics_section:
description: Used attribute.
type: string
used_attr_in_telemetry_section:
description: Used attribute.
type: string

Expand All @@ -26,4 +29,14 @@ metrics:
unit: "1"
gauge:
value_type: double
attributes: [used_attr]
attributes: [used_attr_in_metrics_section]

telemetry:
metrics:
metric:
enabled: true
description: Metric.
unit: "1"
gauge:
value_type: double
attributes: [used_attr_in_telemetry_section]
63 changes: 35 additions & 28 deletions cmd/mdatagen/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,9 @@ func (md *metadata) validateResourceAttributes() error {
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 = errors.Join(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 = errors.Join(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 = errors.Join(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 = errors.Join(errs, fmt.Errorf(`metric "%v" refers to undefined attributes: %v`, mn, unknownAttrs))
}
}
errs = errors.Join(errs, md.validateAttributes(usedAttrs))
errs = errors.Join(errs, validateMetrics(md.Metrics, md.Attributes, usedAttrs),
validateMetrics(md.Telemetry.Metrics, md.Attributes, usedAttrs),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Frapschen, is there already a unit test that covers this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please see the changed file cmd/mdatagen/testdata/unused_attribute.yaml, it will make sure the attributes that are used in telemetry.metric will not encounter unused attributes: [xxx] error.

md.validateAttributes(usedAttrs))
return errs
}

Expand Down Expand Up @@ -204,3 +179,35 @@ func (md *metadata) validateAttributes(usedAttrs map[attributeName]bool) error {
}
return errs
}

func validateMetrics(metrics map[metricName]metric, attributes map[attributeName]attribute, usedAttrs map[attributeName]bool) error {
var errs error
for mn, m := range metrics {
if m.Sum == nil && m.Gauge == nil {
errs = errors.Join(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 = errors.Join(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 = errors.Join(errs, fmt.Errorf(`metric "%v": %w`, mn, err))
continue
}
unknownAttrs := make([]attributeName, 0, len(m.Attributes))
for _, attr := range m.Attributes {
if _, ok := attributes[attr]; ok {
usedAttrs[attr] = true
} else {
unknownAttrs = append(unknownAttrs, attr)
}
}
if len(unknownAttrs) > 0 {
errs = errors.Join(errs, fmt.Errorf(`metric "%v" refers to undefined attributes: %v`, mn, unknownAttrs))
}
}
return errs
}
Loading