1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-12-07 23:32:49 +02:00
Files
opentelemetry-go/sdk/resource/resource_test.go

213 lines
4.9 KiB
Go
Raw Normal View History

// 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 resource_test
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/sdk/resource"
)
var (
kv11 = core.Key("k1").String("v11")
kv12 = core.Key("k1").String("v12")
kv21 = core.Key("k2").String("v21")
kv31 = core.Key("k3").String("v31")
kv41 = core.Key("k4").String("v41")
)
func TestNew(t *testing.T) {
cases := []struct {
name string
in []core.KeyValue
want []core.KeyValue
}{
{
name: "New with common key order1",
in: []core.KeyValue{kv11, kv12, kv21},
want: []core.KeyValue{kv11, kv21},
},
{
name: "New with common key order2",
in: []core.KeyValue{kv12, kv11, kv21},
want: []core.KeyValue{kv12, kv21},
},
{
name: "New with nil",
in: nil,
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 12:15:36 -07:00
want: nil,
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
res := resource.New(c.in...)
if diff := cmp.Diff(
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 12:15:36 -07:00
res.Attributes(),
c.want,
cmp.AllowUnexported(core.Value{})); diff != "" {
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
func TestMerge(t *testing.T) {
cases := []struct {
name string
a, b *resource.Resource
want []core.KeyValue
}{
{
name: "Merge with no overlap, no nil",
a: resource.New(kv11, kv31),
b: resource.New(kv21, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
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 12:15:36 -07:00
{
name: "Merge with no overlap, no nil, not interleaved",
a: resource.New(kv11, kv21),
b: resource.New(kv31, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
{
name: "Merge with common key order1",
a: resource.New(kv11),
b: resource.New(kv12, kv21),
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 12:15:36 -07:00
want: []core.KeyValue{kv11, kv21},
},
{
name: "Merge with common key order2",
a: resource.New(kv12, kv21),
b: resource.New(kv11),
want: []core.KeyValue{kv12, kv21},
},
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 12:15:36 -07:00
{
name: "Merge with common key order4",
a: resource.New(kv11, kv21, kv41),
b: resource.New(kv31, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
{
name: "Merge with no keys",
a: resource.New(),
b: resource.New(),
want: nil,
},
{
name: "Merge with first resource no keys",
a: resource.New(),
b: resource.New(kv21),
want: []core.KeyValue{kv21},
},
{
name: "Merge with second resource no keys",
a: resource.New(kv11),
b: resource.New(),
want: []core.KeyValue{kv11},
},
{
name: "Merge with first resource nil",
a: nil,
b: resource.New(kv21),
want: []core.KeyValue{kv21},
},
{
name: "Merge with second resource nil",
a: resource.New(kv11),
b: nil,
want: []core.KeyValue{kv11},
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
res := resource.Merge(c.a, c.b)
if diff := cmp.Diff(
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 12:15:36 -07:00
res.Attributes(),
c.want,
cmp.AllowUnexported(core.Value{})); diff != "" {
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
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 12:15:36 -07:00
func TestString(t *testing.T) {
for _, test := range []struct {
kvs []core.KeyValue
want string
}{
{
kvs: nil,
want: "Resource()",
},
{
kvs: []core.KeyValue{},
want: "Resource()",
},
{
kvs: []core.KeyValue{kv11},
want: "Resource(k1=v11)",
},
{
kvs: []core.KeyValue{kv11, kv12},
want: "Resource(k1=v11)",
},
{
kvs: []core.KeyValue{kv11, kv21},
want: "Resource(k1=v11,k2=v21)",
},
{
kvs: []core.KeyValue{kv21, kv11},
want: "Resource(k1=v11,k2=v21)",
},
{
kvs: []core.KeyValue{kv11, kv21, kv31},
want: "Resource(k1=v11,k2=v21,k3=v31)",
},
{
kvs: []core.KeyValue{kv31, kv11, kv21},
want: "Resource(k1=v11,k2=v21,k3=v31)",
},
{
kvs: []core.KeyValue{core.Key("A").String("a"), core.Key("B").String("b")},
want: "Resource(A=a,B=b)",
},
{
kvs: []core.KeyValue{core.Key("A").String("a,B=b")},
want: `Resource(A=a\,B\=b)`,
},
{
kvs: []core.KeyValue{core.Key("A").String(`a,B\=b`)},
want: `Resource(A=a\,B\\\=b)`,
},
{
kvs: []core.KeyValue{core.Key("A=a,B").String(`b`)},
want: `Resource(A\=a\,B=b)`,
},
{
kvs: []core.KeyValue{core.Key(`A=a\,B`).String(`b`)},
want: `Resource(A\=a\\\,B=b)`,
},
} {
if got := resource.New(test.kvs...).String(); got != test.want {
t.Errorf("Resource(%v).String() = %q, want %q", test.kvs, got, test.want)
}
}
}