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

81 lines
2.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package xreceiver
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/consumer/xconsumer"
"go.opentelemetry.io/collector/internal/componentalias"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/internal"
)
var testID = component.MustNewID("test")
func TestNewFactoryWithProfiles(t *testing.T) {
testType := component.MustNewType("test")
defaultCfg := struct{}{}
factory := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithProfiles(createProfiles, component.StabilityLevelAlpha),
)
assert.Equal(t, testType, factory.Type())
assert.EqualValues(t, &defaultCfg, factory.CreateDefaultConfig())
assert.Equal(t, component.StabilityLevelAlpha, factory.ProfilesStability())
_, err := factory.CreateProfiles(context.Background(), receiver.Settings{ID: testID}, &defaultCfg, nil)
require.NoError(t, err)
wrongID := component.MustNewID("wrong")
wrongIDErrStr := internal.ErrIDMismatch(wrongID, testType).Error()
_, err = factory.CreateProfiles(context.Background(), receiver.Settings{ID: wrongID}, &defaultCfg, nil)
assert.EqualError(t, err, wrongIDErrStr)
}
var nopInstance = &nopReceiver{
Consumer: consumertest.NewNop(),
}
// nopReceiver stores consumed traces and metrics for testing purposes.
type nopReceiver struct {
component.StartFunc
component.ShutdownFunc
consumertest.Consumer
}
func createProfiles(context.Context, receiver.Settings, component.Config, xconsumer.Profiles) (Profiles, error) {
return nopInstance, nil
}
func TestNewFactoryWithDeprecatedAlias(t *testing.T) {
testType := component.MustNewType("newname")
aliasType := component.MustNewType("oldname")
defaultCfg := struct{}{}
f := NewFactory(
testType,
func() component.Config { return &defaultCfg },
WithProfiles(createProfiles, component.StabilityLevelAlpha),
WithDeprecatedTypeAlias(aliasType),
)
assert.Equal(t, testType, f.Type())
assert.Equal(t, aliasType, f.(*factory).Factory.(componentalias.TypeAliasHolder).DeprecatedAlias())
assert.EqualValues(t, &defaultCfg, f.CreateDefaultConfig())
_, err := f.CreateProfiles(context.Background(), receiver.Settings{ID: component.MustNewID("newname")}, &defaultCfg, consumertest.NewNop())
require.NoError(t, err)
_, err = f.CreateProfiles(context.Background(), receiver.Settings{ID: component.MustNewID("oldname")}, &defaultCfg, consumertest.NewNop())
require.NoError(t, err)
_, err = f.CreateProfiles(context.Background(), receiver.Settings{ID: component.MustNewID("wrongname")}, &defaultCfg, consumertest.NewNop())
require.Error(t, err)
}