Skip to content

Commit fbd5d3f

Browse files
authored
[mdatagen] Enable configurable attributes for metrics (#14281)
Reopens [13900](#13900) #### Description Modified `cmd/mdatagen` to allow for two new fields in the configuration yaml of metrics. These new configuration options will allow for a user to enable or disable attributes (i.e. reduce dimensionality of metrics being generated) from their collector configuration. The modified `MetricsBuilder` generated by `mdatagen` does not record disabled attributes and automatically re-aggregates metrics based on the resulting enabled set of attributes. There are four different aggregation strategies supported (`sum, average, min, and max`) which can be specified as a setting in `config.yaml`. The changes to the config.yaml are: ```yaml receiver: someMetricReceiver: ... metrics: ... attributes: [<list of attributes to include for metric at runtime>] # new aggregation_strategy: <sum|avg|min|max> # new ``` Attribute `requirement_level` takes president over this re-aggregation and any metric attribute with a `requirement_level` of `required` cannot be disabled in this way. For more information on `requirement_level` please see [13913](#13913) The set of attributes provided by the user _must_ be contained in the set of attributes defined in the metadata.yaml, including an attribute which is not defined in the metrics `metadata` file will fail. Also, attempting to omit an attribute defined as `requirement_level: required` will cause the configuration to fail and the collector not to start. Omitting the field entirely will default to the defined set of metric attributes in the `metadata` file with a requirement level of `recommended` or above. Due to its large scope and foundational nature, a featuregate has been added for this change. This gate is represented by a new field in the `metadata.yaml`, `reaggregation_enabled: <bool>`. Setting this to `true` will enable the new codegen with spatial reaggregation features. <!-- Issue number if applicable --> #### Link to tracking issue Fixes [10726](#10726) <!--Describe what testing was performed and which tests were added.--> #### Testing Generated tests have been updated to test new aggregation behavior. <!--Describe the documentation added.--> #### Documentation ran `make gogenerate` ran `make generate` <!--Please delete paragraphs that you did not use before submitting.-->
1 parent 250a1ca commit fbd5d3f

34 files changed

+3414
-543
lines changed

.chloggen/10726-reag.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/otlp)
7+
component: 'cmd/mdatagen'
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add the ability to disable attributes at the metric level and re-aggregate data points based off of these new dimensions
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [10726]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api, user]

.github/workflows/utils/cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@
402402
"protos",
403403
"ptraceotlp",
404404
"queuebatch",
405+
"reaggregate",
405406
"receiverhelper",
406407
"receiverprofiles",
407408
"receivertest",

cmd/mdatagen/internal/command.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ func templatize(tmplFile string, md Metadata) *template.Template {
231231
"attributeInfo": func(an AttributeName) Attribute {
232232
return md.Attributes[an]
233233
},
234+
"defaultAttributes": func(ans []AttributeName) []string {
235+
var atts []string
236+
for _, an := range ans {
237+
if md.Attributes[an].IsNotOptIn() {
238+
atts = append(atts, string(md.Attributes[an].Name()))
239+
}
240+
}
241+
return atts
242+
},
243+
"requiredAttributes": func(ans []AttributeName) []string {
244+
var atts []string
245+
for _, an := range ans {
246+
if md.Attributes[an].IsRequired() {
247+
atts = append(atts, string(md.Attributes[an].Name()))
248+
}
249+
}
250+
return atts
251+
},
234252
"getEventConditionalAttributes": func(attrs map[AttributeName]Attribute) []AttributeName {
235253
seen := make(map[AttributeName]bool)
236254
used := make([]AttributeName, 0)

cmd/mdatagen/internal/loader_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestLoadMetadata(t *testing.T) {
5151
Description: "This receiver is used for testing purposes to check the output of mdatagen.",
5252
SemConvVersion: "1.38.0",
5353
PackageName: "go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver",
54+
ReaggregationEnabled: true,
5455
Status: &Status{
5556
DisableCodeCov: true,
5657
Class: "receiver",
@@ -263,6 +264,18 @@ func TestLoadMetadata(t *testing.T) {
263264
Mono: Mono{Monotonic: true},
264265
},
265266
},
267+
"reaggregate.metric": {
268+
Signal: Signal{
269+
Enabled: true,
270+
Description: "Metric for testing spacial reaggregation",
271+
Stability: Stability{Level: component.StabilityLevelBeta},
272+
Attributes: []AttributeName{"string_attr", "boolean_attr"},
273+
},
274+
Unit: strPtr("1"),
275+
Gauge: &Gauge{
276+
MetricValueType: MetricValueType{pmetric.NumberDataPointValueTypeDouble},
277+
},
278+
},
266279
"system.cpu.time": {
267280
Signal: Signal{
268281
Enabled: true,

cmd/mdatagen/internal/metadata.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Metadata struct {
2626
Parent string `mapstructure:"parent"`
2727
// Status information for the component.
2828
Status *Status `mapstructure:"status"`
29+
// Spatial Re-aggregation featuregate.
30+
ReaggregationEnabled bool `mapstructure:"reaggregation_enabled"`
2931
// The name of the package that will be generated.
3032
GeneratedPackageName string `mapstructure:"generated_package_name"`
3133
// Telemetry information for the component.
@@ -412,6 +414,17 @@ func (a Attribute) IsConditional() bool {
412414
return a.RequirementLevel == AttributeRequirementLevelConditionallyRequired
413415
}
414416

417+
// IsRequired returns true if the attribute is required.
418+
func (a Attribute) IsRequired() bool {
419+
return a.RequirementLevel == AttributeRequirementLevelRequired
420+
}
421+
422+
// IsNotOptIn returns true if the attribute is any requirement_level above
423+
// opt_in
424+
func (a Attribute) IsNotOptIn() bool {
425+
return a.RequirementLevel != AttributeRequirementLevelOptIn
426+
}
427+
415428
// UnmarshalText implements the encoding.TextUnmarshaler interface.
416429
func (rl *AttributeRequirementLevel) UnmarshalText(text []byte) error {
417430
switch string(text) {
@@ -476,6 +489,31 @@ func (a Attribute) TestValue() string {
476489
return ""
477490
}
478491

492+
func (a Attribute) TestValueTwo() string {
493+
if a.Enum != nil {
494+
return fmt.Sprintf(`%q`, a.Enum[1])
495+
}
496+
switch a.Type.ValueType {
497+
case pcommon.ValueTypeEmpty:
498+
return ""
499+
case pcommon.ValueTypeStr:
500+
return fmt.Sprintf(`"%s-val-2"`, a.FullName)
501+
case pcommon.ValueTypeInt:
502+
return strconv.Itoa(len(a.FullName) + 1)
503+
case pcommon.ValueTypeDouble:
504+
return fmt.Sprintf("%f", 1.1+float64(len(a.FullName)))
505+
case pcommon.ValueTypeBool:
506+
return strconv.FormatBool(len(a.FullName)%2 == 1)
507+
case pcommon.ValueTypeMap:
508+
return fmt.Sprintf(`map[string]any{"key3": "%s-val3", "key4": "%s-val4"}`, a.FullName, a.FullName)
509+
case pcommon.ValueTypeSlice:
510+
return fmt.Sprintf(`[]any{"%s-item3", "%s-item4"}`, a.FullName, a.FullName)
511+
case pcommon.ValueTypeBytes:
512+
return fmt.Sprintf(`[]byte("%s-val-2")`, a.FullName)
513+
}
514+
return ""
515+
}
516+
479517
type Signal struct {
480518
// Enabled defines whether the signal is enabled by default.
481519
Enabled bool `mapstructure:"enabled"`

cmd/mdatagen/internal/sampleconnector/documentation.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ Monotonic cumulative sum int metric with string input_type enabled by default.
6060
| slice_attr | Attribute with a slice value. | Any Slice | Recommended |
6161
| map_attr | Attribute with a map value. | Any Map | Recommended |
6262
63+
### reaggregate.metric
64+
65+
Metric for testing spacial reaggregation
66+
67+
| Unit | Metric Type | Value Type | Stability |
68+
| ---- | ----------- | ---------- | --------- |
69+
| 1 | Gauge | Double | Beta |
70+
71+
#### Attributes
72+
73+
| Name | Description | Values | Requirement Level |
74+
| ---- | ----------- | ------ | -------- |
75+
| string_attr | Attribute with any string value. | Any Str | Recommended |
76+
| boolean_attr | Attribute with a boolean value. | Any Bool | Recommended |
77+
6378
## Optional Metrics
6479
6580
The following metrics are not emitted by default. Each of them can be enabled by applying the following configuration:

cmd/mdatagen/internal/sampleconnector/internal/metadata/generated_config.go

Lines changed: 68 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/sampleconnector/internal/metadata/generated_config_test.go

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)