From 5fc39ba6334f396c3b5497bf6d29db12823cb50c Mon Sep 17 00:00:00 2001 From: Murphy Chen Date: Fri, 13 Sep 2024 05:03:25 +0800 Subject: [PATCH] [mdatagen] mdatagen support validate metrics in in telemetry section (#10942) #### Description I have a use case, I define an attribute and try to use it in `telemetry.metric` however I encounter an error: `unused attributes: [xxx]` So, I push this PR to enhance the `validateMetrics()` func to support validate metrics in `telemetry.metric` #### Link to tracking issue fix Fixes https://github.com/open-telemetry/opentelemetry-collector/issues/10925 --------- Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com> --- .chloggen/fix-metadatagen-validate.yaml | 25 ++++++++ cmd/mdatagen/testdata/unused_attribute.yaml | 17 +++++- cmd/mdatagen/validate.go | 63 ++++++++++++--------- cmd/mdatagen/validate_test.go | 4 +- 4 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 .chloggen/fix-metadatagen-validate.yaml diff --git a/.chloggen/fix-metadatagen-validate.yaml b/.chloggen/fix-metadatagen-validate.yaml new file mode 100644 index 00000000000..37d5122793f --- /dev/null +++ b/.chloggen/fix-metadatagen-validate.yaml @@ -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: [] diff --git a/cmd/mdatagen/testdata/unused_attribute.yaml b/cmd/mdatagen/testdata/unused_attribute.yaml index d71586a7714..a3348712f8b 100644 --- a/cmd/mdatagen/testdata/unused_attribute.yaml +++ b/cmd/mdatagen/testdata/unused_attribute.yaml @@ -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 @@ -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] \ No newline at end of file diff --git a/cmd/mdatagen/validate.go b/cmd/mdatagen/validate.go index 63108f29470..4685b1e2b1c 100644 --- a/cmd/mdatagen/validate.go +++ b/cmd/mdatagen/validate.go @@ -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), + 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 +} diff --git a/cmd/mdatagen/validate_test.go b/cmd/mdatagen/validate_test.go index 497b726bca8..fcc62428648 100644 --- a/cmd/mdatagen/validate_test.go +++ b/cmd/mdatagen/validate_test.go @@ -69,12 +69,12 @@ func TestValidate(t *testing.T) { { name: "testdata/no_metric_type.yaml", wantErr: "metric system.cpu.time doesn't have a metric type key, " + - "one of the following has to be specified: sum, gauge", + "one of the following has to be specified: sum, gauge, histogram", }, { name: "testdata/two_metric_types.yaml", wantErr: "metric system.cpu.time has more than one metric type keys, " + - "only one of the following has to be specified: sum, gauge", + "only one of the following has to be specified: sum, gauge, histogram", }, { name: "testdata/invalid_input_type.yaml",