1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/attribute/iterator_test.go

149 lines
3.4 KiB
Go
Raw Normal View History

Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package attribute_test
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
import (
2020-05-21 08:20:29 +02:00
"fmt"
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
)
func TestIterator(t *testing.T) {
one := attribute.String("one", "1")
two := attribute.Int("two", 2)
lbl := attribute.NewSet(one, two)
iter := lbl.Iter()
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
require.Equal(t, 2, iter.Len())
require.True(t, iter.Next())
require.Equal(t, one, iter.Attribute())
idx, attr := iter.IndexedAttribute()
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
require.Equal(t, 0, idx)
require.Equal(t, one, attr)
require.Equal(t, 2, iter.Len())
require.True(t, iter.Next())
require.Equal(t, two, iter.Attribute())
idx, attr = iter.IndexedAttribute()
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
require.Equal(t, 1, idx)
require.Equal(t, two, attr)
require.Equal(t, 2, iter.Len())
require.False(t, iter.Next())
require.Equal(t, 2, iter.Len())
}
func TestEmptyIterator(t *testing.T) {
lbl := attribute.NewSet()
iter := lbl.Iter()
Update Resource (#613) * Update Resource When looking at grouping telemetry in an exporter based on the Resource it is ideal if a map can be make with the key being represented by a Resource. However, given the Resource is not hashable, this is not possible. This add a `String` method that can be used as a map key during grouping. Additionally, this means the Resource now implements the `Stringer` interface providing human-readable info when prited. The internal structure of the Resource is changed. A static slice containing all key-values in a sorted order replaces the existing map. Additionally a set of keys is added to accommodate lookup during `Merge`. Also, the string representation is kept in an internal field so as to save processing for the `String` method (all fields are assumed to be static after creation). The `Attributes` method now returns a sorted slice of the associated key-values. The `Merge` method has been updated to support the changed structure of the Resource. New tests are added to validate the `String` method. * Update comment * Change loop into returned append * Update key-value less func Keys are unique in this package, treat them that way. * Remove unnecessary allocation on empty attributes * Update `Merge` method Remove incomplete sorting of merged slices. Instead use the `sort` package. Add tests to catch sorting failure identified. * Apply suggestions from code review Co-Authored-By: ET <evantorrie@users.noreply.github.com> * Escape Resource string representation To ensure uniqueness of the string representation, the key-value content needs to be escaped. * Switch to an eager evaluation for the `String` method * Refactor `Merge` method Leave optimization to the future and simplify the merge. * Add AttributeIterator Include a method for a user of the Resource to iterate over the related attributes without needed to copy the attributes. * Fix ineffectual * Fix lint * Add licenses * keys -> keySet for Resource Co-authored-by: ET <evantorrie@users.noreply.github.com> Co-authored-by: Rahul Patel <rahulpa@google.com>
2020-04-07 21:15:36 +02:00
require.Equal(t, 0, iter.Len())
require.False(t, iter.Next())
}
2020-05-21 08:20:29 +02:00
func TestMergedIterator(t *testing.T) {
type inputs struct {
name string
keys1 []string
keys2 []string
expect []string
}
makeAttributes := func(keys []string, num int) (result []attribute.KeyValue) {
2020-05-21 08:20:29 +02:00
for _, k := range keys {
result = append(result, attribute.Int(k, num))
2020-05-21 08:20:29 +02:00
}
return
}
for _, input := range []inputs{
{
name: "one overlap",
keys1: []string{"A", "B"},
keys2: []string{"B", "C"},
expect: []string{"A/1", "B/1", "C/2"},
},
{
name: "reversed one overlap",
keys1: []string{"B", "A"},
keys2: []string{"C", "B"},
expect: []string{"A/1", "B/1", "C/2"},
},
{
name: "one empty",
keys1: nil,
keys2: []string{"C", "B"},
expect: []string{"B/2", "C/2"},
},
{
name: "two empty",
keys1: []string{"C", "B"},
keys2: nil,
expect: []string{"B/1", "C/1"},
},
{
name: "no overlap both",
keys1: []string{"C"},
keys2: []string{"B"},
expect: []string{"B/2", "C/1"},
},
{
name: "one empty single two",
keys1: nil,
keys2: []string{"B"},
expect: []string{"B/2"},
},
{
name: "two empty single one",
keys1: []string{"A"},
keys2: nil,
expect: []string{"A/1"},
},
{
name: "all empty",
keys1: nil,
keys2: nil,
expect: nil,
},
{
name: "full overlap",
keys1: []string{"A", "B", "C", "D"},
keys2: []string{"A", "B", "C", "D"},
expect: []string{"A/1", "B/1", "C/1", "D/1"},
},
} {
t.Run(input.name, func(t *testing.T) {
attr1 := makeAttributes(input.keys1, 1)
attr2 := makeAttributes(input.keys2, 2)
2020-05-21 08:20:29 +02:00
set1 := attribute.NewSet(attr1...)
set2 := attribute.NewSet(attr2...)
2020-05-21 08:20:29 +02:00
merge := attribute.NewMergeIterator(&set1, &set2)
2020-05-21 08:20:29 +02:00
var result []string
for merge.Next() {
attr := merge.Attribute()
result = append(result, fmt.Sprint(attr.Key, "/", attr.Value.Emit()))
2020-05-21 08:20:29 +02:00
}
require.Equal(t, input.expect, result)
})
}
}