Skip to content

Commit

Permalink
[mdatagen] move telemetry into its own file (#10037)
Browse files Browse the repository at this point in the history
This is in preparation for using mdatagen for component telemetry.

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten authored Apr 29, 2024
1 parent 1a5da25 commit ce09d97
Show file tree
Hide file tree
Showing 48 changed files with 1,301 additions and 177 deletions.
2 changes: 2 additions & 0 deletions cmd/mdatagen/embeded_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
path.Join(rootDir, "package_test.go.tmpl"): {},
path.Join(rootDir, "readme.md.tmpl"): {},
path.Join(rootDir, "status.go.tmpl"): {},
path.Join(rootDir, "telemetry.go.tmpl"): {},
path.Join(rootDir, "telemetry_test.go.tmpl"): {},
path.Join(rootDir, "testdata", "config.yaml.tmpl"): {},
}
count = 0
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func run(ymlPath string) error {
filepath.Join(codeDir, "generated_status.go"), md, "metadata"); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "telemetry.go.tmpl"),
filepath.Join(codeDir, "generated_telemetry.go"), md, "metadata"); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "telemetry_test.go.tmpl"),
filepath.Join(codeDir, "generated_telemetry_test.go"), md, "metadata"); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "component_test.go.tmpl"),
filepath.Join(ymlDir, "generated_component_test.go"), md, packageName); err != nil {
return err
Expand Down
90 changes: 77 additions & 13 deletions cmd/mdatagen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,6 @@ package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
var (
Expand All @@ -448,6 +446,80 @@ var (
const (
MetricsStability = component.StabilityLevelBeta
)
`,
},
{
name: "foo component with alpha status",
md: metadata{
Type: "foo",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelAlpha: {"metrics"},
},
Distributions: []string{"contrib"},
Class: "receiver",
},
},
expected: `// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"go.opentelemetry.io/collector/component"
)
var (
Type = component.MustNewType("foo")
)
const (
MetricsStability = component.StabilityLevelAlpha
)
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpdir := t.TempDir()
err := generateFile("templates/status.go.tmpl",
filepath.Join(tmpdir, "generated_status.go"), tt.md, "metadata")
require.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_status.go")) // nolint: gosec
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
}
}

func TestGenerateTelemetryMetadata(t *testing.T) {
tests := []struct {
name string
output string
md metadata
expected string
}{
{
name: "foo component with beta status",
md: metadata{
Type: "foo",
Status: &Status{
Stability: map[component.StabilityLevel][]string{
component.StabilityLevelBeta: {"metrics"},
},
Distributions: []string{"contrib"},
Class: "receiver",
},
},
expected: `// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
Expand Down Expand Up @@ -480,14 +552,6 @@ import (
"go.opentelemetry.io/otel/trace"
)
var (
Type = component.MustNewType("foo")
)
const (
MetricsStability = component.StabilityLevelAlpha
)
func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("")
}
Expand All @@ -502,10 +566,10 @@ func Tracer(settings component.TelemetrySettings) trace.Tracer {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpdir := t.TempDir()
err := generateFile("templates/status.go.tmpl",
filepath.Join(tmpdir, "generated_status.go"), tt.md, "metadata")
err := generateFile("templates/telemetry.go.tmpl",
filepath.Join(tmpdir, "generated_telemetry.go"), tt.md, "metadata")
require.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_status.go")) // nolint: gosec
actual, err := os.ReadFile(filepath.Join(tmpdir, "generated_telemetry.go")) // nolint: gosec
require.NoError(t, err)
require.Equal(t, tt.expected, string(actual))
})
Expand Down
10 changes: 0 additions & 10 deletions cmd/mdatagen/templates/status.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package {{ .Package }}

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)

var (
Expand All @@ -19,11 +17,3 @@ const (
{{- end }}
{{- end }}
)

func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("{{ .ScopeName }}")
}

func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("{{ .ScopeName }}")
}
17 changes: 17 additions & 0 deletions cmd/mdatagen/templates/telemetry.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Code generated by mdatagen. DO NOT EDIT.

package {{ .Package }}

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
)

func Meter(settings component.TelemetrySettings) metric.Meter {
return settings.MeterProvider.Meter("{{ .ScopeName }}")
}

func Tracer(settings component.TelemetrySettings) trace.Tracer {
return settings.TracerProvider.Tracer("{{ .ScopeName }}")
}
63 changes: 63 additions & 0 deletions cmd/mdatagen/templates/telemetry_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Code generated by mdatagen. DO NOT EDIT.

package {{ .Package }}

import (
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
embeddedmetric "go.opentelemetry.io/otel/metric/embedded"
noopmetric "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
embeddedtrace "go.opentelemetry.io/otel/trace/embedded"
nooptrace "go.opentelemetry.io/otel/trace/noop"

"go.opentelemetry.io/collector/component"
)

type mockMeter struct {
noopmetric.Meter
name string
}
type mockMeterProvider struct {
embeddedmetric.MeterProvider
}

func (m mockMeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter {
return mockMeter{name: name}
}

type mockTracer struct {
nooptrace.Tracer
name string
}

type mockTracerProvider struct {
embeddedtrace.TracerProvider
}

func (m mockTracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
return mockTracer{name: name}
}

func TestProviders(t *testing.T) {
set := component.TelemetrySettings{
MeterProvider: mockMeterProvider{},
TracerProvider: mockTracerProvider{},
}

meter := Meter(set)
if m, ok := meter.(mockMeter); ok {
require.Equal(t, "{{ .ScopeName }}", m.name)
} else {
require.Fail(t, "returned Meter not mockMeter")
}

tracer := Tracer(set)
if m, ok := tracer.(mockTracer); ok {
require.Equal(t, "{{ .ScopeName }}", m.name)
} else {
require.Fail(t, "returned Meter not mockTracer")
}
}
11 changes: 0 additions & 11 deletions connector/forwardconnector/internal/metadata/generated_status.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ce09d97

Please sign in to comment.