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

Merged
merged 9 commits into from
Feb 12, 2025
Merged
25 changes: 20 additions & 5 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ import (
// Resources should be passed and stored as pointers
// (`*resource.Resource`). The `nil` value is equivalent to an empty
// Resource.
//
// Note that the Go == operator compares not just the resource attributes but
// also all other internals of the Resource type. Therefore, Resource values
tigrannajaryan marked this conversation as resolved.
Show resolved Hide resolved
// should not be used as map or database keys. In general, the [Resource.Equal]
tigrannajaryan marked this conversation as resolved.
Show resolved Hide resolved
// method should be used instead of direct comparison with ==, since that
// method ensures the correct comparison of resource attributes, and the
// [attribute.Distinct] returned from [Resource.Equivalent] should be used for
// map and database keys instead.
type Resource struct {
attrs attribute.Set
schemaURL string
}

// Compile-time check that the Resource remains comparable.
var _ map[Resource]struct{} = nil

var (
defaultResource *Resource
defaultResourceOnce sync.Once
Expand Down Expand Up @@ -137,15 +148,19 @@ func (r *Resource) Iter() attribute.Iterator {
return r.attrs.Iter()
}

// Equal returns true when a Resource is equivalent to this Resource.
func (r *Resource) Equal(eq *Resource) bool {
// Equal returns whether r and o represent the same resource. Two resources can
// be equal even if they have different schema URLs.
//
// See the documentation on the [Resource] type for the pitfalls of using ==
// with Resource values; most code should use Equal instead.
func (r *Resource) Equal(o *Resource) bool {
if r == nil {
r = Empty()
}
if eq == nil {
eq = Empty()
if o == nil {
o = Empty()
}
return r.Equivalent() == eq.Equivalent()
return r.Equivalent() == o.Equivalent()
}

// Merge creates a new [Resource] by merging a and b.
Expand Down
Loading