Skip to content

Commit

Permalink
[chore] update mdatagen with latest changes from contrib (#9436)
Browse files Browse the repository at this point in the history
Update mdatagen with a few changes that happened in contrib, ahead of
deleting mdatagen from contrib.

---------

Co-authored-by: Alex Boten <aboten@lightstep.com>
Co-authored-by: Alex Boten <alex@boten.ca>
  • Loading branch information
3 people authored Feb 1, 2024
1 parent 617615a commit 5bcd109
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 12 deletions.
93 changes: 93 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ func templatize(tmplFile string, md metadata) *template.Template {
"isExtension": func() bool {
return md.Status.Class == "extension"
},
"isConnector": func() bool {
return md.Status.Class == "connector"
},
"skipLifecycle": func() bool {
return md.Tests.SkipLifecycle
},
Expand Down Expand Up @@ -223,6 +226,96 @@ func templatize(tmplFile string, md metadata) *template.Template {
}
return false
},
"supportsLogsToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_logs" {
return true
}
}
}
return false
},
"supportsLogsToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_metrics" {
return true
}
}
}
return false
},
"supportsLogsToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "logs_to_traces" {
return true
}
}
}
return false
},
"supportsMetricsToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_logs" {
return true
}
}
}
return false
},
"supportsMetricsToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_metrics" {
return true
}
}
}
return false
},
"supportsMetricsToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "metrics_to_traces" {
return true
}
}
}
return false
},
"supportsTracesToLogs": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_logs" {
return true
}
}
}
return false
},
"supportsTracesToMetrics": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_metrics" {
return true
}
}
}
return false
},
"supportsTracesToTraces": func() bool {
for _, signals := range md.Status.Stability {
for _, s := range signals {
if s == "traces_to_traces" {
return true
}
}
}
return false
},
"expectConsumerError": func() bool {
return md.Tests.ExpectConsumerError
},
Expand Down
39 changes: 38 additions & 1 deletion cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Test_runContents(t *testing.T) {
wantMetricsGenerated bool
wantConfigGenerated bool
wantStatusGenerated bool
wantTestsGenerated bool
wantErr bool
}{
{
Expand All @@ -43,10 +44,37 @@ func Test_runContents(t *testing.T) {
yml: "status_only.yaml",
wantStatusGenerated: true,
},
{
yml: "with_tests_receiver.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_exporter.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_processor.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_extension.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
{
yml: "with_tests_connector.yaml",
wantTestsGenerated: true,
wantStatusGenerated: true,
},
}
for _, tt := range tests {
t.Run(tt.yml, func(t *testing.T) {
tmpdir := t.TempDir()
tmpdir := filepath.Join(t.TempDir(), "shortname")
err := os.MkdirAll(tmpdir, 0750)
require.NoError(t, err)
ymlContent, err := os.ReadFile(filepath.Join("testdata", tt.yml))
require.NoError(t, err)
metadataFile := filepath.Join(tmpdir, "metadata.yaml")
Expand Down Expand Up @@ -92,6 +120,15 @@ foo
require.NoError(t, err)
require.Contains(t, string(contents), "foo")
}

if tt.wantTestsGenerated {
require.FileExists(t, filepath.Join(tmpdir, "generated_component_test.go"))
contents, err := os.ReadFile(filepath.Join(tmpdir, "generated_component_test.go")) // nolint: gosec
require.NoError(t, err)
require.Contains(t, string(contents), "func Test")
} else {
require.NoFileExists(t, filepath.Join(tmpdir, "generated_component_test.go"))
}
})
}
}
Expand Down
1 change: 1 addition & 0 deletions cmd/mdatagen/metadata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ status:
codeowners:
active: [string]
emeritus: [string]
unsupported_platforms: [<linux|windows>]

# Optional: OTel Semantic Conventions version that will be associated with the scraped metrics.
# This attribute should be set for metrics compliant with OTel Semantic Conventions.
Expand Down
11 changes: 6 additions & 5 deletions cmd/mdatagen/statusdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ type Codeowners struct {
}

type Status struct {
Stability map[string][]string `mapstructure:"stability"`
Distributions []string `mapstructure:"distributions"`
Class string `mapstructure:"class"`
Warnings []string `mapstructure:"warnings"`
Codeowners *Codeowners `mapstructure:"codeowners"`
Stability map[string][]string `mapstructure:"stability"`
Distributions []string `mapstructure:"distributions"`
Class string `mapstructure:"class"`
Warnings []string `mapstructure:"warnings"`
Codeowners *Codeowners `mapstructure:"codeowners"`
UnsupportedPlatforms []string `mapstructure:"unsupported_platforms"`
}

