Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[component] Change component.Type underlying type to a struct #9472

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am keeping this for now, but we can revisit once we tackle #9429

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The alternative is to change this to type DataType string)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an alias to Type because we use component.ID (which is not generic) for the pipeline.ID. Because of that we made this equal with 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
Loading