1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-22 03:38:42 +02:00
Tyler Yahn 8e97011ea8
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

66 lines
1.9 KiB
Go

// 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
import "go.opentelemetry.io/otel/api/core"
// AttributeIterator allows iterating over an ordered set of Resource attributes.
//
// The typical use of the iterator assuming a Resource named `res`, is
// something like the following:
//
// for iter := res.Iter(); iter.Next(); {
// attr := iter.Attribute()
// // or, if an index is needed:
// // idx, attr := iter.IndexedAttribute()
//
// // ...
// }
type AttributeIterator struct {
attrs []core.KeyValue
idx int
}
// NewAttributeIterator creates an iterator going over a passed attrs.
func NewAttributeIterator(attrs []core.KeyValue) AttributeIterator {
return AttributeIterator{attrs: attrs, idx: -1}
}
// Next moves the iterator to the next attribute.
// Returns false if there are no more attributes.
func (i *AttributeIterator) Next() bool {
i.idx++
return i.idx < i.Len()
}
// Attribute returns current attribute.
//
// Must be called only after Next returns true.
func (i *AttributeIterator) Attribute() core.KeyValue {
return i.attrs[i.idx]
}
// IndexedAttribute returns current index and attribute.
//
// Must be called only after Next returns true.
func (i *AttributeIterator) IndexedAttribute() (int, core.KeyValue) {
return i.idx, i.Attribute()
}
// Len returns a number of attributes.
func (i *AttributeIterator) Len() int {
return len(i.attrs)
}