Skip to content

Commit

Permalink
[cmd/mdatagen] [chore] Small fixes for new resource attributes filter (
Browse files Browse the repository at this point in the history
…open-telemetry#9950)

- Make sure we always pass a string to the filter.Match even if the
attribute value has a different type. Otherwise, it panics.
- Make sure we show the if_configured warning if the user sets
include/exclude without enabled.
- Simplify generated tests

Follow up to
open-telemetry#9660
  • Loading branch information
dmitryax authored Apr 12, 2024
1 parent 878bba2 commit b1e075f
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 143 deletions.

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.

Original file line number Diff line number Diff line change
Expand Up @@ -58,84 +58,54 @@ none_set:
string.resource.attr_to_be_removed:
enabled: false
filter_set_include:
metrics:
default.metric:
enabled: true
default.metric.to_be_removed:
enabled: true
metric.input_type:
enabled: true
optional.metric:
enabled: true
optional.metric.empty_unit:
enabled: true
resource_attributes:
map.resource.attr:
enabled: true
include:
- regexp: ".*"
optional.resource.attr:
enabled: true
include:
- regexp: ".*"
slice.resource.attr:
enabled: true
include:
- regexp: ".*"
string.enum.resource.attr:
enabled: true
include:
- regexp: ".*"
string.resource.attr:
enabled: true
include:
- regexp: ".*"
string.resource.attr_disable_warning:
enabled: true
include:
- regexp: ".*"
string.resource.attr_remove_warning:
enabled: true
include:
- regexp: ".*"
string.resource.attr_to_be_removed:
enabled: true
include:
- regexp: ".*"
filter_set_exclude:
metrics:
default.metric:
enabled: true
default.metric.to_be_removed:
enabled: true
metric.input_type:
enabled: true
optional.metric:
enabled: true
optional.metric.empty_unit:
enabled: true
resource_attributes:
map.resource.attr:
enabled: true
exclude:
- regexp: ".*"
optional.resource.attr:
enabled: true
exclude:
- strict: "optional.resource.attr-val"
slice.resource.attr:
enabled: true
exclude:
- regexp: ".*"
string.enum.resource.attr:
enabled: true
exclude:
- strict: "one"
string.resource.attr:
enabled: true
exclude:
- strict: "string.resource.attr-val"
string.resource.attr_disable_warning:
enabled: true
exclude:
- strict: "string.resource.attr_disable_warning-val"
string.resource.attr_remove_warning:
enabled: true
exclude:
- strict: "string.resource.attr_remove_warning-val"
string.resource.attr_to_be_removed:
enabled: true
exclude:
- strict: "string.resource.attr_to_be_removed-val"
14 changes: 8 additions & 6 deletions cmd/mdatagen/templates/config.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

package {{ .Package }}

{{ if or .Metrics .ResourceAttributes -}}
import "go.opentelemetry.io/collector/confmap"
{{- end }}
{{ if .ResourceAttributes -}}
import "go.opentelemetry.io/collector/filter"
{{- end }}
import (
"go.opentelemetry.io/collector/confmap"
{{ if and .Metrics .ResourceAttributes -}}
"go.opentelemetry.io/collector/filter"
{{- end }}
)
{{ if .Metrics -}}

// MetricConfig provides common config for a particular metric.
Expand Down Expand Up @@ -51,8 +51,10 @@ func DefaultMetricsConfig() MetricsConfig {
// ResourceAttributeConfig provides common config for a particular resource attribute.
type ResourceAttributeConfig struct {
Enabled bool `mapstructure:"enabled"`
{{- if .Metrics }}
Include []filter.Config `mapstructure:"include"`
Exclude []filter.Config `mapstructure:"exclude"`
{{- end }}

enabledSetByUser bool
}
Expand Down
18 changes: 8 additions & 10 deletions cmd/mdatagen/templates/metrics.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSetting
}
{{- end }}
{{- if $attr.Warnings.IfConfigured }}
if mbc.ResourceAttributes.{{ $name.Render }}.enabledSetByUser {
if mbc.ResourceAttributes.{{ $name.Render }}.enabledSetByUser || mbc.ResourceAttributes.{{ $name.Render }}.Include != nil || mbc.ResourceAttributes.{{ $name.Render }}.Exclude != nil {
settings.Logger.Warn("[WARNING] `{{ $name }}` should not be configured: {{ $attr.Warnings.IfConfigured }}")
}
{{- end }}
Expand Down Expand Up @@ -284,16 +284,14 @@ func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {
op(rm)
}
{{ if .ResourceAttributes -}}
for name, val := range rm.Resource().Attributes().AsRaw() {
if filter, ok := mb.resourceAttributeIncludeFilter[name]; ok {
if !filter.Matches(val) {
return
}
for attr, filter := range mb.resourceAttributeIncludeFilter {
if val, ok := rm.Resource().Attributes().Get(attr); ok && !filter.Matches(val.AsString()) {
return
}
if filter, ok := mb.resourceAttributeExcludeFilter[name]; ok {
if filter.Matches(val) {
return
}
}
for attr, filter := range mb.resourceAttributeExcludeFilter {
if val, ok := rm.Resource().Attributes().Get(attr); ok && filter.Matches(val.AsString()) {
return
}
}
{{- end }}
Expand Down
Loading

0 comments on commit b1e075f

Please sign in to comment.