Debian Codegraph @

processor/processorhelper/xprocessorhelper/profiles_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package xprocessorhelper
5
6 import (
7 "context"
8 "errors"
9 "sync"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "go.opentelemetry.io/collector/component"
16 "go.opentelemetry.io/collector/component/componenttest"
17 "go.opentelemetry.io/collector/consumer"
18 "go.opentelemetry.io/collector/consumer/consumertest"
19 "go.opentelemetry.io/collector/pdata/pprofile"
20 "go.opentelemetry.io/collector/processor/processorhelper"
21 "go.opentelemetry.io/collector/processor/processortest"
22 )
23
24 var testProfilesCfg = struct{}{}
25
26 func TestNewProfiles(t *testing.T) {
27 pp, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), newTestPProcessor(nil))
28 require.NoError(t, err)
29
30 assert.True(t, pp.Capabilities().MutatesData)
31 assert.NoError(t, pp.Start(context.Background(), componenttest.NewNopHost()))
32 assert.NoError(t, pp.ConsumeProfiles(context.Background(), pprofile.NewProfiles()))
33 assert.NoError(t, pp.Shutdown(context.Background()))
34 }
35
36 func TestNewProfiles_WithOptions(t *testing.T) {
37 want := errors.New("my_error")
38 pp, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), newTestPProcessor(nil),
39 WithStart(func(context.Context, component.Host) error { return want }),
40 WithShutdown(func(context.Context) error { return want }),
41 WithCapabilities(consumer.Capabilities{MutatesData: false}))
42 require.NoError(t, err)
43
44 assert.Equal(t, want, pp.Start(context.Background(), componenttest.NewNopHost()))
45 assert.Equal(t, want, pp.Shutdown(context.Background()))
46 assert.False(t, pp.Capabilities().MutatesData)
47 }
48
49 func TestNewProfiles_NilRequiredFields(t *testing.T) {
50 _, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), nil)
51 assert.Error(t, err)
52 }
53
54 func TestNewProfiles_ProcessProfileError(t *testing.T) {
55 want := errors.New("my_error")
56 pp, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), newTestPProcessor(want))
57 require.NoError(t, err)
58 assert.Equal(t, want, pp.ConsumeProfiles(context.Background(), pprofile.NewProfiles()))
59 }
60
61 func TestNewProfiles_ProcessProfilesErrSkipProcessingData(t *testing.T) {
62 pp, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), newTestPProcessor(processorhelper.ErrSkipProcessingData))
63 require.NoError(t, err)
64 assert.NoError(t, pp.ConsumeProfiles(context.Background(), pprofile.NewProfiles()))
65 }
66
67 func newTestPProcessor(retError error) ProcessProfilesFunc {
68 return func(_ context.Context, pd pprofile.Profiles) (pprofile.Profiles, error) {
69 return pd, retError
70 }
71 }
72
73 func TestProfilesConcurrency(t *testing.T) {
74 profilesFunc := func(_ context.Context, pd pprofile.Profiles) (pprofile.Profiles, error) {
75 return pd, nil
76 }
77
78 incomingProfiles := pprofile.NewProfiles()
79 ps := incomingProfiles.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles()
80
81 // Add 3 profiles to the incoming
82 ps.AppendEmpty()
83 ps.AppendEmpty()
84 ps.AppendEmpty()
85
86 pp, err := NewProfiles(context.Background(), processortest.NewNopSettings(processortest.NopType), &testProfilesCfg, consumertest.NewNop(), profilesFunc)
87 require.NoError(t, err)
88 assert.NoError(t, pp.Start(context.Background(), componenttest.NewNopHost()))
89
90 var wg sync.WaitGroup
91 for range 10 {
92 wg.Add(1)
93 go func() {
94 defer wg.Done()
95 for range 10000 {
96 assert.NoError(t, pp.ConsumeProfiles(context.Background(), incomingProfiles))
97 }
98 }()
99 }
100 wg.Wait()
101 assert.NoError(t, pp.Shutdown(context.Background()))
102 }