Skip to content

Commit

Permalink
[component] Change component.Type underlying type to a struct (#9472)
Browse files Browse the repository at this point in the history
**Description:** 

Follow up to #9414 and
open-telemetry/opentelemetry-collector-contrib/pull/31038.

**Link to tracking Issue:** Fixes #9208.
  • Loading branch information
mx-psi committed Mar 6, 2024
1 parent 2832cd5 commit b269362
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_switch-to-type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: component

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Change underlying type of `component.Type` to an opaque struct.

# One or more tracking issues or pull requests related to the change
issues: [9208]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
24 changes: 15 additions & 9 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ func callValidateIfPossible(v reflect.Value) error {
}

// Type is the component type as it is used in the config.
type Type string
type Type struct {
name string
}

// String returns the string representation of the type.
func (t Type) String() string {
return string(t)
return t.name
}

// typeRegexp is used to validate the type of a component.
Expand All @@ -130,12 +132,12 @@ var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`)
// - can only contain ASCII alphanumeric characters and '_'.
func NewType(ty string) (Type, error) {
if len(ty) == 0 {
return Type(""), fmt.Errorf("id must not be empty")
return Type{}, fmt.Errorf("id must not be empty")
}
if !typeRegexp.MatchString(ty) {
return Type(""), fmt.Errorf("invalid character(s) in type %q", ty)
return Type{}, fmt.Errorf("invalid character(s) in type %q", ty)
}
return Type(ty), nil
return Type{name: ty}, nil
}

// MustNewType creates a type. It panics if the type is invalid.
Expand All @@ -155,14 +157,18 @@ func MustNewType(strType string) Type {
// collecting metrics, traces and logs, this can expand in the future.
type DataType = Type

func mustNewDataType(strType string) DataType {
return MustNewType(strType)
}

// Currently supported data types. Add new data types here when new types are supported in the future.
const (
var (
// DataTypeTraces is the data type tag for traces.
DataTypeTraces DataType = "traces"
DataTypeTraces = mustNewDataType("traces")

// DataTypeMetrics is the data type tag for metrics.
DataTypeMetrics DataType = "metrics"
DataTypeMetrics = mustNewDataType("metrics")

// DataTypeLogs is the data type tag for logs.
DataTypeLogs DataType = "logs"
DataTypeLogs = mustNewDataType("logs")
)
2 changes: 1 addition & 1 deletion component/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/require"
)

var _ fmt.Stringer = (Type)("")
var _ fmt.Stringer = Type{}

type configChildStruct struct {
Child errConfig
Expand Down
2 changes: 1 addition & 1 deletion config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
name: "with_auth_configuration_has_extension_and_compression",
settings: ClientConfig{
Endpoint: "localhost:1234",
Auth: &configauth.Authentication{AuthenticatorID: component.NewID("mock")},
Auth: &configauth.Authentication{AuthenticatorID: component.MustNewID("mock")},
Compression: configcompression.TypeGzip,
},
shouldErr: false,
Expand Down

0 comments on commit b269362

Please sign in to comment.