| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package e2e |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
"go.uber.org/zap/zapcore" |
| 18 |
|
| 19 |
"go.opentelemetry.io/collector/component" |
| 20 |
"go.opentelemetry.io/collector/component/componentstatus" |
| 21 |
"go.opentelemetry.io/collector/config/configtelemetry" |
| 22 |
"go.opentelemetry.io/collector/confmap" |
| 23 |
"go.opentelemetry.io/collector/connector" |
| 24 |
"go.opentelemetry.io/collector/connector/connectortest" |
| 25 |
"go.opentelemetry.io/collector/consumer" |
| 26 |
"go.opentelemetry.io/collector/exporter" |
| 27 |
"go.opentelemetry.io/collector/exporter/exportertest" |
| 28 |
"go.opentelemetry.io/collector/extension" |
| 29 |
"go.opentelemetry.io/collector/internal/sharedcomponent" |
| 30 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 31 |
"go.opentelemetry.io/collector/pipeline" |
| 32 |
"go.opentelemetry.io/collector/receiver" |
| 33 |
"go.opentelemetry.io/collector/service" |
| 34 |
"go.opentelemetry.io/collector/service/extensions" |
| 35 |
"go.opentelemetry.io/collector/service/pipelines" |
| 36 |
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry" |
| 37 |
) |
| 38 |
|
| 39 |
var nopType = component.MustNewType("nop") |
| 40 |
|
| 41 |
var wg = sync.WaitGroup{} |
| 42 |
|
| 43 |
func Test_ComponentStatusReporting_SharedInstance(t *testing.T) { |
| 44 |
eventsReceived := make(map[*componentstatus.InstanceID][]*componentstatus.Event) |
| 45 |
exporterFactory := exportertest.NewNopFactory() |
| 46 |
connectorFactory := connectortest.NewNopFactory() |
| 47 |
// Use a different ID than receivertest and exportertest to avoid ambiguous |
| 48 |
// configuration scenarios. Ambiguous IDs are detected in the 'otelcol' package, |
| 49 |
// but lower level packages such as 'service' assume that IDs are disambiguated. |
| 50 |
connID := component.NewIDWithName(nopType, "conn") |
| 51 |
|
| 52 |
set := service.Settings{ |
| 53 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 54 |
CollectorConf: confmap.New(), |
| 55 |
ReceiversConfigs: map[component.ID]component.Config{ |
| 56 |
component.NewID(component.MustNewType("test")): &receiverConfig{}, |
| 57 |
}, |
| 58 |
ReceiversFactories: map[component.Type]receiver.Factory{ |
| 59 |
component.MustNewType("test"): newReceiverFactory(), |
| 60 |
}, |
| 61 |
ExportersConfigs: map[component.ID]component.Config{ |
| 62 |
component.NewID(nopType): exporterFactory.CreateDefaultConfig(), |
| 63 |
}, |
| 64 |
ExportersFactories: map[component.Type]exporter.Factory{ |
| 65 |
nopType: exporterFactory, |
| 66 |
}, |
| 67 |
ConnectorsConfigs: map[component.ID]component.Config{ |
| 68 |
connID: connectorFactory.CreateDefaultConfig(), |
| 69 |
}, |
| 70 |
ConnectorsFactories: map[component.Type]connector.Factory{ |
| 71 |
nopType: connectorFactory, |
| 72 |
}, |
| 73 |
ExtensionsConfigs: map[component.ID]component.Config{ |
| 74 |
component.NewID(component.MustNewType("watcher")): &extensionConfig{eventsReceived}, |
| 75 |
}, |
| 76 |
ExtensionsFactories: map[component.Type]extension.Factory{ |
| 77 |
component.MustNewType("watcher"): newExtensionFactory(), |
| 78 |
}, |
| 79 |
TelemetryFactory: otelconftelemetry.NewFactory(), |
| 80 |
} |
| 81 |
set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest"} |
| 82 |
|
| 83 |
cfg := service.Config{ |
| 84 |
Telemetry: &otelconftelemetry.Config{ |
| 85 |
Logs: otelconftelemetry.LogsConfig{ |
| 86 |
Level: zapcore.InfoLevel, |
| 87 |
Development: false, |
| 88 |
Encoding: "console", |
| 89 |
Sampling: &otelconftelemetry.LogsSamplingConfig{ |
| 90 |
Enabled: true, |
| 91 |
Tick: 10 * time.Second, |
| 92 |
Initial: 100, |
| 93 |
Thereafter: 100, |
| 94 |
}, |
| 95 |
OutputPaths: []string{"stderr"}, |
| 96 |
ErrorOutputPaths: []string{"stderr"}, |
| 97 |
DisableCaller: false, |
| 98 |
DisableStacktrace: false, |
| 99 |
InitialFields: map[string]any(nil), |
| 100 |
}, |
| 101 |
Metrics: otelconftelemetry.MetricsConfig{ |
| 102 |
Level: configtelemetry.LevelNone, |
| 103 |
}, |
| 104 |
}, |
| 105 |
Pipelines: pipelines.Config{ |
| 106 |
pipeline.NewID(pipeline.SignalTraces): { |
| 107 |
Receivers: []component.ID{component.NewID(component.MustNewType("test"))}, |
| 108 |
Exporters: []component.ID{component.NewID(nopType)}, |
| 109 |
}, |
| 110 |
pipeline.NewID(pipeline.SignalMetrics): { |
| 111 |
Receivers: []component.ID{component.NewID(component.MustNewType("test"))}, |
| 112 |
Exporters: []component.ID{component.NewID(nopType)}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
Extensions: extensions.Config{component.NewID(component.MustNewType("watcher"))}, |
| 116 |
} |
| 117 |
|
| 118 |
s, err := service.New(context.Background(), set, cfg) |
| 119 |
require.NoError(t, err) |
| 120 |
|
| 121 |
wg.Add(1) |
| 122 |
err = s.Start(context.Background()) |
| 123 |
require.NoError(t, err) |
| 124 |
wg.Wait() |
| 125 |
err = s.Shutdown(context.Background()) |
| 126 |
require.NoError(t, err) |
| 127 |
|
| 128 |
require.Len(t, eventsReceived, 2) |
| 129 |
|
| 130 |
for instanceID, events := range eventsReceived { |
| 131 |
pipelineIDs := "" |
| 132 |
instanceID.AllPipelineIDs(func(id pipeline.ID) bool { |
| 133 |
pipelineIDs += id.String() + "," |
| 134 |
return true |
| 135 |
}) |
| 136 |
|
| 137 |
t.Logf("checking errors for %v - %v - %v", pipelineIDs, instanceID.Kind().String(), instanceID.ComponentID().String()) |
| 138 |
|
| 139 |
var expectedEvents []*componentstatus.Event |
| 140 |
// The StatusOk is not guaranteed to be in the slice, set it according to the number of captured states |
| 141 |
assert.True(t, len(events) == 4 || len(events) == 5) |
| 142 |
receiverTestAttrs := pcommon.NewMap() |
| 143 |
receiverTestAttrs.PutStr("scraper", "test") |
| 144 |
if len(events) == 4 { |
| 145 |
expectedEvents = []*componentstatus.Event{ |
| 146 |
componentstatus.NewEvent(componentstatus.StatusStarting), |
| 147 |
componentstatus.NewEvent(componentstatus.StatusRecoverableError, componentstatus.WithAttributes(receiverTestAttrs)), |
| 148 |
componentstatus.NewEvent(componentstatus.StatusStopping), |
| 149 |
componentstatus.NewEvent(componentstatus.StatusStopped), |
| 150 |
} |
| 151 |
} else { |
| 152 |
expectedEvents = []*componentstatus.Event{ |
| 153 |
componentstatus.NewEvent(componentstatus.StatusStarting), |
| 154 |
componentstatus.NewEvent(componentstatus.StatusRecoverableError, componentstatus.WithAttributes(receiverTestAttrs)), |
| 155 |
componentstatus.NewEvent(componentstatus.StatusOK), |
| 156 |
componentstatus.NewEvent(componentstatus.StatusStopping), |
| 157 |
componentstatus.NewEvent(componentstatus.StatusStopped), |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
var eventStr strings.Builder |
| 162 |
for i, e := range events { |
| 163 |
fmt.Fprintf(&eventStr, "%v,", e.Status()) |
| 164 |
assert.Equal(t, expectedEvents[i].Status(), e.Status()) |
| 165 |
} |
| 166 |
t.Logf("events received: %v", eventStr.String()) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func newReceiverFactory() receiver.Factory { |
| 171 |
return receiver.NewFactory( |
| 172 |
component.MustNewType("test"), |
| 173 |
createDefaultReceiverConfig, |
| 174 |
receiver.WithTraces(createTraces, component.StabilityLevelStable), |
| 175 |
receiver.WithMetrics(createMetrics, component.StabilityLevelStable), |
| 176 |
) |
| 177 |
} |
| 178 |
|
| 179 |
type testReceiver struct{} |
| 180 |
|
| 181 |
func (t *testReceiver) Start(_ context.Context, host component.Host) error { |
| 182 |
scraperAttrs := pcommon.NewMap() |
| 183 |
scraperAttrs.PutStr("scraper", "test") |
| 184 |
componentstatus.ReportStatus(host, componentstatus.NewEvent( |
| 185 |
componentstatus.StatusRecoverableError, |
| 186 |
componentstatus.WithError(errors.New("test recoverable error")), |
| 187 |
componentstatus.WithAttributes(scraperAttrs), |
| 188 |
)) |
| 189 |
|
| 190 |
go func() { |
| 191 |
componentstatus.ReportStatus(host, componentstatus.NewEvent(componentstatus.StatusOK)) |
| 192 |
wg.Done() |
| 193 |
}() |
| 194 |
return nil |
| 195 |
} |
| 196 |
|
| 197 |
func (t *testReceiver) Shutdown(_ context.Context) error { |
| 198 |
return nil |
| 199 |
} |
| 200 |
|
| 201 |
type receiverConfig struct{} |
| 202 |
|
| 203 |
func createDefaultReceiverConfig() component.Config { |
| 204 |
return &receiverConfig{} |
| 205 |
} |
| 206 |
|
| 207 |
func createTraces( |
| 208 |
_ context.Context, |
| 209 |
_ receiver.Settings, |
| 210 |
cfg component.Config, |
| 211 |
_ consumer.Traces, |
| 212 |
) (receiver.Traces, error) { |
| 213 |
oCfg := cfg.(*receiverConfig) |
| 214 |
r, err := receivers.LoadOrStore( |
| 215 |
oCfg, |
| 216 |
func() (*testReceiver, error) { |
| 217 |
return &testReceiver{}, nil |
| 218 |
}, |
| 219 |
) |
| 220 |
if err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
|
| 224 |
return r, nil |
| 225 |
} |
| 226 |
|
| 227 |
func createMetrics( |
| 228 |
_ context.Context, |
| 229 |
_ receiver.Settings, |
| 230 |
cfg component.Config, |
| 231 |
_ consumer.Metrics, |
| 232 |
) (receiver.Metrics, error) { |
| 233 |
oCfg := cfg.(*receiverConfig) |
| 234 |
r, err := receivers.LoadOrStore( |
| 235 |
oCfg, |
| 236 |
func() (*testReceiver, error) { |
| 237 |
return &testReceiver{}, nil |
| 238 |
}, |
| 239 |
) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
|
| 244 |
return r, nil |
| 245 |
} |
| 246 |
|
| 247 |
var receivers = sharedcomponent.NewMap[*receiverConfig, *testReceiver]() |
| 248 |
|
| 249 |
func newExtensionFactory() extension.Factory { |
| 250 |
return extension.NewFactory( |
| 251 |
component.MustNewType("watcher"), |
| 252 |
createDefaultExtensionConfig, |
| 253 |
create, |
| 254 |
component.StabilityLevelStable, |
| 255 |
) |
| 256 |
} |
| 257 |
|
| 258 |
func create(_ context.Context, _ extension.Settings, cfg component.Config) (extension.Extension, error) { |
| 259 |
oCfg := cfg.(*extensionConfig) |
| 260 |
return &testExtension{ |
| 261 |
eventsReceived: oCfg.eventsReceived, |
| 262 |
}, nil |
| 263 |
} |
| 264 |
|
| 265 |
type testExtension struct { |
| 266 |
eventsReceived map[*componentstatus.InstanceID][]*componentstatus.Event |
| 267 |
} |
| 268 |
|
| 269 |
type extensionConfig struct { |
| 270 |
eventsReceived map[*componentstatus.InstanceID][]*componentstatus.Event |
| 271 |
} |
| 272 |
|
| 273 |
func createDefaultExtensionConfig() component.Config { |
| 274 |
return &extensionConfig{} |
| 275 |
} |
| 276 |
|
| 277 |
// Start implements the component.Component interface. |
| 278 |
func (t *testExtension) Start(_ context.Context, _ component.Host) error { |
| 279 |
return nil |
| 280 |
} |
| 281 |
|
| 282 |
// Shutdown implements the component.Component interface. |
| 283 |
func (t *testExtension) Shutdown(_ context.Context) error { |
| 284 |
return nil |
| 285 |
} |
| 286 |
|
| 287 |
// ComponentStatusChanged implements the extension.StatusWatcher interface. |
| 288 |
func (t *testExtension) ComponentStatusChanged( |
| 289 |
source *componentstatus.InstanceID, |
| 290 |
event *componentstatus.Event, |
| 291 |
) { |
| 292 |
if source.ComponentID() == component.NewID(component.MustNewType("test")) { |
| 293 |
t.eventsReceived[source] = append(t.eventsReceived[source], event) |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// NotifyConfig implements the extensioncapabilities.ConfigWatcher interface. |
| 298 |
func (t *testExtension) NotifyConfig(_ context.Context, _ *confmap.Conf) error { |
| 299 |
return nil |
| 300 |
} |
| 301 |
|
| 302 |
// Ready implements the extensioncapabilities.PipelineWatcher interface. |
| 303 |
func (t *testExtension) Ready() error { |
| 304 |
return nil |
| 305 |
} |
| 306 |
|
| 307 |
// NotReady implements the extensioncapabilities.PipelineWatcher interface. |
| 308 |
func (t *testExtension) NotReady() error { |
| 309 |
return nil |
| 310 |
} |