Skip to content

Commit

Permalink
[chore] Cleanup calls to componenttest.SetupTelemetry (#12026)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jan 6, 2025
1 parent 1ebba3c commit 0d310fa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 50 deletions.
7 changes: 0 additions & 7 deletions component/componenttest/obsreporttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ type TestTelemetry struct {
}

// CheckExporterTraces checks that for the current exported values for trace exporter metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckExporterTraces(sentSpans, sendFailedSpans int64) error {
return checkExporterTraces(tts.reader, tts.id, sentSpans, sendFailedSpans)
}

// CheckExporterMetrics checks that for the current exported values for metrics exporter metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckExporterMetrics(sentMetricsPoints, sendFailedMetricsPoints int64) error {
return checkExporterMetrics(tts.reader, tts.id, sentMetricsPoints, sendFailedMetricsPoints)
}
Expand All @@ -64,7 +62,6 @@ func (tts *TestTelemetry) CheckExporterEnqueueFailedLogs(enqueueFailed int64) er
}

// CheckExporterLogs checks that for the current exported values for logs exporter metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckExporterLogs(sentLogRecords, sendFailedLogRecords int64) error {
return checkExporterLogs(tts.reader, tts.id, sentLogRecords, sendFailedLogRecords)
}
Expand All @@ -75,25 +72,21 @@ func (tts *TestTelemetry) CheckExporterMetricGauge(metric string, val int64, ext
}

// CheckReceiverTraces checks that for the current exported values for trace receiver metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckReceiverTraces(protocol string, acceptedSpans, droppedSpans int64) error {
return checkReceiverTraces(tts.reader, tts.id, protocol, acceptedSpans, droppedSpans)
}

// CheckReceiverLogs checks that for the current exported values for logs receiver metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckReceiverLogs(protocol string, acceptedLogRecords, droppedLogRecords int64) error {
return checkReceiverLogs(tts.reader, tts.id, protocol, acceptedLogRecords, droppedLogRecords)
}

// CheckReceiverMetrics checks that for the current exported values for metrics receiver metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckReceiverMetrics(protocol string, acceptedMetricPoints, droppedMetricPoints int64) error {
return checkReceiverMetrics(tts.reader, tts.id, protocol, acceptedMetricPoints, droppedMetricPoints)
}

// CheckScraperMetrics checks that for the current exported values for metrics scraper metrics match given values.
// Note: SetupTelemetry must be called before this function.
func (tts *TestTelemetry) CheckScraperMetrics(receiver component.ID, scraper component.ID, scrapedMetricPoints, erroredMetricPoints int64) error {
return checkScraperMetrics(tts.reader, receiver, scraper, scrapedMetricPoints, erroredMetricPoints)
}
Expand Down
44 changes: 7 additions & 37 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,23 @@ func TestNewDefaultServerConfig(t *testing.T) {
}

var (
componentID = component.MustNewID("component")
testAuthID = component.MustNewID("testauth")
mockID = component.MustNewID("mock")
doesntExistID = component.MustNewID("doesntexist")
)

func TestDefaultGrpcClientSettings(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

gcs := &ClientConfig{
TLSSetting: configtls.ClientConfig{
Insecure: true,
},
}
opts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
opts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToClientConnOption{})
require.NoError(t, err)
assert.Len(t, opts, 2)
}

