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

Resource.Builder for convenient work with Resource class #3065

Merged
merged 7 commits into from
Mar 31, 2021

Conversation

piotr-sumo
Copy link
Contributor

@piotr-sumo piotr-sumo commented Mar 26, 2021

Fixes #2975

return builder().putAll(this);
}

public static class Builder {
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't use inner classes, can you move to ResourceBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will do.

@@ -168,4 +168,89 @@ private static boolean isValid(String name) {
private static boolean isValidAndNotEmpty(AttributeKey<?> name) {
return !name.getKey().isEmpty() && isValid(name.getKey());
}

public static Resource.Builder builder() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add javadoc, you can probably mostly copy/paste from Attributes

// then
Resource resource = builder.build();
// yes, it's supposed to be the reference check
assertThat(resource != Resource.getDefault()).isTrue();
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't really need this assertion, but if including, assertThat(resource).isNotSameAs(...)

}

@Test
void shouldBuilderCopyResource() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll need to add test coverage of all the new methods

* @param resource {@link Resource} whoose attributes will be copied
* @return this Builder
*/
public Builder putAll(@Nullable Resource resource) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should have defensive null checks for all the methods but we don't need to accept @Nullable here I think

return new Resource.Builder();
}

public Builder toBuilder() {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we add this to incubator first, we would add ResourceBuilder.from(Resource and not add these methods yet. This class seems genuinely useful though and maybe we can go with it without incubator. @jkwatson what do you think?

@piotr-sumo
Copy link
Contributor Author

@anuraaga I've implemented all requested changes. I'm waiting for the info about the incubator - shall I move the code there or not.

@jkwatson jkwatson added the API API related issues label Mar 29, 2021
@jkwatson
Copy link
Contributor

My personal opinion is that this isn't a big enough change to warrant going through the incubation process. @bogdandrutu since this is an API addition, your opinion would be valuable.

@piotr-sumo
Copy link
Contributor Author

@jkwatson I've addressed your comments.
@bogdandrutu could you review this PR?

@anuraaga
Copy link
Contributor

Thanks @piotr-sumo


/** Create the {@link Resource} from this. */
public Resource build() {
return new AutoValue_Resource(attributesBuilder.build());
Copy link
Contributor

Choose a reason for hiding this comment

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

should this just call Resource.create() rather than having an extra reference to the autovalue implementation ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for hint, I've fixed this.

@anuraaga anuraaga merged commit 1a9c909 into open-telemetry:main Mar 31, 2021
@anuraaga
Copy link
Contributor

Thanks @piotr-sumo !

@piotr-sumo
Copy link
Contributor Author

@anuraaga I'm happy to contribute 😄

This was referenced Dec 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API API related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Helper for merging a Resource with one or two key/value pairs
3 participants