Files
Dmitry AnoshinandGitHub 6ed11d9842 Add support for deprecated component type aliases (#14349)
Related to
https://github.com/open-telemetry/opentelemetry-collector/issues/14208

This change allows component authors to register one optional deprecated
alias per component type, enabling smooth migrations when renaming
components. When an alias is used in configuration, the collector will
automatically resolve it to the original component and log a deprecation
warning.

Changes:
- Add `WithDeprecatedTypeAlias` factory option to experimental modules
(xreceiver, xexporter, xprocessor, xconnector)
- Generate alias expansion code in builder template that:
  - Populates factory maps with aliases pointing to original factories
  - Updates module information maps for aliased types
  - Validates that aliases don't conflict with existing component types
- Add deprecation warnings that log when an alias is used: "Using
deprecated alias <alias>; use <original> instead"

Extensions are left aside for now.
2026-01-08 00:01:09 +00:00

129 lines
3.3 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package builders
import (
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/internal/componentalias"
)
// mockFactory is a test factory that implements component.Factory
type mockFactory struct {
factoryType component.Type
}
func (m *mockFactory) Type() component.Type {
return m.factoryType
}
func (m *mockFactory) CreateDefaultConfig() component.Config {
return nil
}
// mockFactoryWithAlias is a test factory that implements both component.Factory and componentalias.TypeAliasHolder
type mockFactoryWithAlias struct {
factoryType component.Type
aliasHolder componentalias.TypeAliasHolder
}
func (m *mockFactoryWithAlias) Type() component.Type {
return m.factoryType
}
func (m *mockFactoryWithAlias) CreateDefaultConfig() component.Config {
return nil
}
func (m *mockFactoryWithAlias) DeprecatedAlias() component.Type {
return m.aliasHolder.DeprecatedAlias()
}
func (m *mockFactoryWithAlias) SetDeprecatedAlias(alias component.Type) {
m.aliasHolder.SetDeprecatedAlias(alias)
}
func TestLogDeprecatedTypeAlias(t *testing.T) {
tests := []struct {
name string
factory component.Factory
usedType component.Type
expectWarning bool
}{
{
name: "no_alias_holder",
factory: &mockFactory{factoryType: component.MustNewType("test")},
usedType: component.MustNewType("test"),
expectWarning: false,
},
{
name: "no_alias_set",
factory: &mockFactoryWithAlias{
factoryType: component.MustNewType("test"),
aliasHolder: componentalias.NewTypeAliasHolder(),
},
usedType: component.MustNewType("test"),
expectWarning: false,
},
{
name: "using_current_type",
factory: func() component.Factory {
f := &mockFactoryWithAlias{
factoryType: component.MustNewType("new"),
aliasHolder: componentalias.NewTypeAliasHolder(),
}
f.aliasHolder.SetDeprecatedAlias(component.MustNewType("old"))
return f
}(),
usedType: component.MustNewType("new"),
expectWarning: false,
},
{
name: "using_deprecated_alias",
factory: func() component.Factory {
f := &mockFactoryWithAlias{
factoryType: component.MustNewType("new"),
aliasHolder: componentalias.NewTypeAliasHolder(),
}
f.aliasHolder.SetDeprecatedAlias(component.MustNewType("old"))
return f
}(),
usedType: component.MustNewType("old"),
expectWarning: true,
},
{
name: "using_unrelated_type",
factory: func() component.Factory {
f := &mockFactoryWithAlias{
factoryType: component.MustNewType("new"),
aliasHolder: componentalias.NewTypeAliasHolder(),
}
f.aliasHolder.SetDeprecatedAlias(component.MustNewType("old"))
return f
}(),
usedType: component.MustNewType("other"),
expectWarning: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
core, logs := observer.New(zap.WarnLevel)
logger := zap.New(core)
logDeprecatedTypeAlias(logger, tt.factory, tt.usedType)
if tt.expectWarning && logs.Len() != 1 {
t.Errorf("expected 1 warning log but got %d", logs.Len())
} else if !tt.expectWarning && logs.Len() > 0 {
t.Errorf("expected no warning log but got %d", logs.Len())
}
})
}
}