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] Add MustNewType constructor for component.Type #9414

Merged
merged 17 commits into from
Feb 2, 2024

Conversation

mx-psi
Copy link
Member

@mx-psi mx-psi commented Jan 29, 2024

Description:

  • Adds component.MustNewType to create a type. This function panics if the type has invalid characters. Add similar functions component.MustNewID and component.MustNewIDWithName.
  • Adds component.Type.String to recover the string
  • Use component.MustNewType, component.MustNewID, component.MustNewIDWithName and component.Type.String everywhere in this codebase. To do this I changed component.Type into an opaque struct and checked for compile-time errors.

Some notes:

  1. All components currently on core and contrib follow this rule. This is still breaking for other components.
  2. A future PR will change this into a struct, to actually validate this (right now you can just do 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

@mx-psi mx-psi requested a review from a team as a code owner January 29, 2024 17:32
@mx-psi mx-psi requested review from Aneurysm9 and evan-bradley and removed request for Aneurysm9 January 29, 2024 17:32
Copy link

codecov bot commented Jan 29, 2024

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (3665732) 90.46% compared to head (78139c7) 90.41%.

Files Patch % Lines
component/identifiable.go 72.22% 4 Missing and 1 partial ⚠️
cmd/mdatagen/validate.go 0.00% 2 Missing and 1 partial ⚠️
component/config.go 86.66% 1 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

@mx-psi
Copy link
Member Author

mx-psi commented Jan 29, 2024

I wonder if I should add a function to do component.NewID(component.MustType(<string literal>)) in one go, since it is a very common pattern in tests and internals.

component/config.go Show resolved Hide resolved
component/config.go Outdated Show resolved Hide resolved
component/config.go Outdated Show resolved Hide resolved
@evan-bradley
Copy link
Contributor

I wonder if I should add a function to do component.NewID(component.MustType(<string literal>)) in one go, since it is a very common pattern in tests and internals.

It sounds like we want to also validate the component name according to what you wrote in #9208, could we transition from NewID to MustNewID and validate component.MustNewID("mytype")? In my opinion we shouldn't require an extra function call to do the validation. This would also line up with component.MustNewIDWithName("mytype", "myname").

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)'
@mx-psi mx-psi changed the title [component] Add MustType constructor for component.Type [component] Add MustNewType constructor for component.Type Jan 30, 2024
Copy link
Member

@bogdandrutu bogdandrutu left a 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?

component/identifiable.go Outdated Show resolved Hide resolved
@mx-psi
Copy link
Member Author

mx-psi commented Jan 31, 2024

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 :)

evan-bradley pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Feb 1, 2024
…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
Copy link
Member

@bogdandrutu bogdandrutu left a 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 {
Copy link
Member

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.

Copy link
Member Author

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

component/config.go Outdated Show resolved Hide resolved
cparkins pushed a commit to AmadeusITGroup/opentelemetry-collector-contrib that referenced this pull request Feb 1, 2024
)

**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
cparkins pushed a commit to AmadeusITGroup/opentelemetry-collector-contrib that referenced this pull request Feb 1, 2024
**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
cparkins pushed a commit to AmadeusITGroup/opentelemetry-collector-contrib that referenced this pull request Feb 1, 2024
**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
cparkins pushed a commit to AmadeusITGroup/opentelemetry-collector-contrib that referenced this pull request Feb 1, 2024
…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
mx-psi added a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Feb 2, 2024
#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
@mx-psi
Copy link
Member Author

mx-psi commented Feb 2, 2024

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

@mx-psi mx-psi merged commit 26c157e into open-telemetry:main Feb 2, 2024
35 of 46 checks passed
@mx-psi mx-psi deleted the mx-psi/component-type-validation branch February 2, 2024 16:33
mx-psi added a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Feb 6, 2024
**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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
)

**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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
**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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
**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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
…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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
open-telemetry#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
anthoai97 pushed a commit to anthoai97/opentelemetry-collector-contrib that referenced this pull request Feb 12, 2024
…1038)

**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
mx-psi added a commit that referenced this pull request Mar 6, 2024
kristofgyuracz pushed a commit to axoflow/countconnector that referenced this pull request Jun 11, 2024
**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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants