From 938b04194cf0190ed5a627fcfefbd504ccda96e6 Mon Sep 17 00:00:00 2001 From: Michael Pleshakov Date: Wed, 6 Mar 2024 17:20:56 -0500 Subject: [PATCH 1/2] Fix missing attribute sent along with the data Problem: F5 telemetry service requires the dataType attribute present, which must be equal to the `@df_datatype` annotation in the scheme. Solution: Add dataType attribute to the generated Attributes() method. Note: the attribute only required for the struct for which we generate the scheme. In the Attribute() method of any embedded structs, dataType attribute must not be set. CLOSES https://github.com/nginxinc/telemetry-exporter/issues/39 --- cmd/generator/code.go | 15 +++++++++++---- cmd/generator/main.go | 9 +++++---- cmd/generator/tests/data_attributes_generated.go | 4 ++++ cmd/generator/tests/data_test.go | 2 ++ .../subtests/anotherdata_attributes_generated.go | 2 ++ .../telemetry/moredata_attributes_generated.go | 2 ++ pkg/telemetry/data_attributes_generated.go | 2 ++ 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cmd/generator/code.go b/cmd/generator/code.go index 2831438..8559ce4 100644 --- a/cmd/generator/code.go +++ b/cmd/generator/code.go @@ -33,6 +33,10 @@ import ( func (d *{{ .StructName }}) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue + {{ if .SchemeDataType }} + attrs = append(attrs, attribute.String("dataType", "{{ .SchemeDataType }}")) + {{ end }} + {{ range .Fields -}} attrs = append(attrs, {{ .AttributesSource }}) {{ end }} @@ -49,6 +53,7 @@ type codeGen struct { TelemetryPackageAlias string ExportablePackagePrefix string StructName string + SchemeDataType string BuildTags string Fields []codeField } @@ -73,10 +78,11 @@ func getAttributeType(kind types.BasicKind) string { } type codeGenConfig struct { - packagePath string - typeName string - buildTags string - fields []field + packagePath string + typeName string + schemeDataType string + buildTags string + fields []field } func generateCode(writer io.Writer, cfg codeGenConfig) error { @@ -129,6 +135,7 @@ func generateCode(writer io.Writer, cfg codeGenConfig) error { TelemetryPackageAlias: telemetryPkgAlias, TelemetryPackagePath: telemetryPkg, StructName: cfg.typeName, + SchemeDataType: cfg.schemeDataType, Fields: codeFields, BuildTags: cfg.buildTags, } diff --git a/cmd/generator/main.go b/cmd/generator/main.go index eeab66b..44a03bc 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -92,10 +92,11 @@ func main() { } codeCfg := codeGenConfig{ - packagePath: result.packagePath, - typeName: *typeName, - fields: result.fields, - buildTags: codeGenBuildTags, + packagePath: result.packagePath, + typeName: *typeName, + schemeDataType: *schemeDataFabricDataType, + fields: result.fields, + buildTags: codeGenBuildTags, } if err := generateCode(file, codeCfg); err != nil { diff --git a/cmd/generator/tests/data_attributes_generated.go b/cmd/generator/tests/data_attributes_generated.go index f58e099..f34bcee 100644 --- a/cmd/generator/tests/data_attributes_generated.go +++ b/cmd/generator/tests/data_attributes_generated.go @@ -15,6 +15,10 @@ import ( 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)) diff --git a/cmd/generator/tests/data_test.go b/cmd/generator/tests/data_test.go index ab646e3..860a4cf 100644 --- a/cmd/generator/tests/data_test.go +++ b/cmd/generator/tests/data_test.go @@ -36,6 +36,7 @@ func TestData_Attributes(t *testing.T) { } expectedAttributes := []attribute.KeyValue{ + attribute.String("dataType", "ngf-product-telemetry"), attribute.String("SomeString", "some string"), attribute.Int64("SomeInt", 42), attribute.Float64("SomeFloat", 3.14), @@ -65,6 +66,7 @@ func TestData_AttributesEmpty(t *testing.T) { data := Data{} expectedAttributes := []attribute.KeyValue{ + attribute.String("dataType", "ngf-product-telemetry"), attribute.String("SomeString", ""), attribute.Int64("SomeInt", 0), attribute.Float64("SomeFloat", 0), diff --git a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go index 755a4fa..d55106f 100644 --- a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go +++ b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go @@ -15,6 +15,8 @@ import ( 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)) diff --git a/cmd/generator/tests/telemetry/moredata_attributes_generated.go b/cmd/generator/tests/telemetry/moredata_attributes_generated.go index a7280ec..89bd12e 100644 --- a/cmd/generator/tests/telemetry/moredata_attributes_generated.go +++ b/cmd/generator/tests/telemetry/moredata_attributes_generated.go @@ -15,6 +15,8 @@ import ( func (d *MoreData) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue + + attrs = append(attrs, attribute.String("StringField", d.StringField)) diff --git a/pkg/telemetry/data_attributes_generated.go b/pkg/telemetry/data_attributes_generated.go index 56a7f9e..06bbf16 100644 --- a/pkg/telemetry/data_attributes_generated.go +++ b/pkg/telemetry/data_attributes_generated.go @@ -13,6 +13,8 @@ import ( 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)) From c429671f316ad49e8c76ed3069857504888bdeac Mon Sep 17 00:00:00 2001 From: Michael Pleshakov Date: Wed, 6 Mar 2024 17:32:08 -0500 Subject: [PATCH 2/2] Trim whitespace a bit --- cmd/generator/code.go | 2 +- cmd/generator/tests/data_attributes_generated.go | 2 -- .../tests/subtests/anotherdata_attributes_generated.go | 2 -- cmd/generator/tests/telemetry/moredata_attributes_generated.go | 2 -- pkg/telemetry/data_attributes_generated.go | 2 -- 5 files changed, 1 insertion(+), 9 deletions(-) diff --git a/cmd/generator/code.go b/cmd/generator/code.go index 8559ce4..8fe92be 100644 --- a/cmd/generator/code.go +++ b/cmd/generator/code.go @@ -33,7 +33,7 @@ import ( func (d *{{ .StructName }}) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - {{ if .SchemeDataType }} + {{- if .SchemeDataType }} attrs = append(attrs, attribute.String("dataType", "{{ .SchemeDataType }}")) {{ end }} diff --git a/cmd/generator/tests/data_attributes_generated.go b/cmd/generator/tests/data_attributes_generated.go index f34bcee..62445cb 100644 --- a/cmd/generator/tests/data_attributes_generated.go +++ b/cmd/generator/tests/data_attributes_generated.go @@ -14,8 +14,6 @@ import ( func (d *Data) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - - attrs = append(attrs, attribute.String("dataType", "ngf-product-telemetry")) diff --git a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go index d55106f..755a4fa 100644 --- a/cmd/generator/tests/subtests/anotherdata_attributes_generated.go +++ b/cmd/generator/tests/subtests/anotherdata_attributes_generated.go @@ -15,8 +15,6 @@ import ( 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)) diff --git a/cmd/generator/tests/telemetry/moredata_attributes_generated.go b/cmd/generator/tests/telemetry/moredata_attributes_generated.go index 89bd12e..a7280ec 100644 --- a/cmd/generator/tests/telemetry/moredata_attributes_generated.go +++ b/cmd/generator/tests/telemetry/moredata_attributes_generated.go @@ -15,8 +15,6 @@ import ( func (d *MoreData) Attributes() []attribute.KeyValue { var attrs []attribute.KeyValue - - attrs = append(attrs, attribute.String("StringField", d.StringField)) diff --git a/pkg/telemetry/data_attributes_generated.go b/pkg/telemetry/data_attributes_generated.go index 06bbf16..56a7f9e 100644 --- a/pkg/telemetry/data_attributes_generated.go +++ b/pkg/telemetry/data_attributes_generated.go @@ -13,8 +13,6 @@ import ( 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))