Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] [cmd/mdatagen] Remove dependency on an internal package #31520

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions cmd/mdatagen/templates/component_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package {{ .Package }}
import (
"context"
"testing"
{{- if or isExporter isProcessor }}
"time"
{{- end }}

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -36,9 +39,12 @@ import (
"go.opentelemetry.io/collector/connector/connectortest"
{{ end }}
"go.opentelemetry.io/collector/confmap/confmaptest"
{{ if or (isExporter) (isProcessor) }}
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
{{ end }}
{{- if or isExporter isProcessor }}
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
{{- end }}
)

// assertNoErrorHost implements a component.Host that asserts that there were no errors.
Expand Down Expand Up @@ -111,19 +117,19 @@ func TestComponentLifecycle(t *testing.T) {
require.NotPanics(t, func() {
switch e := c.(type) {
case exporter.Logs:
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
logs := generateLifecycleTestLogs()
if !e.Capabilities().MutatesData {
logs.MarkReadOnly()
}
err = e.ConsumeLogs(context.Background(), logs)
case exporter.Metrics:
metrics := testdata.GenerateMetricsTwoMetrics()
metrics := generateLifecycleTestMetrics()
if !e.Capabilities().MutatesData {
metrics.MarkReadOnly()
}
err = e.ConsumeMetrics(context.Background(), metrics)
case exporter.Traces:
traces := testdata.GenerateTracesTwoSpansSameResource()
traces := generateLifecycleTestTraces()
if !e.Capabilities().MutatesData {
traces.MarkReadOnly()
}
Expand Down Expand Up @@ -202,19 +208,19 @@ func TestComponentLifecycle(t *testing.T) {
require.NotPanics(t, func() {
switch e := c.(type) {
case processor.Logs:
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
logs := generateLifecycleTestLogs()
if !e.Capabilities().MutatesData {
logs.MarkReadOnly()
}
err = e.ConsumeLogs(context.Background(), logs)
case processor.Metrics:
metrics := testdata.GenerateMetricsTwoMetrics()
metrics := generateLifecycleTestMetrics()
if !e.Capabilities().MutatesData {
metrics.MarkReadOnly()
}
err = e.ConsumeMetrics(context.Background(), metrics)
case processor.Traces:
traces := testdata.GenerateTracesTwoSpansSameResource()
traces := generateLifecycleTestTraces()
if !e.Capabilities().MutatesData {
traces.MarkReadOnly()
}
Expand Down Expand Up @@ -449,4 +455,41 @@ func TestComponentLifecycle(t *testing.T) {
})
}
}
{{ end }}
{{ end }}

{{ if or isExporter isProcessor -}}
func generateLifecycleTestLogs() plog.Logs {
logs := plog.NewLogs()
rl := logs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("resource", "R1")
l := rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
l.Body().SetStr("test log message")
l.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return logs
}

func generateLifecycleTestMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
rm := metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("resource", "R1")
m := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
m.SetName("test_metric")
dp := m.SetEmptyGauge().DataPoints().AppendEmpty()
dp.Attributes().PutStr("test_attr", "value_1")
dp.SetIntValue(123)
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return metrics
}

func generateLifecycleTestTraces() ptrace.Traces {
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().PutStr("resource", "R1")
span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.Attributes().PutStr("test_attr", "value_1")
span.SetName("test_span")
span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(-1 * time.Second)))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return traces
}
{{- end }}
47 changes: 43 additions & 4 deletions cmd/otelcontribcol/exporters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -20,6 +21,10 @@ import (
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/exporter/otlpexporter"
"go.opentelemetry.io/collector/exporter/otlphttpexporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter"
Expand Down Expand Up @@ -62,7 +67,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/tencentcloudlogserviceexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/zipkinexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
)

func TestDefaultExporters(t *testing.T) {
Expand Down Expand Up @@ -631,19 +635,19 @@ func verifyExporterLifecycle(t *testing.T, factory exporter.Factory, getConfigFn
assert.NotPanics(t, func() {
switch e := exp.(type) {
case exporter.Logs:
logs := testdata.GenerateLogsManyLogRecordsSameResource(2)
logs := generateTestLogs()
if !e.Capabilities().MutatesData {
logs.MarkReadOnly()
}
err = e.ConsumeLogs(ctx, logs)
case exporter.Metrics:
metrics := testdata.GenerateMetricsTwoMetrics()
metrics := generateTestMetrics()
if !e.Capabilities().MutatesData {
metrics.MarkReadOnly()
}
err = e.ConsumeMetrics(ctx, metrics)
case exporter.Traces:
traces := testdata.GenerateTracesTwoSpansSameResource()
traces := generateTestTraces()
if !e.Capabilities().MutatesData {
traces.MarkReadOnly()
}
Expand All @@ -658,6 +662,41 @@ func verifyExporterLifecycle(t *testing.T, factory exporter.Factory, getConfigFn
}
}

func generateTestLogs() plog.Logs {
logs := plog.NewLogs()
rl := logs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("resource", "R1")
l := rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
l.Body().SetStr("test log message")
l.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return logs
}

func generateTestMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
rm := metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("resource", "R1")
m := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
m.SetName("test_metric")
dp := m.Gauge().DataPoints().AppendEmpty()
dp.Attributes().PutStr("test_attr", "value_1")
dp.SetIntValue(123)
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return metrics
}

func generateTestTraces() ptrace.Traces {
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().PutStr("resource", "R1")
span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.Attributes().PutStr("test_attr", "value_1")
span.SetName("test_span")
span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(-1 * time.Second)))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return traces
}

// verifyExporterShutdown is used to test if an exporter type can be shutdown without being started first.
func verifyExporterShutdown(tb testing.TB, factory exporter.Factory, getConfigFn getExporterConfigFn) {
ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/dbstorage v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/filestorage v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor v0.95.0
Expand Down Expand Up @@ -210,6 +209,7 @@ require (
go.opentelemetry.io/collector/extension/ballastextension v0.95.0
go.opentelemetry.io/collector/extension/zpagesextension v0.95.0
go.opentelemetry.io/collector/otelcol v0.95.0
go.opentelemetry.io/collector/pdata v1.2.0
go.opentelemetry.io/collector/processor v0.95.0
go.opentelemetry.io/collector/processor/batchprocessor v0.95.0
go.opentelemetry.io/collector/processor/memorylimiterprocessor v0.95.0
Expand Down Expand Up @@ -536,6 +536,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.95.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.95.0 // indirect
Expand Down Expand Up @@ -665,7 +666,6 @@ require (
go.opentelemetry.io/collector/confmap/provider/yamlprovider v0.95.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.95.0 // indirect
go.opentelemetry.io/collector/featuregate v1.2.0 // indirect
go.opentelemetry.io/collector/pdata v1.2.0 // indirect
go.opentelemetry.io/collector/semconv v0.95.0 // indirect
go.opentelemetry.io/collector/service v0.95.0 // indirect
go.opentelemetry.io/contrib/config v0.3.0 // indirect
Expand Down
48 changes: 43 additions & 5 deletions exporter/alertmanagerexporter/generated_component_test.go

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

9 changes: 0 additions & 9 deletions exporter/alertmanagerexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.95.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.95.0
github.com/prometheus/common v0.46.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.95.0
Expand Down Expand Up @@ -83,11 +82,3 @@ retract (
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden
Loading
Loading