diff --git a/.chloggen/codeboten_mdatagen-goleak.yaml b/.chloggen/codeboten_mdatagen-goleak.yaml new file mode 100644 index 00000000000..2103e9743aa --- /dev/null +++ b/.chloggen/codeboten_mdatagen-goleak.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# 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. otlpreceiver) +component: mdatagen + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: enable goleak tests by default via mdatagen + +# One or more tracking issues or pull requests related to the change +issues: [9959] + +# (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: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/cmd/mdatagen/embeded_templates_test.go b/cmd/mdatagen/embeded_templates_test.go index 1f4c4684e4e..ca8e61883c9 100644 --- a/cmd/mdatagen/embeded_templates_test.go +++ b/cmd/mdatagen/embeded_templates_test.go @@ -28,6 +28,7 @@ func TestEnsureTemplatesLoaded(t *testing.T) { path.Join(rootDir, "resource_test.go.tmpl"): {}, path.Join(rootDir, "config.go.tmpl"): {}, path.Join(rootDir, "config_test.go.tmpl"): {}, + path.Join(rootDir, "package_test.go.tmpl"): {}, path.Join(rootDir, "readme.md.tmpl"): {}, path.Join(rootDir, "status.go.tmpl"): {}, path.Join(rootDir, "testdata", "config.yaml.tmpl"): {}, diff --git a/exporter/exportertest/package_test.go b/cmd/mdatagen/internal/samplereceiver/generated_package_test.go similarity index 51% rename from exporter/exportertest/package_test.go rename to cmd/mdatagen/internal/samplereceiver/generated_package_test.go index 8fa8a72b85f..ba2afcd94ca 100644 --- a/exporter/exportertest/package_test.go +++ b/cmd/mdatagen/internal/samplereceiver/generated_package_test.go @@ -1,7 +1,6 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. -package exportertest +package samplereceiver import ( "testing" diff --git a/cmd/mdatagen/loader.go b/cmd/mdatagen/loader.go index 90fd6571878..d6a72f4297d 100644 --- a/cmd/mdatagen/loader.go +++ b/cmd/mdatagen/loader.go @@ -201,11 +201,22 @@ func (a attribute) TestValue() string { return "" } +type ignore struct { + Top []string `mapstructure:"top"` + Any []string `mapstructure:"any"` +} + +type goLeak struct { + Skip bool `mapstructure:"skip"` + Ignore ignore `mapstructure:"ignore"` +} + type tests struct { - Config any `mapstructure:"config"` - SkipLifecycle bool `mapstructure:"skip_lifecycle"` - SkipShutdown bool `mapstructure:"skip_shutdown"` - ExpectConsumerError bool `mapstructure:"expect_consumer_error"` + Config any `mapstructure:"config"` + SkipLifecycle bool `mapstructure:"skip_lifecycle"` + SkipShutdown bool `mapstructure:"skip_shutdown"` + GoLeak goLeak `mapstructure:"goleak"` + ExpectConsumerError bool `mapstructure:"expect_consumer_error"` } type metadata struct { diff --git a/cmd/mdatagen/main.go b/cmd/mdatagen/main.go index 0f458d5cf5b..35f5acedfc9 100644 --- a/cmd/mdatagen/main.go +++ b/cmd/mdatagen/main.go @@ -66,6 +66,10 @@ func run(ymlPath string) error { filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil { return err } + if err = generateFile(filepath.Join(tmplDir, "package_test.go.tmpl"), + filepath.Join(ymlDir, "generated_package_test.go"), md, packageName); err != nil { + return err + } } if _, err = os.Stat(filepath.Join(ymlDir, "README.md")); err == nil { diff --git a/cmd/mdatagen/main_test.go b/cmd/mdatagen/main_test.go index ff3a70ccac2..695b988cbe8 100644 --- a/cmd/mdatagen/main_test.go +++ b/cmd/mdatagen/main_test.go @@ -23,6 +23,8 @@ func TestRunContents(t *testing.T) { wantMetricsGenerated bool wantConfigGenerated bool wantStatusGenerated bool + wantGoleakIgnore bool + wantGoleakSkip bool wantErr bool }{ { @@ -64,6 +66,16 @@ func TestRunContents(t *testing.T) { yml: "with_tests_connector.yaml", wantStatusGenerated: true, }, + { + yml: "with_goleak_ignores.yaml", + wantStatusGenerated: true, + wantGoleakIgnore: true, + }, + { + yml: "with_goleak_skip.yaml", + wantStatusGenerated: true, + wantGoleakSkip: true, + }, } for _, tt := range tests { t.Run(tt.yml, func(t *testing.T) { @@ -123,6 +135,27 @@ foo require.Contains(t, string(contents), "func Test") _, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors) require.NoError(t, err) + + require.FileExists(t, filepath.Join(tmpdir, "generated_package_test.go")) + contents, err = os.ReadFile(filepath.Join(tmpdir, "generated_package_test.go")) // nolint: gosec + require.NoError(t, err) + require.Contains(t, string(contents), "func TestMain") + _, err = parser.ParseFile(token.NewFileSet(), "", contents, parser.DeclarationErrors) + require.NoError(t, err) + + if tt.wantGoleakSkip { + require.Contains(t, string(contents), "skipping goleak test") + } else { + require.NotContains(t, string(contents), "skipping goleak test") + } + + if tt.wantGoleakIgnore { + require.Contains(t, string(contents), "IgnoreTopFunction") + require.Contains(t, string(contents), "IgnoreAnyFunction") + } else { + require.NotContains(t, string(contents), "IgnoreTopFunction") + require.NotContains(t, string(contents), "IgnoreAnyFunction") + } }) } } diff --git a/cmd/mdatagen/metadata-schema.yaml b/cmd/mdatagen/metadata-schema.yaml index bb6ae4f412a..4d016948b86 100644 --- a/cmd/mdatagen/metadata-schema.yaml +++ b/cmd/mdatagen/metadata-schema.yaml @@ -118,3 +118,8 @@ tests: skip_shutdown: false # false by default # Whether it's expected that the Consume[Logs|Metrics|Traces] method will return an error with the given configuration. expect_consumer_error: true # false by default + goleak: # {} by default generates a package_test to enable check for leaks + skip: false # set to true if goleak tests should be skipped + ignore: + top: [string] # Optional: array of strings representing functions that should be ignore via IgnoreTopFunction + any: [string] # Optional: array of strings representing functions that should be ignore via IgnoreAnyFunction diff --git a/cmd/mdatagen/templates/package_test.go.tmpl b/cmd/mdatagen/templates/package_test.go.tmpl new file mode 100644 index 00000000000..87dde88a1fa --- /dev/null +++ b/cmd/mdatagen/templates/package_test.go.tmpl @@ -0,0 +1,17 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package {{ .Package }} + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + {{- if .Tests.GoLeak.Skip }} + // skipping goleak test as per metadata.yml configuration + {{- else }} + goleak.VerifyTestMain(m {{- range $val := .Tests.GoLeak.Ignore.Top}}, goleak.IgnoreTopFunction("{{$val}}"){{end}}{{- range $val := .Tests.GoLeak.Ignore.Any}}, goleak.IgnoreAnyFunction("{{$val}}"){{end}} ) + {{- end }} +} diff --git a/cmd/mdatagen/testdata/with_goleak_ignores.yaml b/cmd/mdatagen/testdata/with_goleak_ignores.yaml new file mode 100644 index 00000000000..18ee688ea38 --- /dev/null +++ b/cmd/mdatagen/testdata/with_goleak_ignores.yaml @@ -0,0 +1,14 @@ +type: foobar + +status: + class: connector + stability: + beta: [traces_to_metrics, traces_to_traces, traces_to_logs, metrics_to_logs, metrics_to_metrics, metrics_to_traces, logs_to_logs, logs_to_metrics, logs_to_traces] + +tests: + goleak: + ignore: + top: + - "testfunc1" + any: + - "testfunc2" \ No newline at end of file diff --git a/cmd/mdatagen/testdata/with_goleak_skip.yaml b/cmd/mdatagen/testdata/with_goleak_skip.yaml new file mode 100644 index 00000000000..583d8af185d --- /dev/null +++ b/cmd/mdatagen/testdata/with_goleak_skip.yaml @@ -0,0 +1,10 @@ +type: foobar + +status: + class: connector + stability: + beta: [traces_to_metrics, traces_to_traces, traces_to_logs, metrics_to_logs, metrics_to_metrics, metrics_to_traces, logs_to_logs, logs_to_metrics, logs_to_traces] + +tests: + goleak: + skip: true \ No newline at end of file diff --git a/connector/forwardconnector/package_test.go b/connector/forwardconnector/generated_package_test.go similarity index 62% rename from connector/forwardconnector/package_test.go rename to connector/forwardconnector/generated_package_test.go index 54cc0fd73ce..6ea5f1097be 100644 --- a/connector/forwardconnector/package_test.go +++ b/connector/forwardconnector/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package forwardconnector diff --git a/exporter/debugexporter/package_test.go b/exporter/debugexporter/generated_package_test.go similarity index 62% rename from exporter/debugexporter/package_test.go rename to exporter/debugexporter/generated_package_test.go index 320777f1ab8..f4beaf9a9b9 100644 --- a/exporter/debugexporter/package_test.go +++ b/exporter/debugexporter/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package debugexporter diff --git a/exporter/loggingexporter/package_test.go b/exporter/loggingexporter/generated_package_test.go similarity index 62% rename from exporter/loggingexporter/package_test.go rename to exporter/loggingexporter/generated_package_test.go index 0c1b40739cc..14eb697fde5 100644 --- a/exporter/loggingexporter/package_test.go +++ b/exporter/loggingexporter/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package loggingexporter diff --git a/extension/extensiontest/package_test.go b/exporter/nopexporter/generated_package_test.go similarity index 51% rename from extension/extensiontest/package_test.go rename to exporter/nopexporter/generated_package_test.go index 22f7af4bd14..1bcb87f7640 100644 --- a/extension/extensiontest/package_test.go +++ b/exporter/nopexporter/generated_package_test.go @@ -1,7 +1,6 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. -package extensiontest +package nopexporter import ( "testing" diff --git a/exporter/nopexporter/go.mod b/exporter/nopexporter/go.mod index 628bc3fa763..7348ab95faf 100644 --- a/exporter/nopexporter/go.mod +++ b/exporter/nopexporter/go.mod @@ -13,6 +13,7 @@ require ( go.opentelemetry.io/collector/pdata v1.5.0 go.opentelemetry.io/otel/metric v1.25.0 go.opentelemetry.io/otel/trace v1.25.0 + go.uber.org/goleak v1.3.0 ) require ( diff --git a/exporter/otlpexporter/package_test.go b/exporter/otlpexporter/generated_package_test.go similarity index 61% rename from exporter/otlpexporter/package_test.go rename to exporter/otlpexporter/generated_package_test.go index c5fb1007f5b..9612d731def 100644 --- a/exporter/otlpexporter/package_test.go +++ b/exporter/otlpexporter/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package otlpexporter diff --git a/exporter/otlphttpexporter/package_test.go b/exporter/otlphttpexporter/generated_package_test.go similarity index 62% rename from exporter/otlphttpexporter/package_test.go rename to exporter/otlphttpexporter/generated_package_test.go index 07858386cc4..625e0cd9304 100644 --- a/exporter/otlphttpexporter/package_test.go +++ b/exporter/otlphttpexporter/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package otlphttpexporter diff --git a/extension/ballastextension/package_test.go b/extension/ballastextension/generated_package_test.go similarity index 62% rename from extension/ballastextension/package_test.go rename to extension/ballastextension/generated_package_test.go index 9035e22edbc..3f80a3e2754 100644 --- a/extension/ballastextension/package_test.go +++ b/extension/ballastextension/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package ballastextension diff --git a/extension/memorylimiterextension/generated_package_test.go b/extension/memorylimiterextension/generated_package_test.go new file mode 100644 index 00000000000..92a130658b4 --- /dev/null +++ b/extension/memorylimiterextension/generated_package_test.go @@ -0,0 +1,13 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package memorylimiterextension + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/extension/memorylimiterextension/go.mod b/extension/memorylimiterextension/go.mod index 9be98b970bc..5d6f8abb3eb 100644 --- a/extension/memorylimiterextension/go.mod +++ b/extension/memorylimiterextension/go.mod @@ -10,6 +10,7 @@ require ( go.opentelemetry.io/collector/extension v0.98.0 go.opentelemetry.io/otel/metric v1.25.0 go.opentelemetry.io/otel/trace v1.25.0 + go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 ) diff --git a/extension/zpagesextension/package_test.go b/extension/zpagesextension/generated_package_test.go similarity index 62% rename from extension/zpagesextension/package_test.go rename to extension/zpagesextension/generated_package_test.go index 40ce0f11cc4..a34cf6a2031 100644 --- a/extension/zpagesextension/package_test.go +++ b/extension/zpagesextension/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package zpagesextension diff --git a/processor/batchprocessor/package_test.go b/processor/batchprocessor/generated_package_test.go similarity index 62% rename from processor/batchprocessor/package_test.go rename to processor/batchprocessor/generated_package_test.go index 58d64e47b57..d8cd79854bc 100644 --- a/processor/batchprocessor/package_test.go +++ b/processor/batchprocessor/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package batchprocessor diff --git a/processor/memorylimiterprocessor/package_test.go b/processor/memorylimiterprocessor/generated_package_test.go similarity index 63% rename from processor/memorylimiterprocessor/package_test.go rename to processor/memorylimiterprocessor/generated_package_test.go index 77bee80493d..969659943d8 100644 --- a/processor/memorylimiterprocessor/package_test.go +++ b/processor/memorylimiterprocessor/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package memorylimiterprocessor diff --git a/receiver/nopreceiver/generated_package_test.go b/receiver/nopreceiver/generated_package_test.go new file mode 100644 index 00000000000..0472e5e1cd3 --- /dev/null +++ b/receiver/nopreceiver/generated_package_test.go @@ -0,0 +1,13 @@ +// Code generated by mdatagen. DO NOT EDIT. + +package nopreceiver + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/receiver/nopreceiver/go.mod b/receiver/nopreceiver/go.mod index 1b3b7d06e92..eb2b4b7d37b 100644 --- a/receiver/nopreceiver/go.mod +++ b/receiver/nopreceiver/go.mod @@ -12,6 +12,7 @@ require ( go.opentelemetry.io/collector/receiver v0.98.0 go.opentelemetry.io/otel/metric v1.25.0 go.opentelemetry.io/otel/trace v1.25.0 + go.uber.org/goleak v1.3.0 ) require ( diff --git a/receiver/otlpreceiver/package_test.go b/receiver/otlpreceiver/generated_package_test.go similarity index 61% rename from receiver/otlpreceiver/package_test.go rename to receiver/otlpreceiver/generated_package_test.go index 3a57035a327..5e08a4e06fc 100644 --- a/receiver/otlpreceiver/package_test.go +++ b/receiver/otlpreceiver/generated_package_test.go @@ -1,5 +1,4 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 +// Code generated by mdatagen. DO NOT EDIT. package otlpreceiver