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

Document and check resource comparability #6272

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MrAlias
Copy link
Contributor

@MrAlias MrAlias commented Feb 5, 2025

Ensure backwards compatibility by adding a compile-time check that the Resource remains comparable.

Document the shortcomings of direct comparison of a Resource.

Ensure backwards compatibility by adding a compile-time check that the
Resource remains comparable.

Document the shortcomings of direct comparison of a Resource.
@MrAlias MrAlias added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Feb 5, 2025
@MrAlias MrAlias added this to the v1.35.0 milestone Feb 5, 2025
// While Resource is comparable, its comparison should not be relied on when
// checking equality. The [Resource.Equal] method should be used to check
// equality between two resources and the [attribute.Distinct] returned from
// [Resource.Equivalent] should be used for map keys instead. No guantee of the
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we actually add this? Wouldn't it be breaking if resource comparisons stoped working?

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 intending to say that comparison is going away, just that the result of that comparison is not guaranteed to be stable. Is that not what you are interpreting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Line 36 is being added to ensure the resource will always remain comparable FWIW.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is acceptable in Go ecosystem. Especially given that the type already has Equal method.

I would suggest also giving a similar comment to Equal. Example: https://pkg.go.dev/time#Time.Equal I mean adding something similar to

See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

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 would suggest also giving a similar comment to Equal. Example: https://pkg.go.dev/time#Time.Equal I mean adding something similar to

See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

Updated, PTAL.

Copy link
Member

Choose a reason for hiding this comment

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

LGTM. I am not resolving so that @dashpole can see the conversation.

Copy link

codecov bot commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.8%. Comparing base (0c62fd1) to head (794e970).

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #6272   +/-   ##
=====================================
  Coverage   81.8%   81.8%           
=====================================
  Files        283     283           
  Lines      24892   24892           
=====================================
  Hits       20381   20381           
  Misses      4107    4107           
  Partials     404     404           

see 1 file with indirect coverage changes

// While Resource is comparable, its comparison should not be relied on when
// checking equality. The [Resource.Equal] method should be used to check
// equality between two resources and the [attribute.Distinct] returned from
// [Resource.Equivalent] should be used for map keys instead. No guantee of the
Copy link
Member

Choose a reason for hiding this comment

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

I think this is acceptable in Go ecosystem. Especially given that the type already has Equal method.

I would suggest also giving a similar comment to Equal. Example: https://pkg.go.dev/time#Time.Equal I mean adding something similar to

See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

@pellared pellared requested a review from dashpole February 5, 2025 22:38
//
// Note that the Go == operator compares not just the resource attributes but
// also all other internals of the Resource type. Therefore, Resource values
// should not be used as map or database keys. In general, the [Resource.Equal]
Copy link
Member

Choose a reason for hiding this comment

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

Given this assertion ("should not be used as..."), I am a bit confused about the goal of line 38. If the we are saying "don't use Resource as a map key" why are we explicitly requiring it to be usable as a map key on line 38?

I get that formal backward compatibility requires it but if this was never a reasonable thing to do, perhaps we can decide that it is not a practical backward compatibility requirement we are bound by?
Do we have a document that says comparability is a backward compatibility criteria? Or perhaps a document that says "what compiled in the past should compile in the future" as a catch-all guarantee?

Copy link
Member

@pellared pellared Feb 5, 2025

Choose a reason for hiding this comment

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

This is inspired by how the Go standard library handles such issues. See: https://pkg.go.dev/time#Time

I would say we do our best to follow https://go.dev/doc/go1compat which is kind of:

what compiled in the past should compile in the future

with some exceptions.

The Collector does have it documented here: https://github.com/open-telemetry/opentelemetry-collector/blob/main/VERSIONING.md#go-modules

Probably we should also document it in our https://github.com/open-telemetry/opentelemetry-go/blob/main/VERSIONING.md

Copy link
Member

Choose a reason for hiding this comment

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

with some exceptions.

What are the exceptions? The doc you linked to says: "It is intended that programs written to the Go 1 specification will continue to compile and run correctly, unchanged, over the lifetime of that specification". The "continue to compile" phrase doesn't seem to leave much room for exceptions.

Copy link
Member

@pellared pellared Feb 5, 2025

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I created #6274

Copy link
Member

Choose a reason for hiding this comment

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

OK. Can I argue for one more exception? That maintaining accidental comparability of the structs is not something we care about, unless it is explicitly documented (like it is done for attribute.Set)?

In this particular case it seems to create a whole lot of work to keep the Resource comparable, but unclear there is anyone to benefit from this work.

To be clear, I am not advocating for breaking a compatibility promise. I am merely suggesting that what previously was not explicitly promised is added as another exception (since we don't mind having exceptions).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants