Skip to content

Commit

Permalink
[cmd/mdatagen] support map and slice attrs (#18487)
Browse files Browse the repository at this point in the history
Add support for map and slice attributes

Add support for map and slice resource and datapoints attributes in
order to fulfill #18272.
  • Loading branch information
fredthomsen authored Apr 29, 2023
1 parent 87bc8af commit eecccc9
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 14 deletions.
16 changes: 16 additions & 0 deletions .chloggen/mdatagenSupportMapAndSliceAttrs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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. filelogreceiver)
component: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for slice and map attributes.

# One or more tracking issues related to the change
issues: [18272]

# (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:
4 changes: 4 additions & 0 deletions cmd/mdatagen/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The metric will be become optional soon.
| string_attr | Attribute with any string value. | Any Str |
| state | Integer attribute with overridden name. | Any Int |
| enum_attr | Attribute with a known set of string values. | Str: ``red``, ``green``, ``blue`` |
| slice_attr | Attribute with a slice value. | Any Slice |
| map_attr | Attribute with a map value. | Any Map |
### default.metric.to_be_removed
Expand Down Expand Up @@ -69,6 +71,8 @@ metrics:
| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| map.resource.attr | Resource attribute with a map value. | Any Map | true |
| optional.resource.attr | Explicitly disabled ResourceAttribute. | Any Str | false |
| slice.resource.attr | Resource attribute with a slice value. | Any Slice | true |
| string.enum.resource.attr | Resource attribute with a known set of string values. | Str: ``one``, ``two`` | true |
| string.resource.attr | Resource attribute with any string value. | Any Str | true |
34 changes: 31 additions & 3 deletions cmd/mdatagen/internal/metadata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions cmd/mdatagen/internal/metadata/generated_metrics_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/mdatagen/internal/metadata/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ all_set:
optional.metric:
enabled: true
resource_attributes:
map.resource.attr:
enabled: true
optional.resource.attr:
enabled: true
slice.resource.attr:
enabled: true
string.enum.resource.attr:
enabled: true
string.resource.attr:
Expand All @@ -23,8 +27,12 @@ none_set:
optional.metric:
enabled: false
resource_attributes:
map.resource.attr:
enabled: false
optional.resource.attr:
enabled: false
slice.resource.attr:
enabled: false
string.enum.resource.attr:
enabled: false
string.resource.attr:
Expand Down
12 changes: 10 additions & 2 deletions cmd/mdatagen/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (mvt *ValueType) UnmarshalText(text []byte) error {
mvt.ValueType = pcommon.ValueTypeBool
case "bytes":
mvt.ValueType = pcommon.ValueTypeBytes
case "slice":
mvt.ValueType = pcommon.ValueTypeSlice
case "map":
mvt.ValueType = pcommon.ValueTypeMap
default:
return fmt.Errorf("invalid type: %q", vtStr)
}
Expand All @@ -89,6 +93,10 @@ func (mvt ValueType) Primitive() string {
return "bool"
case pcommon.ValueTypeBytes:
return "[]byte"
case pcommon.ValueTypeSlice:
return "[]any"
case pcommon.ValueTypeMap:
return "map[string]any"
default:
return ""
}
Expand All @@ -105,9 +113,9 @@ func (mvt ValueType) TestValue() string {
case pcommon.ValueTypeBool:
return "true"
case pcommon.ValueTypeMap:
return `pcommon.NewMap()`
return `map[string]any{"onek": "onev", "twok": "twov"}`
case pcommon.ValueTypeSlice:
return `pcommon.NewSlice()`
return `[]any{"one", "two"}`
}
return ""
}
Expand Down
31 changes: 29 additions & 2 deletions cmd/mdatagen/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func Test_loadMetadata(t *testing.T) {
ValueType: pcommon.ValueTypeStr,
},
},
"slice.resource.attr": {
Description: "Resource attribute with a slice value.",
Enabled: true,
Type: ValueType{
ValueType: pcommon.ValueTypeSlice,
},
},
"map.resource.attr": {
Description: "Resource attribute with a map value.",
Enabled: true,
Type: ValueType{
ValueType: pcommon.ValueTypeMap,
},
},
},
Attributes: map[attributeName]attribute{
"enum_attr": {
Expand Down Expand Up @@ -85,7 +99,20 @@ func Test_loadMetadata(t *testing.T) {
Type: ValueType{
ValueType: pcommon.ValueTypeBool,
},
}},
},
"slice_attr": {
Description: "Attribute with a slice value.",
Type: ValueType{
ValueType: pcommon.ValueTypeSlice,
},
},
"map_attr": {
Description: "Attribute with a map value.",
Type: ValueType{
ValueType: pcommon.ValueTypeMap,
},
},
},
Metrics: map[metricName]metric{
"default.metric": {
Enabled: true,
Expand All @@ -100,7 +127,7 @@ func Test_loadMetadata(t *testing.T) {
Aggregated: Aggregated{Aggregation: pmetric.AggregationTemporalityCumulative},
Mono: Mono{Monotonic: true},
},
Attributes: []attributeName{"string_attr", "overridden_int_attr", "enum_attr"},
Attributes: []attributeName{"string_attr", "overridden_int_attr", "enum_attr", "slice_attr", "map_attr"},
},
"optional.metric": {
Enabled: false,
Expand Down
20 changes: 19 additions & 1 deletion cmd/mdatagen/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ resource_attributes:
type: string
enabled: false

slice.resource.attr:
description: Resource attribute with a slice value.
type: slice
enabled: true

map.resource.attr:
description: Resource attribute with a map value.
type: map
enabled: true

attributes:
string_attr:
description: Attribute with any string value.
Expand All @@ -40,6 +50,14 @@ attributes:
description: Attribute with a boolean value.
type: bool

slice_attr:
description: Attribute with a slice value.
type: slice

map_attr:
description: Attribute with a map value.
type: map

metrics:
default.metric:
enabled: true
Expand All @@ -50,7 +68,7 @@ metrics:
value_type: int
monotonic: true
aggregation: cumulative
attributes: [string_attr, overridden_int_attr, enum_attr]
attributes: [string_attr, overridden_int_attr, enum_attr, slice_attr, map_attr]
warnings:
if_enabled_not_set: This metric will be disabled by default soon.

Expand Down
4 changes: 2 additions & 2 deletions cmd/mdatagen/metric-metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ resource_attributes:
# Optional: array of attribute values if they are static values (currently, only string type is supported).
enum:
# Required: attribute value type.
type: <string|int|double|bool|bytes>
type: <string|int|double|bool|bytes|slice|map>

# Optional: map of attribute definitions with the key being the attribute name and value
# being described below.
Expand All @@ -29,7 +29,7 @@ attributes:
# Optional: array of attribute values if they are static values (currently, only string type is supported).
enum:
# Required: attribute value type.
type: <string|int|double|bool|bytes>
type: <string|int|double|bool|bytes|slice|map>

# Required: map of metric names with the key being the metric name and value
# being described below.
Expand Down
8 changes: 8 additions & 0 deletions cmd/mdatagen/templates/metrics.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func (m *metric{{ $name.Render }}) recordDataPoint(start pcommon.Timestamp, ts p
{{- range $metric.Attributes }}
{{- if eq (attributeInfo .).Type.Primitive "[]byte" }}
dp.Attributes().PutEmptyBytes("{{ attributeName . }}").FromRaw({{ .RenderUnexported }}AttributeValue)
{{- else if eq (attributeInfo .).Type.Primitive "[]any" }}
dp.Attributes().PutEmptySlice("{{ attributeName . }}").FromRaw({{ .RenderUnexported }}AttributeValue)
{{- else if eq (attributeInfo .).Type.Primitive "map[string]any" }}
dp.Attributes().PutEmptyMap("{{ attributeName . }}").FromRaw({{ .RenderUnexported }}AttributeValue)
{{- else }}
dp.Attributes().Put{{ (attributeInfo .).Type }}("{{ attributeName .}}", {{ .RenderUnexported }}AttributeValue)
{{- end }}
Expand Down Expand Up @@ -285,6 +289,10 @@ func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOptio
if ras.{{ $name.Render }}.Enabled {
{{- if eq $attr.Type.Primitive "[]byte" }}
rm.Resource().Attributes().PutEmptyBytes("{{ attributeName $name}}").FromRaw(val)
{{- else if eq $attr.Type.Primitive "[]any" }}
rm.Resource().Attributes().PutEmptySlice("{{ attributeName $name}}").FromRaw(val)
{{- else if eq $attr.Type.Primitive "map[string]any" }}
rm.Resource().Attributes().PutEmptyMap("{{ attributeName $name}}").FromRaw(val)
{{- else }}
rm.Resource().Attributes().Put{{ $attr.Type }}("{{ attributeName $name}}", val)
{{- end }}
Expand Down
Loading

0 comments on commit eecccc9

Please sign in to comment.