-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[mdatagen] mdatagen support validate metrics in in telemetry section #10942
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
Changes from 2 commits
12d1744
35cef75
0195401
2d13c6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @Frapschen, is there already a unit test that covers this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please see the changed file |
||
md.validateAttributes(usedAttrs)) | ||
return errs | ||
} | ||
|
||
|
@@ -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 && m.Histogram == 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, histogram", mn)) | ||
continue | ||
} | ||
if (m.Sum != nil && m.Gauge != nil) || (m.Sum != nil && m.Histogram != nil) || (m.Gauge != nil && m.Histogram != 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, histogram", 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 | ||
} |
Uh oh!
There was an error while loading. Please reload this page.