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

Refactor io.opentelemetry.instrumentation.resources.ContainerResource to avoid using null #6889

Merged
merged 3 commits into from
Oct 19, 2022

Conversation

edysli
Copy link
Contributor

@edysli edysli commented Oct 15, 2022

While I was looking at issues #6694 and open-telemetry/opentelemetry-java#2337, I saw that the code in io.opentelemetry.instrumentation.resources.ContainerResource used null several times as return value which isn't safe. Nowadays, Optional is better suited to signal the absence of a result, so I refactored ContainerResource to use Optionals instead of null.

On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to #6694.

@edysli edysli requested a review from a team October 15, 2022 22:28
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 15, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

Refactor class `ContainerResource` so that its methods return
`Optional`s instead of `null`. This makes for safer code that doesn't
rely on `null` to signal the absence of result. Moreover,
`Optional.map()` and `Optional.orElseGet()` use the same functional
style as `Stream`, which is well readable.

Issue-Id: open-telemetry#6694
Issue-Id: open-telemetry/opentelemetry-java#2337
@edysli edysli force-pushed the less_nulls_in_ContainerResource branch from 2eb89f9 to 29cbbfa Compare October 15, 2022 22:30
Comment on lines +63 to +66
.map(ContainerResource::getIdFromLine)
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't

Suggested change
.map(ContainerResource::getIdFromLine)
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
.flatMap(ContainerResource::getIdFromLine);

work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, unfortunately flatMap() won't work here because ContainerResource::getIdFromLine returns Optional<String> and we end up with Stream<Optional<String>>. It would work as the last step though, to "pop" the Optional<Optional<String>>:

Suggested change
.map(ContainerResource::getIdFromLine)
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
.map(ContainerResource::getIdFromLine)
.filter(Optional::isPresent)
.findFirst()
.flatMap(identity());

Is the last line clearer this way?

@mateuszrzeszutek
Copy link
Member

Nowadays, Optional is better suited to signal the absence of a result, so I refactored ContainerResource to use Optionals instead of null.

In general I agree with this statement; but keep in mind that this project prefers to use @Nullable annotations in public APIs -- that's just the convention here. I think it's fine to use it here though.

On the way, I also refactored this class's unit tests into parameterised tests to reduce test code duplication. These improvements should help implementing a solution to #6694.

Looks pretty good! 🚀 Thanks!

Several test cases in `ContainerResourceTest` have the same outcome,
so instead of duplicating the assertions inside each test method, take
advantage of `@ParameterizedTest` to inject inputs and expected
outputs multiple times into the same test. This improves test code
readability by reducing test method length and making identical cases
more obvious.

Also rename test methods to better express the tested code's expected
behaviour.

Issue-Id: open-telemetry#6694
Issue-Id: open-telemetry/opentelemetry-java#2337
@edysli edysli force-pushed the less_nulls_in_ContainerResource branch from 29cbbfa to 034a2c1 Compare October 17, 2022 19:38
@edysli
Copy link
Contributor Author

edysli commented Oct 17, 2022

Thank you very much for the review @mateuszrzeszutek ! Would you mind labelling this PR with "hacktoberfest-accepted" so it counts toward my Hacktoberfest 2022 efforts? 😁

@trask trask enabled auto-merge (squash) October 19, 2022 01:46
@trask trask merged commit 6fb1f00 into open-telemetry:main Oct 19, 2022
dmarkwat pushed a commit to dmarkwat/opentelemetry-java-instrumentation that referenced this pull request Oct 22, 2022
…e` to avoid using null (open-telemetry#6889)

While I was looking at issues
open-telemetry#6694 and
open-telemetry/opentelemetry-java#2337, I saw that the code in
`io.opentelemetry.instrumentation.resources.ContainerResource` used
`null` several times as return value which isn't safe. Nowadays,
`Optional` is better suited to signal the absence of a result, so I
refactored `ContainerResource` to use `Optional`s instead of null.

On the way, I also refactored this class's unit tests into parameterised
tests to reduce test code duplication. These improvements should help
implementing a solution to
open-telemetry#6694.

Co-authored-by: Trask Stalnaker <[email protected]>
trask added a commit that referenced this pull request Oct 23, 2022
This was discussed a long while back, but never documented (and recently
came up in #6889)

Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
LironKS pushed a commit to helios/opentelemetry-java-instrumentation that referenced this pull request Oct 23, 2022
…e` to avoid using null (open-telemetry#6889)

While I was looking at issues
open-telemetry#6694 and
open-telemetry/opentelemetry-java#2337, I saw that the code in
`io.opentelemetry.instrumentation.resources.ContainerResource` used
`null` several times as return value which isn't safe. Nowadays,
`Optional` is better suited to signal the absence of a result, so I
refactored `ContainerResource` to use `Optional`s instead of null.

On the way, I also refactored this class's unit tests into parameterised
tests to reduce test code duplication. These improvements should help
implementing a solution to
open-telemetry#6694.

Co-authored-by: Trask Stalnaker <[email protected]>
LironKS pushed a commit to helios/opentelemetry-java-instrumentation that referenced this pull request Oct 31, 2022
…e` to avoid using null (open-telemetry#6889)

While I was looking at issues
open-telemetry#6694 and
open-telemetry/opentelemetry-java#2337, I saw that the code in
`io.opentelemetry.instrumentation.resources.ContainerResource` used
`null` several times as return value which isn't safe. Nowadays,
`Optional` is better suited to signal the absence of a result, so I
refactored `ContainerResource` to use `Optional`s instead of null.

On the way, I also refactored this class's unit tests into parameterised
tests to reduce test code duplication. These improvements should help
implementing a solution to
open-telemetry#6694.

Co-authored-by: Trask Stalnaker <[email protected]>
LironKS pushed a commit to helios/opentelemetry-java-instrumentation that referenced this pull request Oct 31, 2022
This was discussed a long while back, but never documented (and recently
came up in open-telemetry#6889)

Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
LironKS pushed a commit to helios/opentelemetry-java-instrumentation that referenced this pull request Dec 4, 2022
…e` to avoid using null (open-telemetry#6889)

While I was looking at issues
open-telemetry#6694 and
open-telemetry/opentelemetry-java#2337, I saw that the code in
`io.opentelemetry.instrumentation.resources.ContainerResource` used
`null` several times as return value which isn't safe. Nowadays,
`Optional` is better suited to signal the absence of a result, so I
refactored `ContainerResource` to use `Optional`s instead of null.

On the way, I also refactored this class's unit tests into parameterised
tests to reduce test code duplication. These improvements should help
implementing a solution to
open-telemetry#6694.

Co-authored-by: Trask Stalnaker <[email protected]>
LironKS pushed a commit to helios/opentelemetry-java-instrumentation that referenced this pull request Dec 4, 2022
This was discussed a long while back, but never documented (and recently
came up in open-telemetry#6889)

Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants