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

51 lines
1.5 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package componentalias // import "go.opentelemetry.io/collector/internal/componentalias"
import (
"errors"
"fmt"
"go.opentelemetry.io/collector/component"
)
type TypeAliasHolder interface {
DeprecatedAlias() component.Type
SetDeprecatedAlias(component.Type)
}
func NewTypeAliasHolder() TypeAliasHolder {
ta := typeAlias(component.Type{})
return &ta
}
type typeAlias component.Type
// DeprecatedAlias returns the deprecated type typeAlias for this component, if any.
// Returns an empty component.Type if no typeAlias is configured.
func (ta *typeAlias) DeprecatedAlias() component.Type {
return component.Type(*ta)
}
// SetDeprecatedAlias sets the deprecated type typeAlias.
func (ta *typeAlias) SetDeprecatedAlias(newAlias component.Type) {
*ta = typeAlias(newAlias)
}
// ValidateComponentType returns an error if the provided factory does not match the provided component ID.
// It checks both the current type and any deprecated alias type.
func ValidateComponentType(f component.Factory, id component.ID) error {
if id.Type() == f.Type() {
return nil
}
errMsg := fmt.Sprintf("component type mismatch: component ID %q does not have type %q", id, f.Type())
if aliasHolder, ok := f.(TypeAliasHolder); ok && aliasHolder.DeprecatedAlias().String() != "" {
if id.Type() == aliasHolder.DeprecatedAlias() {
return nil
}
errMsg += fmt.Sprintf(" or deprecated alias type %q", aliasHolder.DeprecatedAlias())
}
return errors.New(errMsg)
}