Skip to content

Commit

Permalink
[mdatagen] Add support of enum field in resource attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax committed Nov 24, 2022
1 parent ec725ec commit cb8fdb5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 22 deletions.
17 changes: 17 additions & 0 deletions .chloggen/mdatagen-enum-resource-attrs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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: cmd/mdatagen

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

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

# (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: |
`resource_attributes::enum` values in metadata.yaml are now properly supported in metrics builder developer interface.
7 changes: 7 additions & 0 deletions cmd/mdatagen/metrics.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
type ResourceMetricsOption func(pmetric.ResourceMetrics)

{{- range $name, $attr := .ResourceAttributes }}
{{- range $attr.Enum }}
// With{{ $name.Render }}{{ . | publicVar }} sets "{{ $name }}={{ . }}" attribute for current resource.
func With{{ $name.Render }}{{ . | publicVar }}(rm pmetric.ResourceMetrics) {
rm.Resource().Attributes().PutStr("{{ attributeKey $name}}", "{{ . }}")
}
{{- else }}
// With{{ $name.Render }} sets provided value as "{{ $name }}" attribute for current resource.
func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOption {
return func(rm pmetric.ResourceMetrics) {
Expand All @@ -223,6 +229,7 @@ func With{{ $name.Render }}(val {{ $attr.Type.Primitive }}) ResourceMetricsOptio
{{- end }}
}
}
{{- end }}
{{ end }}

// WithStartTimeOverride overrides start time for all the resource metrics data points.
Expand Down
13 changes: 7 additions & 6 deletions cmd/mdatagen/metrics_test.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ func TestAllMetrics(t *testing.T) {
{{- end }}

metrics := mb.Emit(
{{- $sep := "" }}
{{- range $name, $info := .ResourceAttributes -}}
{{ $sep }}With{{ $name.Render }}({{- if $info.Enum }}Attribute{{ $name.Render }}(1){{ else }}{{ $info.Type.TestValue }}{{ end }})
{{- $sep = ", " }}
{{- end -}}
{{- $sep := "" }}
{{- range $name, $info := .ResourceAttributes -}}
{{- $sep }}With{{ $name.Render }}
{{- if $info.Enum }}{{ index $info.Enum 0 | publicVar }}{{ else }}({{- $info.Type.TestValue }}){{ end }}
{{- $sep = ", " }}
{{- end -}}
)

assert.Equal(t, 1, metrics.ResourceMetrics().Len())
Expand All @@ -74,7 +75,7 @@ func TestAllMetrics(t *testing.T) {
attrVal, ok {{ $assignSign }} rm.Resource().Attributes().Get("{{ $name }}")
assert.True(t, ok)
{{- if $info.Enum }}
assert.Equal(t, Attribute{{ $name.Render }}(1).String(), attrVal.Str())
assert.Equal(t, "{{ index $info.Enum 0 }}", attrVal.Str())
{{- else }}
assert.EqualValues(t, {{ $info.Type.TestValue }}, attrVal.{{ $info.Type }}())
{{- end }}
Expand Down

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

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

3 changes: 1 addition & 2 deletions receiver/flinkmetricsreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ resource_attributes:
flink.resource.type:
description: The flink scope type in which a metric belongs to.
type: string
# TODO: Add enum support to resource attributes
# enum: [ jobmanager, taskmanager ]
enum: [ jobmanager, taskmanager ]

attributes:
operator_name:
Expand Down
4 changes: 2 additions & 2 deletions receiver/flinkmetricsreceiver/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *flinkmetricsScraper) processJobmanagerMetrics(now pcommon.Timestamp, jo
}
s.mb.EmitForResource(
metadata.WithHostName(jobmanagerMetrics.Host),
metadata.WithFlinkResourceType("jobmanager"),
metadata.WithFlinkResourceTypeJobmanager,
)
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *flinkmetricsScraper) processTaskmanagerMetrics(now pcommon.Timestamp, t
s.mb.EmitForResource(
metadata.WithHostName(taskmanagerMetrics.Host),
metadata.WithFlinkTaskmanagerID(taskmanagerMetrics.TaskmanagerID),
metadata.WithFlinkResourceType("taskmanager"),
metadata.WithFlinkResourceTypeTaskmanager,
)
}
}
Expand Down
5 changes: 0 additions & 5 deletions receiver/nsxtreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ resource_attributes:
nsxt.node.type:
description: The type of NSX Node.
type: string
# TODO: Add support for enum in resource attributes
# enum:
# - manager
# - host
# - edge
device.id:
description: The name of the network interface.
type: string
Expand Down

0 comments on commit cb8fdb5

Please sign in to comment.