Skip to content

Commit 306c939

Browse files
authored
Deprecate component_test in favor of metadatatest (#11812)
The main idea behind this is that the test helpers were generated in the main package, hard to find and understand and also impossible to use outside the main package, see the [service/internal/proctelemetry/process_telemetry_test.go](https://github.com/open-telemetry/opentelemetry-collector/compare/main...bogdandrutu:opentelemetry-collector:dep-comp-test?expand=1#diff-ed642eb1c968a9308a07e87292f77af1e0830a7af2d87111c5abacde4fc87446). Signed-off-by: Bogdan Drutu <[email protected]>
1 parent d143b4c commit 306c939

File tree

48 files changed

+1801
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1801
-181
lines changed

.chloggen/dep-comp-test.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: deprecation
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: mdatagen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Deprecate component_test in favor of metadatatest
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [11812]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

cmd/mdatagen/internal/command.go

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func run(ymlPath string) error {
7878
if err = os.MkdirAll(codeDir, 0o700); err != nil {
7979
return fmt.Errorf("unable to create output directory %q: %w", codeDir, err)
8080
}
81+
testDir := filepath.Join(ymlDir, "internal", md.GeneratedPackageName+"test")
82+
if err = os.MkdirAll(testDir, 0o700); err != nil {
83+
return fmt.Errorf("unable to create output test directory %q: %w", codeDir, err)
84+
}
8185
if md.Status != nil {
8286
if md.Status.Class != "cmd" && md.Status.Class != "pkg" && !md.Status.NotComponent {
8387
if err = generateFile(filepath.Join(tmplDir, "status.go.tmpl"),
@@ -114,6 +118,8 @@ func run(ymlPath string) error {
114118
}
115119
toGenerate[filepath.Join(tmplDir, "telemetry.go.tmpl")] = filepath.Join(codeDir, "generated_telemetry.go")
116120
toGenerate[filepath.Join(tmplDir, "telemetry_test.go.tmpl")] = filepath.Join(codeDir, "generated_telemetry_test.go")
121+
toGenerate[filepath.Join(tmplDir, "telemetrytest.go.tmpl")] = filepath.Join(testDir, "generated_telemetrytest.go")
122+
toGenerate[filepath.Join(tmplDir, "telemetrytest_test.go.tmpl")] = filepath.Join(testDir, "generated_telemetrytest_test.go")
117123
}
118124

119125
if len(md.Metrics) != 0 || len(md.Telemetry.Metrics) != 0 || len(md.ResourceAttributes) != 0 { // if there's metrics or internal metrics, generate documentation for them

cmd/mdatagen/internal/embedded_templates_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func TestEnsureTemplatesLoaded(t *testing.T) {
3636
path.Join(rootDir, "telemetry.go.tmpl"): {},
3737
path.Join(rootDir, "telemetry_test.go.tmpl"): {},
3838
path.Join(rootDir, "testdata", "config.yaml.tmpl"): {},
39+
path.Join(rootDir, "telemetrytest.go.tmpl"): {},
40+
path.Join(rootDir, "telemetrytest_test.go.tmpl"): {},
3941
}
4042
count = 0
4143
)

cmd/mdatagen/internal/samplereceiver/generated_component_telemetry_test.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_telemetry.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadatatest/generated_telemetrytest.go

+83
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadatatest/generated_telemetrytest_test.go

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/metrics_test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99

1010
"github.com/stretchr/testify/require"
1111
"go.opentelemetry.io/otel/sdk/metric/metricdata"
12+
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
1213

1314
"go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver/internal/metadata"
15+
"go.opentelemetry.io/collector/cmd/mdatagen/internal/samplereceiver/internal/metadatatest"
1416
"go.opentelemetry.io/collector/component/componenttest"
1517
"go.opentelemetry.io/collector/consumer/consumertest"
1618
"go.opentelemetry.io/collector/receiver/receivertest"
@@ -24,11 +26,11 @@ func TestGeneratedMetrics(t *testing.T) {
2426
}
2527

2628
func TestComponentTelemetry(t *testing.T) {
27-
tt := setupTestTelemetry()
29+
tt := metadatatest.SetupTelemetry()
2830
factory := NewFactory()
2931
receiver, err := factory.CreateMetrics(context.Background(), tt.NewSettings(), componenttest.NewNopHost(), new(consumertest.MetricsSink))
3032
require.NoError(t, err)
31-
tt.assertMetrics(t, []metricdata.Metrics{
33+
tt.AssertMetrics(t, []metricdata.Metrics{
3234
{
3335
Name: "otelcol_batch_size_trigger_send",
3436
Description: "Number of times the batch was sent due to a size trigger [deprecated since v0.110.0]",
@@ -57,11 +59,11 @@ func TestComponentTelemetry(t *testing.T) {
5759
},
5860
},
5961
},
60-
})
62+
}, metricdatatest.IgnoreTimestamp())
6163
rcv, ok := receiver.(nopReceiver)
6264
require.True(t, ok)
6365
rcv.initOptionalMetric()
64-
tt.assertMetrics(t, []metricdata.Metrics{
66+
tt.AssertMetrics(t, []metricdata.Metrics{
6567
{
6668
Name: "otelcol_batch_size_trigger_send",
6769
Description: "Number of times the batch was sent due to a size trigger [deprecated since v0.110.0]",
@@ -102,6 +104,6 @@ func TestComponentTelemetry(t *testing.T) {
102104
},
103105
},
104106
},
105-
})
107+
}, metricdatatest.IgnoreTimestamp())
106108
require.NoError(t, tt.Shutdown(context.Background()))
107109
}

cmd/mdatagen/internal/templates/component_telemetry_test.go.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
{{- end }}
2424
)
2525

26+
// Deprecated: [v0.117.0] use metadatatest.Telemetry
2627
type componentTestTelemetry struct {
2728
reader *sdkmetric.ManualReader
2829
meterProvider *sdkmetric.MeterProvider
@@ -44,6 +45,7 @@ func (tt *componentTestTelemetry) newTelemetrySettings() component.TelemetrySett
4445
return set
4546
}
4647

48+
// Deprecated: [v0.116.0] use metadatatest.SetupTelemetry
4749
func setupTestTelemetry() componentTestTelemetry {
4850
reader := sdkmetric.NewManualReader()
4951
return componentTestTelemetry{

cmd/mdatagen/internal/templates/telemetry.go.tmpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
{{- end }}
1515

1616
"go.opentelemetry.io/otel/metric"
17+
{{- if .Telemetry.Metrics }}
18+
noopmetric "go.opentelemetry.io/otel/metric/noop"
19+
{{- end }}
1720
"go.opentelemetry.io/otel/trace"
1821

1922
"go.opentelemetry.io/collector/component"
@@ -128,7 +131,7 @@ func getLeveledMeter(meter metric.Meter, cfgLevel, srvLevel configtelemetry.Leve
128131
if cfgLevel <= srvLevel {
129132
return meter
130133
}
131-
return noop.Meter{}
134+
return noopmetric.Meter{}
132135
}
133136

134137
{{- end }}

cmd/mdatagen/internal/templates/telemetry_test.go.tmpl

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"go.opentelemetry.io/collector/component"
1717
"go.opentelemetry.io/collector/component/componenttest"
18-
"go.opentelemetry.io/collector/config/configtelemetry"
1918
)
2019

2120
type mockMeter struct {

0 commit comments

Comments
 (0)