func (s *Status) SortedDistributions() []string {
Expand Down
137 changes: 131 additions & 6 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Code generated by mdatagen. DO NOT EDIT.

{{ if len .Status.UnsupportedPlatforms -}}
//go:build {{ range $i, $v := .Status.UnsupportedPlatforms }}{{ if $i }} && {{ end }}!{{ . }}{{ end }}
{{- end }}

package {{ .Package }}

import (
Expand All @@ -26,6 +30,11 @@ import (
{{ end }}
{{ if isExtension }}
"go.opentelemetry.io/collector/extension/extensiontest"
{{ end }}
{{ if isConnector }}
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/connectortest"
{{ end }}
"go.opentelemetry.io/collector/confmap/confmaptest"
{{ if or (isExporter) (isProcessor) }}
Expand Down Expand Up @@ -53,13 +62,12 @@ func (aneh *assertNoErrorHost) ReportFatalError(err error) {
assert.NoError(aneh, err)
}


func TestCheckConfigStruct(t *testing.T) {
componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig())
}

{{ if isExporter }}
func Test_ComponentLifecycle(t *testing.T) {
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
Expand Down Expand Up @@ -150,7 +158,7 @@ func Test_ComponentLifecycle(t *testing.T) {
{{ end }}

{{ if isProcessor }}
func Test_ComponentLifecycle(t *testing.T) {
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
Expand Down Expand Up @@ -239,7 +247,7 @@ func Test_ComponentLifecycle(t *testing.T) {
{{ end }}

{{ if isReceiver }}
func Test_ComponentLifecycle(t *testing.T) {
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
Expand Down Expand Up @@ -308,7 +316,7 @@ func Test_ComponentLifecycle(t *testing.T) {
{{ end }}

{{ if isExtension }}
func Test_ComponentLifecycle(t *testing.T) {
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

cm, err := confmaptest.LoadConf("metadata.yaml")
Expand All @@ -334,11 +342,128 @@ func Test_ComponentLifecycle(t *testing.T) {
require.NoError(t, err)
require.NoError(t, firstExt.Start(context.Background(), newAssertNoErrorHost(t)))
require.NoError(t, firstExt.Shutdown(context.Background()))

secondExt, err := factory.CreateExtension(context.Background(), extensiontest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, secondExt.Start(context.Background(), newAssertNoErrorHost(t)))
require.NoError(t, secondExt.Shutdown(context.Background()))
})
}
{{ end }}

{{ if isConnector }}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()

tests := []struct{
name string
createFn func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error)
}{
{{ if supportsLogsToLogs }}
{
name: "logs_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsLogsToMetrics }}
{
name: "logs_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsLogsToTraces }}
{
name: "logs_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsToTraces(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToLogs }}
{
name: "metrics_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToMetrics }}
{
name: "metrics_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsMetricsToTraces }}
{
name: "metrics_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsToTraces(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToLogs }}
{
name: "traces_to_logs",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToLogs(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToMetrics }}
{
name: "traces_to_metrics",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToMetrics(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
{{ if supportsTracesToTraces }}
{
name: "traces_to_traces",
createFn: func(ctx context.Context, set connector.CreateSettings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesToTraces(ctx, set, cfg, consumertest.NewNop())
},
},
{{ end }}
}

cm, err := confmaptest.LoadConf("metadata.yaml")
require.NoError(t, err)
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub("tests::config")
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

for _, test := range tests {
t.Run(test.name + "-shutdown", func(t *testing.T) {
c, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})

t.Run(test.name + "-lifecycle", func(t *testing.T) {
{{ if skipLifecycle }}
// TODO support lifecycle
t.SkipNow()
{{ end }}
firstConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
host := newAssertNoErrorHost(t)
require.NoError(t, err)
require.NoError(t, firstConnector.Start(context.Background(), host))
require.NoError(t, firstConnector.Shutdown(context.Background()))
secondConnector, err := test.createFn(context.Background(), connectortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
require.NoError(t, secondConnector.Start(context.Background(), host))
require.NoError(t, secondConnector.Shutdown(context.Background()))
})
}
}
{{ end }}
Loading

0 comments on commit 5bcd109

Please sign in to comment.