-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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] Add MustNewType constructor for component.Type #9414
[component] Add MustNewType constructor for component.Type #9414
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #9414 +/- ##
==========================================
- Coverage 90.46% 90.41% -0.05%
==========================================
Files 344 344
Lines 18024 18058 +34
==========================================
+ Hits 16306 16328 +22
- Misses 1390 1399 +9
- Partials 328 331 +3 ☔ View full report in Codecov by Sentry. |
I wonder if I should add a function to do |
It sounds like we want to also validate the component name according to what you wrote in #9208, could we transition from |
To make the change I ran: - rg 'component.NewID\(component.MustNewType\(.*?\)\)' -l | xargs sd 'component.NewID\(component.MustNewType\((".*?")\)\)' 'component.MustNewID($1)' - rg 'component.NewIDWithName\(component.MustNewType\(.*?\), .*?\)' -l | xargs sd 'component.NewIDWithName\(component.MustNewType\((.*?)\), (.*?)\)' 'component.MustNewIDWithName($1, $2)'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, some feedback:
- I would prefer to also have a
NewType
with error since some may not feel comfortable with panic (not sure why, but good to have it I think). - Can metadatagen actually check the type and fail to generate code if the type is not ok?
Done :) |
🤔 This test is failing consistently, but I am not sure how it is related to the changes on this PR
edit: Ah, I guess it comes from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/2f5b0bc52cd160886b4e1ee03461704d6c440a8d/exporter/awsxrayexporter/awsxray_test.go#L73? |
…Ds (#30994) **Description:** - Makes the test on awsxray exporter use the `exportertest.NewNopCreateSettings().ID`. This is because this is the ID passed to `newTracesExporter`: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/2f5b0bc52cd160886b4e1ee03461704d6c440a8d/exporter/awsxrayexporter/awsxray_test.go#L109 Currently, this makes no difference, but it will once the ID on NewNopCreateSettings is fixed. - Also fixes the hostmetrics receiver ID, just for fun :) **Link to tracking Issue:** Needed for open-telemetry/opentelemetry-collector/pull/9414
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can merge after fixing or disagreeing with my nit comment.
// can only contain ASCII alphanumeric characters and '_'. | ||
// This must be kept in sync with the regex in component/config.go. | ||
var typeRegexp = regexp.MustCompile(`^[a-zA-Z][0-9a-zA-Z_]*$`) | ||
|
||
func (md *metadata) validateType() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call NewType
and ignore return value, only return error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that that would be better, but it's not easy to do: it would mean adding a replace
statement on cmd/mdatagen/go.mod
for component
, and that makes go install
fail. See e.g. open-telemetry/opentelemetry-collector-contrib/issues/27855 for a recent instance of this issue with telemetrygen
) **Description:** Before this change an empty name was passed to the scraperhelper. **Link to tracking Issue:** This is needed to make contrib tests pass on open-telemetry/opentelemetry-collector/pull/9414
**Description:** Same as open-telemetry#30917, before this change an empty name could be passed to the scraperhelper. I think this is the only other instance of this pattern **Link to tracking Issue**: This is needed to make contrib tests pass on open-telemetry/opentelemetry-collector/pull/9414
**Description:** Similar to open-telemetry#30917 and open-telemetry#30925, the ID was empty before. **Link to tracking Issue:** This is needed to make contrib tests pass on open-telemetry/opentelemetry-collector#9414
…Ds (open-telemetry#30994) **Description:** - Makes the test on awsxray exporter use the `exportertest.NewNopCreateSettings().ID`. This is because this is the ID passed to `newTracesExporter`: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/2f5b0bc52cd160886b4e1ee03461704d6c440a8d/exporter/awsxrayexporter/awsxray_test.go#L109 Currently, this makes no difference, but it will once the ID on NewNopCreateSettings is fixed. - Also fixes the hostmetrics receiver ID, just for fun :) **Link to tracking Issue:** Needed for open-telemetry/opentelemetry-collector/pull/9414
#31015) **Description:** Explicit uses `component.Type` when building string to compare to in test. Remove comment that will soon be outdated about the component.Type. **Link to tracking Issue:** Needed for open-telemetry/opentelemetry-collector/pull/9414
I have limited visibility into contrib tests because they are broken because of unrelated reasons, but I am reasonably sure that all the tests related to this have been fixed on contrib, so I will merge this by (my TZ) EOD |
**Description:** Follow up to open-telemetry/opentelemetry-collector/pull/9414, adds the same changes in `mdatagen` regarding validation of `component.Type`s. On this PR the validation logic and templating is a bit more complex because of subcomponents. I will add these changes back on core once this PR is merged. **Link to tracking Issue:** open-telemetry/opentelemetry-collector/issues/9208
**Description:** Follow up to #9414 and open-telemetry/opentelemetry-collector-contrib/pull/31038. **Link to tracking Issue:** Fixes #9208.
Description:
component.MustNewType
to create a type. This function panics if the type has invalid characters. Add similar functionscomponent.MustNewID
andcomponent.MustNewIDWithName
.component.Type.String
to recover the stringcomponent.MustNewType
,component.MustNewID
,component.MustNewIDWithName
andcomponent.Type.String
everywhere in this codebase. To do this I changedcomponent.Type
into an opaque struct and checked for compile-time errors.Some notes:
component.Type("anything")
to bypass validation). I want to do this in two steps to avoid breaking contrib tests: we first introduce this function, and after that we change into a struct.Link to tracking Issue: Updates #9208