diff --git a/.golangci.yml b/.golangci.yml index 7829405..b93acfe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,8 +36,7 @@ linters-settings: gocyclo: min-complexity: 15 govet: - enable: - - fieldalignment + enable-all: true lll: line-length: 120 dupword: @@ -47,8 +46,12 @@ linters: enable: - asasalint - asciicheck + - bidichk + - contextcheck - dupword + - durationcheck - errcheck + - errchkjson - errname - errorlint - exportloopref @@ -56,6 +59,8 @@ linters: - forcetypeassert - ginkgolinter - gocheckcompilerdirectives + - gochecksumtype + - gocritic - gocyclo - godot - gofmt @@ -71,6 +76,7 @@ linters: - loggercheck - makezero - misspell + - musttag - nilerr - noctx - nolintlint @@ -83,6 +89,7 @@ linters: - spancheck - staticcheck - stylecheck + - tagalign - tenv - thelper - tparallel diff --git a/Makefile b/Makefile index c8615f1..7b24102 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ help: Makefile ## Display this help unit-test: go test ./pkg/... -race -shuffle=on -coverprofile=cover.out -covermode=atomic go tool cover -html=cover.out -o cover.html - go test -tags generator ./cmd/generator/... -race -coverprofile=generator-cover.out + go test -tags generator ./cmd/generator/... -race -shuffle=on -coverprofile=generator-cover.out go tool cover -html=generator-cover.out -o generator-cover.html .PHONY: clean-go-cache @@ -43,8 +43,8 @@ generate: ## Run go generate .PHONY: generator-tests generator-tests: ## Regenerate the generator generated files and run generator unit tests go generate -tags generator ./cmd/generator/... # ensure the generated files generated by the generator are up to date - go test -tags generator ./cmd/generator/... -race -coverprofile=generator-cover.out + go test -tags generator ./cmd/generator/... -race -shuffle=on -coverprofile=generator-cover.out .PHONY: functional-test functional-test: ## Run functional tests - go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --race --keep-going --fail-on-pending --trace --covermode=atomic --coverprofile=functional-cover.out -r tests + go run github.com/onsi/ginkgo/v2/ginkgo --randomize-all --randomize-suites --race --keep-going --fail-on-pending --trace --covermode=atomic --coverprofile=functional-cover.out --force-newlines -p -v -r tests diff --git a/cmd/generator/code.go b/cmd/generator/code.go index 8fe92be..7d48d63 100644 --- a/cmd/generator/code.go +++ b/cmd/generator/code.go @@ -15,19 +15,20 @@ import ( var telemetryPackagePath = reflect.TypeOf((*telemetry.Exportable)(nil)).Elem().PkgPath() -const codeTemplate = `{{ if .BuildTags }}//go:build {{ .BuildTags }}{{ end }} +const codeTemplate = `{{- if .BuildTags }}//go:build {{ .BuildTags }} +{{ end -}} package {{ .PackageName }} + /* This is a generated file. DO NOT EDIT. */ import ( "go.opentelemetry.io/otel/attribute" + {{- if .TelemetryPackagePath }} - {{ if .TelemetryPackagePath }} - {{ if .TelemetryPackageAlias }}{{ .TelemetryPackageAlias }} {{ end -}} - "{{ .TelemetryPackagePath }}" - {{ end }} + {{ if .TelemetryPackageAlias }}{{ .TelemetryPackageAlias }} {{ end }}"{{ .TelemetryPackagePath }}" + {{- end }} ) func (d *{{ .StructName }}) Attributes() []attribute.KeyValue { @@ -35,11 +36,13 @@ func (d *{{ .StructName }}) Attributes() []attribute.KeyValue { {{- if .SchemeDataType }} attrs = append(attrs, attribute.String("dataType", "{{ .SchemeDataType }}")) - {{ end }} + {{- end }} - {{ range .Fields -}} + {{- range .Fields }} + {{- if .AttributesSource }} attrs = append(attrs, {{ .AttributesSource }}) - {{ end }} + {{- end }} + {{- end }} return attrs } @@ -91,15 +94,16 @@ func generateCode(writer io.Writer, cfg codeGenConfig) error { for _, f := range cfg.fields { var cf codeField - if f.embeddedStruct { + switch { + case f.embeddedStruct: cf = codeField{ AttributesSource: fmt.Sprintf(`d.%s.Attributes()...`, f.name), } - } else if f.slice { + case f.slice: cf = codeField{ AttributesSource: fmt.Sprintf(`attribute.%sSlice("%s", d.%s)`, getAttributeType(f.fieldType), f.name, f.name), } - } else { + default: cf = codeField{ AttributesSource: fmt.Sprintf(`attribute.%s("%s", d.%s)`, getAttributeType(f.fieldType), f.name, f.name), } diff --git a/cmd/generator/code_test.go b/cmd/generator/code_test.go index 58fa582..483050f 100644 --- a/cmd/generator/code_test.go +++ b/cmd/generator/code_test.go @@ -24,16 +24,16 @@ func TestGenerateCode(t *testing.T) { _ = tests.Data{} // depends on the type being defined - parsingResult, err := parse(cfg) + pResult, err := parse(cfg) g.Expect(err).ToNot(HaveOccurred()) var buf bytes.Buffer codeCfg := codeGenConfig{ - packagePath: parsingResult.packagePath, + packagePath: pResult.packagePath, typeName: "Data", - fields: parsingResult.fields, + fields: pResult.fields, } g.Expect(generateCode(&buf, codeCfg)).To(Succeed()) diff --git a/cmd/generator/scheme.go b/cmd/generator/scheme.go index 2d8294a..d87f3af 100644 --- a/cmd/generator/scheme.go +++ b/cmd/generator/scheme.go @@ -69,15 +69,16 @@ func generateScheme(writer io.Writer, cfg schemeGenConfig) error { var createSchemeFields func([]field) createSchemeFields = func(fields []field) { for _, f := range fields { - if f.slice { + switch { + case f.slice: schemeFields = append(schemeFields, schemeField{ Comment: f.docString, Type: fmt.Sprintf("union {null, array<%s>}", getAvroPrimitiveType(f.fieldType)), Name: f.name, }) - } else if f.embeddedStruct { + case f.embeddedStruct: createSchemeFields(f.embeddedStructFields) - } else { + default: schemeFields = append(schemeFields, schemeField{ Comment: f.docString, Type: getAvroPrimitiveType(f.fieldType) + "?", diff --git a/cmd/generator/scheme_test.go b/cmd/generator/scheme_test.go index b1b1f70..0b88335 100644 --- a/cmd/generator/scheme_test.go +++ b/cmd/generator/scheme_test.go @@ -24,7 +24,7 @@ func TestGenerateScheme(t *testing.T) { _ = tests.Data{} // depends on the type being defined - parsingResult, err := parse(parseCfg) + pResult, err := parse(parseCfg) g.Expect(err).ToNot(HaveOccurred()) @@ -35,7 +35,7 @@ func TestGenerateScheme(t *testing.T) { protocol: "avro", dataFabricDataType: "telemetry", record: parseCfg.typeName, - fields: parsingResult.fields, + fields: pResult.fields, } g.Expect(generateScheme(&buf, schemeCfg)).To(Succeed()) diff --git a/cmd/generator/tests/data_attributes_generated.go b/cmd/generator/tests/data_attributes_generated.go index 62445cb..48658f4 100644 --- a/cmd/generator/tests/data_attributes_generated.go +++ b/cmd/generator/tests/data_attributes_generated.go @@ -1,5 +1,6 @@ //go:build generator package tests + /* This is a generated file. DO NOT EDIT. */ @@ -7,16 +8,12 @@ This is a generated file. DO NOT EDIT. import ( "go.opentelemetry.io/otel/attribute" - "github.com/nginxinc/telemetry-exporter/pkg/telemetry" - ) func (d *Data) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue attrs = append(attrs, attribute.String("dataType", "ngf-product-telemetry")) - - attrs = append(attrs, attribute.String("SomeString", d.SomeString)) attrs = append(attrs, attribute.Int64("SomeInt", d.SomeInt)) attrs = append(attrs, attribute.Float64("SomeFloat", d.SomeFloat)) @@ -26,7 +23,6 @@ func (d *Data) Attributes() []attribute.KeyValue { attrs = append(attrs, attribute.Float64Slice("SomeFloats", d.SomeFloats)) attrs = append(attrs, attribute.BoolSlice("SomeBools", d.SomeBools)) attrs = append(attrs, d.AnotherData.Attributes()...) - return attrs } diff --git a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go index 755a4fa..a442f49 100644 --- a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go +++ b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go @@ -1,5 +1,6 @@ //go:build generator package subtests + /* This is a generated file. DO NOT EDIT. */ @@ -7,14 +8,11 @@ This is a generated file. DO NOT EDIT. import ( "go.opentelemetry.io/otel/attribute" - "github.com/nginxinc/telemetry-exporter/pkg/telemetry" - ) func (d *AnotherData) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - attrs = append(attrs, attribute.String("AnotherSomeString", d.AnotherSomeString)) attrs = append(attrs, attribute.Int64("AnotherSomeInt", d.AnotherSomeInt)) attrs = append(attrs, attribute.Float64("AnotherSomeFloat", d.AnotherSomeFloat)) @@ -23,7 +21,6 @@ func (d *AnotherData) Attributes() []attribute.KeyValue { attrs = append(attrs, attribute.Int64Slice("AnotherSomeInts", d.AnotherSomeInts)) attrs = append(attrs, attribute.Float64Slice("AnotherSomeFloats", d.AnotherSomeFloats)) attrs = append(attrs, attribute.BoolSlice("AnotherSomeBools", d.AnotherSomeBools)) - return attrs } diff --git a/cmd/generator/tests/telemetry/moredata_attributes_generated.go b/cmd/generator/tests/telemetry/moredata_attributes_generated.go index a7280ec..8bcd678 100644 --- a/cmd/generator/tests/telemetry/moredata_attributes_generated.go +++ b/cmd/generator/tests/telemetry/moredata_attributes_generated.go @@ -1,5 +1,6 @@ //go:build generator package telemetry + /* This is a generated file. DO NOT EDIT. */ @@ -7,16 +8,12 @@ This is a generated file. DO NOT EDIT. import ( "go.opentelemetry.io/otel/attribute" - ngxTelemetry "github.com/nginxinc/telemetry-exporter/pkg/telemetry" - ) func (d *MoreData) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - attrs = append(attrs, attribute.String("StringField", d.StringField)) - return attrs } diff --git a/pkg/telemetry/data_attributes_generated.go b/pkg/telemetry/data_attributes_generated.go index 56a7f9e..b13876f 100644 --- a/pkg/telemetry/data_attributes_generated.go +++ b/pkg/telemetry/data_attributes_generated.go @@ -1,18 +1,15 @@ - package telemetry + /* This is a generated file. DO NOT EDIT. */ import ( "go.opentelemetry.io/otel/attribute" - - ) func (d *Data) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - attrs = append(attrs, attribute.String("ProjectName", d.ProjectName)) attrs = append(attrs, attribute.String("ProjectVersion", d.ProjectVersion)) attrs = append(attrs, attribute.String("ProjectArchitecture", d.ProjectArchitecture)) @@ -21,7 +18,6 @@ func (d *Data) Attributes() []attribute.KeyValue { attrs = append(attrs, attribute.String("ClusterPlatform", d.ClusterPlatform)) attrs = append(attrs, attribute.String("InstallationID", d.InstallationID)) attrs = append(attrs, attribute.Int64("ClusterNodeCount", d.ClusterNodeCount)) - return attrs } diff --git a/tests/exporter_test.go b/tests/exporter_test.go index cd40c7d..1a8ebd7 100644 --- a/tests/exporter_test.go +++ b/tests/exporter_test.go @@ -103,7 +103,7 @@ var _ = Describe("Exporter", func() { ) BeforeEach(func() { - ctx := context.Background() + ctx = context.Background() // Run the collector container @@ -131,7 +131,7 @@ var _ = Describe("Exporter", func() { Cmd: []string{"--config=/" + collectorCfgName}, } - collector, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ + collector, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, })