Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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: []
2 changes: 1 addition & 1 deletion cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type metric struct {
Sum *sum `mapstructure:"sum,omitempty"`
// Gauge stores metadata for gauge metric type
Gauge *gauge `mapstructure:"gauge,omitempty"`
// Gauge stores metadata for gauge metric type
// Histogram stores metadata for histogram metric type
Histogram *histogram `mapstructure:"histogram,omitempty"`

// Attributes is the list of attributes that the metric emits.
Expand Down
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 && 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
Loading