| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package testcomponents |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component/componenttest" |
| 14 |
"go.opentelemetry.io/collector/connector" |
| 15 |
"go.opentelemetry.io/collector/connector/connectortest" |
| 16 |
"go.opentelemetry.io/collector/connector/xconnector" |
| 17 |
"go.opentelemetry.io/collector/consumer" |
| 18 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 19 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 20 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 21 |
"go.opentelemetry.io/collector/pipeline" |
| 22 |
) |
| 23 |
|
| 24 |
func TestExampleRouter(t *testing.T) { |
| 25 |
conn := &ExampleRouter{} |
| 26 |
host := componenttest.NewNopHost() |
| 27 |
assert.False(t, conn.Started()) |
| 28 |
require.NoError(t, conn.Start(context.Background(), host)) |
| 29 |
assert.True(t, conn.Started()) |
| 30 |
|
| 31 |
assert.False(t, conn.Stopped()) |
| 32 |
require.NoError(t, conn.Shutdown(context.Background())) |
| 33 |
assert.True(t, conn.Stopped()) |
| 34 |
} |
| 35 |
|
| 36 |
func TestTracesRouter(t *testing.T) { |
| 37 |
leftID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_left") |
| 38 |
rightID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_right") |
| 39 |
|
| 40 |
sinkLeft := new(consumertest.TracesSink) |
| 41 |
sinkRight := new(consumertest.TracesSink) |
| 42 |
|
| 43 |
// The service will build a router to give to every connector. |
| 44 |
// Many connectors will just call router.ConsumeTraces, |
| 45 |
// but some implementation will call RouteTraces instead. |
| 46 |
router := connector.NewTracesRouter( |
| 47 |
map[pipeline.ID]consumer.Traces{ |
| 48 |
leftID: sinkLeft, |
| 49 |
rightID: sinkRight, |
| 50 |
}) |
| 51 |
|
| 52 |
cfg := ExampleRouterConfig{Traces: &LeftRightConfig{Left: leftID, Right: rightID}} |
| 53 |
tr, err := ExampleRouterFactory.CreateTracesToTraces( |
| 54 |
context.Background(), connectortest.NewNopSettings(ExampleRouterFactory.Type()), cfg, router) |
| 55 |
require.NoError(t, err) |
| 56 |
assert.False(t, tr.Capabilities().MutatesData) |
| 57 |
|
| 58 |
td := testdata.GenerateTraces(1) |
| 59 |
|
| 60 |
require.NoError(t, tr.ConsumeTraces(context.Background(), td)) |
| 61 |
assert.Len(t, sinkRight.AllTraces(), 1) |
| 62 |
assert.Empty(t, sinkLeft.AllTraces()) |
| 63 |
|
| 64 |
require.NoError(t, tr.ConsumeTraces(context.Background(), td)) |
| 65 |
assert.Len(t, sinkRight.AllTraces(), 1) |
| 66 |
assert.Len(t, sinkLeft.AllTraces(), 1) |
| 67 |
|
| 68 |
assert.NoError(t, tr.ConsumeTraces(context.Background(), td)) |
| 69 |
assert.NoError(t, tr.ConsumeTraces(context.Background(), td)) |
| 70 |
assert.NoError(t, tr.ConsumeTraces(context.Background(), td)) |
| 71 |
assert.Len(t, sinkRight.AllTraces(), 3) |
| 72 |
assert.Len(t, sinkLeft.AllTraces(), 2) |
| 73 |
} |
| 74 |
|
| 75 |
func TestMetricsRouter(t *testing.T) { |
| 76 |
leftID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_left") |
| 77 |
rightID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_right") |
| 78 |
|
| 79 |
sinkLeft := new(consumertest.MetricsSink) |
| 80 |
sinkRight := new(consumertest.MetricsSink) |
| 81 |
|
| 82 |
// The service will build a router to give to every connector. |
| 83 |
// Many connectors will just call router.ConsumeMetrics, |
| 84 |
// but some implementation will call RouteMetrics instead. |
| 85 |
router := connector.NewMetricsRouter( |
| 86 |
map[pipeline.ID]consumer.Metrics{ |
| 87 |
leftID: sinkLeft, |
| 88 |
rightID: sinkRight, |
| 89 |
}) |
| 90 |
|
| 91 |
cfg := ExampleRouterConfig{Metrics: &LeftRightConfig{Left: leftID, Right: rightID}} |
| 92 |
mr, err := ExampleRouterFactory.CreateMetricsToMetrics( |
| 93 |
context.Background(), connectortest.NewNopSettings(ExampleRouterFactory.Type()), cfg, router) |
| 94 |
require.NoError(t, err) |
| 95 |
assert.False(t, mr.Capabilities().MutatesData) |
| 96 |
|
| 97 |
md := testdata.GenerateMetrics(1) |
| 98 |
|
| 99 |
require.NoError(t, mr.ConsumeMetrics(context.Background(), md)) |
| 100 |
assert.Len(t, sinkRight.AllMetrics(), 1) |
| 101 |
assert.Empty(t, sinkLeft.AllMetrics()) |
| 102 |
|
| 103 |
require.NoError(t, mr.ConsumeMetrics(context.Background(), md)) |
| 104 |
assert.Len(t, sinkRight.AllMetrics(), 1) |
| 105 |
assert.Len(t, sinkLeft.AllMetrics(), 1) |
| 106 |
|
| 107 |
assert.NoError(t, mr.ConsumeMetrics(context.Background(), md)) |
| 108 |
assert.NoError(t, mr.ConsumeMetrics(context.Background(), md)) |
| 109 |
assert.NoError(t, mr.ConsumeMetrics(context.Background(), md)) |
| 110 |
assert.Len(t, sinkRight.AllMetrics(), 3) |
| 111 |
assert.Len(t, sinkLeft.AllMetrics(), 2) |
| 112 |
} |
| 113 |
|
| 114 |
func TestLogsRouter(t *testing.T) { |
| 115 |
leftID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_left") |
| 116 |
rightID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_right") |
| 117 |
|
| 118 |
sinkLeft := new(consumertest.LogsSink) |
| 119 |
sinkRight := new(consumertest.LogsSink) |
| 120 |
|
| 121 |
// The service will build a router to give to every connector. |
| 122 |
// Many connectors will just call router.ConsumeLogs, |
| 123 |
// but some implementation will call RouteLogs instead. |
| 124 |
router := connector.NewLogsRouter( |
| 125 |
map[pipeline.ID]consumer.Logs{ |
| 126 |
leftID: sinkLeft, |
| 127 |
rightID: sinkRight, |
| 128 |
}) |
| 129 |
|
| 130 |
cfg := ExampleRouterConfig{Logs: &LeftRightConfig{Left: leftID, Right: rightID}} |
| 131 |
lr, err := ExampleRouterFactory.CreateLogsToLogs( |
| 132 |
context.Background(), connectortest.NewNopSettings(ExampleRouterFactory.Type()), cfg, router) |
| 133 |
require.NoError(t, err) |
| 134 |
assert.False(t, lr.Capabilities().MutatesData) |
| 135 |
|
| 136 |
ld := testdata.GenerateLogs(1) |
| 137 |
|
| 138 |
require.NoError(t, lr.ConsumeLogs(context.Background(), ld)) |
| 139 |
assert.Len(t, sinkRight.AllLogs(), 1) |
| 140 |
assert.Empty(t, sinkLeft.AllLogs()) |
| 141 |
|
| 142 |
require.NoError(t, lr.ConsumeLogs(context.Background(), ld)) |
| 143 |
assert.Len(t, sinkRight.AllLogs(), 1) |
| 144 |
assert.Len(t, sinkLeft.AllLogs(), 1) |
| 145 |
|
| 146 |
assert.NoError(t, lr.ConsumeLogs(context.Background(), ld)) |
| 147 |
assert.NoError(t, lr.ConsumeLogs(context.Background(), ld)) |
| 148 |
assert.NoError(t, lr.ConsumeLogs(context.Background(), ld)) |
| 149 |
assert.Len(t, sinkRight.AllLogs(), 3) |
| 150 |
assert.Len(t, sinkLeft.AllLogs(), 2) |
| 151 |
} |
| 152 |
|
| 153 |
func TestProfilesRouter(t *testing.T) { |
| 154 |
leftID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_left") |
| 155 |
rightID := pipeline.NewIDWithName(pipeline.SignalTraces, "sink_right") |
| 156 |
|
| 157 |
sinkLeft := new(consumertest.ProfilesSink) |
| 158 |
sinkRight := new(consumertest.ProfilesSink) |
| 159 |
|
| 160 |
// The service will build a router to give to every connector. |
| 161 |
// Many connectors will just call router.ConsumeProfiles, |
| 162 |
// but some implementation will call RouteProfiles instead. |
| 163 |
router := xconnector.NewProfilesRouter( |
| 164 |
map[pipeline.ID]xconsumer.Profiles{ |
| 165 |
leftID: sinkLeft, |
| 166 |
rightID: sinkRight, |
| 167 |
}) |
| 168 |
|
| 169 |
cfg := ExampleRouterConfig{Profiles: &LeftRightConfig{Left: leftID, Right: rightID}} |
| 170 |
tr, err := ExampleRouterFactory.CreateProfilesToProfiles( |
| 171 |
context.Background(), connectortest.NewNopSettings(ExampleRouterFactory.Type()), cfg, router) |
| 172 |
require.NoError(t, err) |
| 173 |
assert.False(t, tr.Capabilities().MutatesData) |
| 174 |
|
| 175 |
td := testdata.GenerateProfiles(1) |
| 176 |
|
| 177 |
require.NoError(t, tr.ConsumeProfiles(context.Background(), td)) |
| 178 |
assert.Len(t, sinkRight.AllProfiles(), 1) |
| 179 |
assert.Empty(t, sinkLeft.AllProfiles()) |
| 180 |
|
| 181 |
require.NoError(t, tr.ConsumeProfiles(context.Background(), td)) |
| 182 |
assert.Len(t, sinkRight.AllProfiles(), 1) |
| 183 |
assert.Len(t, sinkLeft.AllProfiles(), 1) |
| 184 |
|
| 185 |
assert.NoError(t, tr.ConsumeProfiles(context.Background(), td)) |
| 186 |
assert.NoError(t, tr.ConsumeProfiles(context.Background(), td)) |
| 187 |
assert.NoError(t, tr.ConsumeProfiles(context.Background(), td)) |
| 188 |
assert.Len(t, sinkRight.AllProfiles(), 3) |
| 189 |
assert.Len(t, sinkLeft.AllProfiles(), 2) |
| 190 |
} |