Skip to content

Commit

Permalink
[mdatagen] mdatagen support validate metrics in in telemetry section (#…
Browse files Browse the repository at this point in the history
…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 #10925

---------

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
Frapschen and codeboten authored Sep 12, 2024
1 parent 3bcb341 commit 5fc39ba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 32 deletions.
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),
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 && 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
}
4 changes: 2 additions & 2 deletions cmd/mdatagen/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5fc39ba

Please sign in to comment.