Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/mdatagen] Add ability to generate configs for resource attributes #21664

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions .chloggen/mdatagen-allow-resource-attrs-only.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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: Allow setting resource_attributes without introducing the metrics builder.

# One or more tracking issues related to the change
issues: [21516]
6 changes: 5 additions & 1 deletion cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func run(ymlPath string) error {
return err
}
}
if len(md.Metrics) == 0 {
if len(md.Metrics) == 0 && len(md.ResourceAttributes) == 0 {
return nil
}

Expand All @@ -100,6 +100,10 @@ func run(ymlPath string) error {
return err
}

if len(md.Metrics) == 0 {
return nil
}

if err = generateFile(filepath.Join(tmplDir, "metrics.go.tmpl"),
filepath.Join(codeDir, "generated_metrics.go"), md); err != nil {
return err
Expand Down
53 changes: 26 additions & 27 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,55 +28,43 @@ import (

func Test_runContents(t *testing.T) {
tests := []struct {
name string
yml string
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantErr bool
}{
{
name: "valid metadata",
yml: `
type: metricreceiver
metrics:
metric:
enabled: true
description: Description.
unit: s
gauge:
value_type: double`,
yml: "invalid.yaml",
wantErr: true,
},
{
yml: "metrics_and_type.yaml",
wantMetricsGenerated: true,
wantConfigGenerated: true,
},
{
name: "invalid yaml",
yml: "invalid",
wantErr: true,
yml: "resource_attributes_only.yaml",
wantConfigGenerated: true,
},
{
name: "no metrics but status present",
yml: `
type: metricreceiver
status:
class: exporter
stability:
beta: [traces, metrics, logs]
distributions: [contrib]
`,
yml: "status_only.yaml",
wantStatusGenerated: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.yml, func(t *testing.T) {
tmpdir := t.TempDir()

ymlContent, err := os.ReadFile(filepath.Join("testdata", tt.yml))
require.NoError(t, err)
metadataFile := filepath.Join(tmpdir, "metadata.yaml")
require.NoError(t, os.WriteFile(metadataFile, []byte(tt.yml), 0600))
require.NoError(t, os.WriteFile(metadataFile, ymlContent, 0600))
require.NoError(t, os.WriteFile(filepath.Join(tmpdir, "README.md"), []byte(`
<!-- status autogenerated section -->
foo
<!-- end autogenerated section -->`), 0600))

err := run(metadataFile)
err = run(metadataFile)
if tt.wantErr {
require.Error(t, err)
return
Expand All @@ -85,11 +73,22 @@ foo

if tt.wantMetricsGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics.go"))
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics_test.go"))
require.FileExists(t, filepath.Join(tmpdir, "documentation.md"))
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_metrics_test.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "documentation.md"))
}

if tt.wantConfigGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config.go"))
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config_test.go"))
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config.go"))
require.NoFileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_config_test.go"))
}

if tt.wantStatusGenerated {
require.FileExists(t, filepath.Join(tmpdir, "internal/metadata/generated_status.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "README.md"))
Expand Down
4 changes: 4 additions & 0 deletions cmd/mdatagen/templates/config.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package {{ .Package }}

{{ if .Metrics -}}
import "go.opentelemetry.io/collector/confmap"

// MetricConfig provides common config for a particular metric.
Expand Down Expand Up @@ -39,6 +40,7 @@ func DefaultMetricsConfig() MetricsConfig {
{{- end }}
}
}
{{- end }}

{{ if .ResourceAttributes -}}
// ResourceAttributeConfig provides common config for a particular resource attribute.
Expand All @@ -64,6 +66,7 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig {
}
{{- end }}

{{ if .Metrics -}}
// MetricsBuilderConfig is a configuration for {{ .Type }} metrics builder.
type MetricsBuilderConfig struct {
Metrics MetricsConfig `mapstructure:"metrics"`
Expand All @@ -80,3 +83,4 @@ func DefaultMetricsBuilderConfig() MetricsBuilderConfig {
{{- end }}
}
}
{{- end }}
48 changes: 48 additions & 0 deletions cmd/mdatagen/templates/config_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.opentelemetry.io/collector/confmap/confmaptest"
)

{{ if .Metrics }}
func TestMetricsBuilderConfig(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -77,3 +78,50 @@ func loadMetricsBuilderConfig(t *testing.T, name string) MetricsBuilderConfig {
require.NoError(t, component.UnmarshalConfig(sub, &cfg))
return cfg
}

{{- else }}

func TestResourceAttributesConfig(t *testing.T) {
tests := []struct {
name string
want ResourceAttributesConfig
}{
{
name: "default",
want: DefaultResourceAttributesConfig(),
},
{
name: "all_set",
want: ResourceAttributesConfig{
{{- range $name, $_ := .ResourceAttributes }}
{{ $name.Render }}: ResourceAttributeConfig{Enabled: true},
{{- end }}
},
},
{
name: "none_set",
want: ResourceAttributesConfig{
{{- range $name, $_ := .ResourceAttributes }}
{{ $name.Render }}: ResourceAttributeConfig{Enabled: false},
{{- end }}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
sub, err := cm.Sub(tt.name)
require.NoError(t, err)
sub, err = sub.Sub("resource_attributes")
require.NoError(t, err)
cfg := DefaultResourceAttributesConfig()
require.NoError(t, component.UnmarshalConfig(sub, &cfg))

if diff := cmp.Diff(tt.want, cfg, cmpopts.IgnoreUnexported(ResourceAttributeConfig{})); diff != "" {
t.Errorf("Config mismatch (-expected +actual):\n%s", diff)
}
})
}
}
{{- end }}
4 changes: 4 additions & 0 deletions cmd/mdatagen/templates/testdata/config.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
default:
all_set:
{{- if .Metrics }}
metrics:
{{- range $name, $_ := .Metrics }}
{{ $name }}:
enabled: true
{{- end }}
{{- end }}
{{- if .ResourceAttributes }}
resource_attributes:
{{- range $name, $_ := .ResourceAttributes }}
Expand All @@ -13,11 +15,13 @@ all_set:
{{- end }}
{{- end }}
none_set:
{{- if .Metrics }}
metrics:
{{- range $name, $_ := .Metrics }}
{{ $name }}:
enabled: false
{{- end }}
{{- end }}
{{- if .ResourceAttributes }}
resource_attributes:
{{- range $name, $_ := .ResourceAttributes }}
Expand Down
1 change: 1 addition & 0 deletions cmd/mdatagen/testdata/invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid
8 changes: 8 additions & 0 deletions cmd/mdatagen/testdata/metrics_and_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: metricreceiver
metrics:
metric:
enabled: true
description: Description.
unit: s
gauge:
value_type: double
5 changes: 5 additions & 0 deletions cmd/mdatagen/testdata/resource_attributes_only.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource_attributes:
res.attr1:
description: Resource attribute 1.
type: string
enabled: true
6 changes: 6 additions & 0 deletions cmd/mdatagen/testdata/status_only.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: metricreceiver
status:
class: exporter
stability:
beta: [traces, metrics, logs]
distributions: [contrib]
1 change: 1 addition & 0 deletions receiver/azuremonitorreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor v0.8.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.0.0
github.com/google/go-cmp v0.5.9
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.77.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.77.0
github.com/stretchr/testify v1.8.2
Expand Down
1 change: 1 addition & 0 deletions receiver/azuremonitorreceiver/go.sum

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.

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