| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package otelcol |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"sync" |
| 13 |
"syscall" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
"go.uber.org/zap" |
| 20 |
yaml "go.yaml.in/yaml/v3" |
| 21 |
|
| 22 |
"go.opentelemetry.io/collector/component" |
| 23 |
"go.opentelemetry.io/collector/component/componentstatus" |
| 24 |
"go.opentelemetry.io/collector/confmap" |
| 25 |
"go.opentelemetry.io/collector/extension" |
| 26 |
"go.opentelemetry.io/collector/processor/processortest" |
| 27 |
"go.opentelemetry.io/collector/service/telemetry" |
| 28 |
"go.opentelemetry.io/collector/service/telemetry/telemetrytest" |
| 29 |
) |
| 30 |
|
| 31 |
func TestStateString(t *testing.T) { |
| 32 |
assert.Equal(t, "Starting", StateStarting.String()) |
| 33 |
assert.Equal(t, "Running", StateRunning.String()) |
| 34 |
assert.Equal(t, "Closing", StateClosing.String()) |
| 35 |
assert.Equal(t, "Closed", StateClosed.String()) |
| 36 |
assert.Equal(t, "UNKNOWN", State(13).String()) |
| 37 |
} |
| 38 |
|
| 39 |
func TestCollectorStartAsGoRoutine(t *testing.T) { |
| 40 |
set := CollectorSettings{ |
| 41 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 42 |
Factories: nopFactories, |
| 43 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 44 |
} |
| 45 |
col, err := NewCollector(set) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
wg := startCollector(context.Background(), t, col) |
| 49 |
|
| 50 |
assert.Eventually(t, func() bool { |
| 51 |
return StateRunning == col.GetState() |
| 52 |
}, 2*time.Second, 200*time.Millisecond) |
| 53 |
|
| 54 |
col.Shutdown() |
| 55 |
col.Shutdown() |
| 56 |
wg.Wait() |
| 57 |
assert.Equal(t, StateClosed, col.GetState()) |
| 58 |
} |
| 59 |
|
| 60 |
func TestCollectorCancelContext(t *testing.T) { |
| 61 |
set := CollectorSettings{ |
| 62 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 63 |
Factories: nopFactories, |
| 64 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 65 |
} |
| 66 |
col, err := NewCollector(set) |
| 67 |
require.NoError(t, err) |
| 68 |
|
| 69 |
ctx, cancel := context.WithCancel(context.Background()) |
| 70 |
wg := startCollector(ctx, t, col) |
| 71 |
|
| 72 |
assert.Eventually(t, func() bool { |
| 73 |
return StateRunning == col.GetState() |
| 74 |
}, 2*time.Second, 200*time.Millisecond) |
| 75 |
|
| 76 |
cancel() |
| 77 |
wg.Wait() |
| 78 |
assert.Equal(t, StateClosed, col.GetState()) |
| 79 |
} |
| 80 |
|
| 81 |
func TestCollectorStateAfterConfigChange(t *testing.T) { |
| 82 |
var watcher confmap.WatcherFunc |
| 83 |
fileProvider := newFakeProvider("file", func(_ context.Context, uri string, w confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 84 |
watcher = w |
| 85 |
conf := newConfFromFile(t, uri[5:]) |
| 86 |
return confmap.NewRetrieved(conf) |
| 87 |
}) |
| 88 |
|
| 89 |
shutdownRequests := make(chan chan struct{}) |
| 90 |
shutdown := func(ctx context.Context) error { |
| 91 |
unblock := make(chan struct{}) |
| 92 |
select { |
| 93 |
case <-ctx.Done(): |
| 94 |
case shutdownRequests <- unblock: |
| 95 |
select { |
| 96 |
case <-unblock: |
| 97 |
case <-ctx.Done(): |
| 98 |
} |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
factories, err := nopFactories() |
| 103 |
require.NoError(t, err) |
| 104 |
factories.Telemetry = telemetry.NewFactory( |
| 105 |
func() component.Config { return fakeTelemetryConfig{} }, |
| 106 |
telemetrytest.WithLogger(zap.NewNop(), shutdown), |
| 107 |
) |
| 108 |
|
| 109 |
set := ConfigProviderSettings{ |
| 110 |
ResolverSettings: confmap.ResolverSettings{ |
| 111 |
URIs: []string{filepath.Join("testdata", "otelcol-nop.yaml")}, |
| 112 |
ProviderFactories: []confmap.ProviderFactory{ |
| 113 |
fileProvider, |
| 114 |
}, |
| 115 |
}, |
| 116 |
} |
| 117 |
col, err := NewCollector(CollectorSettings{ |
| 118 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 119 |
Factories: func() (Factories, error) { return factories, nil }, |
| 120 |
ConfigProviderSettings: set, |
| 121 |
}) |
| 122 |
require.NoError(t, err) |
| 123 |
|
| 124 |
wg := startCollector(context.Background(), t, col) |
| 125 |
assert.Eventually(t, func() bool { |
| 126 |
return StateRunning == col.GetState() |
| 127 |
}, 10*time.Second, 10*time.Millisecond) |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
watcher(&confmap.ChangeEvent{}) |
| 134 |
unblock := <-shutdownRequests |
| 135 |
assert.Equal(t, StateClosing, col.GetState()) |
| 136 |
close(unblock) |
| 137 |
assert.Eventually(t, func() bool { |
| 138 |
return StateRunning == col.GetState() |
| 139 |
}, 10*time.Second, 10*time.Millisecond) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
watcher(&confmap.ChangeEvent{}) |
| 145 |
unblock = <-shutdownRequests |
| 146 |
assert.Equal(t, StateClosing, col.GetState()) |
| 147 |
col.Shutdown() |
| 148 |
close(unblock) |
| 149 |
|
| 150 |
|
| 151 |
close(<-shutdownRequests) |
| 152 |
wg.Wait() |
| 153 |
assert.Equal(t, StateClosed, col.GetState()) |
| 154 |
} |
| 155 |
|
| 156 |
func TestCollectorReportError(t *testing.T) { |
| 157 |
col, err := NewCollector(CollectorSettings{ |
| 158 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 159 |
Factories: nopFactories, |
| 160 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 161 |
}) |
| 162 |
require.NoError(t, err) |
| 163 |
|
| 164 |
wg := startCollector(context.Background(), t, col) |
| 165 |
|
| 166 |
assert.Eventually(t, func() bool { |
| 167 |
return StateRunning == col.GetState() |
| 168 |
}, 2*time.Second, 200*time.Millisecond) |
| 169 |
|
| 170 |
col.asyncErrorChannel <- errors.New("err2") |
| 171 |
|
| 172 |
wg.Wait() |
| 173 |
assert.Equal(t, StateClosed, col.GetState()) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
func NewStatusWatcherExtensionFactory( |
| 178 |
onStatusChanged func(source *componentstatus.InstanceID, event *componentstatus.Event), |
| 179 |
) extension.Factory { |
| 180 |
return extension.NewFactory( |
| 181 |
component.MustNewType("statuswatcher"), |
| 182 |
func() component.Config { |
| 183 |
return &struct{}{} |
| 184 |
}, |
| 185 |
func(context.Context, extension.Settings, component.Config) (extension.Extension, error) { |
| 186 |
return &statusWatcherExtension{onStatusChanged: onStatusChanged}, nil |
| 187 |
}, |
| 188 |
component.StabilityLevelStable) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
type statusWatcherExtension struct { |
| 194 |
component.StartFunc |
| 195 |
component.ShutdownFunc |
| 196 |
onStatusChanged func(source *componentstatus.InstanceID, event *componentstatus.Event) |
| 197 |
} |
| 198 |
|
| 199 |
func (e statusWatcherExtension) ComponentStatusChanged(source *componentstatus.InstanceID, event *componentstatus.Event) { |
| 200 |
e.onStatusChanged(source, event) |
| 201 |
} |
| 202 |
|
| 203 |
func TestComponentStatusWatcher(t *testing.T) { |
| 204 |
factories, err := nopFactories() |
| 205 |
require.NoError(t, err) |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
unhealthyProcessorFactory := processortest.NewUnhealthyProcessorFactory() |
| 210 |
factories.Processors[unhealthyProcessorFactory.Type()] = unhealthyProcessorFactory |
| 211 |
|
| 212 |
|
| 213 |
changedComponents := map[*componentstatus.InstanceID][]componentstatus.Status{} |
| 214 |
var mux sync.Mutex |
| 215 |
onStatusChanged := func(source *componentstatus.InstanceID, event *componentstatus.Event) { |
| 216 |
if source.ComponentID().Type() != unhealthyProcessorFactory.Type() { |
| 217 |
return |
| 218 |
} |
| 219 |
mux.Lock() |
| 220 |
defer mux.Unlock() |
| 221 |
changedComponents[source] = append(changedComponents[source], event.Status()) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
factory := NewStatusWatcherExtensionFactory(onStatusChanged) |
| 227 |
factories.Extensions[factory.Type()] = factory |
| 228 |
|
| 229 |
|
| 230 |
col, err := NewCollector(CollectorSettings{ |
| 231 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 232 |
Factories: func() (Factories, error) { return factories, nil }, |
| 233 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-statuswatcher.yaml")}), |
| 234 |
}) |
| 235 |
require.NoError(t, err) |
| 236 |
|
| 237 |
|
| 238 |
wg := startCollector(context.Background(), t, col) |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
startupStatuses1 := []componentstatus.Status{ |
| 243 |
componentstatus.StatusStarting, |
| 244 |
componentstatus.StatusOK, |
| 245 |
componentstatus.StatusRecoverableError, |
| 246 |
} |
| 247 |
startupStatuses2 := []componentstatus.Status{ |
| 248 |
componentstatus.StatusStarting, |
| 249 |
componentstatus.StatusRecoverableError, |
| 250 |
} |
| 251 |
|
| 252 |
startupStatuses := func(actualStatuses []componentstatus.Status) []componentstatus.Status { |
| 253 |
if len(actualStatuses)%2 == 1 { |
| 254 |
return startupStatuses1 |
| 255 |
} |
| 256 |
return startupStatuses2 |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
assert.Eventually(t, func() bool { |
| 262 |
mux.Lock() |
| 263 |
defer mux.Unlock() |
| 264 |
|
| 265 |
for k, v := range changedComponents { |
| 266 |
|
| 267 |
assert.Equal(t, component.NewID(unhealthyProcessorFactory.Type()), k.ComponentID()) |
| 268 |
|
| 269 |
assert.Equal(t, startupStatuses(v), v) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
return len(changedComponents) == 3 |
| 277 |
}, 2*time.Second, time.Millisecond*100) |
| 278 |
|
| 279 |
col.Shutdown() |
| 280 |
wg.Wait() |
| 281 |
|
| 282 |
|
| 283 |
for _, v := range changedComponents { |
| 284 |
expectedStatuses := append([]componentstatus.Status{}, startupStatuses(v)...) |
| 285 |
expectedStatuses = append(expectedStatuses, componentstatus.StatusStopping, componentstatus.StatusStopped) |
| 286 |
assert.Equal(t, expectedStatuses, v) |
| 287 |
} |
| 288 |
|
| 289 |
assert.Equal(t, StateClosed, col.GetState()) |
| 290 |
} |
| 291 |
|
| 292 |
func TestCollectorSendSignal(t *testing.T) { |
| 293 |
col, err := NewCollector(CollectorSettings{ |
| 294 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 295 |
Factories: nopFactories, |
| 296 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 297 |
}) |
| 298 |
require.NoError(t, err) |
| 299 |
|
| 300 |
wg := startCollector(context.Background(), t, col) |
| 301 |
|
| 302 |
assert.Eventually(t, func() bool { |
| 303 |
return StateRunning == col.GetState() |
| 304 |
}, 2*time.Second, 200*time.Millisecond) |
| 305 |
|
| 306 |
col.signalsChannel <- syscall.SIGHUP |
| 307 |
|
| 308 |
assert.Eventually(t, func() bool { |
| 309 |
return StateRunning == col.GetState() |
| 310 |
}, 2*time.Second, 200*time.Millisecond) |
| 311 |
|
| 312 |
col.signalsChannel <- syscall.SIGTERM |
| 313 |
|
| 314 |
wg.Wait() |
| 315 |
assert.Equal(t, StateClosed, col.GetState()) |
| 316 |
} |
| 317 |
|
| 318 |
func TestCollectorFailedShutdown(t *testing.T) { |
| 319 |
t.Skip("This test was using telemetry shutdown failure, switch to use a component that errors on shutdown.") |
| 320 |
|
| 321 |
col, err := NewCollector(CollectorSettings{ |
| 322 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 323 |
Factories: nopFactories, |
| 324 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 325 |
}) |
| 326 |
require.NoError(t, err) |
| 327 |
|
| 328 |
wg := sync.WaitGroup{} |
| 329 |
wg.Add(1) |
| 330 |
go func() { |
| 331 |
defer wg.Done() |
| 332 |
assert.EqualError(t, col.Run(context.Background()), "failed to shutdown collector telemetry: err1") |
| 333 |
}() |
| 334 |
|
| 335 |
assert.Eventually(t, func() bool { |
| 336 |
return StateRunning == col.GetState() |
| 337 |
}, 2*time.Second, 200*time.Millisecond) |
| 338 |
|
| 339 |
col.Shutdown() |
| 340 |
|
| 341 |
wg.Wait() |
| 342 |
assert.Equal(t, StateClosed, col.GetState()) |
| 343 |
} |
| 344 |
|
| 345 |
func TestCollectorStartInvalidConfig(t *testing.T) { |
| 346 |
col, err := NewCollector(CollectorSettings{ |
| 347 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 348 |
Factories: nopFactories, |
| 349 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}), |
| 350 |
}) |
| 351 |
require.NoError(t, err) |
| 352 |
assert.EqualError(t, col.Run(context.Background()), "invalid configuration: service::pipelines::traces: references processor \"invalid\" which is not configured") |
| 353 |
} |
| 354 |
|
| 355 |
func TestNewCollectorInvalidConfigProviderSettings(t *testing.T) { |
| 356 |
_, err := NewCollector(CollectorSettings{ |
| 357 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 358 |
Factories: nopFactories, |
| 359 |
ConfigProviderSettings: ConfigProviderSettings{}, |
| 360 |
}) |
| 361 |
require.Error(t, err) |
| 362 |
} |
| 363 |
|
| 364 |
func TestNewCollectorUseConfig(t *testing.T) { |
| 365 |
set := newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}) |
| 366 |
|
| 367 |
col, err := NewCollector(CollectorSettings{ |
| 368 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 369 |
Factories: nopFactories, |
| 370 |
ConfigProviderSettings: set, |
| 371 |
}) |
| 372 |
require.NoError(t, err) |
| 373 |
require.NotNil(t, col.configProvider) |
| 374 |
} |
| 375 |
|
| 376 |
func TestNewCollectorValidatesResolverSettings(t *testing.T) { |
| 377 |
set := ConfigProviderSettings{ |
| 378 |
ResolverSettings: confmap.ResolverSettings{ |
| 379 |
URIs: []string{filepath.Join("testdata", "otelcol-nop.yaml")}, |
| 380 |
}, |
| 381 |
} |
| 382 |
|
| 383 |
_, err := NewCollector(CollectorSettings{ |
| 384 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 385 |
Factories: nopFactories, |
| 386 |
ConfigProviderSettings: set, |
| 387 |
}) |
| 388 |
require.Error(t, err) |
| 389 |
} |
| 390 |
|
| 391 |
func TestCollectorRun(t *testing.T) { |
| 392 |
tests := map[string]struct { |
| 393 |
factories func() (Factories, error) |
| 394 |
configFile string |
| 395 |
}{ |
| 396 |
"nop": { |
| 397 |
factories: nopFactories, |
| 398 |
configFile: "otelcol-nop.yaml", |
| 399 |
}, |
| 400 |
} |
| 401 |
|
| 402 |
for name, test := range tests { |
| 403 |
t.Run(name, func(t *testing.T) { |
| 404 |
set := CollectorSettings{ |
| 405 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 406 |
Factories: test.factories, |
| 407 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", test.configFile)}), |
| 408 |
} |
| 409 |
col, err := NewCollector(set) |
| 410 |
require.NoError(t, err) |
| 411 |
|
| 412 |
wg := startCollector(context.Background(), t, col) |
| 413 |
|
| 414 |
col.Shutdown() |
| 415 |
wg.Wait() |
| 416 |
assert.Equal(t, StateClosed, col.GetState()) |
| 417 |
}) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
func TestCollectorRun_AfterShutdown(t *testing.T) { |
| 422 |
set := CollectorSettings{ |
| 423 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 424 |
Factories: nopFactories, |
| 425 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 426 |
} |
| 427 |
col, err := NewCollector(set) |
| 428 |
require.NoError(t, err) |
| 429 |
|
| 430 |
|
| 431 |
require.NotPanics(t, func() { col.Shutdown() }) |
| 432 |
|
| 433 |
wg := startCollector(context.Background(), t, col) |
| 434 |
|
| 435 |
col.Shutdown() |
| 436 |
wg.Wait() |
| 437 |
assert.Equal(t, StateClosed, col.GetState()) |
| 438 |
} |
| 439 |
|
| 440 |
func TestCollectorRun_Errors(t *testing.T) { |
| 441 |
tests := map[string]struct { |
| 442 |
settings CollectorSettings |
| 443 |
expectedErr string |
| 444 |
}{ |
| 445 |
"factories_error": { |
| 446 |
settings: CollectorSettings{ |
| 447 |
Factories: func() (Factories, error) { |
| 448 |
return Factories{}, errors.New("no factories for you") |
| 449 |
}, |
| 450 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 451 |
}, |
| 452 |
expectedErr: "failed to initialize factories: no factories for you", |
| 453 |
}, |
| 454 |
"invalid_processor": { |
| 455 |
settings: CollectorSettings{ |
| 456 |
Factories: nopFactories, |
| 457 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}), |
| 458 |
}, |
| 459 |
expectedErr: `invalid configuration: service::pipelines::traces: references processor "invalid" which is not configured`, |
| 460 |
}, |
| 461 |
"invalid_telemetry_config": { |
| 462 |
settings: CollectorSettings{ |
| 463 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 464 |
Factories: nopFactories, |
| 465 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-telemetry.yaml")}), |
| 466 |
}, |
| 467 |
expectedErr: "failed to get config: cannot unmarshal the configuration: decoding failed due to the following error(s):\n\n'service.telemetry' has invalid keys: unknown", |
| 468 |
}, |
| 469 |
"missing_telemetry_factory": { |
| 470 |
settings: CollectorSettings{ |
| 471 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 472 |
Factories: func() (Factories, error) { |
| 473 |
factories, _ := nopFactories() |
| 474 |
factories.Telemetry = nil |
| 475 |
return factories, nil |
| 476 |
}, |
| 477 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-otelconftelemetry.yaml")}), |
| 478 |
}, |
| 479 |
expectedErr: "failed to get config: cannot unmarshal the configuration: otelcol.Factories.Telemetry must not be nil. For example, you can use otelconftelemetry.NewFactory to build a telemetry factory", |
| 480 |
}, |
| 481 |
} |
| 482 |
|
| 483 |
for name, test := range tests { |
| 484 |
t.Run(name, func(t *testing.T) { |
| 485 |
col, err := NewCollector(test.settings) |
| 486 |
require.NoError(t, err) |
| 487 |
|
| 488 |
|
| 489 |
err = col.Run(context.Background()) |
| 490 |
require.EqualError(t, err, test.expectedErr) |
| 491 |
|
| 492 |
|
| 493 |
assert.Equal(t, StateClosed, col.GetState()) |
| 494 |
}) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
func TestCollectorDryRun(t *testing.T) { |
| 499 |
tests := map[string]struct { |
| 500 |
settings CollectorSettings |
| 501 |
expectedErr string |
| 502 |
}{ |
| 503 |
"factories_error": { |
| 504 |
settings: CollectorSettings{ |
| 505 |
Factories: func() (Factories, error) { |
| 506 |
return Factories{}, errors.New("no factories for you") |
| 507 |
}, |
| 508 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 509 |
}, |
| 510 |
expectedErr: "failed to initialize factories: no factories for you", |
| 511 |
}, |
| 512 |
"invalid_processor": { |
| 513 |
settings: CollectorSettings{ |
| 514 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 515 |
Factories: nopFactories, |
| 516 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid.yaml")}), |
| 517 |
}, |
| 518 |
expectedErr: `service::pipelines::traces: references processor "invalid" which is not configured`, |
| 519 |
}, |
| 520 |
"invalid_connector_use_unused_exp": { |
| 521 |
settings: CollectorSettings{ |
| 522 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 523 |
Factories: nopFactories, |
| 524 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-connector-unused-exp.yaml")}), |
| 525 |
}, |
| 526 |
expectedErr: `failed to build pipelines: connector "nop/connector1" used as receiver in [logs/in2] pipeline but not used in any supported exporter pipeline`, |
| 527 |
}, |
| 528 |
"invalid_connector_use_unused_rec": { |
| 529 |
settings: CollectorSettings{ |
| 530 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 531 |
Factories: nopFactories, |
| 532 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-connector-unused-rec.yaml")}), |
| 533 |
}, |
| 534 |
expectedErr: `failed to build pipelines: connector "nop/connector1" used as exporter in [logs/in2] pipeline but not used in any supported receiver pipeline`, |
| 535 |
}, |
| 536 |
"cyclic_connector": { |
| 537 |
settings: CollectorSettings{ |
| 538 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 539 |
Factories: nopFactories, |
| 540 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-cyclic-connector.yaml")}), |
| 541 |
}, |
| 542 |
expectedErr: `failed to build pipelines: cycle detected: connector "nop/forward" (traces to traces) -> connector "nop/forward" (traces to traces)`, |
| 543 |
}, |
| 544 |
"invalid_telemetry_config": { |
| 545 |
settings: CollectorSettings{ |
| 546 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 547 |
Factories: nopFactories, |
| 548 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-invalid-telemetry.yaml")}), |
| 549 |
}, |
| 550 |
expectedErr: "failed to get config: cannot unmarshal the configuration: decoding failed due to the following error(s):\n\n'service.telemetry' has invalid keys: unknown", |
| 551 |
}, |
| 552 |
"missing_telemetry_factory": { |
| 553 |
settings: CollectorSettings{ |
| 554 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 555 |
Factories: func() (Factories, error) { |
| 556 |
factories, _ := nopFactories() |
| 557 |
factories.Telemetry = nil |
| 558 |
return factories, nil |
| 559 |
}, |
| 560 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-otelconftelemetry.yaml")}), |
| 561 |
}, |
| 562 |
expectedErr: "failed to get config: cannot unmarshal the configuration: otelcol.Factories.Telemetry must not be nil. For example, you can use otelconftelemetry.NewFactory to build a telemetry factory", |
| 563 |
}, |
| 564 |
} |
| 565 |
|
| 566 |
for name, test := range tests { |
| 567 |
t.Run(name, func(t *testing.T) { |
| 568 |
col, err := NewCollector(test.settings) |
| 569 |
require.NoError(t, err) |
| 570 |
|
| 571 |
err = col.DryRun(context.Background()) |
| 572 |
if test.expectedErr == "" { |
| 573 |
require.NoError(t, err) |
| 574 |
} else { |
| 575 |
require.EqualError(t, err, test.expectedErr) |
| 576 |
} |
| 577 |
}) |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
func startCollector(ctx context.Context, t *testing.T, col *Collector) *sync.WaitGroup { |
| 582 |
wg := &sync.WaitGroup{} |
| 583 |
wg.Add(1) |
| 584 |
go func() { |
| 585 |
defer wg.Done() |
| 586 |
assert.NoError(t, col.Run(ctx)) |
| 587 |
}() |
| 588 |
return wg |
| 589 |
} |
| 590 |
|
| 591 |
type failureProvider struct{} |
| 592 |
|
| 593 |
func newFailureProvider(_ confmap.ProviderSettings) confmap.Provider { |
| 594 |
return &failureProvider{} |
| 595 |
} |
| 596 |
|
| 597 |
func (fmp *failureProvider) Retrieve(context.Context, string, confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 598 |
return nil, errors.New("a failure occurred during configuration retrieval") |
| 599 |
} |
| 600 |
|
| 601 |
func (*failureProvider) Scheme() string { |
| 602 |
return "file" |
| 603 |
} |
| 604 |
|
| 605 |
func (*failureProvider) Shutdown(context.Context) error { |
| 606 |
return nil |
| 607 |
} |
| 608 |
|
| 609 |
type fakeProvider struct { |
| 610 |
scheme string |
| 611 |
ret func(ctx context.Context, uri string, watcher confmap.WatcherFunc) (*confmap.Retrieved, error) |
| 612 |
logger *zap.Logger |
| 613 |
} |
| 614 |
|
| 615 |
func (f *fakeProvider) Retrieve(ctx context.Context, uri string, watcher confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 616 |
return f.ret(ctx, uri, watcher) |
| 617 |
} |
| 618 |
|
| 619 |
func (f *fakeProvider) Scheme() string { |
| 620 |
return f.scheme |
| 621 |
} |
| 622 |
|
| 623 |
func (f *fakeProvider) Shutdown(context.Context) error { |
| 624 |
return nil |
| 625 |
} |
| 626 |
|
| 627 |
func newFakeProvider(scheme string, ret func(ctx context.Context, uri string, watcher confmap.WatcherFunc) (*confmap.Retrieved, error)) confmap.ProviderFactory { |
| 628 |
return confmap.NewProviderFactory(func(ps confmap.ProviderSettings) confmap.Provider { |
| 629 |
return &fakeProvider{ |
| 630 |
scheme: scheme, |
| 631 |
ret: ret, |
| 632 |
logger: ps.Logger, |
| 633 |
} |
| 634 |
}) |
| 635 |
} |
| 636 |
|
| 637 |
func newEnvProvider() confmap.ProviderFactory { |
| 638 |
return newFakeProvider("env", func(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 639 |
|
| 640 |
|
| 641 |
if uri[0:4] != "env:" { |
| 642 |
uri = "env:" + uri |
| 643 |
} |
| 644 |
switch uri { |
| 645 |
case "env:COMPLEX_VALUE": |
| 646 |
return confmap.NewRetrieved([]any{"localhost:3042"}) |
| 647 |
case "env:HOST": |
| 648 |
return confmap.NewRetrieved("localhost") |
| 649 |
case "env:OS": |
| 650 |
return confmap.NewRetrieved("ubuntu") |
| 651 |
case "env:PR": |
| 652 |
return confmap.NewRetrieved("amd") |
| 653 |
case "env:PORT": |
| 654 |
return confmap.NewRetrieved(3044) |
| 655 |
case "env:INT": |
| 656 |
return confmap.NewRetrieved(1) |
| 657 |
case "env:INT32": |
| 658 |
return confmap.NewRetrieved(32) |
| 659 |
case "env:INT64": |
| 660 |
return confmap.NewRetrieved(64) |
| 661 |
case "env:FLOAT32": |
| 662 |
return confmap.NewRetrieved(float32(3.25)) |
| 663 |
case "env:FLOAT64": |
| 664 |
return confmap.NewRetrieved(float64(6.4)) |
| 665 |
case "env:BOOL": |
| 666 |
return confmap.NewRetrieved(true) |
| 667 |
} |
| 668 |
return nil, errors.New("impossible") |
| 669 |
}) |
| 670 |
} |
| 671 |
|
| 672 |
func newDefaultConfigProviderSettings(tb testing.TB, uris []string) ConfigProviderSettings { |
| 673 |
fileProvider := newFakeProvider("file", func(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 674 |
return confmap.NewRetrieved(newConfFromFile(tb, uri[5:])) |
| 675 |
}) |
| 676 |
return ConfigProviderSettings{ |
| 677 |
ResolverSettings: confmap.ResolverSettings{ |
| 678 |
URIs: uris, |
| 679 |
ProviderFactories: []confmap.ProviderFactory{ |
| 680 |
fileProvider, |
| 681 |
newEnvProvider(), |
| 682 |
}, |
| 683 |
}, |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
func newConfFromFile(tb testing.TB, fileName string) map[string]any { |
| 689 |
content, err := os.ReadFile(filepath.Clean(fileName)) |
| 690 |
require.NoErrorf(tb, err, "unable to read the file %v", fileName) |
| 691 |
|
| 692 |
var data map[string]any |
| 693 |
require.NoError(tb, yaml.Unmarshal(content, &data), "unable to parse yaml") |
| 694 |
|
| 695 |
return confmap.NewFromStringMap(data).ToStringMap() |
| 696 |
} |
| 697 |
|
| 698 |
func TestProviderAndConverterModules(t *testing.T) { |
| 699 |
set := CollectorSettings{ |
| 700 |
BuildInfo: component.NewDefaultBuildInfo(), |
| 701 |
Factories: nopFactories, |
| 702 |
ConfigProviderSettings: newDefaultConfigProviderSettings(t, []string{filepath.Join("testdata", "otelcol-nop.yaml")}), |
| 703 |
ProviderModules: map[string]string{ |
| 704 |
"nop": "go.opentelemetry.io/collector/confmap/provider/testprovider v1.2.3", |
| 705 |
}, |
| 706 |
ConverterModules: []string{ |
| 707 |
"go.opentelemetry.io/collector/converter/testconverter v1.2.3", |
| 708 |
}, |
| 709 |
} |
| 710 |
col, err := NewCollector(set) |
| 711 |
require.NoError(t, err) |
| 712 |
wg := startCollector(context.Background(), t, col) |
| 713 |
require.NoError(t, err) |
| 714 |
providerModules := map[string]string{ |
| 715 |
"nop": "go.opentelemetry.io/collector/confmap/provider/testprovider v1.2.3", |
| 716 |
} |
| 717 |
converterModules := []string{ |
| 718 |
"go.opentelemetry.io/collector/converter/testconverter v1.2.3", |
| 719 |
} |
| 720 |
assert.Equal(t, providerModules, col.set.ProviderModules) |
| 721 |
assert.Equal(t, converterModules, col.set.ConverterModules) |
| 722 |
col.Shutdown() |
| 723 |
wg.Wait() |
| 724 |
} |