1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-20 03:30:02 +02:00

resource.Merge uses label.MergeIterator

This commit is contained in:
jmacd 2020-05-21 00:33:39 -07:00
parent 0b5080372a
commit 1c8c5df4db
2 changed files with 23 additions and 10 deletions

View File

@ -91,13 +91,12 @@ func (i *Iterator) ToSlice() []kv.KeyValue {
return slice
}
// NewMergeIterator returns a MergeIterator for merging two label set
// iterators. Duplicates are resolved by taking the value from the
// first iterator.
func NewMergeIterator(iter1, iter2 Iterator) MergeItererator {
// NewMergeIterator returns a MergeIterator for merging two label sets
// Duplicates are resolved by taking the value from the first set.
func NewMergeIterator(s1, s2 *Set) MergeItererator {
mi := MergeItererator{
one: makeOne(iter1),
two: makeOne(iter2),
one: makeOne(s1.Iter()),
two: makeOne(s2.Iter()),
}
return mi
}

View File

@ -89,15 +89,24 @@ func (r *Resource) Equal(eq *Resource) bool {
// If there are common keys between resource a and b, then the value
// from resource a is preserved.
func Merge(a, b *Resource) *Resource {
if a == nil && b == nil {
return Empty()
}
if a == nil {
a = Empty()
return b
}
if b == nil {
b = Empty()
return a
}
// Note: 'b' is listed first so that 'a' will overwrite with
// last-value-wins in label.Key()
combine := append(b.Attributes(), a.Attributes()...)
// combine := append(b.Attributes(), a.Attributes()...)
mi := label.NewMergeIterator(a.LabelSet(), b.LabelSet())
combine := make([]kv.KeyValue, 0, a.Len()+b.Len())
for mi.Next() {
combine = append(combine, mi.Label())
}
return New(combine...)
}
@ -111,10 +120,15 @@ func Empty() *Resource {
// between two resources. This value is suitable for use as a key in
// a map.
func (r *Resource) Equivalent() label.Distinct {
return r.LabelSet().Equivalent()
}
// LabelSet returns the equivalent *label.Set.
func (r *Resource) LabelSet() *label.Set {
if r == nil {
r = Empty()
}
return r.labels.Equivalent()
return &r.labels
}
// MarshalJSON encodes labels as a JSON list of { "Key": "...", "Value": ... }