8000 Fix a bug when parsing a struct in the github.com/nginxinc/telemetry-… · nginx/telemetry-exporter@86bf656 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86bf656

Browse files
committed
Fix a bug when parsing a struct in the github.com/nginxinc/telemetry-exporter/pkg/telemetry package
1 parent 05d1255 commit 86bf656

File tree

7 files changed

+98
-4
lines changed

7 files changed

+98
-4
lines changed

cmd/generator/code.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ This is a generated file. DO NOT EDIT.
2424
import (
2525
"go.opentelemetry.io/otel/attribute"
2626
27-
{{ if .TelemetryPackagePath }}"{{ .TelemetryPackagePath }}"{{ end }}
27+
{{ if .TelemetryPackagePath }}
28+
{{ if .TelemetryPackageAlias }}{{ .TelemetryPackageAlias }} {{ end -}}
29+
"{{ .TelemetryPackagePath }}"
30+
{{ end }}
2831
)
2932
3033
func (d *{{ .StructName }}) Attributes() []attribute.KeyValue {
@@ -43,6 +46,7 @@ var _ {{ .ExportablePackagePrefix }}Exportable = (*{{ .StructName }})(nil)
4346
type codeGen struct {
4447
PackageName string
4548
TelemetryPackagePath string
49+
TelemetryPackageAlias string
4650
ExportablePackagePrefix string
4751
StructName string
4852
BuildTags string
@@ -98,18 +102,31 @@ func generateCode(writer io.Writer, cfg codeGenConfig) error {
98102
codeFields = append(codeFields, cf)
99103
}
100104

101-
var telemetryPkg string
102-
var exportablePkgPrefix string
105+
const alias = "ngxTelemetry"
106+
107+
var (
108+
telemetryPkg string
109+
exportablePkgPrefix string
110+
telemetryPkgAlias string
111+
)
103112

104113
// check if we generate code for the type in the telemetry package or any other package
105114
if cfg.packagePath != telemetryPackagePath {
106115
telemetryPkg = telemetryPackagePath
107-
exportablePkgPrefix = getPackageName(telemetryPackagePath) + "."
116+
117+
// if the name of the package is the same as the telemetry package, we need to use an alias
118+
if getPackageName(cfg.packagePath) == getPackageName(telemetryPackagePath) {
119+
exportablePkgPrefix = alias + "."
120+
telemetryPkgAlias = alias
121+
} else {
122+
exportablePkgPrefix = getPackageName(telemetryPackagePath) + "."
123+
}
108124
}
109125

110126
cg := codeGen{
111127
PackageName: getPackageName(cfg.packagePath),
112128
ExportablePackagePrefix: exportablePkgPrefix,
129+
TelemetryPackageAlias: telemetryPkgAlias,
113130
TelemetryPackagePath: telemetryPkg,
114131
StructName: cfg.typeName,
115132
Fields: codeFields,

cmd/generator/tests/data_attributes_generated.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ This is a generated file. DO NOT EDIT.
66

77
import (
88
"go.opentelemetry.io/otel/attribute"
9+
10+
911
"github.com/nginxinc/telemetry-exporter/pkg/telemetry"
12+
1013
)
1114

1215
func (d *Data) Attributes() []attribute.KeyValue {

cmd/generator/tests/subtests/anotherdata_attributes_generated.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ This is a generated file. DO NOT EDIT.
66

77
import (
88
"go.opentelemetry.io/otel/attribute"
9+
10+
911
"github.com/nginxinc/telemetry-exporter/pkg/telemetry"
12+
1013
)
1114

1215
func (d *AnotherData) Attributes() []attribute.KeyValue {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build generator
2+
3+
package telemetry
4+
5+
// MoreData is used to ensure that the generator produces the correct code for a struct in a package with the name
6+
// 'telemetry'.
7+
// Correctness is confirmed by the fact the generated code compiles.
8+
//
9+
//go:generate go run -tags generator github.com/nginxinc/telemetry-exporter/cmd/generator -type=MoreData -build-tags=generator
10+
type MoreData struct {
11+
// StringField is a string field.
12+
StringField string
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build generator
2+
package telemetry
3+
/*
4+
This is a generated file. DO NOT EDIT.
5+
*/
6+
7+
import (
8+
"go.opentelemetry.io/otel/attribute"
9+
10+
11+
ngxTelemetry "github.com/nginxinc/telemetry-exporter/pkg/telemetry"
12+
13+
)
14+
15+
func (d *MoreData) Attributes() []attribute.KeyValue {
16+
var attrs []attribute.KeyValue
17+
18+
attrs = append(attrs, attribute.String("StringField", d.StringField))
19+
20+
21+
return attrs
22+
}
23+
24+
var _ ngxTelemetry.Exportable = (*MoreData)(nil)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package telemetry
3+
/*
4+
This is a generated file. DO NOT EDIT.
5+
*/
6+
7+
import (
8+
"go.opentelemetry.io/otel/attribute"
9+
10+
11+
)
12+
13+
func (d *Data) Attributes() []attribute.KeyValue {
14+
var attrs []attribute.KeyValue
15+
16+
attrs = append(attrs, attribute.Int64("Nodes", d.Nodes))
17+
18+
19+
return attrs
20+
}
21+
22+
var _ Exportable = (*Data)(nil)

pkg/telemetry/exporter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import (
1111
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1212
)
1313

14+
// Data includes common telemetry data points.
15+
// FIXME(pleshakov): Define the data points.
16+
// Currently, only one data point is added, for the only reason that we can make sure the generator
17+
// generates code for a struct defined in this package.
18+
// https://github.com/nginxinc/telemetry-exporter/issues/8 will define the actual data points.
19+
//
20+
//go:generate go run -tags=generator github.com/nginxinc/telemetry-exporter/cmd/generator -type Data
21+
type Data struct {
22+
// Nodes is a number of nodes.
23+
Nodes int64
24+
}
25+
1426
// Exportable allows exporting telemetry data using the Exporter.
1527
type Exportable interface {
1628
// Attributes returns a list of key-value pairs that represent the telemetry data.

0 commit comments

Comments
 (0)
0