func TestGrpcClientExtraOption(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

gcs := &ClientConfig{
TLSSetting: configtls.ClientConfig{
Insecure: true,
Expand All @@ -127,7 +118,7 @@ func TestGrpcClientExtraOption(t *testing.T) {
opts, err := gcs.getGrpcDialOptions(
context.Background(),
componenttest.NewNopHost(),
tt.TelemetrySettings(),
componenttest.NewNopTelemetrySettings(),
[]ToClientConnOption{WithGrpcDialOption(extraOpt)},
)
require.NoError(t, err)
Expand All @@ -136,10 +127,6 @@ func TestGrpcClientExtraOption(t *testing.T) {
}

func TestAllGrpcClientSettings(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

tests := []struct {
settings ClientConfig
name string
Expand Down Expand Up @@ -235,7 +222,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts, err := test.settings.getGrpcDialOptions(context.Background(), test.host, tt.TelemetrySettings(), []ToClientConnOption{})
opts, err := test.settings.getGrpcDialOptions(context.Background(), test.host, componenttest.NewNopTelemetrySettings(), []ToClientConnOption{})
require.NoError(t, err)
assert.Len(t, opts, 9)
})
Expand Down Expand Up @@ -496,18 +483,14 @@ func TestGRPCClientSettingsError(t *testing.T) {
}

func TestUseSecure(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

gcs := &ClientConfig{
Headers: nil,
Endpoint: "",
Compression: "",
TLSSetting: configtls.ClientConfig{},
Keepalive: nil,
}
dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings(), []ToClientConnOption{})
dialOpts, err := gcs.getGrpcDialOptions(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings(), []ToClientConnOption{})
require.NoError(t, err)
assert.Len(t, dialOpts, 2)
}
Expand Down Expand Up @@ -578,10 +561,6 @@ func TestGRPCServerSettings_ToListener_Error(t *testing.T) {
}

func TestHttpReception(t *testing.T) {
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

tests := []struct {
name string
tlsServerCreds *configtls.ServerConfig
Expand Down Expand Up @@ -709,7 +688,7 @@ func TestHttpReception(t *testing.T) {
Endpoint: ln.Addr().String(),
TLSSetting: *test.tlsClientCreds,
}
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
require.NoError(t, errClient)
defer func() { assert.NoError(t, grpcClientConn.Close()) }()
c := ptraceotlp.NewGRPCClient(grpcClientConn)
Expand All @@ -731,9 +710,6 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping test on windows")
}
tt, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

socketName := tempSocketName(t)
gss := &ServerConfig{
Expand All @@ -758,7 +734,7 @@ func TestReceiveOnUnixDomainSocket(t *testing.T) {
Insecure: true,
},
}
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tt.TelemetrySettings())
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
require.NoError(t, errClient)
defer func() { assert.NoError(t, grpcClientConn.Close()) }()
c := ptraceotlp.NewGRPCClient(grpcClientConn)
Expand Down Expand Up @@ -961,13 +937,7 @@ func TestClientInfoInterceptors(t *testing.T) {
},
}

tel, err := componenttest.SetupTelemetry(componentID)
require.NoError(t, err)
defer func() {
require.NoError(t, tel.Shutdown(context.Background()))
}()

grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), tel.TelemetrySettings())
grpcClientConn, errClient := gcs.ToClientConn(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
require.NoError(t, errClient)
defer func() { assert.NoError(t, grpcClientConn.Close()) }()

Expand Down
11 changes: 5 additions & 6 deletions scraper/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ func TestScrapeController(t *testing.T) {
},
}

for _, tt := range testCases {
test := tt
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
receiverID := component.MustNewID("receiver")
tt, err := componenttest.SetupTelemetry(receiverID)
Expand Down Expand Up @@ -179,9 +178,9 @@ func TestScrapeController(t *testing.T) {

spans := tt.SpanRecorder.Ended()
assertReceiverSpan(t, spans)
assertReceiverViews(t, tt, sink)
assertReceiverMetrics(t, tt, sink)
assertScraperSpan(t, test.scrapeErr, spans)
assertScraperViews(t, tt, test.scrapeErr, sink)
assertScraperMetrics(t, tt, test.scrapeErr, sink)
}

err = mr.Shutdown(context.Background())
Expand Down Expand Up @@ -263,7 +262,7 @@ func assertReceiverSpan(t *testing.T, spans []sdktrace.ReadOnlySpan) {
assert.True(t, receiverSpan)
}

func assertReceiverViews(t *testing.T, tt componenttest.TestTelemetry, sink *consumertest.MetricsSink) {
func assertReceiverMetrics(t *testing.T, tt componenttest.TestTelemetry, sink *consumertest.MetricsSink) {
dataPointCount := 0
for _, md := range sink.AllMetrics() {
dataPointCount += md.DataPointCount()
Expand Down Expand Up @@ -291,7 +290,7 @@ func assertScraperSpan(t *testing.T, expectedErr error, spans []sdktrace.ReadOnl
assert.True(t, scraperSpan)
}

func assertScraperViews(t *testing.T, tt componenttest.TestTelemetry, expectedErr error, sink *consumertest.MetricsSink) {
func assertScraperMetrics(t *testing.T, tt componenttest.TestTelemetry, expectedErr error, sink *consumertest.MetricsSink) {
expectedScraped := int64(sink.DataPointCount())
expectedErrored := int64(0)
if expectedErr != nil {
Expand Down

0 comments on commit 0d310fa

Please sign in to comment.