Skip to content

Commit

Permalink
Fix a bug when parsing a struct in the github.com/nginxinc/telemetry-…
Browse files Browse the repository at this point in the history
…exporter/pkg/telemetry package
  • Loading branch information
pleshakov committed Feb 28, 2024
1 parent 05d1255 commit 86bf656
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
25 changes: 21 additions & 4 deletions cmd/generator/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ This is a generated file. DO NOT EDIT.
import (
"go.opentelemetry.io/otel/attribute"
{{ if .TelemetryPackagePath }}"{{ .TelemetryPackagePath }}"{{ end }}
{{ if .TelemetryPackagePath }}
{{ if .TelemetryPackageAlias }}{{ .TelemetryPackageAlias }} {{ end -}}
"{{ .TelemetryPackagePath }}"
{{ end }}
)
func (d *{{ .StructName }}) Attributes() []attribute.KeyValue {
Expand All @@ -43,6 +46,7 @@ var _ {{ .ExportablePackagePrefix }}Exportable = (*{{ .StructName }})(nil)
type codeGen struct {
PackageName string
TelemetryPackagePath string
TelemetryPackageAlias string
ExportablePackagePrefix string
StructName string
BuildTags string
Expand Down Expand Up @@ -98,18 +102,31 @@ func generateCode(writer io.Writer, cfg codeGenConfig) error {
codeFields = append(codeFields, cf)
}

var telemetryPkg string
var exportablePkgPrefix string
const alias = "ngxTelemetry"

var (
telemetryPkg string
exportablePkgPrefix string
telemetryPkgAlias string
)

// check if we generate code for the type in the telemetry package or any other package
if cfg.packagePath != telemetryPackagePath {
telemetryPkg = telemetryPackagePath
exportablePkgPrefix = getPackageName(telemetryPackagePath) + "."

// if the name of the package is the same as the telemetry package, we need to use an alias
if getPackageName(cfg.packagePath) == getPackageName(telemetryPackagePath) {
exportablePkgPrefix = alias + "."
telemetryPkgAlias = alias
} else {
exportablePkgPrefix = getPackageName(telemetryPackagePath) + "."
}
}

cg := codeGen{
PackageName: getPackageName(cfg.packagePath),
ExportablePackagePrefix: exportablePkgPrefix,
TelemetryPackageAlias: telemetryPkgAlias,
TelemetryPackagePath: telemetryPkg,
StructName: cfg.typeName,
Fields: codeFields,
Expand Down
3 changes: 3 additions & 0 deletions cmd/generator/tests/data_attributes_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ 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 {
Expand Down
13 changes: 13 additions & 0 deletions cmd/generator/tests/telemetry/moredata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build generator

package telemetry

// MoreData is used to ensure that the generator produces the correct code for a struct in a package with the name
// 'telemetry'.
// Correctness is confirmed by the fact the generated code compiles.
//
//go:generate go run -tags generator github.com/nginxinc/telemetry-exporter/cmd/generator -type=MoreData -build-tags=generator
type MoreData struct {
// StringField is a string field.
StringField string
}
24 changes: 24 additions & 0 deletions cmd/generator/tests/telemetry/moredata_attributes_generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//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
}

var _ ngxTelemetry.Exportable = (*MoreData)(nil)
22 changes: 22 additions & 0 deletions pkg/telemetry/data_attributes_generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

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.Int64("Nodes", d.Nodes))


return attrs
}

var _ Exportable = (*Data)(nil)
12 changes: 12 additions & 0 deletions pkg/telemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// Data includes common telemetry data points.
// FIXME(pleshakov): Define the data points.
// Currently, only one data point is added, for the only reason that we can make sure the generator
// generates code for a struct defined in this package.
// https://github.com/nginxinc/telemetry-exporter/issues/8 will define the actual data points.
//
//go:generate go run -tags=generator github.com/nginxinc/telemetry-exporter/cmd/generator -type Data
type Data struct {
// Nodes is a number of nodes.
Nodes int64
}

// Exportable allows exporting telemetry data using the Exporter.
type Exportable interface {
// Attributes returns a list of key-value pairs that represent the telemetry data.
Expand Down

0 comments on commit 86bf656

Please sign in to comment.