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

Align linters with other projects #186

Merged
merged 2 commits into from
Aug 12, 2024
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: 9 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ linters-settings:
gocyclo:
min-complexity: 15
govet:
enable:
- fieldalignment
enable-all: true
lll:
line-length: 120
dupword:
Expand All @@ -47,15 +46,21 @@ linters:
enable:
- asasalint
- asciicheck
- bidichk
- contextcheck
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exportloopref
- fatcontext
- forcetypeassert
- ginkgolinter
- gocheckcompilerdirectives
- gochecksumtype
- gocritic
- gocyclo
- godot
- gofmt
Expand All @@ -71,6 +76,7 @@ linters:
- loggercheck
- makezero
- misspell
- musttag
- nilerr
- noctx
- nolintlint
Expand All @@ -83,6 +89,7 @@ linters:
- spancheck
- staticcheck
- stylecheck
- tagalign
- tenv
- thelper
- tparallel
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
26 changes: 15 additions & 11 deletions cmd/generator/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,34 @@ 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 {
var attrs []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
}
Expand Down Expand Up @@ -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),
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/generator/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 4 additions & 3 deletions cmd/generator/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "?",
Expand Down
4 changes: 2 additions & 2 deletions cmd/generator/scheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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())
Expand Down
6 changes: 1 addition & 5 deletions cmd/generator/tests/data_attributes_generated.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//go:build generator
package tests

/*
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))
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
//go:build generator
package subtests

/*
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))
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//go:build generator
package telemetry

/*
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
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/telemetry/data_attributes_generated.go
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions tests/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var _ = Describe("Exporter", func() {
)

BeforeEach(func() {
ctx := context.Background()
ctx = context.Background()

// Run the collector container

Expand Down Expand Up @@ -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,
})
Expand Down