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

Resolve schemaUrl when merging ResourceInfo #627

Merged
merged 1 commit into from
Apr 2, 2022

Conversation

lalex
Copy link
Contributor

@lalex lalex commented Mar 21, 2022

Continue with the #445. This PR to resolve a resulting schemaUrl when merging ResourceInfos. Reference to the spec.

@codecov
Copy link

codecov bot commented Mar 21, 2022

Codecov Report

Merging #627 (ea468b3) into main (0bc3731) will decrease coverage by 0.18%.
The diff coverage is 0.00%.

❗ Current head ea468b3 differs from pull request most recent head 89d4d43. Consider uploading reports for the commit 89d4d43 to get more accurate results

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #627      +/-   ##
============================================
- Coverage     84.45%   84.27%   -0.19%     
- Complexity     1141     1146       +5     
============================================
  Files           127      127              
  Lines          2766     2772       +6     
============================================
  Hits           2336     2336              
- Misses          430      436       +6     
Flag Coverage Δ
7.4 84.21% <0.00%> (-0.19%) ⬇️
8.0 84.27% <0.00%> (-0.19%) ⬇️
8.1 84.27% <0.00%> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/SDK/Resource/ResourceInfoFactory.php 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0bc3731...89d4d43. Read the comment docs.

@tidal
Copy link
Member

tidal commented Mar 21, 2022

Thank you!

@@ -42,15 +45,15 @@ public static function create(AttributesInterface $attributes, ?string $schemaUr
*/
public static function merge(ResourceInfo ...$resources): self
Copy link
Member

Choose a reason for hiding this comment

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

Please note that this method is about to be extracted out.

$initialResource = array_shift($resources);

return array_reduce($resources, function (ResourceInfo $old, ResourceInfo $updating): ResourceInfo {
return $old->mergeWith($updating);
Copy link
Member

Choose a reason for hiding this comment

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

Calling the mergeWith will result in unnecessary object creation when used in defaultResource, however this could be optomized later.

@@ -122,4 +125,25 @@ public function getSchemaUrl(): ?string
{
return $this->schemaUrl;
}

private function mergeWith(ResourceInfo $resource): ResourceInfo
Copy link
Member

Choose a reason for hiding this comment

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

Please make this method public and add it to the interface, since static mergeis subject to be moved.

$attributes = $this->attributes->toArray() + $resource->getAttributes()->toArray();

$schemaUrl = null;
if (null === $this->schemaUrl) {
Copy link
Member

Choose a reason for hiding this comment

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

Please, do not use yoda style.
Please, use the accessor for this property here, since the accessor is used in the updatig resource and this will make it easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure that php-cs-fixer is configured properly to avoid yoda style.

Copy link
Member

Choose a reason for hiding this comment

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

No it's not, as there is a lot of it in the code atm, which would needed to be changed (not sure if the fixer can handle this correctly).
However we have agreed in a meetig, that we'd rather want to avoid it in the future.

$schemaUrl = null;
if (null === $this->schemaUrl) {
$schemaUrl = $resource->getSchemaUrl();
} elseif (null === $resource->getSchemaUrl()) {
Copy link
Member

Choose a reason for hiding this comment

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

Please, do not use yoda style.

$schemaUrl = $this->schemaUrl;
} elseif ($this->schemaUrl === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Please refactor these conditions in a method which simply resolves the schemaURL and remove else/elseif.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This piece of code is self-describing and follows the specification. A simplified version could hide an original logic.

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't go for a simplified version, but rather avoiding the "IFs" with return statements.
I know it's to be found like that in the spec, which doesn't really make the spec better in my eyes.
However avoiding "IFs" is my personal issue, so I'd at least ask for it. Feel free to ignore my request. :)

} elseif (null === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;
} elseif ($this->schemaUrl === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;
Copy link
Member

Choose a reason for hiding this comment

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

See above.

*/
public function test_merge_schema_url($oldScheme, $updatingScheme, $expectedScheme, $shouldLog): void
{
if ($shouldLog) {
Copy link
Member

Choose a reason for hiding this comment

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

Please, do not use conditions in tests. This can simply be a separate test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks!


$primary = ResourceInfo::create(new Attributes([]), $oldScheme);
$secondary = ResourceInfo::create(new Attributes([]), $updatingScheme);
$result = ResourceInfo::merge($primary, $secondary);
Copy link
Member

Choose a reason for hiding this comment

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

Please test against the mergeWithmethod here. (See above)

@lalex
Copy link
Contributor Author

lalex commented Mar 22, 2022

Let's postpone this PR until the ResourceInfo refactoring is finished in #621.

@tidal
Copy link
Member

tidal commented Mar 22, 2022

Let's postpone this PR until the ResourceInfo refactoring is finished in #621.

Let's postpone this PR until the ResourceInfo refactoring is finished in #621.

Ok.

@kishannsangani
Copy link
Member

Let's postpone this PR until the ResourceInfo refactoring is finished in #621.

@lalex ResourceInfo refactoring is done in #628 and has been merged in the main branch.

@bobstrecansky
Copy link
Collaborator

I think you should be able to continue work on this when you'd like to @lalex !

@lalex
Copy link
Contributor Author

lalex commented Apr 1, 2022

I've decided to not to go too deep into the process of schema merging. The only significant thing is done is the setting an empty schemaUrl in case of conflict detected.
I think the PR is ready to be merged.

PS. I didn't find a proper way to write a log message from static context. @tidal, could you get a hint?

@brettmc
Copy link
Collaborator

brettmc commented Apr 1, 2022

PS. I didn't find a proper way to write a log message from static context.

Hi @lalex - I don't think logging messages from static methods has come up yet, but looking at LogsMessagesTrait, I suspect it can be changed to use static functions so that it works for both static and non-static use cases. I will have time to play around with that later today.

@tidal
Copy link
Member

tidal commented Apr 2, 2022

@lalex Thanks for your work!

PS. I didn't find a proper way to write a log message from static context. @tidal, could you get a hint?

You could access the LoggerHolder's get method to get the logger as it's static anyway
(The constructor is just there to help with dependendy injection in frameworks)

Anyway,I will merge the PR for now.

@tidal tidal merged commit 445e932 into open-telemetry:main Apr 2, 2022
@yuktea yuktea mentioned this pull request Jun 4, 2022
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