Debian Codegraph @

processor/processortest/shutdown_verifier.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package processortest // import "go.opentelemetry.io/collector/processor/processortest"
5
6 import (
7 "context"
8 "errors"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "go.opentelemetry.io/collector/component"
15 "go.opentelemetry.io/collector/component/componenttest"
16 "go.opentelemetry.io/collector/consumer/consumertest"
17 "go.opentelemetry.io/collector/pdata/testdata"
18 "go.opentelemetry.io/collector/pipeline"
19 "go.opentelemetry.io/collector/processor"
20 )
21
22 func verifyTracesDoesNotProduceAfterShutdown(t *testing.T, factory processor.Factory, cfg component.Config) {
23 // Create a proc and output its produce to a sink.
24 nextSink := new(consumertest.TracesSink)
25 proc, err := factory.CreateTraces(context.Background(), NewNopSettings(factory.Type()), cfg, nextSink)
26 if errors.Is(err, pipeline.ErrSignalNotSupported) {
27 return
28 }
29 require.NoError(t, err)
30 assert.NoError(t, proc.Start(context.Background(), componenttest.NewNopHost()))
31
32 // Send some traces to the proc.
33 const generatedCount = 10
34 for range generatedCount {
35 require.NoError(t, proc.ConsumeTraces(context.Background(), testdata.GenerateTraces(1)))
36 }
37
38 // Now shutdown the proc.
39 assert.NoError(t, proc.Shutdown(context.Background()))
40
41 // The Shutdown() is done. It means the proc must have sent everything we
42 // gave it to the next sink.
43 assert.Equal(t, generatedCount, nextSink.SpanCount())
44 }
45
46 func verifyLogsDoesNotProduceAfterShutdown(t *testing.T, factory processor.Factory, cfg component.Config) {
47 // Create a proc and output its produce to a sink.
48 nextSink := new(consumertest.LogsSink)
49 proc, err := factory.CreateLogs(context.Background(), NewNopSettings(factory.Type()), cfg, nextSink)
50 if errors.Is(err, pipeline.ErrSignalNotSupported) {
51 return
52 }
53 require.NoError(t, err)
54 assert.NoError(t, proc.Start(context.Background(), componenttest.NewNopHost()))
55
56 // Send some logs to the proc.
57 const generatedCount = 10
58 for range generatedCount {
59 require.NoError(t, proc.ConsumeLogs(context.Background(), testdata.GenerateLogs(1)))
60 }
61
62 // Now shutdown the proc.
63 assert.NoError(t, proc.Shutdown(context.Background()))
64
65 // The Shutdown() is done. It means the proc must have sent everything we
66 // gave it to the next sink.
67 assert.Equal(t, generatedCount, nextSink.LogRecordCount())
68 }
69
70 func verifyMetricsDoesNotProduceAfterShutdown(t *testing.T, factory processor.Factory, cfg component.Config) {
71 // Create a proc and output its produce to a sink.
72 nextSink := new(consumertest.MetricsSink)
73 proc, err := factory.CreateMetrics(context.Background(), NewNopSettings(factory.Type()), cfg, nextSink)
74 if errors.Is(err, pipeline.ErrSignalNotSupported) {
75 return
76 }
77 require.NoError(t, err)
78 assert.NoError(t, proc.Start(context.Background(), componenttest.NewNopHost()))
79
80 // Send some metrics to the proc. testdata.GenerateMetrics creates metrics with 2 data points each.
81 const generatedCount = 10
82 for range generatedCount {
83 require.NoError(t, proc.ConsumeMetrics(context.Background(), testdata.GenerateMetrics(1)))
84 }
85
86 // Now shutdown the proc.
87 assert.NoError(t, proc.Shutdown(context.Background()))
88
89 // The Shutdown() is done. It means the proc must have sent everything we
90 // gave it to the next sink.
91 assert.Equal(t, generatedCount*2, nextSink.DataPointCount())
92 }
93
94 // VerifyShutdown verifies the processor doesn't produce telemetry data after shutdown.
95 func VerifyShutdown(t *testing.T, factory processor.Factory, cfg component.Config) {
96 verifyTracesDoesNotProduceAfterShutdown(t, factory, cfg)
97 verifyLogsDoesNotProduceAfterShutdown(t, factory, cfg)
98 verifyMetricsDoesNotProduceAfterShutdown(t, factory, cfg)
99 }