Skip to content

Resolve schemaUrl when merging ResourceInfo#627

Merged
tidal merged 1 commit into
open-telemetry:mainfrom
lalex:schema_url
Apr 2, 2022
Merged

Resolve schemaUrl when merging ResourceInfo#627
tidal merged 1 commit into
open-telemetry:mainfrom
lalex:schema_url

Conversation

@lalex

@lalex lalex commented Mar 21, 2022

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Mar 21, 2022

Copy link
Copy Markdown

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

tidal commented Mar 21, 2022

Copy link
Copy Markdown
Member

Thank you!

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
@@ -42,15 +45,15 @@ public static function create(AttributesInterface $attributes, ?string $schemaUr
*/
public static function merge(ResourceInfo ...$resources): self

Copy link
Copy Markdown
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.

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
$initialResource = array_shift($resources);

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

Copy link
Copy Markdown
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.

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
return $this->schemaUrl;
}

private function mergeWith(ResourceInfo $resource): ResourceInfo

Copy link
Copy Markdown
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.

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
$attributes = $this->attributes->toArray() + $resource->getAttributes()->toArray();

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
$schemaUrl = null;
if (null === $this->schemaUrl) {
$schemaUrl = $resource->getSchemaUrl();
} elseif (null === $resource->getSchemaUrl()) {

Copy link
Copy Markdown
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.

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
$schemaUrl = $this->schemaUrl;
} elseif ($this->schemaUrl === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;
} else {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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. :)

Comment thread src/SDK/Resource/ResourceInfo.php Outdated
} elseif (null === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;
} elseif ($this->schemaUrl === $resource->getSchemaUrl()) {
$schemaUrl = $this->schemaUrl;

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

lalex commented Mar 22, 2022

Copy link
Copy Markdown
Contributor Author

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

@tidal

tidal commented Mar 22, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Contributor

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

@lalex

lalex commented Apr 1, 2022

Copy link
Copy Markdown
Contributor Author

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

brettmc commented Apr 1, 2022

Copy link
Copy Markdown
Contributor

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

tidal commented Apr 2, 2022

Copy link
Copy Markdown
Member

@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.

5 participants